listBody.js 27 KB

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