listBody.js 7.5 KB

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