listBody.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. import { TAPi18n } from '/imports/i18n';
  3. import { Spinner } from '/client/lib/spinner';
  4. const subManager = new SubsManager();
  5. const InfiniteScrollIter = 10;
  6. BlazeComponent.extendComponent({
  7. onCreated() {
  8. // for infinite scrolling
  9. this.cardlimit = new ReactiveVar(InfiniteScrollIter);
  10. },
  11. mixins() {
  12. return [];
  13. },
  14. customFieldsSum() {
  15. return CustomFields.find({
  16. boardIds: { $in: [Session.get('currentBoard')] },
  17. showSumAtTopOfList: true,
  18. });
  19. },
  20. openForm(options) {
  21. options = options || {};
  22. options.position = options.position || 'top';
  23. const forms = this.childComponents('inlinedForm');
  24. let form = forms.find(component => {
  25. return component.data().position === options.position;
  26. });
  27. if (!form && forms.length > 0) {
  28. form = forms[0];
  29. }
  30. form.open();
  31. },
  32. addCard(evt) {
  33. evt.preventDefault();
  34. const firstCardDom = this.find('.js-minicard:first');
  35. const lastCardDom = this.find('.js-minicard:last');
  36. const textarea = $(evt.currentTarget).find('textarea');
  37. const position = this.currentData().position;
  38. const title = textarea.val().trim();
  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 formComponent = this.cardFormComponent();
  46. const members = formComponent.members.get();
  47. const labelIds = formComponent.labels.get();
  48. const customFields = formComponent.customFields.get();
  49. const board = this.data().board();
  50. let linkedId = '';
  51. let swimlaneId = '';
  52. let cardType = 'cardType-card';
  53. if (title) {
  54. if (board.isTemplatesBoard()) {
  55. swimlaneId = this.parentComponent()
  56. .parentComponent()
  57. .data()._id; // Always swimlanes view
  58. const swimlane = Swimlanes.findOne(swimlaneId);
  59. // If this is the card templates swimlane, insert a card template
  60. if (swimlane.isCardTemplatesSwimlane()) cardType = 'template-card';
  61. // If this is the board templates swimlane, insert a board template and a linked card
  62. else if (swimlane.isBoardTemplatesSwimlane()) {
  63. linkedId = Boards.insert({
  64. title,
  65. permission: 'private',
  66. type: 'template-board',
  67. });
  68. Swimlanes.insert({
  69. title: TAPi18n.__('default'),
  70. boardId: linkedId,
  71. });
  72. cardType = 'cardType-linkedBoard';
  73. }
  74. } else if (Utils.boardView() === 'board-view-swimlanes')
  75. swimlaneId = this.parentComponent()
  76. .parentComponent()
  77. .data()._id;
  78. else if (
  79. Utils.boardView() === 'board-view-lists' ||
  80. Utils.boardView() === 'board-view-cal' ||
  81. !Utils.boardView()
  82. )
  83. swimlaneId = board.getDefaultSwimline()._id;
  84. const nextCardNumber = board.getNextCardNumber();
  85. const _id = Cards.insert({
  86. title,
  87. members,
  88. labelIds,
  89. customFields,
  90. listId: this.data()._id,
  91. boardId: board._id,
  92. sort: sortIndex,
  93. swimlaneId,
  94. type: cardType,
  95. cardNumber: nextCardNumber,
  96. linkedId,
  97. });
  98. // if the displayed card count is less than the total cards in the list,
  99. // we need to increment the displayed card count to prevent the spinner
  100. // to appear
  101. const cardCount = this.data()
  102. .cards(this.idOrNull(swimlaneId))
  103. .count();
  104. if (this.cardlimit.get() < cardCount) {
  105. this.cardlimit.set(this.cardlimit.get() + InfiniteScrollIter);
  106. }
  107. // In case the filter is active we need to add the newly inserted card in
  108. // the list of exceptions -- cards that are not filtered. Otherwise the
  109. // card will disappear instantly.
  110. // See https://github.com/wekan/wekan/issues/80
  111. Filter.addException(_id);
  112. // We keep the form opened, empty it, and scroll to it.
  113. textarea.val('').focus();
  114. autosize.update(textarea);
  115. if (position === 'bottom') {
  116. this.scrollToBottom();
  117. }
  118. }
  119. },
  120. cardFormComponent() {
  121. for (const inlinedForm of this.childComponents('inlinedForm')) {
  122. const [addCardForm] = inlinedForm.childComponents('addCardForm');
  123. if (addCardForm) {
  124. return addCardForm;
  125. }
  126. }
  127. return null;
  128. },
  129. scrollToBottom() {
  130. const container = this.firstNode();
  131. $(container).animate({
  132. scrollTop: container.scrollHeight,
  133. });
  134. },
  135. clickOnMiniCard(evt) {
  136. if (MultiSelection.isActive() || evt.shiftKey) {
  137. evt.stopImmediatePropagation();
  138. evt.preventDefault();
  139. const methodName = evt.shiftKey ? 'toggleRange' : 'toggle';
  140. MultiSelection[methodName](this.currentData()._id);
  141. // If the card is already selected, we want to de-select it.
  142. // XXX We should probably modify the minicard href attribute instead of
  143. // overwriting the event in case the card is already selected.
  144. } else if (Utils.isMiniScreen()) {
  145. evt.preventDefault();
  146. Session.set('popupCardId', this.currentData()._id);
  147. this.cardDetailsPopup(evt);
  148. } else if (Session.equals('currentCard', this.currentData()._id)) {
  149. evt.stopImmediatePropagation();
  150. evt.preventDefault();
  151. Utils.goBoardId(Session.get('currentBoard'));
  152. }
  153. },
  154. cardIsSelected() {
  155. return Session.equals('currentCard', this.currentData()._id);
  156. },
  157. toggleMultiSelection(evt) {
  158. evt.stopPropagation();
  159. evt.preventDefault();
  160. MultiSelection.toggle(this.currentData()._id);
  161. },
  162. idOrNull(swimlaneId) {
  163. if (
  164. Utils.boardView() === 'board-view-swimlanes' ||
  165. this.data()
  166. .board()
  167. .isTemplatesBoard()
  168. )
  169. return swimlaneId;
  170. return undefined;
  171. },
  172. cardsWithLimit(swimlaneId) {
  173. const limit = this.cardlimit.get();
  174. const defaultSort = { sort: 1 };
  175. const sortBy = Session.get('sortBy') ? Session.get('sortBy') : defaultSort;
  176. const selector = {
  177. listId: this.currentData()._id,
  178. archived: false,
  179. };
  180. if (swimlaneId) selector.swimlaneId = swimlaneId;
  181. return Cards.find(Filter.mongoSelector(selector), {
  182. // sort: ['sort'],
  183. sort: sortBy,
  184. limit,
  185. });
  186. },
  187. showSpinner(swimlaneId) {
  188. const list = Template.currentData();
  189. return list.cards(swimlaneId).count() > this.cardlimit.get();
  190. },
  191. canSeeAddCard() {
  192. return (
  193. !this.reachedWipLimit() &&
  194. Meteor.user() &&
  195. Meteor.user().isBoardMember() &&
  196. !Meteor.user().isCommentOnly() &&
  197. !Meteor.user().isWorker()
  198. );
  199. },
  200. reachedWipLimit() {
  201. const list = Template.currentData();
  202. return (
  203. !list.getWipLimit('soft') &&
  204. list.getWipLimit('enabled') &&
  205. list.getWipLimit('value') <= list.cards().count()
  206. );
  207. },
  208. cardDetailsPopup(event) {
  209. if (!Popup.isOpen()) {
  210. Popup.open("cardDetails")(event);
  211. }
  212. },
  213. events() {
  214. return [
  215. {
  216. 'click .js-minicard': this.clickOnMiniCard,
  217. 'click .js-toggle-multi-selection': this.toggleMultiSelection,
  218. 'click .open-minicard-composer': this.scrollToBottom,
  219. submit: this.addCard,
  220. },
  221. ];
  222. },
  223. }).register('listBody');
  224. function toggleValueInReactiveArray(reactiveValue, value) {
  225. const array = reactiveValue.get();
  226. const valueIndex = array.indexOf(value);
  227. if (valueIndex === -1) {
  228. array.push(value);
  229. } else {
  230. array.splice(valueIndex, 1);
  231. }
  232. reactiveValue.set(array);
  233. }
  234. BlazeComponent.extendComponent({
  235. onCreated() {
  236. this.labels = new ReactiveVar([]);
  237. this.members = new ReactiveVar([]);
  238. this.customFields = new ReactiveVar([]);
  239. const currentBoardId = Session.get('currentBoard');
  240. arr = [];
  241. _.forEach(
  242. ReactiveCache.getBoard(currentBoardId)
  243. .customFields()
  244. .fetch(),
  245. function (field) {
  246. if (field.automaticallyOnCard || field.alwaysOnCard)
  247. arr.push({ _id: field._id, value: null });
  248. },
  249. );
  250. this.customFields.set(arr);
  251. },
  252. reset() {
  253. this.labels.set([]);
  254. this.members.set([]);
  255. this.customFields.set([]);
  256. },
  257. getLabels() {
  258. const currentBoardId = Session.get('currentBoard');
  259. if (ReactiveCache.getBoard(currentBoardId).labels) {
  260. return ReactiveCache.getBoard(currentBoardId).labels.filter(label => {
  261. return this.labels.get().indexOf(label._id) > -1;
  262. });
  263. }
  264. return false;
  265. },
  266. pressKey(evt) {
  267. // Pressing Enter should submit the card
  268. if (evt.keyCode === 13 && !evt.shiftKey) {
  269. evt.preventDefault();
  270. const $form = $(evt.currentTarget).closest('form');
  271. // XXX For some reason $form.submit() does not work (it's probably a bug
  272. // of blaze-component related to the fact that the submit event is non-
  273. // bubbling). This is why we click on the submit button instead -- which
  274. // work.
  275. $form.find('button[type=submit]').click();
  276. // Pressing Tab should open the form of the next column, and Maj+Tab go
  277. // in the reverse order
  278. } else if (evt.keyCode === 9) {
  279. evt.preventDefault();
  280. const isReverse = evt.shiftKey;
  281. const list = $(`#js-list-${this.data().listId}`);
  282. const listSelector = '.js-list:not(.js-list-composer)';
  283. let nextList = list[isReverse ? 'prev' : 'next'](listSelector).get(0);
  284. // If there is no next list, loop back to the beginning.
  285. if (!nextList) {
  286. nextList = $(listSelector + (isReverse ? ':last' : ':first')).get(0);
  287. }
  288. BlazeComponent.getComponentForElement(nextList).openForm({
  289. position: this.data().position,
  290. });
  291. }
  292. },
  293. events() {
  294. return [
  295. {
  296. keydown: this.pressKey,
  297. 'click .js-link': Popup.open('linkCard'),
  298. 'click .js-search': Popup.open('searchElement'),
  299. 'click .js-card-template': Popup.open('searchElement'),
  300. },
  301. ];
  302. },
  303. onRendered() {
  304. const editor = this;
  305. const $textarea = this.$('textarea');
  306. autosize($textarea);
  307. $textarea.escapeableTextComplete(
  308. [
  309. // User mentions
  310. {
  311. match: /\B@([\w.-]*)$/,
  312. search(term, callback) {
  313. const currentBoard = Utils.getCurrentBoard();
  314. callback(
  315. $.map(currentBoard.activeMembers(), member => {
  316. const user = Users.findOne(member.userId);
  317. return user.username.indexOf(term) === 0 ? user : null;
  318. }),
  319. );
  320. },
  321. template(user) {
  322. if (user.profile && user.profile.fullname) {
  323. return (user.username + " (" + user.profile.fullname + ")");
  324. }
  325. return user.username;
  326. },
  327. replace(user) {
  328. toggleValueInReactiveArray(editor.members, user._id);
  329. return '';
  330. },
  331. index: 1,
  332. },
  333. // Labels
  334. {
  335. match: /\B#(\w*)$/,
  336. search(term, callback) {
  337. const currentBoard = Utils.getCurrentBoard();
  338. callback(
  339. $.map(currentBoard.labels, label => {
  340. if (label.name == undefined) {
  341. label.name = "";
  342. }
  343. if (
  344. label.name.indexOf(term) > -1 ||
  345. label.color.indexOf(term) > -1
  346. ) {
  347. return label;
  348. }
  349. return null;
  350. }),
  351. );
  352. },
  353. template(label) {
  354. return Blaze.toHTMLWithData(Template.autocompleteLabelLine, {
  355. hasNoName: !label.name,
  356. colorName: label.color,
  357. labelName: label.name || label.color,
  358. });
  359. },
  360. replace(label) {
  361. toggleValueInReactiveArray(editor.labels, label._id);
  362. return '';
  363. },
  364. index: 1,
  365. },
  366. ],
  367. {
  368. // When the autocomplete menu is shown we want both a press of both `Tab`
  369. // or `Enter` to validation the auto-completion. We also need to stop the
  370. // event propagation to prevent the card from submitting (on `Enter`) or
  371. // going on the next column (on `Tab`).
  372. onKeydown(evt, commands) {
  373. if (evt.keyCode === 9 || evt.keyCode === 13) {
  374. evt.stopPropagation();
  375. return commands.KEY_ENTER;
  376. }
  377. return null;
  378. },
  379. },
  380. );
  381. },
  382. }).register('addCardForm');
  383. BlazeComponent.extendComponent({
  384. onCreated() {
  385. this.selectedBoardId = new ReactiveVar('');
  386. this.selectedSwimlaneId = new ReactiveVar('');
  387. this.selectedListId = new ReactiveVar('');
  388. this.boardId = Session.get('currentBoard');
  389. // In order to get current board info
  390. subManager.subscribe('board', this.boardId, false);
  391. this.board = ReactiveCache.getBoard(this.boardId);
  392. // List where to insert card
  393. const list = $(Popup._getTopStack().openerElement).closest('.js-list');
  394. this.listId = Blaze.getData(list[0])._id;
  395. // Swimlane where to insert card
  396. const swimlane = $(Popup._getTopStack().openerElement).closest(
  397. '.js-swimlane',
  398. );
  399. this.swimlaneId = '';
  400. if (Utils.boardView() === 'board-view-swimlanes')
  401. this.swimlaneId = Blaze.getData(swimlane[0])._id;
  402. else if (Utils.boardView() === 'board-view-lists' || !Utils.boardView)
  403. this.swimlaneId = Swimlanes.findOne({ boardId: this.boardId })._id;
  404. },
  405. boards() {
  406. const boards = Boards.find(
  407. {
  408. archived: false,
  409. 'members.userId': Meteor.userId(),
  410. _id: { $ne: Session.get('currentBoard') },
  411. type: 'board',
  412. },
  413. {
  414. sort: { sort: 1 /* boards default sorting */ },
  415. },
  416. );
  417. return boards;
  418. },
  419. swimlanes() {
  420. if (!this.selectedBoardId.get()) {
  421. return [];
  422. }
  423. const swimlanes = Swimlanes.find({ boardId: this.selectedBoardId.get() });
  424. if (swimlanes.count())
  425. this.selectedSwimlaneId.set(swimlanes.fetch()[0]._id);
  426. return swimlanes;
  427. },
  428. lists() {
  429. if (!this.selectedBoardId.get()) {
  430. return [];
  431. }
  432. const lists = Lists.find({ boardId: this.selectedBoardId.get() });
  433. if (lists.count()) this.selectedListId.set(lists.fetch()[0]._id);
  434. return lists;
  435. },
  436. cards() {
  437. if (!this.board) {
  438. return [];
  439. }
  440. const ownCardsIds = this.board.cards().map(card => {
  441. return card.linkedId || card._id;
  442. });
  443. return Cards.find({
  444. boardId: this.selectedBoardId.get(),
  445. swimlaneId: this.selectedSwimlaneId.get(),
  446. listId: this.selectedListId.get(),
  447. archived: false,
  448. linkedId: { $nin: ownCardsIds },
  449. _id: { $nin: ownCardsIds },
  450. type: { $nin: ['template-card'] },
  451. });
  452. },
  453. events() {
  454. return [
  455. {
  456. 'change .js-select-boards'(evt) {
  457. subManager.subscribe('board', $(evt.currentTarget).val(), false);
  458. this.selectedBoardId.set($(evt.currentTarget).val());
  459. },
  460. 'change .js-select-swimlanes'(evt) {
  461. this.selectedSwimlaneId.set($(evt.currentTarget).val());
  462. },
  463. 'change .js-select-lists'(evt) {
  464. this.selectedListId.set($(evt.currentTarget).val());
  465. },
  466. 'click .js-done'(evt) {
  467. // LINK CARD
  468. evt.stopPropagation();
  469. evt.preventDefault();
  470. const linkedId = $('.js-select-cards option:selected').val();
  471. if (!linkedId) {
  472. Popup.back();
  473. return;
  474. }
  475. const _id = Cards.insert({
  476. title: $('.js-select-cards option:selected').text(), //dummy
  477. listId: this.listId,
  478. swimlaneId: this.swimlaneId,
  479. boardId: this.boardId,
  480. sort: Lists.findOne(this.listId)
  481. .cards()
  482. .count(),
  483. type: 'cardType-linkedCard',
  484. linkedId,
  485. });
  486. Filter.addException(_id);
  487. Popup.back();
  488. },
  489. 'click .js-link-board'(evt) {
  490. //LINK BOARD
  491. evt.stopPropagation();
  492. evt.preventDefault();
  493. const impBoardId = $('.js-select-boards option:selected').val();
  494. if (
  495. !impBoardId ||
  496. Cards.findOne({ linkedId: impBoardId, archived: false })
  497. ) {
  498. Popup.back();
  499. return;
  500. }
  501. const _id = Cards.insert({
  502. title: $('.js-select-boards option:selected').text(), //dummy
  503. listId: this.listId,
  504. swimlaneId: this.swimlaneId,
  505. boardId: this.boardId,
  506. sort: Lists.findOne(this.listId)
  507. .cards()
  508. .count(),
  509. type: 'cardType-linkedBoard',
  510. linkedId: impBoardId,
  511. });
  512. Filter.addException(_id);
  513. Popup.back();
  514. },
  515. },
  516. ];
  517. },
  518. }).register('linkCardPopup');
  519. BlazeComponent.extendComponent({
  520. mixins() {
  521. return [];
  522. },
  523. onCreated() {
  524. this.isCardTemplateSearch = $(Popup._getTopStack().openerElement).hasClass(
  525. 'js-card-template',
  526. );
  527. this.isListTemplateSearch = $(Popup._getTopStack().openerElement).hasClass(
  528. 'js-list-template',
  529. );
  530. this.isSwimlaneTemplateSearch = $(
  531. Popup._getTopStack().openerElement,
  532. ).hasClass('js-open-add-swimlane-menu');
  533. this.isBoardTemplateSearch = $(Popup._getTopStack().openerElement).hasClass(
  534. 'js-add-board',
  535. );
  536. this.isTemplateSearch =
  537. this.isCardTemplateSearch ||
  538. this.isListTemplateSearch ||
  539. this.isSwimlaneTemplateSearch ||
  540. this.isBoardTemplateSearch;
  541. let board = {};
  542. if (this.isTemplateSearch) {
  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 = ReactiveCache.getBoard(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 = ReactiveCache.getBoard(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'));