listBody.js 7.5 KB

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