listBody.js 7.4 KB

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