listBody.js 25 KB

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