listBody.js 24 KB

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