listBody.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. const subManager = new SubsManager();
  2. const InfiniteScrollIter = 10;
  3. BlazeComponent.extendComponent({
  4. onCreated() {
  5. // for infinite scrolling
  6. this.cardlimit = new ReactiveVar(InfiniteScrollIter);
  7. },
  8. onRendered() {
  9. const spinner = this.find('.sk-spinner-list');
  10. if (spinner) {
  11. const options = {
  12. root: null, // we check if the spinner is on the current viewport
  13. rootMargin: '0px',
  14. threshold: 0.25,
  15. };
  16. const observer = new IntersectionObserver((entries) => {
  17. entries.forEach((entry) => {
  18. if (entry.isIntersecting) {
  19. this.cardlimit.set(this.cardlimit.get() + InfiniteScrollIter);
  20. }
  21. });
  22. }, options);
  23. observer.observe(spinner);
  24. }
  25. },
  26. mixins() {
  27. return [Mixins.PerfectScrollbar];
  28. },
  29. openForm(options) {
  30. options = options || {};
  31. options.position = options.position || 'top';
  32. const forms = this.childComponents('inlinedForm');
  33. let form = forms.find((component) => {
  34. return component.data().position === options.position;
  35. });
  36. if (!form && forms.length > 0) {
  37. form = forms[0];
  38. }
  39. form.open();
  40. },
  41. addCard(evt) {
  42. evt.preventDefault();
  43. const firstCardDom = this.find('.js-minicard:first');
  44. const lastCardDom = this.find('.js-minicard:last');
  45. const textarea = $(evt.currentTarget).find('textarea');
  46. const position = this.currentData().position;
  47. const title = textarea.val().trim();
  48. const formComponent = this.childComponents('addCardForm')[0];
  49. let sortIndex;
  50. if (position === 'top') {
  51. sortIndex = Utils.calculateIndex(null, firstCardDom).base;
  52. } else if (position === 'bottom') {
  53. sortIndex = Utils.calculateIndex(lastCardDom, null).base;
  54. }
  55. const members = formComponent.members.get();
  56. const labelIds = formComponent.labels.get();
  57. const customFields = formComponent.customFields.get();
  58. const board = this.data().board();
  59. let linkedId = '';
  60. let swimlaneId = '';
  61. const boardView = Meteor.user().profile.boardView;
  62. let cardType = 'cardType-card';
  63. if (title) {
  64. if (board.isTemplatesBoard()) {
  65. swimlaneId = this.parentComponent().parentComponent().data()._id; // Always swimlanes view
  66. const swimlane = Swimlanes.findOne(swimlaneId);
  67. // If this is the card templates swimlane, insert a card template
  68. if (swimlane.isCardTemplatesSwimlane())
  69. cardType = 'template-card';
  70. // If this is the board templates swimlane, insert a board template and a linked card
  71. else if (swimlane.isBoardTemplatesSwimlane()) {
  72. linkedId = Boards.insert({
  73. title,
  74. permission: 'private',
  75. type: 'template-board',
  76. });
  77. Swimlanes.insert({
  78. title: TAPi18n.__('default'),
  79. boardId: linkedId,
  80. });
  81. cardType = 'cardType-linkedBoard';
  82. }
  83. } else if (boardView === 'board-view-swimlanes')
  84. swimlaneId = this.parentComponent().parentComponent().data()._id;
  85. else if ((boardView === 'board-view-lists') || (boardView === 'board-view-cal'))
  86. swimlaneId = board.getDefaultSwimline()._id;
  87. const _id = Cards.insert({
  88. title,
  89. members,
  90. labelIds,
  91. customFields,
  92. listId: this.data()._id,
  93. boardId: board._id,
  94. sort: sortIndex,
  95. swimlaneId,
  96. type: cardType,
  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().cards(this.idOrNull(swimlaneId)).count();
  103. if (this.cardlimit.get() < cardCount) {
  104. this.cardlimit.set(this.cardlimit.get() + InfiniteScrollIter);
  105. }
  106. // In case the filter is active we need to add the newly inserted card in
  107. // the list of exceptions -- cards that are not filtered. Otherwise the
  108. // card will disappear instantly.
  109. // See https://github.com/wekan/wekan/issues/80
  110. Filter.addException(_id);
  111. // We keep the form opened, empty it, and scroll to it.
  112. textarea.val('').focus();
  113. autosize.update(textarea);
  114. if (position === 'bottom') {
  115. this.scrollToBottom();
  116. }
  117. formComponent.reset();
  118. }
  119. },
  120. scrollToBottom() {
  121. const container = this.firstNode();
  122. $(container).animate({
  123. scrollTop: container.scrollHeight,
  124. });
  125. },
  126. clickOnMiniCard(evt) {
  127. if (MultiSelection.isActive() || evt.shiftKey) {
  128. evt.stopImmediatePropagation();
  129. evt.preventDefault();
  130. const methodName = evt.shiftKey ? 'toggleRange' : 'toggle';
  131. MultiSelection[methodName](this.currentData()._id);
  132. // If the card is already selected, we want to de-select it.
  133. // XXX We should probably modify the minicard href attribute instead of
  134. // overwriting the event in case the card is already selected.
  135. } else if (Session.equals('currentCard', this.currentData()._id)) {
  136. evt.stopImmediatePropagation();
  137. evt.preventDefault();
  138. Utils.goBoardId(Session.get('currentBoard'));
  139. }
  140. },
  141. cardIsSelected() {
  142. return Session.equals('currentCard', this.currentData()._id);
  143. },
  144. toggleMultiSelection(evt) {
  145. evt.stopPropagation();
  146. evt.preventDefault();
  147. MultiSelection.toggle(this.currentData()._id);
  148. },
  149. idOrNull(swimlaneId) {
  150. const currentUser = Meteor.user();
  151. if (currentUser.profile.boardView === 'board-view-swimlanes' ||
  152. this.data().board().isTemplatesBoard())
  153. return swimlaneId;
  154. return undefined;
  155. },
  156. cardsWithLimit(swimlaneId) {
  157. const limit = this.cardlimit.get();
  158. const selector = {
  159. listId: this.currentData()._id,
  160. archived: false,
  161. };
  162. if (swimlaneId)
  163. selector.swimlaneId = swimlaneId;
  164. return Cards.find(Filter.mongoSelector(selector), {
  165. sort: ['sort'],
  166. limit,
  167. });
  168. },
  169. showSpinner(swimlaneId) {
  170. const list = Template.currentData();
  171. return list.cards(swimlaneId).count() > this.cardlimit.get();
  172. },
  173. canSeeAddCard() {
  174. return !this.reachedWipLimit() && Meteor.user() && Meteor.user().isBoardMember() && !Meteor.user().isCommentOnly();
  175. },
  176. reachedWipLimit() {
  177. const list = Template.currentData();
  178. return !list.getWipLimit('soft') && list.getWipLimit('enabled') && list.getWipLimit('value') <= list.cards().count();
  179. },
  180. events() {
  181. return [{
  182. 'click .js-minicard': this.clickOnMiniCard,
  183. 'click .js-toggle-multi-selection': this.toggleMultiSelection,
  184. 'click .open-minicard-composer': this.scrollToBottom,
  185. submit: this.addCard,
  186. }];
  187. },
  188. }).register('listBody');
  189. function toggleValueInReactiveArray(reactiveValue, value) {
  190. const array = reactiveValue.get();
  191. const valueIndex = array.indexOf(value);
  192. if (valueIndex === -1) {
  193. array.push(value);
  194. } else {
  195. array.splice(valueIndex, 1);
  196. }
  197. reactiveValue.set(array);
  198. }
  199. BlazeComponent.extendComponent({
  200. onCreated() {
  201. this.labels = new ReactiveVar([]);
  202. this.members = new ReactiveVar([]);
  203. this.customFields = new ReactiveVar([]);
  204. const currentBoardId = Session.get('currentBoard');
  205. arr = [];
  206. _.forEach(Boards.findOne(currentBoardId).customFields().fetch(), function(field){
  207. if(field.automaticallyOnCard)
  208. arr.push({_id: field._id, value: null});
  209. });
  210. this.customFields.set(arr);
  211. },
  212. reset() {
  213. this.labels.set([]);
  214. this.members.set([]);
  215. this.customFields.set([]);
  216. },
  217. getLabels() {
  218. const currentBoardId = Session.get('currentBoard');
  219. return Boards.findOne(currentBoardId).labels.filter((label) => {
  220. return this.labels.get().indexOf(label._id) > -1;
  221. });
  222. },
  223. pressKey(evt) {
  224. // Pressing Enter should submit the card
  225. if (evt.keyCode === 13 && !evt.shiftKey) {
  226. evt.preventDefault();
  227. const $form = $(evt.currentTarget).closest('form');
  228. // XXX For some reason $form.submit() does not work (it's probably a bug
  229. // of blaze-component related to the fact that the submit event is non-
  230. // bubbling). This is why we click on the submit button instead -- which
  231. // work.
  232. $form.find('button[type=submit]').click();
  233. // Pressing Tab should open the form of the next column, and Maj+Tab go
  234. // in the reverse order
  235. } else if (evt.keyCode === 9) {
  236. evt.preventDefault();
  237. const isReverse = evt.shiftKey;
  238. const list = $(`#js-list-${this.data().listId}`);
  239. const listSelector = '.js-list:not(.js-list-composer)';
  240. let nextList = list[isReverse ? 'prev' : 'next'](listSelector).get(0);
  241. // If there is no next list, loop back to the beginning.
  242. if (!nextList) {
  243. nextList = $(listSelector + (isReverse ? ':last' : ':first')).get(0);
  244. }
  245. BlazeComponent.getComponentForElement(nextList).openForm({
  246. position:this.data().position,
  247. });
  248. }
  249. },
  250. events() {
  251. return [{
  252. keydown: this.pressKey,
  253. 'click .js-link': Popup.open('linkCard'),
  254. 'click .js-search': Popup.open('searchElement'),
  255. 'click .js-card-template': Popup.open('searchElement'),
  256. }];
  257. },
  258. onRendered() {
  259. const editor = this;
  260. const $textarea = this.$('textarea');
  261. autosize($textarea);
  262. $textarea.escapeableTextComplete([
  263. // User mentions
  264. {
  265. match: /\B@([\w.]*)$/,
  266. search(term, callback) {
  267. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  268. callback($.map(currentBoard.activeMembers(), (member) => {
  269. const user = Users.findOne(member.userId);
  270. return user.username.indexOf(term) === 0 ? user : null;
  271. }));
  272. },
  273. template(user) {
  274. return user.username;
  275. },
  276. replace(user) {
  277. toggleValueInReactiveArray(editor.members, user._id);
  278. return '';
  279. },
  280. index: 1,
  281. },
  282. // Labels
  283. {
  284. match: /\B#(\w*)$/,
  285. search(term, callback) {
  286. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  287. callback($.map(currentBoard.labels, (label) => {
  288. if (label.name.indexOf(term) > -1 ||
  289. label.color.indexOf(term) > -1) {
  290. return label;
  291. }
  292. return null;
  293. }));
  294. },
  295. template(label) {
  296. return Blaze.toHTMLWithData(Template.autocompleteLabelLine, {
  297. hasNoName: !label.name,
  298. colorName: label.color,
  299. labelName: label.name || label.color,
  300. });
  301. },
  302. replace(label) {
  303. toggleValueInReactiveArray(editor.labels, label._id);
  304. return '';
  305. },
  306. index: 1,
  307. },
  308. ], {
  309. // When the autocomplete menu is shown we want both a press of both `Tab`
  310. // or `Enter` to validation the auto-completion. We also need to stop the
  311. // event propagation to prevent the card from submitting (on `Enter`) or
  312. // going on the next column (on `Tab`).
  313. onKeydown(evt, commands) {
  314. if (evt.keyCode === 9 || evt.keyCode === 13) {
  315. evt.stopPropagation();
  316. return commands.KEY_ENTER;
  317. }
  318. return null;
  319. },
  320. });
  321. },
  322. }).register('addCardForm');
  323. BlazeComponent.extendComponent({
  324. onCreated() {
  325. this.selectedBoardId = new ReactiveVar('');
  326. this.selectedSwimlaneId = new ReactiveVar('');
  327. this.selectedListId = new ReactiveVar('');
  328. this.boardId = Session.get('currentBoard');
  329. // In order to get current board info
  330. subManager.subscribe('board', this.boardId);
  331. this.board = Boards.findOne(this.boardId);
  332. // List where to insert card
  333. const list = $(Popup._getTopStack().openerElement).closest('.js-list');
  334. this.listId = Blaze.getData(list[0])._id;
  335. // Swimlane where to insert card
  336. const swimlane = $(Popup._getTopStack().openerElement).closest('.js-swimlane');
  337. this.swimlaneId = '';
  338. const boardView = Meteor.user().profile.boardView;
  339. if (boardView === 'board-view-swimlanes')
  340. this.swimlaneId = Blaze.getData(swimlane[0])._id;
  341. else if (boardView === 'board-view-lists')
  342. this.swimlaneId = Swimlanes.findOne({boardId: this.boardId})._id;
  343. },
  344. boards() {
  345. const boards = Boards.find({
  346. archived: false,
  347. 'members.userId': Meteor.userId(),
  348. _id: {$ne: Session.get('currentBoard')},
  349. type: 'board',
  350. }, {
  351. sort: ['title'],
  352. });
  353. return boards;
  354. },
  355. swimlanes() {
  356. if (!this.selectedBoardId.get()) {
  357. return [];
  358. }
  359. const swimlanes = Swimlanes.find({boardId: this.selectedBoardId.get()});
  360. if (swimlanes.count())
  361. this.selectedSwimlaneId.set(swimlanes.fetch()[0]._id);
  362. return swimlanes;
  363. },
  364. lists() {
  365. if (!this.selectedBoardId.get()) {
  366. return [];
  367. }
  368. const lists = Lists.find({boardId: this.selectedBoardId.get()});
  369. if (lists.count())
  370. this.selectedListId.set(lists.fetch()[0]._id);
  371. return lists;
  372. },
  373. cards() {
  374. if (!this.board) {
  375. return [];
  376. }
  377. const ownCardsIds = this.board.cards().map((card) => { return card.linkedId || card._id; });
  378. return Cards.find({
  379. boardId: this.selectedBoardId.get(),
  380. swimlaneId: this.selectedSwimlaneId.get(),
  381. listId: this.selectedListId.get(),
  382. archived: false,
  383. linkedId: {$nin: ownCardsIds},
  384. _id: {$nin: ownCardsIds},
  385. type: {$nin: ['template-card']},
  386. });
  387. },
  388. events() {
  389. return [{
  390. 'change .js-select-boards'(evt) {
  391. subManager.subscribe('board', $(evt.currentTarget).val());
  392. this.selectedBoardId.set($(evt.currentTarget).val());
  393. },
  394. 'change .js-select-swimlanes'(evt) {
  395. this.selectedSwimlaneId.set($(evt.currentTarget).val());
  396. },
  397. 'change .js-select-lists'(evt) {
  398. this.selectedListId.set($(evt.currentTarget).val());
  399. },
  400. 'click .js-done' (evt) {
  401. // LINK CARD
  402. evt.stopPropagation();
  403. evt.preventDefault();
  404. const linkedId = $('.js-select-cards option:selected').val();
  405. if (!linkedId) {
  406. Popup.close();
  407. return;
  408. }
  409. const _id = Cards.insert({
  410. title: $('.js-select-cards option:selected').text(), //dummy
  411. listId: this.listId,
  412. swimlaneId: this.swimlaneId,
  413. boardId: this.boardId,
  414. sort: Lists.findOne(this.listId).cards().count(),
  415. type: 'cardType-linkedCard',
  416. linkedId,
  417. });
  418. Filter.addException(_id);
  419. Popup.close();
  420. },
  421. 'click .js-link-board' (evt) {
  422. //LINK BOARD
  423. evt.stopPropagation();
  424. evt.preventDefault();
  425. const impBoardId = $('.js-select-boards option:selected').val();
  426. if (!impBoardId || Cards.findOne({linkedId: impBoardId, archived: false})) {
  427. Popup.close();
  428. return;
  429. }
  430. const _id = Cards.insert({
  431. title: $('.js-select-boards option:selected').text(), //dummy
  432. listId: this.listId,
  433. swimlaneId: this.swimlaneId,
  434. boardId: this.boardId,
  435. sort: Lists.findOne(this.listId).cards().count(),
  436. type: 'cardType-linkedBoard',
  437. linkedId: impBoardId,
  438. });
  439. Filter.addException(_id);
  440. Popup.close();
  441. },
  442. }];
  443. },
  444. }).register('linkCardPopup');
  445. BlazeComponent.extendComponent({
  446. mixins() {
  447. return [Mixins.PerfectScrollbar];
  448. },
  449. onCreated() {
  450. this.isCardTemplateSearch = $(Popup._getTopStack().openerElement).hasClass('js-card-template');
  451. this.isListTemplateSearch = $(Popup._getTopStack().openerElement).hasClass('js-list-template');
  452. this.isSwimlaneTemplateSearch = $(Popup._getTopStack().openerElement).hasClass('js-open-add-swimlane-menu');
  453. this.isBoardTemplateSearch = $(Popup._getTopStack().openerElement).hasClass('js-add-board');
  454. this.isTemplateSearch = this.isCardTemplateSearch ||
  455. this.isListTemplateSearch ||
  456. this.isSwimlaneTemplateSearch ||
  457. this.isBoardTemplateSearch;
  458. let board = {};
  459. if (this.isTemplateSearch) {
  460. board = Boards.findOne(Meteor.user().profile.templatesBoardId);
  461. } else {
  462. // Prefetch first non-current board id
  463. board = Boards.findOne({
  464. archived: false,
  465. 'members.userId': Meteor.userId(),
  466. _id: {$nin: [Session.get('currentBoard'), Meteor.user().profile.templatesBoardId]},
  467. });
  468. }
  469. if (!board) {
  470. Popup.close();
  471. return;
  472. }
  473. const boardId = board._id;
  474. // Subscribe to this board
  475. subManager.subscribe('board', boardId);
  476. this.selectedBoardId = new ReactiveVar(boardId);
  477. if (!this.isBoardTemplateSearch) {
  478. this.boardId = Session.get('currentBoard');
  479. // In order to get current board info
  480. subManager.subscribe('board', this.boardId);
  481. this.swimlaneId = '';
  482. // Swimlane where to insert card
  483. const swimlane = $(Popup._getTopStack().openerElement).parents('.js-swimlane');
  484. if (Meteor.user().profile.boardView === 'board-view-swimlanes')
  485. this.swimlaneId = Blaze.getData(swimlane[0])._id;
  486. else
  487. this.swimlaneId = Swimlanes.findOne({boardId: this.boardId})._id;
  488. // List where to insert card
  489. const list = $(Popup._getTopStack().openerElement).closest('.js-list');
  490. this.listId = Blaze.getData(list[0])._id;
  491. }
  492. this.term = new ReactiveVar('');
  493. },
  494. boards() {
  495. const boards = Boards.find({
  496. archived: false,
  497. 'members.userId': Meteor.userId(),
  498. _id: {$ne: Session.get('currentBoard')},
  499. type: 'board',
  500. }, {
  501. sort: ['title'],
  502. });
  503. return boards;
  504. },
  505. results() {
  506. if (!this.selectedBoardId) {
  507. return [];
  508. }
  509. const board = Boards.findOne(this.selectedBoardId.get());
  510. if (!this.isTemplateSearch || this.isCardTemplateSearch) {
  511. return board.searchCards(this.term.get(), false);
  512. } else if (this.isListTemplateSearch) {
  513. return board.searchLists(this.term.get());
  514. } else if (this.isSwimlaneTemplateSearch) {
  515. return board.searchSwimlanes(this.term.get());
  516. } else if (this.isBoardTemplateSearch) {
  517. const boards = board.searchBoards(this.term.get());
  518. boards.forEach((board) => {
  519. subManager.subscribe('board', board.linkedId);
  520. });
  521. return boards;
  522. } else {
  523. return [];
  524. }
  525. },
  526. events() {
  527. return [{
  528. 'change .js-select-boards'(evt) {
  529. subManager.subscribe('board', $(evt.currentTarget).val());
  530. this.selectedBoardId.set($(evt.currentTarget).val());
  531. },
  532. 'submit .js-search-term-form'(evt) {
  533. evt.preventDefault();
  534. this.term.set(evt.target.searchTerm.value);
  535. },
  536. 'click .js-minicard'(evt) {
  537. // 0. Common
  538. const title = $('.js-element-title').val().trim();
  539. if (!title)
  540. return;
  541. const element = Blaze.getData(evt.currentTarget);
  542. element.title = title;
  543. let _id = '';
  544. if (!this.isTemplateSearch || this.isCardTemplateSearch) {
  545. // Card insertion
  546. // 1. Common
  547. element.sort = Lists.findOne(this.listId).cards().count();
  548. // 1.A From template
  549. if (this.isTemplateSearch) {
  550. element.type = 'cardType-card';
  551. element.linkedId = '';
  552. _id = element.copy(this.boardId, this.swimlaneId, this.listId);
  553. // 1.B Linked card
  554. } else {
  555. delete element._id;
  556. element.type = 'cardType-linkedCard';
  557. element.linkedId = element.linkedId || element._id;
  558. _id = Cards.insert(element);
  559. }
  560. Filter.addException(_id);
  561. // List insertion
  562. } else if (this.isListTemplateSearch) {
  563. element.sort = Swimlanes.findOne(this.swimlaneId).lists().count();
  564. element.type = 'list';
  565. _id = element.copy(this.boardId, this.swimlaneId);
  566. } else if (this.isSwimlaneTemplateSearch) {
  567. element.sort = Boards.findOne(this.boardId).swimlanes().count();
  568. element.type = 'swimlalne';
  569. _id = element.copy(this.boardId);
  570. } else if (this.isBoardTemplateSearch) {
  571. board = Boards.findOne(element.linkedId);
  572. board.sort = Boards.find({archived: false}).count();
  573. board.type = 'board';
  574. board.title = element.title;
  575. delete board.slug;
  576. _id = board.copy();
  577. }
  578. Popup.close();
  579. },
  580. }];
  581. },
  582. }).register('searchElementPopup');