listBody.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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. // Prevent custom focus movement on Tab key for accessibility
  282. // evt.preventDefault();
  283. const isReverse = evt.shiftKey;
  284. const list = $(`#js-list-${this.data().listId}`);
  285. const listSelector = '.js-list:not(.js-list-composer)';
  286. let nextList = list[isReverse ? 'prev' : 'next'](listSelector).get(0);
  287. // If there is no next list, loop back to the beginning.
  288. if (!nextList) {
  289. nextList = $(listSelector + (isReverse ? ':last' : ':first')).get(0);
  290. }
  291. BlazeComponent.getComponentForElement(nextList).openForm({
  292. position: this.data().position,
  293. });
  294. }
  295. },
  296. events() {
  297. return [
  298. {
  299. keydown: this.pressKey,
  300. 'click .js-link': Popup.open('linkCard'),
  301. 'click .js-search': Popup.open('searchElement'),
  302. 'click .js-card-template': Popup.open('searchElement'),
  303. },
  304. ];
  305. },
  306. onRendered() {
  307. const editor = this;
  308. const $textarea = this.$('textarea');
  309. autosize($textarea);
  310. $textarea.escapeableTextComplete(
  311. [
  312. // User mentions
  313. {
  314. match: /\B@([\w.-]*)$/,
  315. search(term, callback) {
  316. const currentBoard = Utils.getCurrentBoard();
  317. callback(
  318. $.map(currentBoard.activeMembers(), member => {
  319. const user = ReactiveCache.getUser(member.userId);
  320. return user.username.indexOf(term) === 0 ? user : null;
  321. }),
  322. );
  323. },
  324. template(user) {
  325. if (user.profile && user.profile.fullname) {
  326. return (user.username + " (" + user.profile.fullname + ")");
  327. }
  328. return user.username;
  329. },
  330. replace(user) {
  331. toggleValueInReactiveArray(editor.members, user._id);
  332. return '';
  333. },
  334. index: 1,
  335. },
  336. // Labels
  337. {
  338. match: /\B#(\w*)$/,
  339. search(term, callback) {
  340. const currentBoard = Utils.getCurrentBoard();
  341. callback(
  342. $.map(currentBoard.labels, label => {
  343. if (label.name == undefined) {
  344. label.name = "";
  345. }
  346. if (
  347. label.name.indexOf(term) > -1 ||
  348. label.color.indexOf(term) > -1
  349. ) {
  350. return label;
  351. }
  352. return null;
  353. }),
  354. );
  355. },
  356. template(label) {
  357. return Blaze.toHTMLWithData(Template.autocompleteLabelLine, {
  358. hasNoName: !label.name,
  359. colorName: label.color,
  360. labelName: label.name || label.color,
  361. });
  362. },
  363. replace(label) {
  364. toggleValueInReactiveArray(editor.labels, label._id);
  365. return '';
  366. },
  367. index: 1,
  368. },
  369. ],
  370. {
  371. // When the autocomplete menu is shown we want both a press of both `Tab`
  372. // or `Enter` to validation the auto-completion. We also need to stop the
  373. // event propagation to prevent the card from submitting (on `Enter`) or
  374. // going on the next column (on `Tab`).
  375. onKeydown(evt, commands) {
  376. // Prevent custom focus movement on Tab key for accessibility
  377. // if (evt.keyCode === 9 || evt.keyCode === 13) {
  378. // evt.stopPropagation();
  379. // return commands.KEY_ENTER;
  380. //}
  381. return null;
  382. },
  383. },
  384. );
  385. },
  386. }).register('addCardForm');
  387. BlazeComponent.extendComponent({
  388. onCreated() {
  389. this.selectedBoardId = new ReactiveVar('');
  390. this.selectedSwimlaneId = new ReactiveVar('');
  391. this.selectedListId = new ReactiveVar('');
  392. this.boardId = Session.get('currentBoard');
  393. // In order to get current board info
  394. subManager.subscribe('board', this.boardId, false);
  395. this.board = ReactiveCache.getBoard(this.boardId);
  396. // List where to insert card
  397. this.list = $(Popup._getTopStack().openerElement).closest('.js-list');
  398. this.listId = Blaze.getData(this.list[0])._id;
  399. // Swimlane where to insert card
  400. const swimlane = $(Popup._getTopStack().openerElement).closest(
  401. '.js-swimlane',
  402. );
  403. this.swimlaneId = '';
  404. if (Utils.boardView() === 'board-view-swimlanes')
  405. this.swimlaneId = Blaze.getData(swimlane[0])._id;
  406. else if (Utils.boardView() === 'board-view-lists' || !Utils.boardView)
  407. this.swimlaneId = ReactiveCache.getSwimlane({ boardId: this.boardId })._id;
  408. },
  409. boards() {
  410. const ret = ReactiveCache.getBoards(
  411. {
  412. archived: false,
  413. 'members.userId': Meteor.userId(),
  414. _id: { $ne: Session.get('currentBoard') },
  415. type: 'board',
  416. },
  417. {
  418. sort: { sort: 1 /* boards default sorting */ },
  419. },
  420. );
  421. return ret;
  422. },
  423. swimlanes() {
  424. if (!this.selectedBoardId.get()) {
  425. return [];
  426. }
  427. const swimlanes = ReactiveCache.getSwimlanes(
  428. {
  429. boardId: this.selectedBoardId.get()
  430. },
  431. {
  432. sort: { sort: 1 },
  433. });
  434. if (swimlanes.length)
  435. this.selectedSwimlaneId.set(swimlanes[0]._id);
  436. return swimlanes;
  437. },
  438. lists() {
  439. if (!this.selectedBoardId.get()) {
  440. return [];
  441. }
  442. const lists = ReactiveCache.getLists(
  443. {
  444. boardId: this.selectedBoardId.get()
  445. },
  446. {
  447. sort: { sort: 1 },
  448. });
  449. if (lists.length) this.selectedListId.set(lists[0]._id);
  450. return lists;
  451. },
  452. cards() {
  453. if (!this.board) {
  454. return [];
  455. }
  456. const ownCardsIds = this.board.cards().map(card => card.getRealId());
  457. const ret = ReactiveCache.getCards(
  458. {
  459. boardId: this.selectedBoardId.get(),
  460. swimlaneId: this.selectedSwimlaneId.get(),
  461. listId: this.selectedListId.get(),
  462. archived: false,
  463. linkedId: { $nin: ownCardsIds },
  464. _id: { $nin: ownCardsIds },
  465. type: { $nin: ['template-card'] },
  466. },
  467. {
  468. sort: { sort: 1 },
  469. });
  470. return ret;
  471. },
  472. getSortIndex() {
  473. const position = this.currentData().position;
  474. let ret;
  475. if (position === 'top') {
  476. const firstCardDom = this.list.find('.js-minicard:first')[0];
  477. ret = Utils.calculateIndex(null, firstCardDom).base;
  478. } else if (position === 'bottom') {
  479. const lastCardDom = this.list.find('.js-minicard:last')[0];
  480. ret = Utils.calculateIndex(lastCardDom, null).base;
  481. }
  482. return ret;
  483. },
  484. events() {
  485. return [
  486. {
  487. 'change .js-select-boards'(evt) {
  488. subManager.subscribe('board', $(evt.currentTarget).val(), false);
  489. this.selectedBoardId.set($(evt.currentTarget).val());
  490. },
  491. 'change .js-select-swimlanes'(evt) {
  492. this.selectedSwimlaneId.set($(evt.currentTarget).val());
  493. },
  494. 'change .js-select-lists'(evt) {
  495. this.selectedListId.set($(evt.currentTarget).val());
  496. },
  497. 'click .js-done'(evt) {
  498. // LINK CARD
  499. evt.stopPropagation();
  500. evt.preventDefault();
  501. const linkedId = $('.js-select-cards option:selected').val();
  502. if (!linkedId) {
  503. Popup.back();
  504. return;
  505. }
  506. const nextCardNumber = this.board.getNextCardNumber();
  507. const sortIndex = this.getSortIndex();
  508. const _id = Cards.insert({
  509. title: $('.js-select-cards option:selected').text(), //dummy
  510. listId: this.listId,
  511. swimlaneId: this.swimlaneId,
  512. boardId: this.boardId,
  513. sort: sortIndex,
  514. type: 'cardType-linkedCard',
  515. linkedId,
  516. cardNumber: nextCardNumber,
  517. });
  518. Filter.addException(_id);
  519. Popup.back();
  520. },
  521. 'click .js-link-board'(evt) {
  522. //LINK BOARD
  523. evt.stopPropagation();
  524. evt.preventDefault();
  525. const impBoardId = $('.js-select-boards option:selected').val();
  526. if (
  527. !impBoardId ||
  528. ReactiveCache.getCard({ linkedId: impBoardId, archived: false })
  529. ) {
  530. Popup.back();
  531. return;
  532. }
  533. const nextCardNumber = this.board.getNextCardNumber();
  534. const sortIndex = this.getSortIndex();
  535. const _id = Cards.insert({
  536. title: $('.js-select-boards option:selected').text(), //dummy
  537. listId: this.listId,
  538. swimlaneId: this.swimlaneId,
  539. boardId: this.boardId,
  540. sort: sortIndex,
  541. type: 'cardType-linkedBoard',
  542. linkedId: impBoardId,
  543. cardNumber: nextCardNumber,
  544. });
  545. Filter.addException(_id);
  546. Popup.back();
  547. },
  548. },
  549. ];
  550. },
  551. }).register('linkCardPopup');
  552. Template.linkCardPopup.helpers({
  553. isTitleDefault(title) {
  554. // https://github.com/wekan/wekan/issues/4763
  555. // https://github.com/wekan/wekan/issues/4742
  556. // Translation text for "default" does not work, it returns an object.
  557. // When that happens, try use translation "defaultdefault" that has same content of default, or return text "Default".
  558. // This can happen, if swimlane does not have name.
  559. // Yes, this is fixing the symptom (Swimlane title does not have title)
  560. // instead of fixing the problem (Add Swimlane title when creating swimlane)
  561. // because there could be thousands of swimlanes, adding name Default to all of them
  562. // would be very slow.
  563. if (title.startsWith("key 'default") && title.endsWith('returned an object instead of string.')) {
  564. if (`${TAPi18n.__('defaultdefault')}`.startsWith("key 'default") && `${TAPi18n.__('defaultdefault')}`.endsWith('returned an object instead of string.')) {
  565. return 'Default';
  566. } else {
  567. return `${TAPi18n.__('defaultdefault')}`;
  568. }
  569. } else if (title === 'Default') {
  570. return `${TAPi18n.__('defaultdefault')}`;
  571. } else {
  572. return title;
  573. }
  574. },
  575. });
  576. BlazeComponent.extendComponent({
  577. mixins() {
  578. return [];
  579. },
  580. onCreated() {
  581. this.isCardTemplateSearch = $(Popup._getTopStack().openerElement).hasClass(
  582. 'js-card-template',
  583. );
  584. this.isListTemplateSearch = $(Popup._getTopStack().openerElement).hasClass(
  585. 'js-list-template',
  586. );
  587. this.isSwimlaneTemplateSearch = $(
  588. Popup._getTopStack().openerElement,
  589. ).hasClass('js-open-add-swimlane-menu');
  590. this.isBoardTemplateSearch = $(Popup._getTopStack().openerElement).hasClass(
  591. 'js-add-board',
  592. );
  593. this.isTemplateSearch =
  594. this.isCardTemplateSearch ||
  595. this.isListTemplateSearch ||
  596. this.isSwimlaneTemplateSearch ||
  597. this.isBoardTemplateSearch;
  598. this.board = {};
  599. if (this.isTemplateSearch) {
  600. const boardId = (ReactiveCache.getCurrentUser().profile || {}).templatesBoardId;
  601. if (boardId) {
  602. subManager.subscribe('board', boardId, false);
  603. this.board = ReactiveCache.getBoard(boardId);
  604. }
  605. } else {
  606. this.board = Utils.getCurrentBoard();
  607. }
  608. if (!this.board) {
  609. Popup.back();
  610. return;
  611. }
  612. this.boardId = this.board._id;
  613. // Subscribe to this board
  614. subManager.subscribe('board', this.boardId, false);
  615. this.selectedBoardId = new ReactiveVar(this.boardId);
  616. this.list = $(Popup._getTopStack().openerElement).closest('.js-list');
  617. if (!this.isBoardTemplateSearch) {
  618. this.swimlaneId = '';
  619. // Swimlane where to insert card
  620. const swimlane = $(Popup._getTopStack().openerElement).parents(
  621. '.js-swimlane',
  622. );
  623. if (Utils.boardView() === 'board-view-swimlanes')
  624. this.swimlaneId = Blaze.getData(swimlane[0])._id;
  625. else this.swimlaneId = ReactiveCache.getSwimlane({ boardId: this.boardId })._id;
  626. // List where to insert card
  627. this.listId = Blaze.getData(this.list[0])._id;
  628. }
  629. this.term = new ReactiveVar('');
  630. },
  631. boards() {
  632. const ret = ReactiveCache.getBoards(
  633. {
  634. archived: false,
  635. 'members.userId': Meteor.userId(),
  636. _id: { $ne: Session.get('currentBoard') },
  637. type: 'board',
  638. },
  639. {
  640. sort: { sort: 1 /* boards default sorting */ },
  641. },
  642. );
  643. return ret;
  644. },
  645. results() {
  646. if (!this.selectedBoardId) {
  647. return [];
  648. }
  649. const board = ReactiveCache.getBoard(this.selectedBoardId.get());
  650. if (!this.isTemplateSearch || this.isCardTemplateSearch) {
  651. return board.searchCards(this.term.get(), false);
  652. } else if (this.isListTemplateSearch) {
  653. return board.searchLists(this.term.get());
  654. } else if (this.isSwimlaneTemplateSearch) {
  655. return board.searchSwimlanes(this.term.get());
  656. } else if (this.isBoardTemplateSearch) {
  657. const boards = board.searchBoards(this.term.get());
  658. boards.forEach(board => {
  659. subManager.subscribe('board', board.linkedId, false);
  660. });
  661. return boards;
  662. } else {
  663. return [];
  664. }
  665. },
  666. getSortIndex() {
  667. const position = this.data().position;
  668. let ret;
  669. if (position === 'top') {
  670. const firstCardDom = this.list.find('.js-minicard:first')[0];
  671. ret = Utils.calculateIndex(null, firstCardDom).base;
  672. } else if (position === 'bottom') {
  673. const lastCardDom = this.list.find('.js-minicard:last')[0];
  674. ret = Utils.calculateIndex(lastCardDom, null).base;
  675. }
  676. return ret;
  677. },
  678. events() {
  679. return [
  680. {
  681. 'change .js-select-boards'(evt) {
  682. subManager.subscribe('board', $(evt.currentTarget).val(), false);
  683. this.selectedBoardId.set($(evt.currentTarget).val());
  684. },
  685. 'submit .js-search-term-form'(evt) {
  686. evt.preventDefault();
  687. this.term.set(evt.target.searchTerm.value);
  688. },
  689. 'click .js-minicard'(evt) {
  690. // 0. Common
  691. const title = $('.js-element-title')
  692. .val()
  693. .trim();
  694. if (!title) return;
  695. const element = Blaze.getData(evt.currentTarget);
  696. element.title = title;
  697. let _id = '';
  698. if (!this.isTemplateSearch || this.isCardTemplateSearch) {
  699. // Card insertion
  700. // 1. Common
  701. element.cardNumber = this.board.getNextCardNumber();
  702. element.sort = this.getSortIndex();
  703. // 1.A From template
  704. if (this.isTemplateSearch) {
  705. element.type = 'cardType-card';
  706. element.linkedId = '';
  707. _id = element.copy(this.boardId, this.swimlaneId, this.listId);
  708. // 1.B Linked card
  709. } else {
  710. _id = element.link(this.boardId, this.swimlaneId, this.listId);
  711. }
  712. Filter.addException(_id);
  713. // List insertion
  714. } else if (this.isListTemplateSearch) {
  715. element.sort = ReactiveCache.getSwimlane(this.swimlaneId)
  716. .lists()
  717. .length;
  718. element.type = 'list';
  719. _id = element.copy(this.boardId, this.swimlaneId);
  720. } else if (this.isSwimlaneTemplateSearch) {
  721. element.sort = ReactiveCache.getBoard(this.boardId)
  722. .swimlanes()
  723. .length;
  724. element.type = 'swimlane';
  725. _id = element.copy(this.boardId);
  726. } else if (this.isBoardTemplateSearch) {
  727. Meteor.call(
  728. 'copyBoard',
  729. element.linkedId,
  730. {
  731. sort: ReactiveCache.getBoards({ archived: false }).length,
  732. type: 'board',
  733. title: element.title,
  734. },
  735. (err, data) => {
  736. _id = data;
  737. subManager.subscribe('board', _id, false);
  738. FlowRouter.go('board', {
  739. id: _id,
  740. slug: getSlug(element.title),
  741. });
  742. },
  743. );
  744. }
  745. Popup.back();
  746. },
  747. },
  748. ];
  749. },
  750. }).register('searchElementPopup');
  751. (class extends Spinner {
  752. onCreated() {
  753. this.cardlimit = this.parentComponent().cardlimit;
  754. this.listId = this.parentComponent().data()._id;
  755. this.swimlaneId = '';
  756. const isSandstorm =
  757. Meteor.settings &&
  758. Meteor.settings.public &&
  759. Meteor.settings.public.sandstorm;
  760. if (isSandstorm) {
  761. const user = ReactiveCache.getCurrentUser();
  762. if (user) {
  763. if (Utils.boardView() === 'board-view-swimlanes') {
  764. this.swimlaneId = this.parentComponent()
  765. .parentComponent()
  766. .parentComponent()
  767. .data()._id;
  768. }
  769. }
  770. } else if (Utils.boardView() === 'board-view-swimlanes') {
  771. this.swimlaneId = this.parentComponent()
  772. .parentComponent()
  773. .parentComponent()
  774. .data()._id;
  775. }
  776. }
  777. onRendered() {
  778. this.spinner = this.find('.sk-spinner-list');
  779. this.container = this.$(this.spinner).parents('.list-body')[0];
  780. $(this.container).on(
  781. `scroll.spinner_${this.swimlaneId}_${this.listId}`,
  782. () => this.updateList(),
  783. );
  784. $(window).on(`resize.spinner_${this.swimlaneId}_${this.listId}`, () =>
  785. this.updateList(),
  786. );
  787. this.updateList();
  788. }
  789. onDestroyed() {
  790. $(this.container).off(`scroll.spinner_${this.swimlaneId}_${this.listId}`);
  791. $(window).off(`resize.spinner_${this.swimlaneId}_${this.listId}`);
  792. }
  793. checkIdleTime() {
  794. return window.requestIdleCallback ||
  795. function (handler) {
  796. const startTime = Date.now();
  797. return setTimeout(function () {
  798. handler({
  799. didTimeout: false,
  800. timeRemaining() {
  801. return Math.max(0, 50.0 - (Date.now() - startTime));
  802. },
  803. });
  804. }, 1);
  805. };
  806. }
  807. updateList() {
  808. // Use fallback when requestIdleCallback is not available on iOS and Safari
  809. // https://www.afasterweb.com/2017/11/20/utilizing-idle-moments/
  810. if (this.spinnerInView()) {
  811. this.cardlimit.set(this.cardlimit.get() + InfiniteScrollIter);
  812. this.checkIdleTime(() => this.updateList());
  813. }
  814. }
  815. spinnerInView() {
  816. // spinner deleted
  817. if (!this.spinner.offsetTop) {
  818. return false;
  819. }
  820. const spinnerViewPosition = this.spinner.offsetTop - this.container.offsetTop + this.spinner.clientHeight;
  821. const parentViewHeight = this.container.clientHeight;
  822. const bottomViewPosition = this.container.scrollTop + parentViewHeight;
  823. return bottomViewPosition > spinnerViewPosition;
  824. }
  825. getSkSpinnerName() {
  826. return "sk-spinner-" + super.getSpinnerName().toLowerCase();
  827. }
  828. }.register('spinnerList'));