listBody.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. const currentBoardId = Session.get('currentBoard');
  135. arr = [];
  136. _.forEach(Boards.findOne(currentBoardId).customFields().fetch(), function(field){
  137. if(field.automaticallyOnCard)
  138. arr.push({_id: field._id, value: null});
  139. });
  140. this.customFields.set(arr);
  141. },
  142. reset() {
  143. this.labels.set([]);
  144. this.members.set([]);
  145. this.customFields.set([]);
  146. },
  147. getLabels() {
  148. const currentBoardId = Session.get('currentBoard');
  149. return Boards.findOne(currentBoardId).labels.filter((label) => {
  150. return this.labels.get().indexOf(label._id) > -1;
  151. });
  152. },
  153. pressKey(evt) {
  154. // Pressing Enter should submit the card
  155. if (evt.keyCode === 13) {
  156. evt.preventDefault();
  157. const $form = $(evt.currentTarget).closest('form');
  158. // XXX For some reason $form.submit() does not work (it's probably a bug
  159. // of blaze-component related to the fact that the submit event is non-
  160. // bubbling). This is why we click on the submit button instead -- which
  161. // work.
  162. $form.find('button[type=submit]').click();
  163. // Pressing Tab should open the form of the next column, and Maj+Tab go
  164. // in the reverse order
  165. } else if (evt.keyCode === 9) {
  166. evt.preventDefault();
  167. const isReverse = evt.shiftKey;
  168. const list = $(`#js-list-${this.data().listId}`);
  169. const listSelector = '.js-list:not(.js-list-composer)';
  170. let nextList = list[isReverse ? 'prev' : 'next'](listSelector).get(0);
  171. // If there is no next list, loop back to the beginning.
  172. if (!nextList) {
  173. nextList = $(listSelector + (isReverse ? ':last' : ':first')).get(0);
  174. }
  175. BlazeComponent.getComponentForElement(nextList).openForm({
  176. position:this.data().position,
  177. });
  178. }
  179. },
  180. events() {
  181. return [{
  182. keydown: this.pressKey,
  183. 'click .js-link': Popup.open('linkCard'),
  184. 'click .js-search': Popup.open('searchCard'),
  185. }];
  186. },
  187. onRendered() {
  188. const editor = this;
  189. const $textarea = this.$('textarea');
  190. autosize($textarea);
  191. $textarea.escapeableTextComplete([
  192. // User mentions
  193. {
  194. match: /\B@([\w.]*)$/,
  195. search(term, callback) {
  196. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  197. callback($.map(currentBoard.activeMembers(), (member) => {
  198. const user = Users.findOne(member.userId);
  199. return user.username.indexOf(term) === 0 ? user : null;
  200. }));
  201. },
  202. template(user) {
  203. return user.username;
  204. },
  205. replace(user) {
  206. toggleValueInReactiveArray(editor.members, user._id);
  207. return '';
  208. },
  209. index: 1,
  210. },
  211. // Labels
  212. {
  213. match: /\B#(\w*)$/,
  214. search(term, callback) {
  215. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  216. callback($.map(currentBoard.labels, (label) => {
  217. if (label.name.indexOf(term) > -1 ||
  218. label.color.indexOf(term) > -1) {
  219. return label;
  220. }
  221. return null;
  222. }));
  223. },
  224. template(label) {
  225. return Blaze.toHTMLWithData(Template.autocompleteLabelLine, {
  226. hasNoName: !label.name,
  227. colorName: label.color,
  228. labelName: label.name || label.color,
  229. });
  230. },
  231. replace(label) {
  232. toggleValueInReactiveArray(editor.labels, label._id);
  233. return '';
  234. },
  235. index: 1,
  236. },
  237. ], {
  238. // When the autocomplete menu is shown we want both a press of both `Tab`
  239. // or `Enter` to validation the auto-completion. We also need to stop the
  240. // event propagation to prevent the card from submitting (on `Enter`) or
  241. // going on the next column (on `Tab`).
  242. onKeydown(evt, commands) {
  243. if (evt.keyCode === 9 || evt.keyCode === 13) {
  244. evt.stopPropagation();
  245. return commands.KEY_ENTER;
  246. }
  247. return null;
  248. },
  249. });
  250. },
  251. }).register('addCardForm');
  252. BlazeComponent.extendComponent({
  253. onCreated() {
  254. // Prefetch first non-current board id
  255. const boardId = Boards.findOne({
  256. archived: false,
  257. 'members.userId': Meteor.userId(),
  258. _id: {$ne: Session.get('currentBoard')},
  259. }, {
  260. sort: ['title'],
  261. })._id;
  262. // Subscribe to this board
  263. subManager.subscribe('board', boardId);
  264. this.selectedBoardId = new ReactiveVar(boardId);
  265. this.selectedSwimlaneId = new ReactiveVar('');
  266. this.selectedListId = new ReactiveVar('');
  267. this.boardId = Session.get('currentBoard');
  268. // In order to get current board info
  269. subManager.subscribe('board', this.boardId);
  270. this.board = Boards.findOne(this.boardId);
  271. // List where to insert card
  272. const list = $(Popup._getTopStack().openerElement).closest('.js-list');
  273. this.listId = Blaze.getData(list[0])._id;
  274. // Swimlane where to insert card
  275. const swimlane = $(Popup._getTopStack().openerElement).closest('.js-swimlane');
  276. this.swimlaneId = '';
  277. const boardView = Meteor.user().profile.boardView;
  278. if (boardView === 'board-view-swimlanes')
  279. this.swimlaneId = Blaze.getData(swimlane[0])._id;
  280. else if (boardView === 'board-view-lists')
  281. this.swimlaneId = Swimlanes.findOne({boardId: this.boardId})._id;
  282. },
  283. boards() {
  284. const boards = Boards.find({
  285. archived: false,
  286. 'members.userId': Meteor.userId(),
  287. _id: {$ne: Session.get('currentBoard')},
  288. }, {
  289. sort: ['title'],
  290. });
  291. return boards;
  292. },
  293. swimlanes() {
  294. if (!this.selectedBoardId) {
  295. return [];
  296. }
  297. const swimlanes = Swimlanes.find({boardId: this.selectedBoardId.get()});
  298. if (swimlanes.count())
  299. this.selectedSwimlaneId.set(swimlanes.fetch()[0]._id);
  300. return swimlanes;
  301. },
  302. lists() {
  303. if (!this.selectedBoardId) {
  304. return [];
  305. }
  306. const lists = Lists.find({boardId: this.selectedBoardId.get()});
  307. if (lists.count())
  308. this.selectedListId.set(lists.fetch()[0]._id);
  309. return lists;
  310. },
  311. cards() {
  312. if (!this.board) {
  313. return [];
  314. }
  315. const ownCardsIds = this.board.cards().map((card) => { return card.linkedId || card._id; });
  316. return Cards.find({
  317. boardId: this.selectedBoardId.get(),
  318. swimlaneId: this.selectedSwimlaneId.get(),
  319. listId: this.selectedListId.get(),
  320. archived: false,
  321. linkedId: {$nin: ownCardsIds},
  322. _id: {$nin: ownCardsIds},
  323. });
  324. },
  325. events() {
  326. return [{
  327. 'change .js-select-boards'(evt) {
  328. subManager.subscribe('board', $(evt.currentTarget).val());
  329. this.selectedBoardId.set($(evt.currentTarget).val());
  330. },
  331. 'change .js-select-swimlanes'(evt) {
  332. this.selectedSwimlaneId.set($(evt.currentTarget).val());
  333. },
  334. 'change .js-select-lists'(evt) {
  335. this.selectedListId.set($(evt.currentTarget).val());
  336. },
  337. 'click .js-done' (evt) {
  338. // LINK CARD
  339. evt.stopPropagation();
  340. evt.preventDefault();
  341. const linkedId = $('.js-select-cards option:selected').val();
  342. if (!linkedId) {
  343. Popup.close();
  344. return;
  345. }
  346. const _id = Cards.insert({
  347. title: $('.js-select-cards option:selected').text(), //dummy
  348. listId: this.listId,
  349. swimlaneId: this.swimlaneId,
  350. boardId: this.boardId,
  351. sort: Lists.findOne(this.listId).cards().count(),
  352. type: 'cardType-linkedCard',
  353. linkedId,
  354. });
  355. Filter.addException(_id);
  356. Popup.close();
  357. },
  358. 'click .js-link-board' (evt) {
  359. //LINK BOARD
  360. evt.stopPropagation();
  361. evt.preventDefault();
  362. const impBoardId = $('.js-select-boards option:selected').val();
  363. if (!impBoardId || Cards.findOne({linkedId: impBoardId, archived: false})) {
  364. Popup.close();
  365. return;
  366. }
  367. const _id = Cards.insert({
  368. title: $('.js-select-boards option:selected').text(), //dummy
  369. listId: this.listId,
  370. swimlaneId: this.swimlaneId,
  371. boardId: this.boardId,
  372. sort: Lists.findOne(this.listId).cards().count(),
  373. type: 'cardType-linkedBoard',
  374. linkedId: impBoardId,
  375. });
  376. Filter.addException(_id);
  377. Popup.close();
  378. },
  379. }];
  380. },
  381. }).register('linkCardPopup');
  382. BlazeComponent.extendComponent({
  383. mixins() {
  384. return [Mixins.PerfectScrollbar];
  385. },
  386. onCreated() {
  387. // Prefetch first non-current board id
  388. let board = Boards.findOne({
  389. archived: false,
  390. 'members.userId': Meteor.userId(),
  391. _id: {$ne: Session.get('currentBoard')},
  392. });
  393. if (!board) {
  394. Popup.close();
  395. return;
  396. }
  397. const boardId = board._id;
  398. // Subscribe to this board
  399. subManager.subscribe('board', boardId);
  400. this.selectedBoardId = new ReactiveVar(boardId);
  401. this.boardId = Session.get('currentBoard');
  402. // In order to get current board info
  403. subManager.subscribe('board', this.boardId);
  404. board = Boards.findOne(this.boardId);
  405. // List where to insert card
  406. const list = $(Popup._getTopStack().openerElement).closest('.js-list');
  407. this.listId = Blaze.getData(list[0])._id;
  408. // Swimlane where to insert card
  409. const swimlane = $(Popup._getTopStack().openerElement).closest('.js-swimlane');
  410. this.swimlaneId = '';
  411. if (board.view === 'board-view-swimlanes')
  412. this.swimlaneId = Blaze.getData(swimlane[0])._id;
  413. else
  414. this.swimlaneId = Swimlanes.findOne({boardId: this.boardId})._id;
  415. this.term = new ReactiveVar('');
  416. },
  417. boards() {
  418. const boards = Boards.find({
  419. archived: false,
  420. 'members.userId': Meteor.userId(),
  421. _id: {$ne: Session.get('currentBoard')},
  422. }, {
  423. sort: ['title'],
  424. });
  425. return boards;
  426. },
  427. results() {
  428. if (!this.selectedBoardId) {
  429. return [];
  430. }
  431. const board = Boards.findOne(this.selectedBoardId.get());
  432. return board.searchCards(this.term.get(), false);
  433. },
  434. events() {
  435. return [{
  436. 'change .js-select-boards'(evt) {
  437. subManager.subscribe('board', $(evt.currentTarget).val());
  438. this.selectedBoardId.set($(evt.currentTarget).val());
  439. },
  440. 'submit .js-search-term-form'(evt) {
  441. evt.preventDefault();
  442. this.term.set(evt.target.searchTerm.value);
  443. },
  444. 'click .js-minicard'(evt) {
  445. // LINK CARD
  446. const card = Blaze.getData(evt.currentTarget);
  447. const _id = Cards.insert({
  448. title: card.title, //dummy
  449. listId: this.listId,
  450. swimlaneId: this.swimlaneId,
  451. boardId: this.boardId,
  452. sort: Lists.findOne(this.listId).cards().count(),
  453. type: 'cardType-linkedCard',
  454. linkedId: card.linkedId || card._id,
  455. });
  456. Filter.addException(_id);
  457. Popup.close();
  458. },
  459. }];
  460. },
  461. }).register('searchCardPopup');