listBody.js 24 KB

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