listBody.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. }];
  178. },
  179. onRendered() {
  180. const editor = this;
  181. const $textarea = this.$('textarea');
  182. autosize($textarea);
  183. $textarea.escapeableTextComplete([
  184. // User mentions
  185. {
  186. match: /\B@([\w.]*)$/,
  187. search(term, callback) {
  188. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  189. callback($.map(currentBoard.activeMembers(), (member) => {
  190. const user = Users.findOne(member.userId);
  191. return user.username.indexOf(term) === 0 ? user : null;
  192. }));
  193. },
  194. template(user) {
  195. return user.username;
  196. },
  197. replace(user) {
  198. toggleValueInReactiveArray(editor.members, user._id);
  199. return '';
  200. },
  201. index: 1,
  202. },
  203. // Labels
  204. {
  205. match: /\B#(\w*)$/,
  206. search(term, callback) {
  207. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  208. callback($.map(currentBoard.labels, (label) => {
  209. if (label.name.indexOf(term) > -1 ||
  210. label.color.indexOf(term) > -1) {
  211. return label;
  212. }
  213. return null;
  214. }));
  215. },
  216. template(label) {
  217. return Blaze.toHTMLWithData(Template.autocompleteLabelLine, {
  218. hasNoName: !label.name,
  219. colorName: label.color,
  220. labelName: label.name || label.color,
  221. });
  222. },
  223. replace(label) {
  224. toggleValueInReactiveArray(editor.labels, label._id);
  225. return '';
  226. },
  227. index: 1,
  228. },
  229. ], {
  230. // When the autocomplete menu is shown we want both a press of both `Tab`
  231. // or `Enter` to validation the auto-completion. We also need to stop the
  232. // event propagation to prevent the card from submitting (on `Enter`) or
  233. // going on the next column (on `Tab`).
  234. onKeydown(evt, commands) {
  235. if (evt.keyCode === 9 || evt.keyCode === 13) {
  236. evt.stopPropagation();
  237. return commands.KEY_ENTER;
  238. }
  239. return null;
  240. },
  241. });
  242. },
  243. }).register('addCardForm');
  244. BlazeComponent.extendComponent({
  245. onCreated() {
  246. subManager.subscribe('board', Session.get('currentBoard'));
  247. this.selectedBoardId = new ReactiveVar(Session.get('currentBoard'));
  248. },
  249. boards() {
  250. const boards = Boards.find({
  251. archived: false,
  252. 'members.userId': Meteor.userId(),
  253. }, {
  254. sort: ['title'],
  255. });
  256. return boards;
  257. },
  258. swimlanes() {
  259. const board = Boards.findOne(this.selectedBoardId.get());
  260. return board.swimlanes();
  261. },
  262. lists() {
  263. const board = Boards.findOne(this.selectedBoardId.get());
  264. return board.lists();
  265. },
  266. cards() {
  267. const board = Boards.findOne(this.selectedBoardId.get());
  268. return board.cards();
  269. },
  270. events() {
  271. return [{
  272. 'change .js-select-boards'(evt) {
  273. this.selectedBoardId.set($(evt.currentTarget).val());
  274. subManager.subscribe('board', this.selectedBoardId.get());
  275. },
  276. 'submit .js-done' (evt) {
  277. // IMPORT CARD
  278. evt.preventDefault();
  279. // XXX We should *not* get the currentCard from the global state, but
  280. // instead from a “component” state.
  281. const card = Cards.findOne(Session.get('currentCard'));
  282. const lSelect = $('.js-select-lists')[0];
  283. const newListId = lSelect.options[lSelect.selectedIndex].value;
  284. const slSelect = $('.js-select-swimlanes')[0];
  285. card.swimlaneId = slSelect.options[slSelect.selectedIndex].value;
  286. Popup.close();
  287. },
  288. 'submit .js-import-board' (evt) {
  289. //IMPORT BOARD
  290. evt.preventDefault();
  291. Popup.close();
  292. },
  293. 'click .js-search': Popup.open('searchCard'),
  294. }];
  295. },
  296. }).register('importCardPopup');
  297. BlazeComponent.extendComponent({
  298. mixins() {
  299. return [Mixins.PerfectScrollbar];
  300. },
  301. onCreated() {
  302. const boardId = Boards.findOne({
  303. archived: false,
  304. 'members.userId': Meteor.userId(),
  305. _id: {$ne: Session.get('currentBoard')},
  306. })._id;
  307. subManager.subscribe('board', boardId);
  308. this.selectedBoardId = new ReactiveVar(boardId);
  309. this.term = new ReactiveVar('');
  310. },
  311. boards() {
  312. const boards = Boards.find({
  313. archived: false,
  314. 'members.userId': Meteor.userId(),
  315. _id: {$ne: Session.get('currentBoard')},
  316. }, {
  317. sort: ['title'],
  318. });
  319. return boards;
  320. },
  321. results() {
  322. const board = Boards.findOne(this.selectedBoardId.get());
  323. return board.searchCards(this.term.get());
  324. },
  325. events() {
  326. return [{
  327. 'change .js-select-boards'(evt) {
  328. this.selectedBoardId.set($(evt.currentTarget).val());
  329. subManager.subscribe('board', this.selectedBoardId.get());
  330. },
  331. 'submit .js-search-term-form'(evt) {
  332. evt.preventDefault();
  333. this.term.set(evt.target.searchTerm.value);
  334. },
  335. 'click .js-minicard'(evt) {
  336. // IMPORT CARD
  337. },
  338. }];
  339. },
  340. }).register('searchCardPopup');