listBody.js 7.1 KB

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