listBody.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. import { TAPi18n } from '/imports/i18n';
  3. import { Spinner } from '/client/lib/spinner';
  4. import { FlowRouter } from 'meteor/ostrio:flow-router-extra';
  5. const subManager = new SubsManager();
  6. const InfiniteScrollIter = 10;
  7. BlazeComponent.extendComponent({
  8. onCreated() {
  9. // for infinite scrolling
  10. this.cardlimit = new ReactiveVar(InfiniteScrollIter);
  11. },
  12. mixins() {
  13. return [];
  14. },
  15. customFieldsSum() {
  16. const ret = ReactiveCache.getCustomFields({
  17. boardIds: { $in: [Session.get('currentBoard')] },
  18. showSumAtTopOfList: true,
  19. });
  20. return ret;
  21. },
  22. openForm(options) {
  23. options = options || {};
  24. options.position = options.position || 'top';
  25. const forms = this.childComponents('inlinedForm');
  26. let form = forms.find(component => {
  27. return component.data().position === options.position;
  28. });
  29. if (!form && forms.length > 0) {
  30. form = forms[0];
  31. }
  32. form.open();
  33. },
  34. addCard(evt) {
  35. evt.preventDefault();
  36. const firstCardDom = this.find('.js-minicard:first');
  37. const lastCardDom = this.find('.js-minicard:last');
  38. const textarea = $(evt.currentTarget).find('textarea');
  39. const position = this.currentData().position;
  40. const title = textarea.val().trim();
  41. let sortIndex;
  42. if (position === 'top') {
  43. sortIndex = Utils.calculateIndex(null, firstCardDom).base;
  44. } else if (position === 'bottom') {
  45. sortIndex = Utils.calculateIndex(lastCardDom, null).base;
  46. }
  47. const formComponent = this.cardFormComponent();
  48. const members = formComponent.members.get();
  49. const labelIds = formComponent.labels.get();
  50. const customFields = formComponent.customFields.get();
  51. const board = this.data().board();
  52. let linkedId = '';
  53. let swimlaneId = '';
  54. let cardType = 'cardType-card';
  55. if (title) {
  56. if (board.isTemplatesBoard()) {
  57. swimlaneId = this.parentComponent()
  58. .parentComponent()
  59. .data()._id; // Always swimlanes view
  60. const swimlane = ReactiveCache.getSwimlane(swimlaneId);
  61. // If this is the card templates swimlane, insert a card template
  62. if (swimlane.isCardTemplatesSwimlane()) cardType = 'template-card';
  63. // If this is the board templates swimlane, insert a board template and a linked card
  64. else if (swimlane.isBoardTemplatesSwimlane()) {
  65. linkedId = Boards.insert({
  66. title,
  67. permission: 'private',
  68. type: 'template-board',
  69. });
  70. Swimlanes.insert({
  71. title: TAPi18n.__('default'),
  72. boardId: linkedId,
  73. });
  74. cardType = 'cardType-linkedBoard';
  75. }
  76. } else if (Utils.boardView() === 'board-view-swimlanes')
  77. swimlaneId = this.parentComponent()
  78. .parentComponent()
  79. .data()._id;
  80. else if (
  81. Utils.boardView() === 'board-view-lists' ||
  82. Utils.boardView() === 'board-view-cal' ||
  83. !Utils.boardView()
  84. )
  85. swimlaneId = board.getDefaultSwimline()._id;
  86. const nextCardNumber = board.getNextCardNumber();
  87. const _id = Cards.insert({
  88. title,
  89. members,
  90. labelIds,
  91. customFields,
  92. listId: this.data()._id,
  93. boardId: board._id,
  94. sort: sortIndex,
  95. swimlaneId,
  96. type: cardType,
  97. cardNumber: nextCardNumber,
  98. linkedId,
  99. });
  100. // if the displayed card count is less than the total cards in the list,
  101. // we need to increment the displayed card count to prevent the spinner
  102. // to appear
  103. const cardCount = this.data()
  104. .cards(this.idOrNull(swimlaneId))
  105. .length;
  106. if (this.cardlimit.get() < cardCount) {
  107. this.cardlimit.set(this.cardlimit.get() + InfiniteScrollIter);
  108. }
  109. // In case the filter is active we need to add the newly inserted card in
  110. // the list of exceptions -- cards that are not filtered. Otherwise the
  111. // card will disappear instantly.
  112. // See https://github.com/wekan/wekan/issues/80
  113. Filter.addException(_id);
  114. // We keep the form opened, empty it, and scroll to it.
  115. textarea.val('').focus();
  116. autosize.update(textarea);
  117. if (position === 'bottom') {
  118. this.scrollToBottom();
  119. }
  120. }
  121. },
  122. cardFormComponent() {
  123. for (const inlinedForm of this.childComponents('inlinedForm')) {
  124. const [addCardForm] = inlinedForm.childComponents('addCardForm');
  125. if (addCardForm) {
  126. return addCardForm;
  127. }
  128. }
  129. return null;
  130. },
  131. scrollToBottom() {
  132. const container = this.firstNode();
  133. $(container).animate({
  134. scrollTop: container.scrollHeight,
  135. });
  136. },
  137. clickOnMiniCard(evt) {
  138. if (MultiSelection.isActive() || evt.shiftKey) {
  139. evt.stopImmediatePropagation();
  140. evt.preventDefault();
  141. const methodName = evt.shiftKey ? 'toggleRange' : 'toggle';
  142. MultiSelection[methodName](this.currentData()._id);
  143. // If the card is already selected, we want to de-select it.
  144. // XXX We should probably modify the minicard href attribute instead of
  145. // overwriting the event in case the card is already selected.
  146. } else if (Utils.isMiniScreen()) {
  147. evt.preventDefault();
  148. Session.set('popupCardId', this.currentData()._id);
  149. this.cardDetailsPopup(evt);
  150. } else if (Session.equals('currentCard', this.currentData()._id)) {
  151. evt.stopImmediatePropagation();
  152. evt.preventDefault();
  153. Utils.goBoardId(Session.get('currentBoard'));
  154. }
  155. },
  156. cardIsSelected() {
  157. return Session.equals('currentCard', this.currentData()._id);
  158. },
  159. toggleMultiSelection(evt) {
  160. evt.stopPropagation();
  161. evt.preventDefault();
  162. MultiSelection.toggle(this.currentData()._id);
  163. },
  164. idOrNull(swimlaneId) {
  165. if (
  166. Utils.boardView() === 'board-view-swimlanes' ||
  167. this.data()
  168. .board()
  169. .isTemplatesBoard()
  170. )
  171. return swimlaneId;
  172. return undefined;
  173. },
  174. cardsWithLimit(swimlaneId) {
  175. const limit = this.cardlimit.get();
  176. const defaultSort = { sort: 1 };
  177. const sortBy = Session.get('sortBy') ? Session.get('sortBy') : defaultSort;
  178. const selector = {
  179. listId: this.currentData()._id,
  180. archived: false,
  181. };
  182. if (swimlaneId) selector.swimlaneId = swimlaneId;
  183. const ret = ReactiveCache.getCards(Filter.mongoSelector(selector), {
  184. // sort: ['sort'],
  185. sort: sortBy,
  186. limit,
  187. }, true);
  188. return ret;
  189. },
  190. showSpinner(swimlaneId) {
  191. const list = Template.currentData();
  192. return list.cards(swimlaneId).length > this.cardlimit.get();
  193. },
  194. canSeeAddCard() {
  195. return (
  196. !this.reachedWipLimit() &&
  197. Utils.canModifyCard()
  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().length
  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. 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 (ReactiveCache.getBoard(currentBoardId).labels) {
  259. return ReactiveCache.getBoard(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 = Utils.getCurrentBoard();
  313. callback(
  314. $.map(currentBoard.activeMembers(), member => {
  315. const user = ReactiveCache.getUser(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 = Utils.getCurrentBoard();
  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 = ReactiveCache.getBoard(this.boardId);
  391. // List where to insert card
  392. this.list = $(Popup._getTopStack().openerElement).closest('.js-list');
  393. this.listId = Blaze.getData(this.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 = ReactiveCache.getSwimlane({ boardId: this.boardId })._id;
  403. },
  404. boards() {
  405. const ret = ReactiveCache.getBoards(
  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 ret;
  417. },
  418. swimlanes() {
  419. if (!this.selectedBoardId.get()) {
  420. return [];
  421. }
  422. const swimlanes = ReactiveCache.getSwimlanes(
  423. {
  424. boardId: this.selectedBoardId.get()
  425. },
  426. {
  427. sort: { sort: 1 },
  428. });
  429. if (swimlanes.length)
  430. this.selectedSwimlaneId.set(swimlanes[0]._id);
  431. return swimlanes;
  432. },
  433. lists() {
  434. if (!this.selectedBoardId.get()) {
  435. return [];
  436. }
  437. const lists = ReactiveCache.getLists(
  438. {
  439. boardId: this.selectedBoardId.get()
  440. },
  441. {
  442. sort: { sort: 1 },
  443. });
  444. if (lists.length) this.selectedListId.set(lists[0]._id);
  445. return lists;
  446. },
  447. cards() {
  448. if (!this.board) {
  449. return [];
  450. }
  451. const ownCardsIds = this.board.cards().map(card => card.getRealId());
  452. const ret = ReactiveCache.getCards(
  453. {
  454. boardId: this.selectedBoardId.get(),
  455. swimlaneId: this.selectedSwimlaneId.get(),
  456. listId: this.selectedListId.get(),
  457. archived: false,
  458. linkedId: { $nin: ownCardsIds },
  459. _id: { $nin: ownCardsIds },
  460. type: { $nin: ['template-card'] },
  461. },
  462. {
  463. sort: { sort: 1 },
  464. });
  465. return ret;
  466. },
  467. getSortIndex() {
  468. const position = this.currentData().position;
  469. let ret;
  470. if (position === 'top') {
  471. const firstCardDom = this.list.find('.js-minicard:first')[0];
  472. ret = Utils.calculateIndex(null, firstCardDom).base;
  473. } else if (position === 'bottom') {
  474. const lastCardDom = this.list.find('.js-minicard:last')[0];
  475. ret = Utils.calculateIndex(lastCardDom, null).base;
  476. }
  477. return ret;
  478. },
  479. events() {
  480. return [
  481. {
  482. 'change .js-select-boards'(evt) {
  483. subManager.subscribe('board', $(evt.currentTarget).val(), false);
  484. this.selectedBoardId.set($(evt.currentTarget).val());
  485. },
  486. 'change .js-select-swimlanes'(evt) {
  487. this.selectedSwimlaneId.set($(evt.currentTarget).val());
  488. },
  489. 'change .js-select-lists'(evt) {
  490. this.selectedListId.set($(evt.currentTarget).val());
  491. },
  492. 'click .js-done'(evt) {
  493. // LINK CARD
  494. evt.stopPropagation();
  495. evt.preventDefault();
  496. const linkedId = $('.js-select-cards option:selected').val();
  497. if (!linkedId) {
  498. Popup.back();
  499. return;
  500. }
  501. const nextCardNumber = this.board.getNextCardNumber();
  502. const sortIndex = this.getSortIndex();
  503. const _id = Cards.insert({
  504. title: $('.js-select-cards option:selected').text(), //dummy
  505. listId: this.listId,
  506. swimlaneId: this.swimlaneId,
  507. boardId: this.boardId,
  508. sort: sortIndex,
  509. type: 'cardType-linkedCard',
  510. linkedId,
  511. cardNumber: nextCardNumber,
  512. });
  513. Filter.addException(_id);
  514. Popup.back();
  515. },
  516. 'click .js-link-board'(evt) {
  517. //LINK BOARD
  518. evt.stopPropagation();
  519. evt.preventDefault();
  520. const impBoardId = $('.js-select-boards option:selected').val();
  521. if (
  522. !impBoardId ||
  523. ReactiveCache.getCard({ linkedId: impBoardId, archived: false })
  524. ) {
  525. Popup.back();
  526. return;
  527. }
  528. const nextCardNumber = this.board.getNextCardNumber();
  529. const sortIndex = this.getSortIndex();
  530. const _id = Cards.insert({
  531. title: $('.js-select-boards option:selected').text(), //dummy
  532. listId: this.listId,
  533. swimlaneId: this.swimlaneId,
  534. boardId: this.boardId,
  535. sort: sortIndex,
  536. type: 'cardType-linkedBoard',
  537. linkedId: impBoardId,
  538. cardNumber: nextCardNumber,
  539. });
  540. Filter.addException(_id);
  541. Popup.back();
  542. },
  543. },
  544. ];
  545. },
  546. }).register('linkCardPopup');
  547. Template.linkCardPopup.helpers({
  548. isTitleDefault(title) {
  549. // https://github.com/wekan/wekan/issues/4763
  550. // https://github.com/wekan/wekan/issues/4742
  551. // Translation text for "default" does not work, it returns an object.
  552. // When that happens, try use translation "defaultdefault" that has same content of default, or return text "Default".
  553. // This can happen, if swimlane does not have name.
  554. // Yes, this is fixing the symptom (Swimlane title does not have title)
  555. // instead of fixing the problem (Add Swimlane title when creating swimlane)
  556. // because there could be thousands of swimlanes, adding name Default to all of them
  557. // would be very slow.
  558. if (title.startsWith("key 'default") && title.endsWith('returned an object instead of string.')) {
  559. if (`${TAPi18n.__('defaultdefault')}`.startsWith("key 'default") && `${TAPi18n.__('defaultdefault')}`.endsWith('returned an object instead of string.')) {
  560. return 'Default';
  561. } else {
  562. return `${TAPi18n.__('defaultdefault')}`;
  563. }
  564. } else if (title === 'Default') {
  565. return `${TAPi18n.__('defaultdefault')}`;
  566. } else {
  567. return title;
  568. }
  569. },
  570. });
  571. BlazeComponent.extendComponent({
  572. mixins() {
  573. return [];
  574. },
  575. onCreated() {
  576. this.isCardTemplateSearch = $(Popup._getTopStack().openerElement).hasClass(
  577. 'js-card-template',
  578. );
  579. this.isListTemplateSearch = $(Popup._getTopStack().openerElement).hasClass(
  580. 'js-list-template',
  581. );
  582. this.isSwimlaneTemplateSearch = $(
  583. Popup._getTopStack().openerElement,
  584. ).hasClass('js-open-add-swimlane-menu');
  585. this.isBoardTemplateSearch = $(Popup._getTopStack().openerElement).hasClass(
  586. 'js-add-board',
  587. );
  588. this.isTemplateSearch =
  589. this.isCardTemplateSearch ||
  590. this.isListTemplateSearch ||
  591. this.isSwimlaneTemplateSearch ||
  592. this.isBoardTemplateSearch;
  593. this.board = {};
  594. if (this.isTemplateSearch) {
  595. const boardId = (ReactiveCache.getCurrentUser().profile || {}).templatesBoardId;
  596. if (boardId) {
  597. subManager.subscribe('board', boardId, false);
  598. this.board = ReactiveCache.getBoard(boardId);
  599. }
  600. } else {
  601. this.board = Utils.getCurrentBoard();
  602. }
  603. if (!this.board) {
  604. Popup.back();
  605. return;
  606. }
  607. this.boardId = this.board._id;
  608. // Subscribe to this board
  609. subManager.subscribe('board', this.boardId, false);
  610. this.selectedBoardId = new ReactiveVar(this.boardId);
  611. this.list = $(Popup._getTopStack().openerElement).closest('.js-list');
  612. if (!this.isBoardTemplateSearch) {
  613. this.swimlaneId = '';
  614. // Swimlane where to insert card
  615. const swimlane = $(Popup._getTopStack().openerElement).parents(
  616. '.js-swimlane',
  617. );
  618. if (Utils.boardView() === 'board-view-swimlanes')
  619. this.swimlaneId = Blaze.getData(swimlane[0])._id;
  620. else this.swimlaneId = ReactiveCache.getSwimlane({ boardId: this.boardId })._id;
  621. // List where to insert card
  622. this.listId = Blaze.getData(this.list[0])._id;
  623. }
  624. this.term = new ReactiveVar('');
  625. },
  626. boards() {
  627. const ret = ReactiveCache.getBoards(
  628. {
  629. archived: false,
  630. 'members.userId': Meteor.userId(),
  631. _id: { $ne: Session.get('currentBoard') },
  632. type: 'board',
  633. },
  634. {
  635. sort: { sort: 1 /* boards default sorting */ },
  636. },
  637. );
  638. return ret;
  639. },
  640. results() {
  641. if (!this.selectedBoardId) {
  642. return [];
  643. }
  644. const board = ReactiveCache.getBoard(this.selectedBoardId.get());
  645. if (!this.isTemplateSearch || this.isCardTemplateSearch) {
  646. return board.searchCards(this.term.get(), false);
  647. } else if (this.isListTemplateSearch) {
  648. return board.searchLists(this.term.get());
  649. } else if (this.isSwimlaneTemplateSearch) {
  650. return board.searchSwimlanes(this.term.get());
  651. } else if (this.isBoardTemplateSearch) {
  652. const boards = board.searchBoards(this.term.get());
  653. boards.forEach(board => {
  654. subManager.subscribe('board', board.linkedId, false);
  655. });
  656. return boards;
  657. } else {
  658. return [];
  659. }
  660. },
  661. getSortIndex() {
  662. const position = this.data().position;
  663. let ret;
  664. if (position === 'top') {
  665. const firstCardDom = this.list.find('.js-minicard:first')[0];
  666. ret = Utils.calculateIndex(null, firstCardDom).base;
  667. } else if (position === 'bottom') {
  668. const lastCardDom = this.list.find('.js-minicard:last')[0];
  669. ret = Utils.calculateIndex(lastCardDom, null).base;
  670. }
  671. return ret;
  672. },
  673. events() {
  674. return [
  675. {
  676. 'change .js-select-boards'(evt) {
  677. subManager.subscribe('board', $(evt.currentTarget).val(), false);
  678. this.selectedBoardId.set($(evt.currentTarget).val());
  679. },
  680. 'submit .js-search-term-form'(evt) {
  681. evt.preventDefault();
  682. this.term.set(evt.target.searchTerm.value);
  683. },
  684. 'click .js-minicard'(evt) {
  685. // 0. Common
  686. const title = $('.js-element-title')
  687. .val()
  688. .trim();
  689. if (!title) return;
  690. const element = Blaze.getData(evt.currentTarget);
  691. element.title = title;
  692. let _id = '';
  693. if (!this.isTemplateSearch || this.isCardTemplateSearch) {
  694. // Card insertion
  695. // 1. Common
  696. element.cardNumber = this.board.getNextCardNumber();
  697. element.sort = this.getSortIndex();
  698. // 1.A From template
  699. if (this.isTemplateSearch) {
  700. element.type = 'cardType-card';
  701. element.linkedId = '';
  702. _id = element.copy(this.boardId, this.swimlaneId, this.listId);
  703. // 1.B Linked card
  704. } else {
  705. _id = element.link(this.boardId, this.swimlaneId, this.listId);
  706. }
  707. Filter.addException(_id);
  708. // List insertion
  709. } else if (this.isListTemplateSearch) {
  710. element.sort = ReactiveCache.getSwimlane(this.swimlaneId)
  711. .lists()
  712. .length;
  713. element.type = 'list';
  714. _id = element.copy(this.boardId, this.swimlaneId);
  715. } else if (this.isSwimlaneTemplateSearch) {
  716. element.sort = ReactiveCache.getBoard(this.boardId)
  717. .swimlanes()
  718. .length;
  719. element.type = 'swimlane';
  720. _id = element.copy(this.boardId);
  721. } else if (this.isBoardTemplateSearch) {
  722. Meteor.call(
  723. 'copyBoard',
  724. element.linkedId,
  725. {
  726. sort: ReactiveCache.getBoards({ archived: false }).length,
  727. type: 'board',
  728. title: element.title,
  729. },
  730. (err, data) => {
  731. _id = data;
  732. subManager.subscribe('board', _id, false);
  733. FlowRouter.go('board', {
  734. id: _id,
  735. slug: getSlug(element.title),
  736. });
  737. },
  738. );
  739. }
  740. Popup.back();
  741. },
  742. },
  743. ];
  744. },
  745. }).register('searchElementPopup');
  746. (class extends Spinner {
  747. onCreated() {
  748. this.cardlimit = this.parentComponent().cardlimit;
  749. this.listId = this.parentComponent().data()._id;
  750. this.swimlaneId = '';
  751. const isSandstorm =
  752. Meteor.settings &&
  753. Meteor.settings.public &&
  754. Meteor.settings.public.sandstorm;
  755. if (isSandstorm) {
  756. const user = ReactiveCache.getCurrentUser();
  757. if (user) {
  758. if (Utils.boardView() === 'board-view-swimlanes') {
  759. this.swimlaneId = this.parentComponent()
  760. .parentComponent()
  761. .parentComponent()
  762. .data()._id;
  763. }
  764. }
  765. } else if (Utils.boardView() === 'board-view-swimlanes') {
  766. this.swimlaneId = this.parentComponent()
  767. .parentComponent()
  768. .parentComponent()
  769. .data()._id;
  770. }
  771. }
  772. onRendered() {
  773. this.spinner = this.find('.sk-spinner-list');
  774. this.container = this.$(this.spinner).parents('.list-body')[0];
  775. $(this.container).on(
  776. `scroll.spinner_${this.swimlaneId}_${this.listId}`,
  777. () => this.updateList(),
  778. );
  779. $(window).on(`resize.spinner_${this.swimlaneId}_${this.listId}`, () =>
  780. this.updateList(),
  781. );
  782. this.updateList();
  783. }
  784. onDestroyed() {
  785. $(this.container).off(`scroll.spinner_${this.swimlaneId}_${this.listId}`);
  786. $(window).off(`resize.spinner_${this.swimlaneId}_${this.listId}`);
  787. }
  788. checkIdleTime() {
  789. return window.requestIdleCallback ||
  790. function (handler) {
  791. const startTime = Date.now();
  792. return setTimeout(function () {
  793. handler({
  794. didTimeout: false,
  795. timeRemaining() {
  796. return Math.max(0, 50.0 - (Date.now() - startTime));
  797. },
  798. });
  799. }, 1);
  800. };
  801. }
  802. updateList() {
  803. // Use fallback when requestIdleCallback is not available on iOS and Safari
  804. // https://www.afasterweb.com/2017/11/20/utilizing-idle-moments/
  805. if (this.spinnerInView()) {
  806. this.cardlimit.set(this.cardlimit.get() + InfiniteScrollIter);
  807. this.checkIdleTime(() => this.updateList());
  808. }
  809. }
  810. spinnerInView() {
  811. // spinner deleted
  812. if (!this.spinner.offsetTop) {
  813. return false;
  814. }
  815. const spinnerViewPosition = this.spinner.offsetTop - this.container.offsetTop + this.spinner.clientHeight;
  816. const parentViewHeight = this.container.clientHeight;
  817. const bottomViewPosition = this.container.scrollTop + parentViewHeight;
  818. return bottomViewPosition > spinnerViewPosition;
  819. }
  820. getSkSpinnerName() {
  821. return "sk-spinner-" + super.getSpinnerName().toLowerCase();
  822. }
  823. }.register('spinnerList'));