listBody.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. import { Spinner } from '/client/lib/spinner';
  2. const subManager = new SubsManager();
  3. const InfiniteScrollIter = 10;
  4. BlazeComponent.extendComponent({
  5. onCreated() {
  6. // for infinite scrolling
  7. this.cardlimit = new ReactiveVar(InfiniteScrollIter);
  8. },
  9. mixins() {
  10. return [];
  11. },
  12. openForm(options) {
  13. options = options || {};
  14. options.position = options.position || 'top';
  15. const forms = this.childComponents('inlinedForm');
  16. let form = forms.find(component => {
  17. return component.data().position === options.position;
  18. });
  19. if (!form && forms.length > 0) {
  20. form = forms[0];
  21. }
  22. form.open();
  23. },
  24. addCard(evt) {
  25. evt.preventDefault();
  26. const firstCardDom = this.find('.js-minicard:first');
  27. const lastCardDom = this.find('.js-minicard:last');
  28. const textarea = $(evt.currentTarget).find('textarea');
  29. const position = this.currentData().position;
  30. const title = textarea.val().trim();
  31. const formComponent = this.childComponents('addCardForm')[0];
  32. let sortIndex;
  33. if (position === 'top') {
  34. sortIndex = Utils.calculateIndex(null, firstCardDom).base;
  35. } else if (position === 'bottom') {
  36. sortIndex = Utils.calculateIndex(lastCardDom, null).base;
  37. }
  38. const members = formComponent.members.get();
  39. const labelIds = formComponent.labels.get();
  40. const customFields = formComponent.customFields.get();
  41. const board = this.data().board();
  42. let linkedId = '';
  43. let swimlaneId = '';
  44. let cardType = 'cardType-card';
  45. if (title) {
  46. if (board.isTemplatesBoard()) {
  47. swimlaneId = this.parentComponent()
  48. .parentComponent()
  49. .data()._id; // Always swimlanes view
  50. const swimlane = Swimlanes.findOne(swimlaneId);
  51. // If this is the card templates swimlane, insert a card template
  52. if (swimlane.isCardTemplatesSwimlane()) cardType = 'template-card';
  53. // If this is the board templates swimlane, insert a board template and a linked card
  54. else if (swimlane.isBoardTemplatesSwimlane()) {
  55. linkedId = Boards.insert({
  56. title,
  57. permission: 'private',
  58. type: 'template-board',
  59. });
  60. Swimlanes.insert({
  61. title: TAPi18n.__('default'),
  62. boardId: linkedId,
  63. });
  64. cardType = 'cardType-linkedBoard';
  65. }
  66. } else if (Utils.boardView() === 'board-view-swimlanes')
  67. swimlaneId = this.parentComponent()
  68. .parentComponent()
  69. .data()._id;
  70. else if (
  71. Utils.boardView() === 'board-view-lists' ||
  72. Utils.boardView() === 'board-view-cal' ||
  73. !Utils.boardView()
  74. )
  75. swimlaneId = board.getDefaultSwimline()._id;
  76. const _id = Cards.insert({
  77. title,
  78. members,
  79. labelIds,
  80. customFields,
  81. listId: this.data()._id,
  82. boardId: board._id,
  83. sort: sortIndex,
  84. swimlaneId,
  85. type: cardType,
  86. linkedId,
  87. });
  88. // if the displayed card count is less than the total cards in the list,
  89. // we need to increment the displayed card count to prevent the spinner
  90. // to appear
  91. const cardCount = this.data()
  92. .cards(this.idOrNull(swimlaneId))
  93. .count();
  94. if (this.cardlimit.get() < cardCount) {
  95. this.cardlimit.set(this.cardlimit.get() + InfiniteScrollIter);
  96. }
  97. // In case the filter is active we need to add the newly inserted card in
  98. // the list of exceptions -- cards that are not filtered. Otherwise the
  99. // card will disappear instantly.
  100. // See https://github.com/wekan/wekan/issues/80
  101. Filter.addException(_id);
  102. // We keep the form opened, empty it, and scroll to it.
  103. textarea.val('').focus();
  104. autosize.update(textarea);
  105. if (position === 'bottom') {
  106. this.scrollToBottom();
  107. }
  108. }
  109. },
  110. scrollToBottom() {
  111. const container = this.firstNode();
  112. $(container).animate({
  113. scrollTop: container.scrollHeight,
  114. });
  115. },
  116. clickOnMiniCard(evt) {
  117. if (MultiSelection.isActive() || evt.shiftKey) {
  118. evt.stopImmediatePropagation();
  119. evt.preventDefault();
  120. const methodName = evt.shiftKey ? 'toggleRange' : 'toggle';
  121. MultiSelection[methodName](this.currentData()._id);
  122. // If the card is already selected, we want to de-select it.
  123. // XXX We should probably modify the minicard href attribute instead of
  124. // overwriting the event in case the card is already selected.
  125. } else if (Session.equals('currentCard', this.currentData()._id)) {
  126. evt.stopImmediatePropagation();
  127. evt.preventDefault();
  128. Utils.goBoardId(Session.get('currentBoard'));
  129. }
  130. },
  131. cardIsSelected() {
  132. return Session.equals('currentCard', this.currentData()._id);
  133. },
  134. toggleMultiSelection(evt) {
  135. evt.stopPropagation();
  136. evt.preventDefault();
  137. MultiSelection.toggle(this.currentData()._id);
  138. },
  139. idOrNull(swimlaneId) {
  140. if (
  141. Utils.boardView() === 'board-view-swimlanes' ||
  142. this.data()
  143. .board()
  144. .isTemplatesBoard()
  145. )
  146. return swimlaneId;
  147. return undefined;
  148. },
  149. cardsWithLimit(swimlaneId) {
  150. const limit = this.cardlimit.get();
  151. const defaultSort = { sort: 1 };
  152. const sortBy = Session.get('sortBy') ? Session.get('sortBy') : defaultSort;
  153. const selector = {
  154. listId: this.currentData()._id,
  155. archived: false,
  156. };
  157. if (swimlaneId) selector.swimlaneId = swimlaneId;
  158. return Cards.find(Filter.mongoSelector(selector), {
  159. // sort: ['sort'],
  160. sort: sortBy,
  161. limit,
  162. });
  163. },
  164. showSpinner(swimlaneId) {
  165. const list = Template.currentData();
  166. return list.cards(swimlaneId).count() > this.cardlimit.get();
  167. },
  168. canSeeAddCard() {
  169. return (
  170. !this.reachedWipLimit() &&
  171. Meteor.user() &&
  172. Meteor.user().isBoardMember() &&
  173. !Meteor.user().isCommentOnly() &&
  174. !Meteor.user().isWorker()
  175. );
  176. },
  177. reachedWipLimit() {
  178. const list = Template.currentData();
  179. return (
  180. !list.getWipLimit('soft') &&
  181. list.getWipLimit('enabled') &&
  182. list.getWipLimit('value') <= list.cards().count()
  183. );
  184. },
  185. events() {
  186. return [
  187. {
  188. 'click .js-minicard': this.clickOnMiniCard,
  189. 'click .js-toggle-multi-selection': this.toggleMultiSelection,
  190. 'click .open-minicard-composer': this.scrollToBottom,
  191. submit: this.addCard,
  192. },
  193. ];
  194. },
  195. }).register('listBody');
  196. function toggleValueInReactiveArray(reactiveValue, value) {
  197. const array = reactiveValue.get();
  198. const valueIndex = array.indexOf(value);
  199. if (valueIndex === -1) {
  200. array.push(value);
  201. } else {
  202. array.splice(valueIndex, 1);
  203. }
  204. reactiveValue.set(array);
  205. }
  206. BlazeComponent.extendComponent({
  207. onCreated() {
  208. this.labels = new ReactiveVar([]);
  209. this.members = new ReactiveVar([]);
  210. this.customFields = new ReactiveVar([]);
  211. const currentBoardId = Session.get('currentBoard');
  212. arr = [];
  213. _.forEach(
  214. Boards.findOne(currentBoardId)
  215. .customFields()
  216. .fetch(),
  217. function(field) {
  218. if (field.automaticallyOnCard || field.alwaysOnCard)
  219. arr.push({ _id: field._id, value: null });
  220. },
  221. );
  222. this.customFields.set(arr);
  223. },
  224. reset() {
  225. this.labels.set([]);
  226. this.members.set([]);
  227. this.customFields.set([]);
  228. },
  229. getLabels() {
  230. const currentBoardId = Session.get('currentBoard');
  231. return Boards.findOne(currentBoardId).labels.filter(label => {
  232. return this.labels.get().indexOf(label._id) > -1;
  233. });
  234. },
  235. pressKey(evt) {
  236. // Pressing Enter should submit the card
  237. if (evt.keyCode === 13 && !evt.shiftKey) {
  238. evt.preventDefault();
  239. const $form = $(evt.currentTarget).closest('form');
  240. // XXX For some reason $form.submit() does not work (it's probably a bug
  241. // of blaze-component related to the fact that the submit event is non-
  242. // bubbling). This is why we click on the submit button instead -- which
  243. // work.
  244. $form.find('button[type=submit]').click();
  245. // Pressing Tab should open the form of the next column, and Maj+Tab go
  246. // in the reverse order
  247. } else if (evt.keyCode === 9) {
  248. evt.preventDefault();
  249. const isReverse = evt.shiftKey;
  250. const list = $(`#js-list-${this.data().listId}`);
  251. const listSelector = '.js-list:not(.js-list-composer)';
  252. let nextList = list[isReverse ? 'prev' : 'next'](listSelector).get(0);
  253. // If there is no next list, loop back to the beginning.
  254. if (!nextList) {
  255. nextList = $(listSelector + (isReverse ? ':last' : ':first')).get(0);
  256. }
  257. BlazeComponent.getComponentForElement(nextList).openForm({
  258. position: this.data().position,
  259. });
  260. }
  261. },
  262. events() {
  263. return [
  264. {
  265. keydown: this.pressKey,
  266. 'click .js-link': Popup.open('linkCard'),
  267. 'click .js-search': Popup.open('searchElement'),
  268. 'click .js-card-template': Popup.open('searchElement'),
  269. },
  270. ];
  271. },
  272. onRendered() {
  273. const editor = this;
  274. const $textarea = this.$('textarea');
  275. autosize($textarea);
  276. $textarea.escapeableTextComplete(
  277. [
  278. // User mentions
  279. {
  280. match: /\B@([\w.]*)$/,
  281. search(term, callback) {
  282. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  283. callback(
  284. $.map(currentBoard.activeMembers(), member => {
  285. const user = Users.findOne(member.userId);
  286. return user.username.indexOf(term) === 0 ? user : null;
  287. }),
  288. );
  289. },
  290. template(user) {
  291. return user.username;
  292. },
  293. replace(user) {
  294. toggleValueInReactiveArray(editor.members, user._id);
  295. return '';
  296. },
  297. index: 1,
  298. },
  299. // Labels
  300. {
  301. match: /\B#(\w*)$/,
  302. search(term, callback) {
  303. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  304. callback(
  305. $.map(currentBoard.labels, label => {
  306. if (
  307. label.name.indexOf(term) > -1 ||
  308. label.color.indexOf(term) > -1
  309. ) {
  310. return label;
  311. }
  312. return null;
  313. }),
  314. );
  315. },
  316. template(label) {
  317. return Blaze.toHTMLWithData(Template.autocompleteLabelLine, {
  318. hasNoName: !label.name,
  319. colorName: label.color,
  320. labelName: label.name || label.color,
  321. });
  322. },
  323. replace(label) {
  324. toggleValueInReactiveArray(editor.labels, label._id);
  325. return '';
  326. },
  327. index: 1,
  328. },
  329. ],
  330. {
  331. // When the autocomplete menu is shown we want both a press of both `Tab`
  332. // or `Enter` to validation the auto-completion. We also need to stop the
  333. // event propagation to prevent the card from submitting (on `Enter`) or
  334. // going on the next column (on `Tab`).
  335. onKeydown(evt, commands) {
  336. if (evt.keyCode === 9 || evt.keyCode === 13) {
  337. evt.stopPropagation();
  338. return commands.KEY_ENTER;
  339. }
  340. return null;
  341. },
  342. },
  343. );
  344. },
  345. }).register('addCardForm');
  346. BlazeComponent.extendComponent({
  347. onCreated() {
  348. this.selectedBoardId = new ReactiveVar('');
  349. this.selectedSwimlaneId = new ReactiveVar('');
  350. this.selectedListId = new ReactiveVar('');
  351. this.boardId = Session.get('currentBoard');
  352. // In order to get current board info
  353. subManager.subscribe('board', this.boardId, false);
  354. this.board = Boards.findOne(this.boardId);
  355. // List where to insert card
  356. const list = $(Popup._getTopStack().openerElement).closest('.js-list');
  357. this.listId = Blaze.getData(list[0])._id;
  358. // Swimlane where to insert card
  359. const swimlane = $(Popup._getTopStack().openerElement).closest(
  360. '.js-swimlane',
  361. );
  362. this.swimlaneId = '';
  363. if (Utils.boardView() === 'board-view-swimlanes')
  364. this.swimlaneId = Blaze.getData(swimlane[0])._id;
  365. else if (Utils.boardView() === 'board-view-lists' || !Utils.boardView)
  366. this.swimlaneId = Swimlanes.findOne({ boardId: this.boardId })._id;
  367. },
  368. boards() {
  369. const boards = Boards.find(
  370. {
  371. archived: false,
  372. 'members.userId': Meteor.userId(),
  373. _id: { $ne: Session.get('currentBoard') },
  374. type: 'board',
  375. },
  376. {
  377. sort: { sort: 1 /* boards default sorting */ },
  378. },
  379. );
  380. return boards;
  381. },
  382. swimlanes() {
  383. if (!this.selectedBoardId.get()) {
  384. return [];
  385. }
  386. const swimlanes = Swimlanes.find({ boardId: this.selectedBoardId.get() });
  387. if (swimlanes.count())
  388. this.selectedSwimlaneId.set(swimlanes.fetch()[0]._id);
  389. return swimlanes;
  390. },
  391. lists() {
  392. if (!this.selectedBoardId.get()) {
  393. return [];
  394. }
  395. const lists = Lists.find({ boardId: this.selectedBoardId.get() });
  396. if (lists.count()) this.selectedListId.set(lists.fetch()[0]._id);
  397. return lists;
  398. },
  399. cards() {
  400. if (!this.board) {
  401. return [];
  402. }
  403. const ownCardsIds = this.board.cards().map(card => {
  404. return card.linkedId || card._id;
  405. });
  406. return Cards.find({
  407. boardId: this.selectedBoardId.get(),
  408. swimlaneId: this.selectedSwimlaneId.get(),
  409. listId: this.selectedListId.get(),
  410. archived: false,
  411. linkedId: { $nin: ownCardsIds },
  412. _id: { $nin: ownCardsIds },
  413. type: { $nin: ['template-card'] },
  414. });
  415. },
  416. events() {
  417. return [
  418. {
  419. 'change .js-select-boards'(evt) {
  420. subManager.subscribe('board', $(evt.currentTarget).val(), false);
  421. this.selectedBoardId.set($(evt.currentTarget).val());
  422. },
  423. 'change .js-select-swimlanes'(evt) {
  424. this.selectedSwimlaneId.set($(evt.currentTarget).val());
  425. },
  426. 'change .js-select-lists'(evt) {
  427. this.selectedListId.set($(evt.currentTarget).val());
  428. },
  429. 'click .js-done'(evt) {
  430. // LINK CARD
  431. evt.stopPropagation();
  432. evt.preventDefault();
  433. const linkedId = $('.js-select-cards option:selected').val();
  434. if (!linkedId) {
  435. Popup.close();
  436. return;
  437. }
  438. const _id = Cards.insert({
  439. title: $('.js-select-cards option:selected').text(), //dummy
  440. listId: this.listId,
  441. swimlaneId: this.swimlaneId,
  442. boardId: this.boardId,
  443. sort: Lists.findOne(this.listId)
  444. .cards()
  445. .count(),
  446. type: 'cardType-linkedCard',
  447. linkedId,
  448. });
  449. Filter.addException(_id);
  450. Popup.close();
  451. },
  452. 'click .js-link-board'(evt) {
  453. //LINK BOARD
  454. evt.stopPropagation();
  455. evt.preventDefault();
  456. const impBoardId = $('.js-select-boards option:selected').val();
  457. if (
  458. !impBoardId ||
  459. Cards.findOne({ linkedId: impBoardId, archived: false })
  460. ) {
  461. Popup.close();
  462. return;
  463. }
  464. const _id = Cards.insert({
  465. title: $('.js-select-boards option:selected').text(), //dummy
  466. listId: this.listId,
  467. swimlaneId: this.swimlaneId,
  468. boardId: this.boardId,
  469. sort: Lists.findOne(this.listId)
  470. .cards()
  471. .count(),
  472. type: 'cardType-linkedBoard',
  473. linkedId: impBoardId,
  474. });
  475. Filter.addException(_id);
  476. Popup.close();
  477. },
  478. },
  479. ];
  480. },
  481. }).register('linkCardPopup');
  482. BlazeComponent.extendComponent({
  483. mixins() {
  484. return [];
  485. },
  486. onCreated() {
  487. this.isCardTemplateSearch = $(Popup._getTopStack().openerElement).hasClass(
  488. 'js-card-template',
  489. );
  490. this.isListTemplateSearch = $(Popup._getTopStack().openerElement).hasClass(
  491. 'js-list-template',
  492. );
  493. this.isSwimlaneTemplateSearch = $(
  494. Popup._getTopStack().openerElement,
  495. ).hasClass('js-open-add-swimlane-menu');
  496. this.isBoardTemplateSearch = $(Popup._getTopStack().openerElement).hasClass(
  497. 'js-add-board',
  498. );
  499. this.isTemplateSearch =
  500. this.isCardTemplateSearch ||
  501. this.isListTemplateSearch ||
  502. this.isSwimlaneTemplateSearch ||
  503. this.isBoardTemplateSearch;
  504. let board = {};
  505. if (this.isTemplateSearch) {
  506. board = Boards.findOne((Meteor.user().profile || {}).templatesBoardId);
  507. } else {
  508. // Prefetch first non-current board id
  509. board = Boards.find({
  510. archived: false,
  511. 'members.userId': Meteor.userId(),
  512. _id: {
  513. $nin: [
  514. Session.get('currentBoard'),
  515. (Meteor.user().profile || {}).templatesBoardId,
  516. ],
  517. },
  518. });
  519. }
  520. if (!board) {
  521. Popup.close();
  522. return;
  523. }
  524. const boardId = board._id;
  525. // Subscribe to this board
  526. subManager.subscribe('board', boardId, false);
  527. this.selectedBoardId = new ReactiveVar(boardId);
  528. if (!this.isBoardTemplateSearch) {
  529. this.boardId = Session.get('currentBoard');
  530. // In order to get current board info
  531. subManager.subscribe('board', this.boardId, false);
  532. this.swimlaneId = '';
  533. // Swimlane where to insert card
  534. const swimlane = $(Popup._getTopStack().openerElement).parents(
  535. '.js-swimlane',
  536. );
  537. if (Utils.boardView() === 'board-view-swimlanes')
  538. this.swimlaneId = Blaze.getData(swimlane[0])._id;
  539. else this.swimlaneId = Swimlanes.findOne({ boardId: this.boardId })._id;
  540. // List where to insert card
  541. const list = $(Popup._getTopStack().openerElement).closest('.js-list');
  542. this.listId = Blaze.getData(list[0])._id;
  543. }
  544. this.term = new ReactiveVar('');
  545. },
  546. boards() {
  547. const boards = Boards.find(
  548. {
  549. archived: false,
  550. 'members.userId': Meteor.userId(),
  551. _id: { $ne: Session.get('currentBoard') },
  552. type: 'board',
  553. },
  554. {
  555. sort: { sort: 1 /* boards default sorting */ },
  556. },
  557. );
  558. return boards;
  559. },
  560. results() {
  561. if (!this.selectedBoardId) {
  562. return [];
  563. }
  564. const board = Boards.findOne(this.selectedBoardId.get());
  565. if (!this.isTemplateSearch || this.isCardTemplateSearch) {
  566. return board.searchCards(this.term.get(), false);
  567. } else if (this.isListTemplateSearch) {
  568. return board.searchLists(this.term.get());
  569. } else if (this.isSwimlaneTemplateSearch) {
  570. return board.searchSwimlanes(this.term.get());
  571. } else if (this.isBoardTemplateSearch) {
  572. const boards = board.searchBoards(this.term.get());
  573. boards.forEach(board => {
  574. subManager.subscribe('board', board.linkedId, false);
  575. });
  576. return boards;
  577. } else {
  578. return [];
  579. }
  580. },
  581. events() {
  582. return [
  583. {
  584. 'change .js-select-boards'(evt) {
  585. subManager.subscribe('board', $(evt.currentTarget).val(), false);
  586. this.selectedBoardId.set($(evt.currentTarget).val());
  587. },
  588. 'submit .js-search-term-form'(evt) {
  589. evt.preventDefault();
  590. this.term.set(evt.target.searchTerm.value);
  591. },
  592. 'click .js-minicard'(evt) {
  593. // 0. Common
  594. const title = $('.js-element-title')
  595. .val()
  596. .trim();
  597. if (!title) return;
  598. const element = Blaze.getData(evt.currentTarget);
  599. element.title = title;
  600. let _id = '';
  601. if (!this.isTemplateSearch || this.isCardTemplateSearch) {
  602. // Card insertion
  603. // 1. Common
  604. element.sort = Lists.findOne(this.listId)
  605. .cards()
  606. .count();
  607. // 1.A From template
  608. if (this.isTemplateSearch) {
  609. element.type = 'cardType-card';
  610. element.linkedId = '';
  611. _id = element.copy(this.boardId, this.swimlaneId, this.listId);
  612. // 1.B Linked card
  613. } else {
  614. _id = element.link(this.boardId, this.swimlaneId, this.listId);
  615. }
  616. Filter.addException(_id);
  617. // List insertion
  618. } else if (this.isListTemplateSearch) {
  619. element.sort = Swimlanes.findOne(this.swimlaneId)
  620. .lists()
  621. .count();
  622. element.type = 'list';
  623. _id = element.copy(this.boardId, this.swimlaneId);
  624. } else if (this.isSwimlaneTemplateSearch) {
  625. element.sort = Boards.findOne(this.boardId)
  626. .swimlanes()
  627. .count();
  628. element.type = 'swimlane';
  629. _id = element.copy(this.boardId);
  630. } else if (this.isBoardTemplateSearch) {
  631. Meteor.call(
  632. 'copyBoard',
  633. element.linkedId,
  634. {
  635. sort: Boards.find({ archived: false }).count(),
  636. type: 'board',
  637. title: element.title,
  638. },
  639. (err, data) => {
  640. _id = data;
  641. },
  642. );
  643. }
  644. Popup.close();
  645. },
  646. },
  647. ];
  648. },
  649. }).register('searchElementPopup');
  650. (class extends Spinner {
  651. onCreated() {
  652. this.cardlimit = this.parentComponent().cardlimit;
  653. this.listId = this.parentComponent().data()._id;
  654. this.swimlaneId = '';
  655. const isSandstorm =
  656. Meteor.settings &&
  657. Meteor.settings.public &&
  658. Meteor.settings.public.sandstorm;
  659. if (isSandstorm) {
  660. const user = Meteor.user();
  661. if (user) {
  662. if (Utils.boardView() === 'board-view-swimlanes') {
  663. this.swimlaneId = this.parentComponent()
  664. .parentComponent()
  665. .parentComponent()
  666. .data()._id;
  667. }
  668. }
  669. } else if (Utils.boardView() === 'board-view-swimlanes') {
  670. this.swimlaneId = this.parentComponent()
  671. .parentComponent()
  672. .parentComponent()
  673. .data()._id;
  674. }
  675. }
  676. onRendered() {
  677. this.spinner = this.find('.sk-spinner-list');
  678. this.container = this.$(this.spinner).parents('.list-body')[0];
  679. $(this.container).on(
  680. `scroll.spinner_${this.swimlaneId}_${this.listId}`,
  681. () => this.updateList(),
  682. );
  683. $(window).on(`resize.spinner_${this.swimlaneId}_${this.listId}`, () =>
  684. this.updateList(),
  685. );
  686. this.updateList();
  687. }
  688. onDestroyed() {
  689. $(this.container).off(`scroll.spinner_${this.swimlaneId}_${this.listId}`);
  690. $(window).off(`resize.spinner_${this.swimlaneId}_${this.listId}`);
  691. }
  692. checkIdleTime() {
  693. return window.requestIdleCallback ||
  694. function(handler) {
  695. const startTime = Date.now();
  696. return setTimeout(function() {
  697. handler({
  698. didTimeout: false,
  699. timeRemaining() {
  700. return Math.max(0, 50.0 - (Date.now() - startTime));
  701. },
  702. });
  703. }, 1);
  704. };
  705. }
  706. updateList() {
  707. // Use fallback when requestIdleCallback is not available on iOS and Safari
  708. // https://www.afasterweb.com/2017/11/20/utilizing-idle-moments/
  709. if (this.spinnerInView()) {
  710. this.cardlimit.set(this.cardlimit.get() + InfiniteScrollIter);
  711. this.checkIdleTime(() => this.updateList());
  712. }
  713. }
  714. spinnerInView() {
  715. // spinner deleted
  716. if (!this.spinner.offsetTop) {
  717. return false;
  718. }
  719. const parentViewHeight = this.container.clientHeight;
  720. const bottomViewPosition = this.container.scrollTop + parentViewHeight;
  721. let spinnerOffsetTop = this.spinner.offsetTop;
  722. const addCard = $(this.container).find("a.open-minicard-composer").first()[0];
  723. if (addCard !== undefined) {
  724. spinnerOffsetTop -= addCard.clientHeight;
  725. }
  726. return bottomViewPosition > spinnerOffsetTop;
  727. }
  728. getSkSpinnerName() {
  729. return "sk-spinner-" + super.getSpinnerName().toLowerCase();
  730. }
  731. }.register('spinnerList'));