listBody.js 24 KB

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