listBody.js 24 KB

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