listBody.js 24 KB

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