not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

368 lines
11 KiB

  1. /**
  2. A control that displays a scrolling list of rows, suitable for displaying
  3. very large lists. _enyo.List_ is optimized such that only a small portion of
  4. the list is rendered at a given time. A flyweight pattern is employed, in
  5. which controls placed inside the list are created once, but rendered for
  6. each list item. For this reason, it's best to use only simple controls in
  7. a List, such as <a href="#enyo.Control">enyo.Control</a> and
  8. <a href="#enyo.Image">enyo.Image</a>.
  9. A List's _components_ block contains the controls to be used for a single
  10. row. This set of controls will be rendered for each row. You may customize
  11. row rendering by handling the _onSetupItem_ event.
  12. Events fired from within list rows contain the _index_ property, which may
  13. be used to identify the row from which the event originated.
  14. The controls inside a List are non-interactive. This means that calling
  15. methods that would normally cause rendering to occur (e.g., _setContent_)
  16. will not do so. However, you can force a row to render by calling
  17. _renderRow(inRow)_.
  18. In addition, you can force a row to be temporarily interactive by calling
  19. _prepareRow(inRow)_. Call the _lockRow_ method when the interaction is
  20. complete.
  21. For more information, see the documentation on
  22. [Lists](https://github.com/enyojs/enyo/wiki/Lists)
  23. in the Enyo Developer Guide.
  24. */
  25. enyo.kind({
  26. name: "enyo.List",
  27. kind: "Scroller",
  28. classes: "enyo-list",
  29. published: {
  30. /**
  31. The number of rows contained in the list. Note that as the amount of
  32. list data changes, _setRows_ can be called to adjust the number of
  33. rows. To re-render the list at the current position when the count
  34. has changed, call the _refresh_ method.
  35. */
  36. count: 0,
  37. /**
  38. The number of rows to be shown on a given list page segment.
  39. There is generally no need to adjust this value.
  40. */
  41. rowsPerPage: 50,
  42. /**
  43. If true, renders the list such that row 0 is at the bottom of the
  44. viewport and the beginning position of the list is scrolled to the
  45. bottom
  46. */
  47. bottomUp: false,
  48. //* If true, multiple selections are allowed
  49. multiSelect: false,
  50. //* If true, the selected item will toggle
  51. toggleSelected: false,
  52. //* If true, the list will assume all rows have the same height for optimization
  53. fixedHeight: false
  54. },
  55. events: {
  56. /**
  57. Fires once per row at render time, with event object:
  58. _{index: <index of row>}_
  59. */
  60. onSetupItem: ""
  61. },
  62. handlers: {
  63. onAnimateFinish: "animateFinish"
  64. },
  65. //* @protected
  66. rowHeight: 0,
  67. listTools: [
  68. {name: "port", classes: "enyo-list-port enyo-border-box", components: [
  69. {name: "generator", kind: "FlyweightRepeater", canGenerate: false, components: [
  70. {tag: null, name: "client"}
  71. ]},
  72. {name: "page0", allowHtml: true, classes: "enyo-list-page"},
  73. {name: "page1", allowHtml: true, classes: "enyo-list-page"}
  74. ]}
  75. ],
  76. create: function() {
  77. this.pageHeights = [];
  78. this.inherited(arguments);
  79. this.getStrategy().translateOptimized = true;
  80. this.bottomUpChanged();
  81. this.multiSelectChanged();
  82. this.toggleSelectedChanged();
  83. },
  84. createStrategy: function() {
  85. this.controlParentName = "strategy";
  86. this.inherited(arguments);
  87. this.createChrome(this.listTools);
  88. this.controlParentName = "client";
  89. this.discoverControlParent();
  90. },
  91. rendered: function() {
  92. this.inherited(arguments);
  93. this.$.generator.node = this.$.port.hasNode();
  94. this.$.generator.generated = true;
  95. this.reset();
  96. },
  97. resizeHandler: function() {
  98. this.inherited(arguments);
  99. this.refresh();
  100. },
  101. bottomUpChanged: function() {
  102. this.$.generator.bottomUp = this.bottomUp;
  103. this.$.page0.applyStyle(this.pageBound, null);
  104. this.$.page1.applyStyle(this.pageBound, null);
  105. this.pageBound = this.bottomUp ? "bottom" : "top";
  106. if (this.hasNode()) {
  107. this.reset();
  108. }
  109. },
  110. multiSelectChanged: function() {
  111. this.$.generator.setMultiSelect(this.multiSelect);
  112. },
  113. toggleSelectedChanged: function() {
  114. this.$.generator.setToggleSelected(this.toggleSelected);
  115. },
  116. countChanged: function() {
  117. if (this.hasNode()) {
  118. this.updateMetrics();
  119. }
  120. },
  121. updateMetrics: function() {
  122. this.defaultPageHeight = this.rowsPerPage * (this.rowHeight || 100);
  123. this.pageCount = Math.ceil(this.count / this.rowsPerPage);
  124. this.portSize = 0;
  125. for (var i=0; i < this.pageCount; i++) {
  126. this.portSize += this.getPageHeight(i);
  127. }
  128. this.adjustPortSize();
  129. },
  130. generatePage: function(inPageNo, inTarget) {
  131. this.page = inPageNo;
  132. var r = this.$.generator.rowOffset = this.rowsPerPage * this.page;
  133. var rpp = this.$.generator.count = Math.min(this.count - r, this.rowsPerPage);
  134. var html = this.$.generator.generateChildHtml();
  135. inTarget.setContent(html);
  136. var pageHeight = inTarget.getBounds().height;
  137. // if rowHeight is not set, use the height from the first generated page
  138. if (!this.rowHeight && pageHeight > 0) {
  139. this.rowHeight = Math.floor(pageHeight / rpp);
  140. this.updateMetrics();
  141. }
  142. // update known page heights
  143. if (!this.fixedHeight) {
  144. var h0 = this.getPageHeight(inPageNo);
  145. if (h0 != pageHeight && pageHeight > 0) {
  146. this.pageHeights[inPageNo] = pageHeight;
  147. this.portSize += pageHeight - h0;
  148. }
  149. }
  150. },
  151. update: function(inScrollTop) {
  152. var updated = false;
  153. // get page info for position
  154. var pi = this.positionToPageInfo(inScrollTop);
  155. // zone line position
  156. var pos = pi.pos + this.scrollerHeight/2;
  157. // leap-frog zone position
  158. var k = Math.floor(pos/Math.max(pi.height, this.scrollerHeight) + 1/2) + pi.no;
  159. // which page number for page0 (even number pages)?
  160. var p = k % 2 == 0 ? k : k-1;
  161. if (this.p0 != p && this.isPageInRange(p)) {
  162. //this.log("update page0", p);
  163. this.generatePage(p, this.$.page0);
  164. this.positionPage(p, this.$.page0);
  165. this.p0 = p;
  166. updated = true;
  167. }
  168. // which page number for page1 (odd number pages)?
  169. p = k % 2 == 0 ? Math.max(1, k-1) : k;
  170. // position data page 1
  171. if (this.p1 != p && this.isPageInRange(p)) {
  172. //this.log("update page1", p);
  173. this.generatePage(p, this.$.page1);
  174. this.positionPage(p, this.$.page1);
  175. this.p1 = p;
  176. updated = true;
  177. }
  178. if (updated && !this.fixedHeight) {
  179. this.adjustBottomPage();
  180. this.adjustPortSize();
  181. }
  182. },
  183. updateForPosition: function(inPos) {
  184. this.update(this.calcPos(inPos));
  185. },
  186. calcPos: function(inPos) {
  187. return (this.bottomUp ? (this.portSize - this.scrollerHeight - inPos) : inPos);
  188. },
  189. adjustBottomPage: function() {
  190. var bp = this.p0 >= this.p1 ? this.$.page0 : this.$.page1;
  191. this.positionPage(bp.pageNo, bp);
  192. },
  193. adjustPortSize: function() {
  194. this.scrollerHeight = this.getBounds().height;
  195. var s = Math.max(this.scrollerHeight, this.portSize);
  196. this.$.port.applyStyle("height", s + "px");
  197. },
  198. positionPage: function(inPage, inTarget) {
  199. inTarget.pageNo = inPage;
  200. var y = this.pageToPosition(inPage);
  201. inTarget.applyStyle(this.pageBound, y + "px");
  202. },
  203. pageToPosition: function(inPage) {
  204. var y = 0;
  205. var p = inPage;
  206. while (p > 0) {
  207. p--;
  208. y += this.getPageHeight(p);
  209. }
  210. return y;
  211. },
  212. positionToPageInfo: function(inY) {
  213. var page = -1;
  214. var p = this.calcPos(inY);
  215. var h = this.defaultPageHeight;
  216. while (p >= 0) {
  217. page++;
  218. h = this.getPageHeight(page);
  219. p -= h;
  220. }
  221. //page = Math.min(page, this.pageCount-1);
  222. return {no: page, height: h, pos: p+h};
  223. },
  224. isPageInRange: function(inPage) {
  225. return inPage == Math.max(0, Math.min(this.pageCount-1, inPage));
  226. },
  227. getPageHeight: function(inPageNo) {
  228. return this.pageHeights[inPageNo] || this.defaultPageHeight;
  229. },
  230. invalidatePages: function() {
  231. this.p0 = this.p1 = null;
  232. // clear the html in our render targets
  233. this.$.page0.setContent("");
  234. this.$.page1.setContent("");
  235. },
  236. invalidateMetrics: function() {
  237. this.pageHeights = [];
  238. this.rowHeight = 0;
  239. this.updateMetrics();
  240. },
  241. scroll: function(inSender, inEvent) {
  242. var r = this.inherited(arguments);
  243. this.update(this.getScrollTop());
  244. return r;
  245. },
  246. //* @public
  247. scrollToBottom: function() {
  248. this.update(this.getScrollBounds().maxTop);
  249. this.inherited(arguments);
  250. },
  251. setScrollTop: function(inScrollTop) {
  252. this.update(inScrollTop);
  253. this.inherited(arguments);
  254. this.twiddle();
  255. },
  256. getScrollPosition: function() {
  257. return this.calcPos(this.getScrollTop());
  258. },
  259. setScrollPosition: function(inPos) {
  260. this.setScrollTop(this.calcPos(inPos));
  261. },
  262. //* Scrolls to a specific row.
  263. scrollToRow: function(inRow) {
  264. var page = Math.floor(inRow / this.rowsPerPage);
  265. var pageRow = inRow % this.rowsPerPage;
  266. var h = this.pageToPosition(page);
  267. // update the page
  268. this.updateForPosition(h);
  269. // call pageToPosition again and this time should return the right pos since the page info is populated
  270. h = this.pageToPosition(page);
  271. this.setScrollPosition(h);
  272. if (page == this.p0 || page == this.p1) {
  273. var rowNode = this.$.generator.fetchRowNode(inRow);
  274. if (rowNode) {
  275. // calc row offset
  276. var offset = rowNode.offsetTop;
  277. if (this.bottomUp) {
  278. offset = this.getPageHeight(page) - rowNode.offsetHeight - offset;
  279. }
  280. var y = this.getScrollPosition() + offset;
  281. this.setScrollPosition(y);
  282. }
  283. }
  284. },
  285. //* Scrolls to the beginning of the list.
  286. scrollToStart: function() {
  287. this[this.bottomUp ? "scrollToBottom" : "scrollToTop"]();
  288. },
  289. //* Scrolls to the end of the list.
  290. scrollToEnd: function() {
  291. this[this.bottomUp ? "scrollToTop" : "scrollToBottom"]();
  292. },
  293. //* Re-renders the list at the current position.
  294. refresh: function() {
  295. this.invalidatePages();
  296. this.update(this.getScrollTop());
  297. this.stabilize();
  298. //FIXME: Necessary evil for Android 4.0.4 refresh bug
  299. if (enyo.platform.android === 4) {
  300. this.twiddle();
  301. }
  302. },
  303. //* Re-renders the list from the beginning.
  304. reset: function() {
  305. this.getSelection().clear();
  306. this.invalidateMetrics();
  307. this.invalidatePages();
  308. this.stabilize();
  309. this.scrollToStart();
  310. },
  311. /**
  312. Returns the _selection_ component that manages the selection state for
  313. this list.
  314. */
  315. getSelection: function() {
  316. return this.$.generator.getSelection();
  317. },
  318. //* Sets the selection state for the given row index.
  319. select: function(inIndex, inData) {
  320. return this.getSelection().select(inIndex, inData);
  321. },
  322. //* Gets the selection state for the given row index.
  323. isSelected: function(inIndex) {
  324. return this.$.generator.isSelected(inIndex);
  325. },
  326. /**
  327. Re-renders the specified row. Call after making modifications to a row,
  328. to force it to render.
  329. */
  330. renderRow: function(inIndex) {
  331. this.$.generator.renderRow(inIndex);
  332. },
  333. //* Prepares the row to become interactive.
  334. prepareRow: function(inIndex) {
  335. this.$.generator.prepareRow(inIndex);
  336. },
  337. //* Restores the row to being non-interactive.
  338. lockRow: function() {
  339. this.$.generator.lockRow();
  340. },
  341. /**
  342. Performs a set of tasks by running the function _inFunc_ on a row (which
  343. must be interactive at the time the tasks are performed). Locks the row
  344. when done.
  345. */
  346. performOnRow: function(inIndex, inFunc, inContext) {
  347. this.$.generator.performOnRow(inIndex, inFunc, inContext);
  348. },
  349. //* @protected
  350. animateFinish: function(inSender) {
  351. this.twiddle();
  352. return true;
  353. },
  354. // FIXME: Android 4.04 has issues with nested composited elements; for example, a SwipeableItem,
  355. // can incorrectly generate taps on its content when it has slid off the screen;
  356. // we address this BUG here by forcing the Scroller to "twiddle" which corrects the bug by
  357. // provoking a dom update.
  358. twiddle: function() {
  359. var s = this.getStrategy();
  360. enyo.call(s, "twiddle");
  361. }
  362. });