listBody.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. const boardId = this.data().board();
  35. let swimlaneId = '';
  36. const boardView = Meteor.user().profile.boardView;
  37. if (boardView === 'board-view-swimlanes')
  38. swimlaneId = this.parentComponent().parentComponent().data()._id;
  39. else if ((boardView === 'board-view-lists') || (boardView === 'board-view-cal'))
  40. swimlaneId = boardId.getDefaultSwimline()._id;
  41. if (title) {
  42. const _id = Cards.insert({
  43. title,
  44. members,
  45. labelIds,
  46. customFields,
  47. listId: this.data()._id,
  48. boardId: boardId._id,
  49. sort: sortIndex,
  50. swimlaneId,
  51. });
  52. // In case the filter is active we need to add the newly inserted card in
  53. // the list of exceptions -- cards that are not filtered. Otherwise the
  54. // card will disappear instantly.
  55. // See https://github.com/wekan/wekan/issues/80
  56. Filter.addException(_id);
  57. // We keep the form opened, empty it, and scroll to it.
  58. textarea.val('').focus();
  59. autosize.update(textarea);
  60. if (position === 'bottom') {
  61. this.scrollToBottom();
  62. }
  63. formComponent.reset();
  64. }
  65. },
  66. scrollToBottom() {
  67. const container = this.firstNode();
  68. $(container).animate({
  69. scrollTop: container.scrollHeight,
  70. });
  71. },
  72. clickOnMiniCard(evt) {
  73. if (MultiSelection.isActive() || evt.shiftKey) {
  74. evt.stopImmediatePropagation();
  75. evt.preventDefault();
  76. const methodName = evt.shiftKey ? 'toggleRange' : 'toggle';
  77. MultiSelection[methodName](this.currentData()._id);
  78. // If the card is already selected, we want to de-select it.
  79. // XXX We should probably modify the minicard href attribute instead of
  80. // overwriting the event in case the card is already selected.
  81. } else if (Session.equals('currentCard', this.currentData()._id)) {
  82. evt.stopImmediatePropagation();
  83. evt.preventDefault();
  84. Utils.goBoardId(Session.get('currentBoard'));
  85. }
  86. },
  87. cardIsSelected() {
  88. return Session.equals('currentCard', this.currentData()._id);
  89. },
  90. toggleMultiSelection(evt) {
  91. evt.stopPropagation();
  92. evt.preventDefault();
  93. MultiSelection.toggle(this.currentData()._id);
  94. },
  95. idOrNull(swimlaneId) {
  96. const currentUser = Meteor.user();
  97. if (currentUser.profile.boardView === 'board-view-swimlanes')
  98. return swimlaneId;
  99. return undefined;
  100. },
  101. canSeeAddCard() {
  102. return !this.reachedWipLimit() && Meteor.user() && Meteor.user().isBoardMember() && !Meteor.user().isCommentOnly();
  103. },
  104. reachedWipLimit() {
  105. const list = Template.currentData();
  106. return !list.getWipLimit('soft') && list.getWipLimit('enabled') && list.getWipLimit('value') <= list.cards().count();
  107. },
  108. events() {
  109. return [{
  110. 'click .js-minicard': this.clickOnMiniCard,
  111. 'click .js-toggle-multi-selection': this.toggleMultiSelection,
  112. 'click .open-minicard-composer': this.scrollToBottom,
  113. submit: this.addCard,
  114. }];
  115. },
  116. }).register('listBody');
  117. function toggleValueInReactiveArray(reactiveValue, value) {
  118. const array = reactiveValue.get();
  119. const valueIndex = array.indexOf(value);
  120. if (valueIndex === -1) {
  121. array.push(value);
  122. } else {
  123. array.splice(valueIndex, 1);
  124. }
  125. reactiveValue.set(array);
  126. }
  127. BlazeComponent.extendComponent({
  128. onCreated() {
  129. this.labels = new ReactiveVar([]);
  130. this.members = new ReactiveVar([]);
  131. this.customFields = new ReactiveVar([]);
  132. },
  133. reset() {
  134. this.labels.set([]);
  135. this.members.set([]);
  136. this.customFields.set([]);
  137. },
  138. getLabels() {
  139. const currentBoardId = Session.get('currentBoard');
  140. return Boards.findOne(currentBoardId).labels.filter((label) => {
  141. return this.labels.get().indexOf(label._id) > -1;
  142. });
  143. },
  144. pressKey(evt) {
  145. // Pressing Enter should submit the card
  146. if (evt.keyCode === 13) {
  147. evt.preventDefault();
  148. const $form = $(evt.currentTarget).closest('form');
  149. // XXX For some reason $form.submit() does not work (it's probably a bug
  150. // of blaze-component related to the fact that the submit event is non-
  151. // bubbling). This is why we click on the submit button instead -- which
  152. // work.
  153. $form.find('button[type=submit]').click();
  154. // Pressing Tab should open the form of the next column, and Maj+Tab go
  155. // in the reverse order
  156. } else if (evt.keyCode === 9) {
  157. evt.preventDefault();
  158. const isReverse = evt.shiftKey;
  159. const list = $(`#js-list-${this.data().listId}`);
  160. const listSelector = '.js-list:not(.js-list-composer)';
  161. let nextList = list[isReverse ? 'prev' : 'next'](listSelector).get(0);
  162. // If there is no next list, loop back to the beginning.
  163. if (!nextList) {
  164. nextList = $(listSelector + (isReverse ? ':last' : ':first')).get(0);
  165. }
  166. BlazeComponent.getComponentForElement(nextList).openForm({
  167. position:this.data().position,
  168. });
  169. }
  170. },
  171. events() {
  172. return [{
  173. keydown: this.pressKey,
  174. }];
  175. },
  176. onRendered() {
  177. const editor = this;
  178. const $textarea = this.$('textarea');
  179. autosize($textarea);
  180. $textarea.escapeableTextComplete([
  181. // User mentions
  182. {
  183. match: /\B@([\w.]*)$/,
  184. search(term, callback) {
  185. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  186. callback($.map(currentBoard.activeMembers(), (member) => {
  187. const user = Users.findOne(member.userId);
  188. return user.username.indexOf(term) === 0 ? user : null;
  189. }));
  190. },
  191. template(user) {
  192. return user.username;
  193. },
  194. replace(user) {
  195. toggleValueInReactiveArray(editor.members, user._id);
  196. return '';
  197. },
  198. index: 1,
  199. },
  200. // Labels
  201. {
  202. match: /\B#(\w*)$/,
  203. search(term, callback) {
  204. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  205. callback($.map(currentBoard.labels, (label) => {
  206. if (label.name.indexOf(term) > -1 ||
  207. label.color.indexOf(term) > -1) {
  208. return label;
  209. }
  210. return null;
  211. }));
  212. },
  213. template(label) {
  214. return Blaze.toHTMLWithData(Template.autocompleteLabelLine, {
  215. hasNoName: !label.name,
  216. colorName: label.color,
  217. labelName: label.name || label.color,
  218. });
  219. },
  220. replace(label) {
  221. toggleValueInReactiveArray(editor.labels, label._id);
  222. return '';
  223. },
  224. index: 1,
  225. },
  226. ], {
  227. // When the autocomplete menu is shown we want both a press of both `Tab`
  228. // or `Enter` to validation the auto-completion. We also need to stop the
  229. // event propagation to prevent the card from submitting (on `Enter`) or
  230. // going on the next column (on `Tab`).
  231. onKeydown(evt, commands) {
  232. if (evt.keyCode === 9 || evt.keyCode === 13) {
  233. evt.stopPropagation();
  234. return commands.KEY_ENTER;
  235. }
  236. return null;
  237. },
  238. });
  239. },
  240. }).register('addCardForm');