listBody.js 20 KB

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