listBody.js 19 KB

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