listBody.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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.componentChildren('inlinedForm');
  12. let form = _.find(forms, (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. let title = textarea.val();
  26. const position = Blaze.getData(evt.currentTarget).position;
  27. let sortIndex;
  28. if (position === 'top') {
  29. sortIndex = Utils.calculateIndex(null, firstCardDom).base;
  30. } else if (position === 'bottom') {
  31. sortIndex = Utils.calculateIndex(lastCardDom, null).base;
  32. }
  33. if ($.trim(title)) {
  34. // Parse for @user and #label mentions, stripping them from the title
  35. // and applying the appropriate users and labels to the card instead.
  36. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  37. // Find all @-mentioned usernames, collect a list of their IDs and strip
  38. // their mention out of the title.
  39. let foundUserIds = [];
  40. currentBoard.members.forEach(member => {
  41. const username = Users.findOne(member.userId).username;
  42. let nameNdx = title.indexOf('@' + username);
  43. if(nameNdx !== -1) {
  44. foundUserIds.push(member.userId);
  45. title = title.substr(0, nameNdx)
  46. + title.substr(nameNdx + username.length + 1);
  47. }
  48. });
  49. // Find all #-mentioned labels (based on their colour or name), collect a
  50. // list of their IDs, and strip their mention out of the title.
  51. let foundLabelIds = [];
  52. currentBoard.labels.forEach(label => {
  53. const labelName = (!label.name || label.name === "")
  54. ? label.color : label.name;
  55. let labelNdx = title.indexOf('#' + labelName);
  56. if(labelNdx !== -1) {
  57. foundLabelIds.push(label._id);
  58. title = title.substr(0, labelNdx)
  59. + title.substr(labelNdx + labelName.length + 1);
  60. }
  61. });
  62. const _id = Cards.insert({
  63. title,
  64. listId: this.data()._id,
  65. boardId: this.data().board()._id,
  66. labelIds: foundLabelIds,
  67. members: foundUserIds,
  68. sort: sortIndex,
  69. });
  70. // In case the filter is active we need to add the newly inserted card in
  71. // the list of exceptions -- cards that are not filtered. Otherwise the
  72. // card will disappear instantly.
  73. // See https://github.com/wekan/wekan/issues/80
  74. Filter.addException(_id);
  75. // We keep the form opened, empty it, and scroll to it.
  76. textarea.val('').focus();
  77. if (position === 'bottom') {
  78. this.scrollToBottom();
  79. }
  80. }
  81. },
  82. scrollToBottom() {
  83. const container = this.firstNode();
  84. $(container).animate({
  85. scrollTop: container.scrollHeight,
  86. });
  87. },
  88. clickOnMiniCard(evt) {
  89. if (MultiSelection.isActive() || evt.shiftKey) {
  90. evt.stopImmediatePropagation();
  91. evt.preventDefault();
  92. const methodName = evt.shiftKey ? 'toggleRange' : 'toggle';
  93. MultiSelection[methodName](this.currentData()._id);
  94. // If the card is already selected, we want to de-select it.
  95. // XXX We should probably modify the minicard href attribute instead of
  96. // overwriting the event in case the card is already selected.
  97. } else if (Session.equals('currentCard', this.currentData()._id)) {
  98. evt.stopImmediatePropagation();
  99. evt.preventDefault();
  100. Utils.goBoardId(Session.get('currentBoard'));
  101. }
  102. },
  103. cardIsSelected() {
  104. return Session.equals('currentCard', this.currentData()._id);
  105. },
  106. toggleMultiSelection(evt) {
  107. evt.stopPropagation();
  108. evt.preventDefault();
  109. MultiSelection.toggle(this.currentData()._id);
  110. },
  111. events() {
  112. return [{
  113. 'click .js-minicard': this.clickOnMiniCard,
  114. 'click .js-toggle-multi-selection': this.toggleMultiSelection,
  115. 'click .open-minicard-composer': this.scrollToBottom,
  116. submit: this.addCard,
  117. }];
  118. },
  119. }).register('listBody');
  120. let dropdownMenuIsOpened = false;
  121. BlazeComponent.extendComponent({
  122. template() {
  123. return 'addCardForm';
  124. },
  125. pressKey(evt) {
  126. // Don't do anything if the drop down is showing
  127. if(dropDownIsOpened) {
  128. return;
  129. }
  130. // Pressing Enter should submit the card
  131. if (evt.keyCode === 13) {
  132. evt.preventDefault();
  133. const $form = $(evt.currentTarget).closest('form');
  134. // XXX For some reason $form.submit() does not work (it's probably a bug
  135. // of blaze-component related to the fact that the submit event is non-
  136. // bubbling). This is why we click on the submit button instead -- which
  137. // work.
  138. $form.find('button[type=submit]').click();
  139. // Pressing Tab should open the form of the next column, and Maj+Tab go
  140. // in the reverse order
  141. } else if (evt.keyCode === 9) {
  142. evt.preventDefault();
  143. const isReverse = evt.shiftKey;
  144. const list = $(`#js-list-${this.data().listId}`);
  145. const listSelector = '.js-list:not(.js-list-composer)';
  146. let nextList = list[isReverse ? 'prev' : 'next'](listSelector).get(0);
  147. // If there is no next list, loop back to the beginning.
  148. if (!nextList) {
  149. nextList = $(listSelector + (isReverse ? ':last' : ':first')).get(0);
  150. }
  151. BlazeComponent.getComponentForElement(nextList).openForm({
  152. position:this.data().position,
  153. });
  154. }
  155. },
  156. events() {
  157. return [{
  158. keydown: this.pressKey,
  159. }];
  160. },
  161. onCreated() {
  162. dropDownIsOpened = false;
  163. },
  164. onRendered() {
  165. const $textarea = this.$('textarea');
  166. $textarea.textcomplete([
  167. // User mentions
  168. {
  169. match: /\B@(\w*)$/,
  170. search(term, callback) {
  171. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  172. callback($.map(currentBoard.members, (member) => {
  173. const username = Users.findOne(member.userId).username;
  174. return username.indexOf(term) === 0 ? username : null;
  175. }));
  176. },
  177. template(value) {
  178. return value;
  179. },
  180. replace(username) {
  181. return `@${username} `;
  182. },
  183. index: 1,
  184. },
  185. // Labels
  186. {
  187. match: /\B#(\w*)$/,
  188. search(term, callback) {
  189. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  190. callback($.map(currentBoard.labels, (label) => {
  191. const labelName = (!label.name || label.name === "") ? label.color : label.name;
  192. return labelName.indexOf(term) === 0 ? labelName : null;
  193. }));
  194. },
  195. template(value) {
  196. return value;
  197. },
  198. replace(label) {
  199. return `#${label} `;
  200. },
  201. index: 1,
  202. },
  203. ]);
  204. // customize hooks for dealing with the dropdowns
  205. $textarea.on({
  206. 'textComplete:show'() {
  207. dropDownIsOpened = true;
  208. },
  209. 'textComplete:hide'() {
  210. Tracker.afterFlush(() => {
  211. dropDownIsOpened = false;
  212. });
  213. },
  214. });
  215. },
  216. }).register('addCardForm');