listBody.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. BlazeComponent.extendComponent({
  2. mixins() {
  3. return [Mixins.PerfectScrollbar];
  4. },
  5. openForm(options) {
  6. options = options || {};
  7. options.position = options.position || 'top';
  8. const forms = this.childComponents('inlinedForm');
  9. let form = forms.find((component) => {
  10. return component.data().position === options.position;
  11. });
  12. if (!form && forms.length > 0) {
  13. form = forms[0];
  14. }
  15. form.open();
  16. },
  17. addCard(evt) {
  18. evt.preventDefault();
  19. const firstCardDom = this.find('.js-minicard:first');
  20. const lastCardDom = this.find('.js-minicard:last');
  21. const textarea = $(evt.currentTarget).find('textarea');
  22. const position = this.currentData().position;
  23. const title = textarea.val().trim();
  24. const formComponent = this.childComponents('addCardForm')[0];
  25. let sortIndex;
  26. if (position === 'top') {
  27. sortIndex = Utils.calculateIndex(null, firstCardDom).base;
  28. } else if (position === 'bottom') {
  29. sortIndex = Utils.calculateIndex(lastCardDom, null).base;
  30. }
  31. const members = formComponent.members.get();
  32. const labelIds = formComponent.labels.get();
  33. const customFields = formComponent.customFields.get();
  34. //console.log('members', members);
  35. //console.log('labelIds', labelIds);
  36. //console.log('customFields', customFields);
  37. const boardId = this.data().board()._id;
  38. let swimlaneId = '';
  39. const boardView = Meteor.user().profile.boardView;
  40. if (boardView === 'board-view-swimlanes')
  41. swimlaneId = this.parentComponent().parentComponent().data()._id;
  42. else if (boardView === 'board-view-lists')
  43. swimlaneId = Swimlanes.findOne({boardId})._id;
  44. if (title) {
  45. const _id = Cards.insert({
  46. title,
  47. members,
  48. labelIds,
  49. customFields,
  50. listId: this.data()._id,
  51. boardId: this.data().board()._id,
  52. sort: sortIndex,
  53. swimlaneId,
  54. });
  55. // In case the filter is active we need to add the newly inserted card in
  56. // the list of exceptions -- cards that are not filtered. Otherwise the
  57. // card will disappear instantly.
  58. // See https://github.com/wekan/wekan/issues/80
  59. Filter.addException(_id);
  60. // We keep the form opened, empty it, and scroll to it.
  61. textarea.val('').focus();
  62. autosize.update(textarea);
  63. if (position === 'bottom') {
  64. this.scrollToBottom();
  65. }
  66. formComponent.reset();
  67. }
  68. },
  69. scrollToBottom() {
  70. const container = this.firstNode();
  71. $(container).animate({
  72. scrollTop: container.scrollHeight,
  73. });
  74. },
  75. clickOnMiniCard(evt) {
  76. if (MultiSelection.isActive() || evt.shiftKey) {
  77. evt.stopImmediatePropagation();
  78. evt.preventDefault();
  79. const methodName = evt.shiftKey ? 'toggleRange' : 'toggle';
  80. MultiSelection[methodName](this.currentData()._id);
  81. // If the card is already selected, we want to de-select it.
  82. // XXX We should probably modify the minicard href attribute instead of
  83. // overwriting the event in case the card is already selected.
  84. } else if (Session.equals('currentCard', this.currentData()._id)) {
  85. evt.stopImmediatePropagation();
  86. evt.preventDefault();
  87. Utils.goBoardId(Session.get('currentBoard'));
  88. }
  89. },
  90. cardIsSelected() {
  91. return Session.equals('currentCard', this.currentData()._id);
  92. },
  93. toggleMultiSelection(evt) {
  94. evt.stopPropagation();
  95. evt.preventDefault();
  96. MultiSelection.toggle(this.currentData()._id);
  97. },
  98. idOrNull(swimlaneId) {
  99. const currentUser = Meteor.user();
  100. if (currentUser.profile.boardView === 'board-view-swimlanes')
  101. return swimlaneId;
  102. return undefined;
  103. },
  104. canSeeAddCard() {
  105. return !this.reachedWipLimit() && Meteor.user() && Meteor.user().isBoardMember() && !Meteor.user().isCommentOnly();
  106. },
  107. reachedWipLimit() {
  108. const list = Template.currentData();
  109. return !list.getWipLimit('soft') && list.getWipLimit('enabled') && list.getWipLimit('value') <= list.cards().count();
  110. },
  111. events() {
  112. return [{
  113. 'click .js-minicard': this.clickOnMiniCard,
  114. 'click .js-toggle-multi-selection': this.toggleMultiSelection,
  115. 'click .open-minicard-composer': this.scrollToBottom,
  116. submit: this.addCard,
  117. }];
  118. },
  119. }).register('listBody');
  120. function toggleValueInReactiveArray(reactiveValue, value) {
  121. const array = reactiveValue.get();
  122. const valueIndex = array.indexOf(value);
  123. if (valueIndex === -1) {
  124. array.push(value);
  125. } else {
  126. array.splice(valueIndex, 1);
  127. }
  128. reactiveValue.set(array);
  129. }
  130. BlazeComponent.extendComponent({
  131. onCreated() {
  132. this.labels = new ReactiveVar([]);
  133. this.members = new ReactiveVar([]);
  134. this.customFields = new ReactiveVar([]);
  135. },
  136. reset() {
  137. this.labels.set([]);
  138. this.members.set([]);
  139. this.customFields.set([]);
  140. },
  141. getLabels() {
  142. const currentBoardId = Session.get('currentBoard');
  143. return Boards.findOne(currentBoardId).labels.filter((label) => {
  144. return this.labels.get().indexOf(label._id) > -1;
  145. });
  146. },
  147. pressKey(evt) {
  148. // Pressing Enter should submit the card
  149. if (evt.keyCode === 13) {
  150. evt.preventDefault();
  151. const $form = $(evt.currentTarget).closest('form');
  152. // XXX For some reason $form.submit() does not work (it's probably a bug
  153. // of blaze-component related to the fact that the submit event is non-
  154. // bubbling). This is why we click on the submit button instead -- which
  155. // work.
  156. $form.find('button[type=submit]').click();
  157. // Pressing Tab should open the form of the next column, and Maj+Tab go
  158. // in the reverse order
  159. } else if (evt.keyCode === 9) {
  160. evt.preventDefault();
  161. const isReverse = evt.shiftKey;
  162. const list = $(`#js-list-${this.data().listId}`);
  163. const listSelector = '.js-list:not(.js-list-composer)';
  164. let nextList = list[isReverse ? 'prev' : 'next'](listSelector).get(0);
  165. // If there is no next list, loop back to the beginning.
  166. if (!nextList) {
  167. nextList = $(listSelector + (isReverse ? ':last' : ':first')).get(0);
  168. }
  169. BlazeComponent.getComponentForElement(nextList).openForm({
  170. position:this.data().position,
  171. });
  172. }
  173. },
  174. events() {
  175. return [{
  176. keydown: this.pressKey,
  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');