listBody.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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-import': Popup.open('importCard'),
  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. })._id;
  253. // Subscribe to this board
  254. subManager.subscribe('board', boardId);
  255. this.selectedBoardId = new ReactiveVar(boardId);
  256. this.selectedSwimlaneId = new ReactiveVar('');
  257. this.selectedListId = new ReactiveVar('');
  258. this.boardId = Session.get('currentBoard');
  259. // In order to get current board info
  260. subManager.subscribe('board', this.boardId);
  261. const board = Boards.findOne(this.boardId);
  262. // List where to insert card
  263. const list = $(Popup._getTopStack().openerElement).closest('.js-list');
  264. this.listId = Blaze.getData(list[0])._id;
  265. // Swimlane where to insert card
  266. const swimlane = $(Popup._getTopStack().openerElement).closest('.js-swimlane');
  267. this.swimlaneId = '';
  268. if (board.view === 'board-view-swimlanes')
  269. this.swimlaneId = Blaze.getData(swimlane[0])._id;
  270. else
  271. this.swimlaneId = Swimlanes.findOne({boardId: this.boardId})._id;
  272. },
  273. boards() {
  274. const boards = Boards.find({
  275. archived: false,
  276. 'members.userId': Meteor.userId(),
  277. _id: {$ne: Session.get('currentBoard')},
  278. }, {
  279. sort: ['title'],
  280. });
  281. return boards;
  282. },
  283. swimlanes() {
  284. const swimlanes = Swimlanes.find({boardId: this.selectedBoardId.get()});
  285. if (swimlanes.count())
  286. this.selectedSwimlaneId.set(swimlanes.fetch()[0]._id);
  287. return swimlanes;
  288. },
  289. lists() {
  290. const lists = Lists.find({boardId: this.selectedBoardId.get()});
  291. if (lists.count())
  292. this.selectedListId.set(lists.fetch()[0]._id);
  293. return lists;
  294. },
  295. cards() {
  296. return Cards.find({
  297. boardId: this.selectedBoardId.get(),
  298. swimlaneId: this.selectedSwimlaneId.get(),
  299. listId: this.selectedListId.get(),
  300. archived: false,
  301. });
  302. },
  303. events() {
  304. return [{
  305. 'change .js-select-boards'(evt) {
  306. this.selectedBoardId.set($(evt.currentTarget).val());
  307. subManager.subscribe('board', this.selectedBoardId.get());
  308. },
  309. 'change .js-select-swimlanes'(evt) {
  310. this.selectedSwimlaneId.set($(evt.currentTarget).val());
  311. },
  312. 'change .js-select-lists'(evt) {
  313. this.selectedListId.set($(evt.currentTarget).val());
  314. },
  315. 'click .js-done' (evt) {
  316. // IMPORT CARD
  317. evt.stopPropagation();
  318. evt.preventDefault();
  319. const _id = Cards.insert({
  320. title: $('.js-select-cards option:selected').text(), //dummy
  321. listId: this.listId,
  322. swimlaneId: this.swimlaneId,
  323. boardId: this.boardId,
  324. sort: Lists.findOne(this.listId).cards().count(),
  325. type: 'cardType-importedCard',
  326. importedId: $('.js-select-cards option:selected').val(),
  327. });
  328. Filter.addException(_id);
  329. Popup.close();
  330. },
  331. 'click .js-import-board' (evt) {
  332. //IMPORT BOARD
  333. evt.stopPropagation();
  334. evt.preventDefault();
  335. const _id = Cards.insert({
  336. title: $('.js-select-boards option:selected').text(), //dummy
  337. listId: this.listId,
  338. swimlaneId: this.swimlaneId,
  339. boardId: this.boardId,
  340. sort: Lists.findOne(this.listId).cards().count(),
  341. type: 'cardType-importedBoard',
  342. importedId: $('.js-select-boards option:selected').val(),
  343. });
  344. Filter.addException(_id);
  345. Popup.close();
  346. },
  347. }];
  348. },
  349. }).register('importCardPopup');
  350. BlazeComponent.extendComponent({
  351. mixins() {
  352. return [Mixins.PerfectScrollbar];
  353. },
  354. onCreated() {
  355. // Prefetch first non-current board id
  356. const boardId = Boards.findOne({
  357. archived: false,
  358. 'members.userId': Meteor.userId(),
  359. _id: {$ne: Session.get('currentBoard')},
  360. })._id;
  361. // Subscribe to this board
  362. subManager.subscribe('board', boardId);
  363. this.selectedBoardId = new ReactiveVar(boardId);
  364. this.boardId = Session.get('currentBoard');
  365. // In order to get current board info
  366. subManager.subscribe('board', this.boardId);
  367. const board = Boards.findOne(this.boardId);
  368. // List where to insert card
  369. const list = $(Popup._getTopStack().openerElement).closest('.js-list');
  370. this.listId = Blaze.getData(list[0])._id;
  371. // Swimlane where to insert card
  372. const swimlane = $(Popup._getTopStack().openerElement).closest('.js-swimlane');
  373. this.swimlaneId = '';
  374. if (board.view === 'board-view-swimlanes')
  375. this.swimlaneId = Blaze.getData(swimlane[0])._id;
  376. else
  377. this.swimlaneId = Swimlanes.findOne({boardId: this.boardId})._id;
  378. this.term = new ReactiveVar('');
  379. },
  380. boards() {
  381. const boards = Boards.find({
  382. archived: false,
  383. 'members.userId': Meteor.userId(),
  384. _id: {$ne: Session.get('currentBoard')},
  385. }, {
  386. sort: ['title'],
  387. });
  388. return boards;
  389. },
  390. results() {
  391. const board = Boards.findOne(this.selectedBoardId.get());
  392. return board.searchCards(this.term.get());
  393. },
  394. events() {
  395. return [{
  396. 'change .js-select-boards'(evt) {
  397. this.selectedBoardId.set($(evt.currentTarget).val());
  398. subManager.subscribe('board', this.selectedBoardId.get());
  399. },
  400. 'submit .js-search-term-form'(evt) {
  401. evt.preventDefault();
  402. this.term.set(evt.target.searchTerm.value);
  403. },
  404. 'click .js-minicard'(evt) {
  405. // IMPORT CARD
  406. const card = Blaze.getData(evt.currentTarget);
  407. const _id = Cards.insert({
  408. title: card.title, //dummy
  409. listId: this.listId,
  410. swimlaneId: this.swimlaneId,
  411. boardId: this.boardId,
  412. sort: Lists.findOne(this.listId).cards().count(),
  413. type: 'cardType-importedCard',
  414. importedId: card._id,
  415. });
  416. Filter.addException(_id);
  417. Popup.close();
  418. },
  419. }];
  420. },
  421. }).register('searchCardPopup');