listBody.js 27 KB

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