listBody.js 24 KB

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