listBody.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. const subManager = new SubsManager();
  2. BlazeComponent.extendComponent({
  3. mixins() {
  4. return [Mixins.PerfectScrollbar];
  5. },
  6. openForm(options) {
  7. options = options || {};
  8. options.position = options.position || 'top';
  9. const forms = this.childComponents('inlinedForm');
  10. let form = forms.find((component) => {
  11. return component.data().position === options.position;
  12. });
  13. if (!form && forms.length > 0) {
  14. form = forms[0];
  15. }
  16. form.open();
  17. },
  18. addCard(evt) {
  19. evt.preventDefault();
  20. const firstCardDom = this.find('.js-minicard:first');
  21. const lastCardDom = this.find('.js-minicard:last');
  22. const textarea = $(evt.currentTarget).find('textarea');
  23. const position = this.currentData().position;
  24. const title = textarea.val().trim();
  25. const formComponent = this.childComponents('addCardForm')[0];
  26. let sortIndex;
  27. if (position === 'top') {
  28. sortIndex = Utils.calculateIndex(null, firstCardDom).base;
  29. } else if (position === 'bottom') {
  30. sortIndex = Utils.calculateIndex(lastCardDom, null).base;
  31. }
  32. const members = formComponent.members.get();
  33. const labelIds = formComponent.labels.get();
  34. const customFields = formComponent.customFields.get();
  35. const boardId = this.data().board();
  36. let swimlaneId = '';
  37. const boardView = Meteor.user().profile.boardView;
  38. if (boardView === 'board-view-swimlanes')
  39. swimlaneId = this.parentComponent().parentComponent().data()._id;
  40. else if ((boardView === 'board-view-lists') || (boardView === 'board-view-cal'))
  41. swimlaneId = boardId.getDefaultSwimline()._id;
  42. if (title) {
  43. const _id = Cards.insert({
  44. title,
  45. members,
  46. labelIds,
  47. customFields,
  48. listId: this.data()._id,
  49. boardId: boardId._id,
  50. sort: sortIndex,
  51. swimlaneId,
  52. type: 'cardType-card',
  53. });
  54. // In case the filter is active we need to add the newly inserted card in
  55. // the list of exceptions -- cards that are not filtered. Otherwise the
  56. // card will disappear instantly.
  57. // See https://github.com/wekan/wekan/issues/80
  58. Filter.addException(_id);
  59. // We keep the form opened, empty it, and scroll to it.
  60. textarea.val('').focus();
  61. autosize.update(textarea);
  62. if (position === 'bottom') {
  63. this.scrollToBottom();
  64. }
  65. formComponent.reset();
  66. }
  67. },
  68. scrollToBottom() {
  69. const container = this.firstNode();
  70. $(container).animate({
  71. scrollTop: container.scrollHeight,
  72. });
  73. },
  74. clickOnMiniCard(evt) {
  75. if (MultiSelection.isActive() || evt.shiftKey) {
  76. evt.stopImmediatePropagation();
  77. evt.preventDefault();
  78. const methodName = evt.shiftKey ? 'toggleRange' : 'toggle';
  79. MultiSelection[methodName](this.currentData()._id);
  80. // If the card is already selected, we want to de-select it.
  81. // XXX We should probably modify the minicard href attribute instead of
  82. // overwriting the event in case the card is already selected.
  83. } else if (Session.equals('currentCard', this.currentData()._id)) {
  84. evt.stopImmediatePropagation();
  85. evt.preventDefault();
  86. Utils.goBoardId(Session.get('currentBoard'));
  87. }
  88. },
  89. cardIsSelected() {
  90. return Session.equals('currentCard', this.currentData()._id);
  91. },
  92. toggleMultiSelection(evt) {
  93. evt.stopPropagation();
  94. evt.preventDefault();
  95. MultiSelection.toggle(this.currentData()._id);
  96. },
  97. idOrNull(swimlaneId) {
  98. const currentUser = Meteor.user();
  99. if (currentUser.profile.boardView === 'board-view-swimlanes')
  100. return swimlaneId;
  101. return undefined;
  102. },
  103. canSeeAddCard() {
  104. return !this.reachedWipLimit() && Meteor.user() && Meteor.user().isBoardMember() && !Meteor.user().isCommentOnly();
  105. },
  106. reachedWipLimit() {
  107. const list = Template.currentData();
  108. return !list.getWipLimit('soft') && list.getWipLimit('enabled') && list.getWipLimit('value') <= list.cards().count();
  109. },
  110. events() {
  111. return [{
  112. 'click .js-minicard': this.clickOnMiniCard,
  113. 'click .js-toggle-multi-selection': this.toggleMultiSelection,
  114. 'click .open-minicard-composer': this.scrollToBottom,
  115. submit: this.addCard,
  116. }];
  117. },
  118. }).register('listBody');
  119. function toggleValueInReactiveArray(reactiveValue, value) {
  120. const array = reactiveValue.get();
  121. const valueIndex = array.indexOf(value);
  122. if (valueIndex === -1) {
  123. array.push(value);
  124. } else {
  125. array.splice(valueIndex, 1);
  126. }
  127. reactiveValue.set(array);
  128. }
  129. BlazeComponent.extendComponent({
  130. onCreated() {
  131. this.labels = new ReactiveVar([]);
  132. this.members = new ReactiveVar([]);
  133. this.customFields = new ReactiveVar([]);
  134. },
  135. reset() {
  136. this.labels.set([]);
  137. this.members.set([]);
  138. this.customFields.set([]);
  139. },
  140. getLabels() {
  141. const currentBoardId = Session.get('currentBoard');
  142. return Boards.findOne(currentBoardId).labels.filter((label) => {
  143. return this.labels.get().indexOf(label._id) > -1;
  144. });
  145. },
  146. pressKey(evt) {
  147. // Pressing Enter should submit the card
  148. if (evt.keyCode === 13) {
  149. evt.preventDefault();
  150. const $form = $(evt.currentTarget).closest('form');
  151. // XXX For some reason $form.submit() does not work (it's probably a bug
  152. // of blaze-component related to the fact that the submit event is non-
  153. // bubbling). This is why we click on the submit button instead -- which
  154. // work.
  155. $form.find('button[type=submit]').click();
  156. // Pressing Tab should open the form of the next column, and Maj+Tab go
  157. // in the reverse order
  158. } else if (evt.keyCode === 9) {
  159. evt.preventDefault();
  160. const isReverse = evt.shiftKey;
  161. const list = $(`#js-list-${this.data().listId}`);
  162. const listSelector = '.js-list:not(.js-list-composer)';
  163. let nextList = list[isReverse ? 'prev' : 'next'](listSelector).get(0);
  164. // If there is no next list, loop back to the beginning.
  165. if (!nextList) {
  166. nextList = $(listSelector + (isReverse ? ':last' : ':first')).get(0);
  167. }
  168. BlazeComponent.getComponentForElement(nextList).openForm({
  169. position:this.data().position,
  170. });
  171. }
  172. },
  173. events() {
  174. return [{
  175. keydown: this.pressKey,
  176. 'click .js-link': Popup.open('linkCard'),
  177. 'click .js-search': Popup.open('searchCard'),
  178. }];
  179. },
  180. onRendered() {
  181. const editor = this;
  182. const $textarea = this.$('textarea');
  183. autosize($textarea);
  184. $textarea.escapeableTextComplete([
  185. // User mentions
  186. {
  187. match: /\B@([\w.]*)$/,
  188. search(term, callback) {
  189. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  190. callback($.map(currentBoard.activeMembers(), (member) => {
  191. const user = Users.findOne(member.userId);
  192. return user.username.indexOf(term) === 0 ? user : null;
  193. }));
  194. },
  195. template(user) {
  196. return user.username;
  197. },
  198. replace(user) {
  199. toggleValueInReactiveArray(editor.members, user._id);
  200. return '';
  201. },
  202. index: 1,
  203. },
  204. // Labels
  205. {
  206. match: /\B#(\w*)$/,
  207. search(term, callback) {
  208. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  209. callback($.map(currentBoard.labels, (label) => {
  210. if (label.name.indexOf(term) > -1 ||
  211. label.color.indexOf(term) > -1) {
  212. return label;
  213. }
  214. return null;
  215. }));
  216. },
  217. template(label) {
  218. return Blaze.toHTMLWithData(Template.autocompleteLabelLine, {
  219. hasNoName: !label.name,
  220. colorName: label.color,
  221. labelName: label.name || label.color,
  222. });
  223. },
  224. replace(label) {
  225. toggleValueInReactiveArray(editor.labels, label._id);
  226. return '';
  227. },
  228. index: 1,
  229. },
  230. ], {
  231. // When the autocomplete menu is shown we want both a press of both `Tab`
  232. // or `Enter` to validation the auto-completion. We also need to stop the
  233. // event propagation to prevent the card from submitting (on `Enter`) or
  234. // going on the next column (on `Tab`).
  235. onKeydown(evt, commands) {
  236. if (evt.keyCode === 9 || evt.keyCode === 13) {
  237. evt.stopPropagation();
  238. return commands.KEY_ENTER;
  239. }
  240. return null;
  241. },
  242. });
  243. },
  244. }).register('addCardForm');
  245. BlazeComponent.extendComponent({
  246. onCreated() {
  247. // Prefetch first non-current board id
  248. const boardId = Boards.findOne({
  249. archived: false,
  250. 'members.userId': Meteor.userId(),
  251. _id: {$ne: Session.get('currentBoard')},
  252. }, {
  253. sort: ['title'],
  254. })._id;
  255. // Subscribe to this board
  256. subManager.subscribe('board', boardId);
  257. this.selectedBoardId = new ReactiveVar(boardId);
  258. this.selectedSwimlaneId = new ReactiveVar('');
  259. this.selectedListId = new ReactiveVar('');
  260. this.boardId = Session.get('currentBoard');
  261. // In order to get current board info
  262. subManager.subscribe('board', this.boardId);
  263. this.board = Boards.findOne(this.boardId);
  264. // List where to insert card
  265. const list = $(Popup._getTopStack().openerElement).closest('.js-list');
  266. this.listId = Blaze.getData(list[0])._id;
  267. // Swimlane where to insert card
  268. const swimlane = $(Popup._getTopStack().openerElement).closest('.js-swimlane');
  269. this.swimlaneId = '';
  270. const boardView = Meteor.user().profile.boardView;
  271. if (boardView === 'board-view-swimlanes')
  272. this.swimlaneId = Blaze.getData(swimlane[0])._id;
  273. else if (boardView === 'board-view-lists')
  274. this.swimlaneId = Swimlanes.findOne({boardId: this.boardId})._id;
  275. },
  276. boards() {
  277. const boards = Boards.find({
  278. archived: false,
  279. 'members.userId': Meteor.userId(),
  280. _id: {$ne: Session.get('currentBoard')},
  281. }, {
  282. sort: ['title'],
  283. });
  284. return boards;
  285. },
  286. swimlanes() {
  287. const swimlanes = Swimlanes.find({boardId: this.selectedBoardId.get()});
  288. if (swimlanes.count())
  289. this.selectedSwimlaneId.set(swimlanes.fetch()[0]._id);
  290. return swimlanes;
  291. },
  292. lists() {
  293. const lists = Lists.find({boardId: this.selectedBoardId.get()});
  294. if (lists.count())
  295. this.selectedListId.set(lists.fetch()[0]._id);
  296. return lists;
  297. },
  298. cards() {
  299. return Cards.find({
  300. boardId: this.selectedBoardId.get(),
  301. swimlaneId: this.selectedSwimlaneId.get(),
  302. listId: this.selectedListId.get(),
  303. archived: false,
  304. linkedId: null,
  305. _id: {$nin: this.board.cards().map((card) => { return card.linkedId || card._id; })},
  306. });
  307. },
  308. events() {
  309. return [{
  310. 'change .js-select-boards'(evt) {
  311. this.selectedBoardId.set($(evt.currentTarget).val());
  312. subManager.subscribe('board', this.selectedBoardId.get());
  313. },
  314. 'change .js-select-swimlanes'(evt) {
  315. this.selectedSwimlaneId.set($(evt.currentTarget).val());
  316. },
  317. 'change .js-select-lists'(evt) {
  318. this.selectedListId.set($(evt.currentTarget).val());
  319. },
  320. 'click .js-done' (evt) {
  321. // LINK CARD
  322. evt.stopPropagation();
  323. evt.preventDefault();
  324. const _id = Cards.insert({
  325. title: $('.js-select-cards option:selected').text(), //dummy
  326. listId: this.listId,
  327. swimlaneId: this.swimlaneId,
  328. boardId: this.boardId,
  329. sort: Lists.findOne(this.listId).cards().count(),
  330. type: 'cardType-linkedCard',
  331. linkedId: $('.js-select-cards option:selected').val(),
  332. });
  333. Filter.addException(_id);
  334. Popup.close();
  335. },
  336. 'click .js-link-board' (evt) {
  337. //LINK BOARD
  338. evt.stopPropagation();
  339. evt.preventDefault();
  340. const impBoardId = $('.js-select-boards option:selected').val();
  341. const _id = Cards.insert({
  342. title: $('.js-select-boards option:selected').text(), //dummy
  343. listId: this.listId,
  344. swimlaneId: this.swimlaneId,
  345. boardId: this.boardId,
  346. sort: Lists.findOne(this.listId).cards().count(),
  347. type: 'cardType-linkedBoard',
  348. linkedId: impBoardId,
  349. });
  350. Filter.addException(_id);
  351. Popup.close();
  352. },
  353. }];
  354. },
  355. }).register('linkCardPopup');
  356. BlazeComponent.extendComponent({
  357. mixins() {
  358. return [Mixins.PerfectScrollbar];
  359. },
  360. onCreated() {
  361. // Prefetch first non-current board id
  362. const boardId = Boards.findOne({
  363. archived: false,
  364. 'members.userId': Meteor.userId(),
  365. _id: {$ne: Session.get('currentBoard')},
  366. })._id;
  367. // Subscribe to this board
  368. subManager.subscribe('board', boardId);
  369. this.selectedBoardId = new ReactiveVar(boardId);
  370. this.boardId = Session.get('currentBoard');
  371. // In order to get current board info
  372. subManager.subscribe('board', this.boardId);
  373. const board = Boards.findOne(this.boardId);
  374. // List where to insert card
  375. const list = $(Popup._getTopStack().openerElement).closest('.js-list');
  376. this.listId = Blaze.getData(list[0])._id;
  377. // Swimlane where to insert card
  378. const swimlane = $(Popup._getTopStack().openerElement).closest('.js-swimlane');
  379. this.swimlaneId = '';
  380. if (board.view === 'board-view-swimlanes')
  381. this.swimlaneId = Blaze.getData(swimlane[0])._id;
  382. else
  383. this.swimlaneId = Swimlanes.findOne({boardId: this.boardId})._id;
  384. this.term = new ReactiveVar('');
  385. },
  386. boards() {
  387. const boards = Boards.find({
  388. archived: false,
  389. 'members.userId': Meteor.userId(),
  390. _id: {$ne: Session.get('currentBoard')},
  391. }, {
  392. sort: ['title'],
  393. });
  394. return boards;
  395. },
  396. results() {
  397. const board = Boards.findOne(this.selectedBoardId.get());
  398. return board.searchCards(this.term.get(), true);
  399. },
  400. events() {
  401. return [{
  402. 'change .js-select-boards'(evt) {
  403. this.selectedBoardId.set($(evt.currentTarget).val());
  404. subManager.subscribe('board', this.selectedBoardId.get());
  405. },
  406. 'submit .js-search-term-form'(evt) {
  407. evt.preventDefault();
  408. this.term.set(evt.target.searchTerm.value);
  409. },
  410. 'click .js-minicard'(evt) {
  411. // LINK CARD
  412. const card = Blaze.getData(evt.currentTarget);
  413. const _id = Cards.insert({
  414. title: card.title, //dummy
  415. listId: this.listId,
  416. swimlaneId: this.swimlaneId,
  417. boardId: this.boardId,
  418. sort: Lists.findOne(this.listId).cards().count(),
  419. type: 'cardType-linkedCard',
  420. linkedId: card._id,
  421. });
  422. Filter.addException(_id);
  423. Popup.close();
  424. },
  425. }];
  426. },
  427. }).register('searchCardPopup');