listBody.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. BlazeComponent.extendComponent({
  2. template() {
  3. return 'listBody';
  4. },
  5. mixins() {
  6. return [Mixins.PerfectScrollbar];
  7. },
  8. openForm(options) {
  9. options = options || {};
  10. options.position = options.position || 'top';
  11. const forms = this.childComponents('inlinedForm');
  12. let form = forms.find((component) => {
  13. return component.data().position === options.position;
  14. });
  15. if (!form && forms.length > 0) {
  16. form = forms[0];
  17. }
  18. form.open();
  19. },
  20. addCard(evt) {
  21. evt.preventDefault();
  22. const firstCardDom = this.find('.js-minicard:first');
  23. const lastCardDom = this.find('.js-minicard:last');
  24. const textarea = $(evt.currentTarget).find('textarea');
  25. const position = this.currentData().position;
  26. const title = textarea.val().trim();
  27. const formComponent = this.childComponents('addCardForm')[0];
  28. let sortIndex;
  29. if (position === 'top') {
  30. sortIndex = Utils.calculateIndex(null, firstCardDom).base;
  31. } else if (position === 'bottom') {
  32. sortIndex = Utils.calculateIndex(lastCardDom, null).base;
  33. }
  34. const members = formComponent.members.get();
  35. const labelIds = formComponent.labels.get();
  36. if (title) {
  37. const _id = Cards.insert({
  38. title,
  39. members,
  40. labelIds,
  41. listId: this.data()._id,
  42. boardId: this.data().board()._id,
  43. sort: sortIndex,
  44. });
  45. // In case the filter is active we need to add the newly inserted card in
  46. // the list of exceptions -- cards that are not filtered. Otherwise the
  47. // card will disappear instantly.
  48. // See https://github.com/wekan/wekan/issues/80
  49. Filter.addException(_id);
  50. // We keep the form opened, empty it, and scroll to it.
  51. textarea.val('').focus();
  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. events() {
  88. return [{
  89. 'click .js-minicard': this.clickOnMiniCard,
  90. 'click .js-toggle-multi-selection': this.toggleMultiSelection,
  91. 'click .open-minicard-composer': this.scrollToBottom,
  92. submit: this.addCard,
  93. }];
  94. },
  95. }).register('listBody');
  96. function toggleValueInReactiveArray(reactiveValue, value) {
  97. const array = reactiveValue.get();
  98. const valueIndex = array.indexOf(value);
  99. if (valueIndex === -1) {
  100. array.push(value);
  101. } else {
  102. array.splice(valueIndex, 1);
  103. }
  104. reactiveValue.set(array);
  105. }
  106. BlazeComponent.extendComponent({
  107. template() {
  108. return 'addCardForm';
  109. },
  110. onCreated() {
  111. this.labels = new ReactiveVar([]);
  112. this.members = new ReactiveVar([]);
  113. },
  114. reset() {
  115. this.labels.set([]);
  116. this.members.set([]);
  117. },
  118. getLabels() {
  119. const currentBoardId = Session.get('currentBoard');
  120. return Boards.findOne(currentBoardId).labels.filter((label) => {
  121. return this.labels.get().indexOf(label._id) > -1;
  122. });
  123. },
  124. pressKey(evt) {
  125. // Pressing Enter should submit the card
  126. if (evt.keyCode === 13) {
  127. evt.preventDefault();
  128. const $form = $(evt.currentTarget).closest('form');
  129. // XXX For some reason $form.submit() does not work (it's probably a bug
  130. // of blaze-component related to the fact that the submit event is non-
  131. // bubbling). This is why we click on the submit button instead -- which
  132. // work.
  133. $form.find('button[type=submit]').click();
  134. // Pressing Tab should open the form of the next column, and Maj+Tab go
  135. // in the reverse order
  136. } else if (evt.keyCode === 9) {
  137. evt.preventDefault();
  138. const isReverse = evt.shiftKey;
  139. const list = $(`#js-list-${this.data().listId}`);
  140. const listSelector = '.js-list:not(.js-list-composer)';
  141. let nextList = list[isReverse ? 'prev' : 'next'](listSelector).get(0);
  142. // If there is no next list, loop back to the beginning.
  143. if (!nextList) {
  144. nextList = $(listSelector + (isReverse ? ':last' : ':first')).get(0);
  145. }
  146. BlazeComponent.getComponentForElement(nextList).openForm({
  147. position:this.data().position,
  148. });
  149. }
  150. },
  151. events() {
  152. return [{
  153. keydown: this.pressKey,
  154. }];
  155. },
  156. onRendered() {
  157. const editor = this;
  158. this.$('textarea').escapeableTextComplete([
  159. // User mentions
  160. {
  161. match: /\B@(\w*)$/,
  162. search(term, callback) {
  163. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  164. callback($.map(currentBoard.activeMembers(), (member) => {
  165. const user = Users.findOne(member.userId);
  166. return user.username.indexOf(term) === 0 ? user : null;
  167. }));
  168. },
  169. template(user) {
  170. return user.username;
  171. },
  172. replace(user) {
  173. toggleValueInReactiveArray(editor.members, user._id);
  174. return '';
  175. },
  176. index: 1,
  177. },
  178. // Labels
  179. {
  180. match: /\B#(\w*)$/,
  181. search(term, callback) {
  182. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  183. callback($.map(currentBoard.labels, (label) => {
  184. if (label.name.indexOf(term) > -1 ||
  185. label.color.indexOf(term) > -1) {
  186. return label;
  187. }
  188. }));
  189. },
  190. template(label) {
  191. return Blaze.toHTMLWithData(Template.autocompleteLabelLine, {
  192. hasNoName: !Boolean(label.name),
  193. colorName: label.color,
  194. labelName: label.name || label.color,
  195. });
  196. },
  197. replace(label) {
  198. toggleValueInReactiveArray(editor.labels, label._id);
  199. return '';
  200. },
  201. index: 1,
  202. },
  203. ], {
  204. // When the autocomplete menu is shown we want both a press of both `Tab`
  205. // or `Enter` to validation the auto-completion. We also need to stop the
  206. // event propagation to prevent the card from submitting (on `Enter`) or
  207. // going on the next column (on `Tab`).
  208. onKeydown(evt, commands) {
  209. if (evt.keyCode === 9 || evt.keyCode === 13) {
  210. evt.stopPropagation();
  211. return commands.KEY_ENTER;
  212. }
  213. },
  214. });
  215. },
  216. }).register('addCardForm');