listBody.js 24 KB

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