listBody.js 27 KB

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