listBody.js 23 KB

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