listBody.js 27 KB

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