ListView.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. var t = require;
  2. var e = module;
  3. var n = exports;
  4. var h,
  5. e =
  6. (this && this.__decorate) ||
  7. function(t, e, i, s) {
  8. var o,
  9. a = arguments.length,
  10. n = a < 3 ? e : null === s ? (s = Object.getOwnPropertyDescriptor(e, i)) : s;
  11. if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) n = Reflect.decorate(t, e, i, s);
  12. else
  13. for (var r = t.length - 1; 0 <= r; r--)
  14. (o = t[r]) && (n = (a < 3 ? o(n) : 3 < a ? o(e, i, n) : o(e, i)) || n);
  15. return 3 < a && n && Object.defineProperty(e, i, n), n;
  16. },
  17. i =
  18. (this && this.__awaiter) ||
  19. function(t, n, r, l) {
  20. return new(r = r || Promise)(function(i, e) {
  21. function s(t) {
  22. try {
  23. a(l.next(t));
  24. } catch (t) {
  25. e(t);
  26. }
  27. }
  28. function o(t) {
  29. try {
  30. a(l.throw(t));
  31. } catch (t) {
  32. e(t);
  33. }
  34. }
  35. function a(t) {
  36. var e;
  37. t.done ?
  38. i(t.value) :
  39. ((e = t.value) instanceof r ?
  40. e :
  41. new r(function(t) {
  42. t(e);
  43. })
  44. ).then(s, o);
  45. }
  46. a((l = l.apply(t, n || [])).next());
  47. });
  48. };
  49. Object.defineProperty(n, "__esModule", { value: !0 }), (n.ListViewBaseLayout = n.ListView = n.ListViewEvent = void 0);
  50. const s = t("App"),
  51. a = t("ItemRenderer");
  52. Object.defineProperty(cc.Node.prototype, "LEFT_TOP", {
  53. configurable: !0,
  54. get: function() {
  55. return cc.v2(this.x + this.width * -this.anchorX, this.y + this.height * (1 - this.anchorY));
  56. }
  57. }),
  58. ((t = h = h || {})[(t.NONE = 0)] = "NONE"),
  59. (t[(t.HORIZONTAL = 1)] = "HORIZONTAL"),
  60. (t[(t.VERTICAL = 2)] = "VERTICAL"),
  61. (t[(t.GRID = 3)] = "GRID"),
  62. (n.ListViewEvent = {
  63. ITEM_TAP: "ITEM_TAP",
  64. ITEM_CREATED: "ITEM_CREATED",
  65. ITEM_DATA_CHANGED: "ITEM_DATA_CHANGED",
  66. SELECTION_CHANGED: "SELECTION_CHANGED",
  67. SELECTION_REACH_MAX: "SELECTION_REACH_MAX"
  68. });
  69. const { ccclass: o, property: r, menu: l } = cc._decorator;
  70. t = class extends cc.Component {
  71. constructor() {
  72. super(...arguments),
  73. (this.viewPort = { x: 0, y: 0, width: 0, height: 0 }),
  74. (this.itemRenderer = null),
  75. (this.itemRendererInline = null),
  76. (this.layoutType = h.NONE),
  77. (this.isMultipleSelect = !1),
  78. (this.maxMultipleSelectNum = 3),
  79. (this.scrollView = null),
  80. (this.isVirtualLayout = !1),
  81. (this.interactable = !0),
  82. (this.gapX = 0),
  83. (this.gapY = 0),
  84. (this.autoGap = !1),
  85. (this.autoPadding = !1),
  86. (this.paddingLeft = 0),
  87. (this.paddingRight = 0),
  88. (this.paddingTop = 0),
  89. (this.paddingBottom = 0),
  90. (this.nodeEmpty = null),
  91. (this.horizontalCenterInScrollView = !1),
  92. (this.lazyLoadItem = !1),
  93. (this.itemScale = 1),
  94. (this.tag = ""),
  95. (this.customViews = []),
  96. (this.itemPool = []),
  97. (this.items = []),
  98. (this.dataSource = []),
  99. (this.cols = 0),
  100. (this.itemWidth = 0),
  101. (this.itemHeight = 0),
  102. (this.initialized = !1),
  103. (this.selectedIndices = []),
  104. (this.selectedItem = null),
  105. (this.selectedItems = []),
  106. (this.disabledItems = []),
  107. (this.lastScrollPos = null),
  108. (this.container = null),
  109. (this._itemUpdateQueue = []),
  110. (this.layout = null);
  111. }
  112. get selectedIndex() {
  113. return this._selectedIndex;
  114. }
  115. set selectedIndex(t) {
  116. this.isMultipleSelect ||
  117. ((this._selectedIndex = t),
  118. (this.selectedIndices = [t]),
  119. (this.selectedItem = this.dataSource[t]),
  120. (this.selectedItems = [this.selectedItem]));
  121. }
  122. onLoad() {
  123. this.init();
  124. }
  125. start() {}
  126. onDestroy() {
  127. this.itemPool.forEach(function(t) {
  128. t.node && t.node.isValid && t.node.destroy();
  129. }),
  130. (this.itemPool = []);
  131. }
  132. init() {
  133. var t = this;
  134. if (!this.initialized) {
  135. switch (this.layoutType) {
  136. case h.HORIZONTAL:
  137. this.layout = new d();
  138. break;
  139. case h.VERTICAL:
  140. this.layout = new u();
  141. break;
  142. case h.NONE:
  143. case h.GRID:
  144. this.layout = new p();
  145. }
  146. if (
  147. ((this.container = new cc.Node()),
  148. this.node.addChild(this.container),
  149. this.itemRendererInline &&
  150. this.itemRendererInline.getParent() == this.node &&
  151. this.itemRendererInline.removeFromParent(!1),
  152. (this.itemRendererClass = this.itemRendererClass || a.default),
  153. this.scrollView)
  154. ) {
  155. if (this.scrollView.content != this.node)
  156. this.node.on(
  157. cc.Node.EventType.POSITION_CHANGED,
  158. function() {
  159. t.updateViewPort(!0);
  160. },
  161. this
  162. );
  163. else {
  164. var e = this.scrollView.node.getComponent(cc.Widget);
  165. e && e.updateAlignment(),
  166. (this.node.anchorX = 0),
  167. (this.node.anchorY = 1),
  168. (this.node.width = Math.max(this.node.width, this.scrollView.node.width));
  169. for (var i = this.node.getParent(), s = []; i && i != this.scrollView.node;) {
  170. var o = i.getComponent(cc.Widget);
  171. o ||
  172. (((o = i.addComponent(cc.Widget)).left = o.right = o.top = o.bottom = 0),
  173. (o.isAlignLeft = o.isAlignRight = o.isAlignTop = o.isAlignBottom = !0),
  174. (o.alignMode = cc.Widget.AlignMode.ALWAYS)),
  175. s.push(o),
  176. (i = i.getParent());
  177. }
  178. s.reverse().forEach(function(t) {
  179. return t.updateAlignment();
  180. }),
  181. (i = this.node.getParent()),
  182. (this.node.x = -i.width * i.anchorX + this.node.width * this.node.anchorX),
  183. (this.node.y = i.height * (1 - i.anchorY) - this.node.height * (1 - this.node.anchorY)),
  184. this.isVirtualLayout &&
  185. ((this.viewPort.width = this.scrollView.node.width),
  186. (this.viewPort.height = this.scrollView.node.height));
  187. }
  188. this.scrollView.node.on("scrolling", this.onScrollChanged, this),
  189. this.scrollView.node.on("scroll-ended", this.onScrollChanged, this),
  190. this.scrollView.node.on(
  191. cc.Node.EventType.SIZE_CHANGED,
  192. function() {
  193. (t.node.width = t.scrollView.node.width), t.updateViewPort(!0);
  194. },
  195. this
  196. );
  197. } else this.isVirtualLayout = !1;
  198. this.initialized = !0;
  199. }
  200. }
  201. updateViewPort(t = !1) {
  202. var e, i;
  203. this.scrollView &&
  204. ((e = this.scrollView.getScrollOffset()),
  205. (i = this.node
  206. .convertToWorldSpaceAR(this.node.LEFT_TOP.sub(this.node.position))
  207. .sub(
  208. this.scrollView.content.convertToWorldSpaceAR(
  209. this.scrollView.content.LEFT_TOP.sub(this.scrollView.content.position)
  210. )
  211. )),
  212. (this.viewPort.x = e.x - i.x),
  213. (this.viewPort.y = e.y + i.y),
  214. (this.viewPort.width = this.scrollView.node.width),
  215. (this.viewPort.height = this.scrollView.node.height)),
  216. t &&
  217. ((this.cols = 0),
  218. this.autoPadding && (this.paddingLeft = this.paddingRight = 0),
  219. this.autoGap && (this.gapX = 0)),
  220. this.checkLayoutParam(0 < this.items.length ? this.items[0].node : null),
  221. this.updateLayout(t);
  222. }
  223. onScrollChanged() {
  224. var t = this.scrollView.getScrollOffset();
  225. (!this.lastScrollPos || 30 < t.sub(this.lastScrollPos).mag()) &&
  226. (this.updateViewPort(), (this.lastScrollPos = t));
  227. }
  228. getItems() {
  229. return this.items;
  230. }
  231. getContentSize() {
  232. return cc.size(
  233. this.node.width, -this.getItemPositionAt(this.dataSource.length - 1).y + this.itemHeight + this.paddingBottom
  234. );
  235. }
  236. refreshView(e = !1) {
  237. this.items.forEach(function(t) {
  238. return t.setData(t.data, e);
  239. });
  240. }
  241. updateDataSource(t, e = !1) {
  242. var i,
  243. s = this;
  244. if (
  245. ((this.dataSource = t),
  246. this.checkLayoutParam(),
  247. this.nodeEmpty && (this.nodeEmpty.active = t.length <= 0),
  248. this.clearSelection(),
  249. this.isVirtualLayout)
  250. )
  251. this.updateLayout(e);
  252. else {
  253. for (var o = 0; o < this.items.length; o++) {
  254. var a = this.items[o];
  255. a.index >= t.length ?
  256. (this.items.slice(o).forEach(function(t) {
  257. t.node.removeFromParent(!1), (t.recycled = !0), s.itemPool.push(t);
  258. }),
  259. this.items.splice(o)) :
  260. a.setData(t[a.index], e),
  261. a.onUnselected();
  262. }
  263. var n = 0 < this.items.length ? this.items[this.items.length - 1].index : -1,
  264. r = t.length - 1 - n;
  265. if (0 < r)
  266. for (o = 0; o < r; o++) {
  267. var l = n + o + 1,
  268. l = this.createItemAt(l, t[l]);
  269. this.items.push(l), l.onUnselected();
  270. }
  271. }
  272. this.layout instanceof d &&
  273. (this.node.width =
  274. this.getItemPositionAt(t.length - 1).x + this.itemWidth + this.paddingLeft + this.paddingRight),
  275. (this.node.height = -this.getItemPositionAt(t.length - 1).y + this.itemHeight + this.paddingBottom),
  276. (this.container.y = this.node.height * (1 - this.node.anchorY)),
  277. (this.container.x = this.node.width * -this.node.anchorX),
  278. (!this.scrollView && (this.scrollView || (this.layoutType != h.GRID && this.layoutType != h.HORIZONTAL))) ||
  279. !this.horizontalCenterInScrollView ||
  280. (this.dataSource.length < this.cols &&
  281. (this.scrollView ?
  282. (this.container.x =
  283. (this.scrollView.node.width -
  284. (this.dataSource.length * this.itemWidth +
  285. this.gapX * (this.dataSource.length - 1))) /
  286. 2) :
  287. (this.container.x +=
  288. ((this.cols - this.dataSource.length) / 2) * (this.itemWidth - this.gapX / 2) -
  289. this.paddingLeft))),
  290. this.scrollView &&
  291. this.layoutType == h.VERTICAL &&
  292. this.horizontalCenterInScrollView &&
  293. ((i =
  294. (this.scrollView.node.height - this.layout.paddingTop) /
  295. (this.itemHeight + this.paddingTop / 2 + this.paddingBottom / 2)),
  296. this.dataSource.length < i &&
  297. (this.scrollView ?
  298. (this.container.y = -(
  299. this.scrollView.node.height -
  300. (this.dataSource.length * this.itemHeight + this.gapY * (this.dataSource.length - 1))
  301. ) /
  302. 2 +
  303. this.paddingTop) :
  304. (this.container.y -=
  305. ((i - this.dataSource.length) / 2) * (this.itemHeight - this.gapY / 2) -
  306. this.paddingTop)));
  307. }
  308. clearSelection() {
  309. var i = this;
  310. this.selectedItems.forEach(function(e) {
  311. var t = i.items.find(function(t) {
  312. return t.data == e;
  313. });
  314. t && t.onUnselected();
  315. }),
  316. (this._selectedIndex = -1),
  317. (this.selectedIndices = []),
  318. (this.selectedItem = null),
  319. (this.selectedItems = []),
  320. this.node.emit(n.ListViewEvent.SELECTION_CHANGED);
  321. }
  322. select(e) {
  323. var t,
  324. i = this;
  325. (e.length == this.selectedItems.length &&
  326. e.every(function(t, e) {
  327. return t == i.selectedItems[e];
  328. })) ||
  329. ((t = e.filter(function(t) {
  330. return i.selectedItems.indexOf(t) < 0;
  331. })),
  332. this.selectedItems
  333. .filter(function(t) {
  334. return e.indexOf(t) < 0;
  335. })
  336. .forEach(function(e) {
  337. var t = i.items.find(function(t) {
  338. return t.data == e;
  339. });
  340. t && t.onUnselected();
  341. }),
  342. (this.selectedItems = e),
  343. (this.selectedIndices = this.selectedItems.map(function(t) {
  344. return i.dataSource.indexOf(t);
  345. })),
  346. e.length <= 1 ?
  347. ((this.selectedItem = e[0]), (this._selectedIndex = this.dataSource.indexOf(e[0]))) :
  348. ((this.selectedItem = null), (this._selectedIndex = -1)),
  349. this.refreshView(),
  350. t.forEach(function(e) {
  351. var t = i.items.find(function(t) {
  352. return t.data == e;
  353. });
  354. t && t.onSelected();
  355. }),
  356. this.node.emit(n.ListViewEvent.SELECTION_CHANGED));
  357. }
  358. selectByIndices(e) {
  359. var t,
  360. i = this;
  361. 0 == e.length && this.clearSelection(),
  362. (e.length == this.selectedIndices.length &&
  363. e.every(function(t, e) {
  364. return t == i.selectedIndices[e];
  365. })) ||
  366. ((t = e.filter(function(t) {
  367. return i.selectedIndices.indexOf(t) < 0;
  368. })),
  369. this.selectedIndices
  370. .filter(function(t) {
  371. return e.indexOf(t) < 0;
  372. })
  373. .forEach(function(e) {
  374. var t = i.items.find(function(t) {
  375. return t.index == e;
  376. });
  377. t && t.onUnselected();
  378. }),
  379. (this.selectedIndices = e),
  380. (this.selectedItems = this.selectedIndices.map(function(t) {
  381. return i.dataSource[t];
  382. })),
  383. e.length <= 1 ?
  384. ((this.selectedItem = this.selectedItems[0]), (this._selectedIndex = e[0])) :
  385. ((this.selectedItem = null), (this._selectedIndex = -1)),
  386. this.refreshView(),
  387. t.forEach(function(e) {
  388. var t = i.items.find(function(t) {
  389. return t.index == e;
  390. });
  391. t && t.onSelected();
  392. }),
  393. this.node.emit(n.ListViewEvent.SELECTION_CHANGED));
  394. }
  395. disableSelectByIndices(i) {
  396. (this.disabledItems = this.dataSource.filter(function(t, e) {
  397. return i.include(e);
  398. })),
  399. this.items.forEach(function(t) {
  400. i.include(t.index) ? t.onDisableSelect() : t.onEnableSelect();
  401. });
  402. }
  403. disableSelect(t) {
  404. (this.disabledItems = t.slice()),
  405. this.items.forEach(function(e) {
  406. t.some(function(t) {
  407. return t == e.data;
  408. }) ?
  409. e.onDisableSelect() :
  410. e.onEnableSelect();
  411. });
  412. }
  413. getCoordinateAt(t) {
  414. return this.layout.getItemCoordinateAtAt(t);
  415. }
  416. refreshLayout() {
  417. (this.cols = 0),
  418. this.autoPadding && (this.paddingLeft = this.paddingRight = 0),
  419. this.autoGap && (this.gapX = 0),
  420. this.checkLayoutParam(0 < this.items.length ? this.items[0].node : null),
  421. this.updateLayout(!0);
  422. }
  423. updateLayout(o = !1) {
  424. var a = this;
  425. this.checkLayoutParam();
  426. var t = cc.rect(this.viewPort.x, this.viewPort.y, this.viewPort.width, this.viewPort.height),
  427. t = f(this.layout.getVisibleIndexStartEnd(t.x, t.y, t.width, t.height), 2),
  428. i = t[0],
  429. s = t[1],
  430. i = i.limit(0, this.dataSource.length - 1),
  431. s = s.limit(0, this.dataSource.length - 1);
  432. if (this.isVirtualLayout) {
  433. var e = 0 < this.items.length ? this.items[0].index : -1,
  434. n = 0 < this.items.length ? this.items[this.items.length - 1].index : -1,
  435. r = this.items.slice();
  436. if (e != i || n != s) {
  437. for (var l, h, t = Math.min(i, e).limit(0), c = Math.max(s, n), d = [], u = [], p = t; p <= c; p++)
  438. (n < p && p < i) ||
  439. (p < e && s < p) ||
  440. (p < e && ((l = this.createItemAt(p, this.dataSource[p])), d.push(l)),
  441. n < p && ((l = this.createItemAt(p, this.dataSource[p])), u.push(l)),
  442. p < i && ((h = r[p - e]), this.recycleItem(h)),
  443. s < p && ((h = r[p - e]), this.recycleItem(h)));
  444. d.reverse().forEach(function(t) {
  445. return a.items.unshift(t);
  446. }),
  447. u.forEach(function(t) {
  448. return a.items.push(t);
  449. }),
  450. this.items.forEach(function(t, e) {
  451. var i = a
  452. .getItemPositionAt(t.index)
  453. .add(cc.v2(t.node.width * t.node.anchorX, -t.node.height * (1 - t.node.anchorY)));
  454. (t.node.position = i), (t.node.zIndex = e);
  455. var s = a.dataSource[t.index];
  456. a.lazyLoadItem ? a.pushItemUpdate(t, s, o) : t.setData(s, o),
  457. a.selectedItems.some(function(t) {
  458. return t == s;
  459. }) ?
  460. t.onSelected() :
  461. t.onUnselected(),
  462. a.disabledItems.some(function(t) {
  463. return t == s;
  464. }) ?
  465. t.onDisableSelect() :
  466. t.onEnableSelect();
  467. });
  468. } else
  469. this.items.forEach(function(t) {
  470. var e = a
  471. .getItemPositionAt(t.index)
  472. .add(cc.v2(t.node.width * t.node.anchorX, -t.node.height * (1 - t.node.anchorY)));
  473. (t.node.position = e), t.setData(a.dataSource[t.index], o), (t.node.active = !0);
  474. });
  475. } else
  476. (i = 0),
  477. (s = this.dataSource.length - 1),
  478. o &&
  479. this.items.forEach(function(t) {
  480. var e = a
  481. .getItemPositionAt(t.index)
  482. .add(cc.v2(t.node.width * t.node.anchorX, -t.node.height * (1 - t.node.anchorY)));
  483. (t.node.position = e),
  484. (t.node.active = t.index >= i && t.index <= s),
  485. t.setData(a.dataSource[t.index], o);
  486. });
  487. }
  488. ensureItemInView(t, e) {
  489. void 0 === e && (e = 0);
  490. var i = f(
  491. this.layout.getVisibleIndexStartEnd(
  492. this.viewPort.x,
  493. this.viewPort.y,
  494. this.viewPort.width,
  495. this.viewPort.height
  496. ),
  497. 2
  498. ),
  499. s = i[0],
  500. i = i[1];
  501. (s <= t && t <= i) ||
  502. ((i = this.getItemPositionAt(t)),
  503. (t = this.node
  504. .convertToWorldSpaceAR(this.node.LEFT_TOP.sub(this.node.position))
  505. .sub(
  506. this.scrollView.content.convertToWorldSpaceAR(
  507. this.scrollView.content.LEFT_TOP.sub(this.scrollView.content.position)
  508. )
  509. )),
  510. i.addSelf(t),
  511. this.scrollView.scrollToOffset(cc.v2(i.x, -i.y), e),
  512. e || ((this.viewPort.x = i.x), (this.viewPort.y = -i.y), this.updateLayout()));
  513. }
  514. focusItem(t, e, i) {
  515. void 0 === e && (e = 0);
  516. t = this.getItemPositionAt(t);
  517. i && t.addSelf(i);
  518. i = this.node
  519. .convertToWorldSpaceAR(this.node.LEFT_TOP.sub(this.node.position))
  520. .sub(
  521. this.scrollView.content.convertToWorldSpaceAR(
  522. this.scrollView.content.LEFT_TOP.sub(this.scrollView.content.position)
  523. )
  524. );
  525. t.addSelf(i),
  526. this.scrollView.scrollToOffset(cc.v2(t.x, -t.y), e),
  527. e || ((this.viewPort.x = t.x), (this.viewPort.y = -t.y), this.updateLayout());
  528. }
  529. scrollToItemByPos(t, e) {
  530. void 0 === e && (e = 0),
  531. this.scrollView.scrollToOffset(cc.v2(t.x, -t.y), e),
  532. e || ((this.viewPort.x = t.x), (this.viewPort.y = -t.y), this.updateLayout());
  533. }
  534. recycleItem(t) {
  535. this.items.splice(this.items.indexOf(t), 1),
  536. t.node.removeFromParent(!1),
  537. (t.recycled = !0),
  538. this.itemPool.push(t);
  539. }
  540. createItemAt(t, e) {
  541. var i,
  542. s = this;
  543. 0 < this.itemPool.length ?
  544. (i = this.itemPool.shift().node) :
  545. ((i = cc.instantiate(this.itemRenderer || this.itemRendererInline)).on(
  546. cc.Node.EventType.TOUCH_END,
  547. function(t) {
  548. (cc.sys.isNative && 0 < t.touch.getID()) || s.onItemTap(o);
  549. },
  550. this
  551. ),
  552. (i.scaleX *= this.itemScale),
  553. (i.scaleY *= this.itemScale));
  554. var o = i.getComponent(this.itemRendererClass);
  555. ((o = o || i.addComponent(this.itemRendererClass)).listView = this).container.addChild(i),
  556. this.checkLayoutParam(i);
  557. var a = this.getItemPositionAt(t).add(
  558. cc.v2(i.width * this.itemScale * i.anchorX, -i.height * this.itemScale * (1 - i.anchorY))
  559. );
  560. return (
  561. (i.position = a),
  562. (o.index = t),
  563. this.lazyLoadItem ? this.pushItemUpdate(o, e) : o.setData(e),
  564. (o.recycled = !1),
  565. this.node.emit(n.ListViewEvent.ITEM_CREATED, o),
  566. o
  567. );
  568. }
  569. getItemPositionAt(t) {
  570. return this.layout.getItemPositionAt(t);
  571. }
  572. addCustomView(t, e) {
  573. this.checkLayoutParam();
  574. var i = { width: e.width, height: e.height };
  575. this.cols &&
  576. e instanceof cc.Node &&
  577. (e.removeFromParent(!1),
  578. (e.position = this.getItemPositionAt(t).add(cc.v2(i.width * e.anchorX, -i.height * (1 - e.anchorY)))),
  579. this.container.addChild(e),
  580. this.customViews.push(e)),
  581. this.layout.addCustomSpace([t, i]),
  582. this.updateLayout(!1);
  583. }
  584. clearCustomView(e) {
  585. void 0 === e && (e = !0),
  586. this.customViews.forEach(function(t) {
  587. t.parent && t.removeFromParent(e), e && t.isValid && t.destroy();
  588. }),
  589. (this.customViews = []),
  590. this.layout.clearCustomSpace();
  591. }
  592. pushItemUpdate(e, t, i = !1) {
  593. var s = this._itemUpdateQueue.slice(1).find(function(t) {
  594. return t.item == e;
  595. });
  596. s
  597. ?
  598. (s.data = t) :
  599. (this._itemUpdateQueue.push({ item: e, data: t, force: i }),
  600. 1 == this._itemUpdateQueue.length && this.handleItemUpdate());
  601. }
  602. handleItemUpdate() {
  603. return i(this, void 0, void 0, function*() {
  604. var t = this._itemUpdateQueue[0];
  605. t &&
  606. (t.item.setData(t.data, t.force),
  607. yield s.default.TimerManager.sleep(0.01),
  608. this._itemUpdateQueue.shift(),
  609. this.handleItemUpdate());
  610. });
  611. }
  612. checkLayoutParam(t) {
  613. var e;
  614. this.initialized || this.init(),
  615. this.cols ||
  616. ((e = (t = t || cc.instantiate(this.itemRenderer || this.itemRendererInline)).width * this.itemScale),
  617. (t = t.height * this.itemScale),
  618. this.autoGap ?
  619. ((this.cols = Math.floor((this.node.width - this.paddingLeft - this.paddingRight) / e) || 1),
  620. this.cols &&
  621. (this.gapX =
  622. (this.node.width - this.paddingLeft - this.paddingRight - this.cols * e) /
  623. (this.cols - 1))) :
  624. (this.cols =
  625. Math.floor((this.node.width - this.paddingLeft - this.paddingRight) / (e + this.gapX)) || 1),
  626. this.autoPadding &&
  627. (this.autoGap ?
  628. ((this.gapX = this.node.width / this.cols - e),
  629. (this.paddingLeft = this.paddingRight = this.gapX / 2)) :
  630. (this.paddingLeft = this.paddingRight =
  631. (this.node.width - this.cols * e - this.gapX * (this.cols - 1)) / 2)),
  632. (this.itemWidth = e),
  633. (this.itemHeight = t),
  634. this.layout.updateParams(
  635. e,
  636. t,
  637. this.cols,
  638. this.gapX,
  639. this.gapY,
  640. this.paddingLeft,
  641. this.paddingRight,
  642. this.paddingTop,
  643. this.paddingBottom
  644. ));
  645. }
  646. onItemTap(t) {
  647. if (!t.checkSelect(-1 == this.selectedIndices.indexOf(t.index))) return !1;
  648. var e,
  649. i,
  650. s = this;
  651. this.interactable ?
  652. ((e = t.index),
  653. this.disabledItems.include(t.data) ||
  654. (0 <= this.selectedIndices.indexOf(e) ?
  655. this.isMultipleSelect &&
  656. (this.selectedIndices.splice(this.selectedIndices.indexOf(e), 1),
  657. this.selectedItems.splice(this.selectedItems.indexOf(t.data), 1),
  658. t.onCancelSelected(),
  659. this.node.emit(n.ListViewEvent.SELECTION_CHANGED)) :
  660. (this.isMultipleSelect ?
  661. this.selectedIndices.length < this.maxMultipleSelectNum ?
  662. (this.selectedIndices.push(e),
  663. this.selectedItems.push(t.data),
  664. (this.selectedIndex = e),
  665. (this.selectedItem = t.data),
  666. t.onClickSelected()) :
  667. this.node.emit(n.ListViewEvent.SELECTION_REACH_MAX) :
  668. (0 <= this.selectedIndex &&
  669. (i = this.items.find(function(t) {
  670. return t.index == s.selectedIndex;
  671. })) &&
  672. i.onCancelSelected(),
  673. (this.selectedIndices = [e]),
  674. (this.selectedItems = [t.data]),
  675. (this.selectedIndex = e),
  676. (this.selectedItem = t.data),
  677. t.onClickSelected(),
  678. this.node.emit(n.ListViewEvent.SELECTION_REACH_MAX)),
  679. this.node.emit(n.ListViewEvent.SELECTION_CHANGED))),
  680. this.node.emit(n.ListViewEvent.ITEM_TAP, t, e)) :
  681. this.node.emit(n.ListViewEvent.ITEM_TAP, t, t.index);
  682. }
  683. };
  684. e([r(cc.Prefab)], t.prototype, "itemRenderer", void 0),
  685. e([r(cc.Node)], t.prototype, "itemRendererInline", void 0),
  686. e([r({ type: cc.Enum(h) })], t.prototype, "layoutType", void 0),
  687. e([r({})], t.prototype, "isMultipleSelect", void 0),
  688. e(
  689. [
  690. r({
  691. type: cc.Integer,
  692. visible: function() {
  693. return this.isMultipleSelect;
  694. }
  695. })
  696. ],
  697. t.prototype,
  698. "maxMultipleSelectNum",
  699. void 0
  700. ),
  701. e([r(cc.ScrollView)], t.prototype, "scrollView", void 0),
  702. e([r({})], t.prototype, "isVirtualLayout", void 0),
  703. e([r({})], t.prototype, "interactable", void 0),
  704. e([r], t.prototype, "gapX", void 0),
  705. e([r], t.prototype, "gapY", void 0),
  706. e([r], t.prototype, "autoGap", void 0),
  707. e([r], t.prototype, "autoPadding", void 0),
  708. e([r], t.prototype, "paddingLeft", void 0),
  709. e([r], t.prototype, "paddingRight", void 0),
  710. e([r], t.prototype, "paddingTop", void 0),
  711. e([r], t.prototype, "paddingBottom", void 0),
  712. e([r(cc.Node)], t.prototype, "nodeEmpty", void 0),
  713. e([r], t.prototype, "horizontalCenterInScrollView", void 0),
  714. e([r], t.prototype, "itemScale", void 0),
  715. e([r], t.prototype, "tag", void 0),
  716. (t = e([o], t)),
  717. (n.ListView = t);
  718. class c {
  719. constructor() {
  720. (this.itemScale = 1), (this.spaces = []);
  721. }
  722. updateParams(t, e, i, s, o, a, n, r, l) {
  723. (this.itemWidth = t),
  724. (this.itemHeight = e),
  725. (this.cols = i),
  726. (this.gapX = s),
  727. (this.gapY = o),
  728. (this.paddingLeft = a),
  729. (this.paddingRight = n),
  730. (this.paddingTop = r),
  731. (this.paddingBottom = l);
  732. }
  733. addCustomSpace(t) {
  734. this.spaces.push(t);
  735. }
  736. clearCustomSpace() {
  737. this.spaces = [];
  738. }
  739. getItemCoordinateAtAt(t) {
  740. return cc.v2(t % this.cols, Math.floor(t / this.cols));
  741. }
  742. getItemPositionAt(t) {
  743. return cc.v2(0, 0);
  744. }
  745. getVisibleIndexStartEnd(t, e, i, s) {
  746. return [0, 0];
  747. }
  748. }
  749. function f(t, e) {
  750. var i = "function" == typeof Symbol && t[Symbol.iterator];
  751. if (!i) return t;
  752. var s,
  753. o,
  754. a = i.call(t),
  755. n = [];
  756. try {
  757. for (;
  758. (void 0 === e || 0 < e--) && !(s = a.next()).done;) n.push(s.value);
  759. } catch (t) {
  760. o = { error: t };
  761. } finally {
  762. try {
  763. s && !s.done && (i = a.return) && i.call(a);
  764. } finally {
  765. if (o) throw o.error;
  766. }
  767. }
  768. return n;
  769. }
  770. class d extends(n.ListViewBaseLayout = c) {
  771. getItemPositionAt(i) {
  772. var s = cc.v2(this.paddingLeft + i * (this.itemWidth + this.gapX), -this.paddingTop);
  773. return (
  774. this.spaces.forEach(function(t) {
  775. var e = f(t, 2),
  776. t = e[0],
  777. e = e[1];
  778. t <= i && (s.x += e.width);
  779. }),
  780. s
  781. );
  782. }
  783. getVisibleIndexStartEnd(i, t, s, e) {
  784. var o = this,
  785. a = Math.floor((-i - this.paddingLeft) / (this.itemWidth + this.gapX)),
  786. n = Math.ceil((-i + s - this.paddingLeft) / (this.itemWidth + this.gapX));
  787. return (
  788. this.spaces.forEach(function(t) {
  789. var e = f(t, 2),
  790. t = e[0],
  791. e = e[1];
  792. t <= a &&
  793. ((i += e.width),
  794. (a = Math.floor((-i - o.paddingLeft) / (o.itemWidth + o.gapX))),
  795. (n = Math.ceil((-i + s - o.paddingLeft) / (o.itemWidth + o.gapX))));
  796. }), [a, n]
  797. );
  798. }
  799. }
  800. class u extends c {
  801. getItemPositionAt(i) {
  802. var s = cc.v2(this.paddingLeft, -(this.paddingTop + i * (this.itemHeight + this.gapY)));
  803. return (
  804. this.spaces.forEach(function(t) {
  805. var e = f(t, 2),
  806. t = e[0],
  807. e = e[1];
  808. t <= i && (s.y -= e.height);
  809. }),
  810. s
  811. );
  812. }
  813. getVisibleIndexStartEnd(t, i, e, s) {
  814. var o = this,
  815. a = Math.floor((i - this.paddingTop) / (this.itemHeight + this.gapY)),
  816. n = Math.ceil((i + s - this.paddingTop) / (this.itemHeight + this.gapY));
  817. return (
  818. this.spaces.forEach(function(t) {
  819. var e = f(t, 2),
  820. t = e[0],
  821. e = e[1];
  822. t <= a &&
  823. ((i -= e.height),
  824. (a = Math.floor((i - o.paddingTop) / (o.itemHeight + o.gapY))),
  825. (n = Math.ceil((i + s - o.paddingTop) / (o.itemHeight + o.gapY))));
  826. }), [a, n]
  827. );
  828. }
  829. }
  830. class p extends c {
  831. getItemPositionAt(i) {
  832. var t = i % this.cols,
  833. e = Math.floor(i / this.cols),
  834. s = cc.v2(
  835. this.paddingLeft + t * (this.itemWidth + this.gapX), -(this.paddingTop + e * (this.itemHeight + this.gapY))
  836. );
  837. return (
  838. this.spaces.forEach(function(t) {
  839. var e = f(t, 2),
  840. t = e[0],
  841. e = e[1];
  842. t <= i && (s.y -= e.height);
  843. }),
  844. s
  845. );
  846. }
  847. getVisibleIndexStartEnd(t, i, e, s) {
  848. var o = this,
  849. a = Math.floor((i - this.paddingTop) / (this.itemHeight + this.gapY)) * this.cols,
  850. n = Math.ceil((i + s - this.paddingTop) / (this.itemHeight + this.gapY)) * this.cols;
  851. return (
  852. this.spaces.forEach(function(t) {
  853. var e = f(t, 2),
  854. t = e[0],
  855. e = e[1];
  856. t <= a &&
  857. ((i -= e.height),
  858. (a = Math.floor((i - o.paddingTop) / (o.itemHeight + o.gapY)) * o.cols),
  859. (n = Math.ceil((i + s - o.paddingTop) / (o.itemHeight + o.gapY)) * o.cols));
  860. }), [a, n]
  861. );
  862. }
  863. }