listBody.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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
  38. // and strip 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) + title.substr(nameNdx + username.length + 1);
  46. }
  47. });
  48. // Find all #-mentioned labels (based on their colour or name),
  49. // collect a list of their IDs, and strip their mention out of
  50. // the title.
  51. let foundLabelIds = [];
  52. currentBoard.labels.forEach(label => {
  53. const labelName = (!label.name || label.name === "") ? label.color : label.name;
  54. let labelNdx = title.indexOf('#' + labelName);
  55. if(labelNdx !== -1) {
  56. foundLabelIds.push(label._id);
  57. title = title.substr(0, labelNdx) + title.substr(labelNdx + labelName.length + 1);
  58. }
  59. });
  60. const _id = Cards.insert({
  61. title,
  62. listId: this.data()._id,
  63. boardId: this.data().board()._id,
  64. labelIds: foundLabelIds,
  65. members: foundUserIds,
  66. sort: sortIndex,
  67. });
  68. // In case the filter is active we need to add the newly inserted card in
  69. // the list of exceptions -- cards that are not filtered. Otherwise the
  70. // card will disappear instantly.
  71. // See https://github.com/wekan/wekan/issues/80
  72. Filter.addException(_id);
  73. // We keep the form opened, empty it, and scroll to it.
  74. textarea.val('').focus();
  75. if (position === 'bottom') {
  76. this.scrollToBottom();
  77. }
  78. }
  79. },
  80. scrollToBottom() {
  81. const container = this.firstNode();
  82. $(container).animate({
  83. scrollTop: container.scrollHeight,
  84. });
  85. },
  86. clickOnMiniCard(evt) {
  87. if (MultiSelection.isActive() || evt.shiftKey) {
  88. evt.stopImmediatePropagation();
  89. evt.preventDefault();
  90. const methodName = evt.shiftKey ? 'toggleRange' : 'toggle';
  91. MultiSelection[methodName](this.currentData()._id);
  92. // If the card is already selected, we want to de-select it.
  93. // XXX We should probably modify the minicard href attribute instead of
  94. // overwriting the event in case the card is already selected.
  95. } else if (Session.equals('currentCard', this.currentData()._id)) {
  96. evt.stopImmediatePropagation();
  97. evt.preventDefault();
  98. Utils.goBoardId(Session.get('currentBoard'));
  99. }
  100. },
  101. cardIsSelected() {
  102. return Session.equals('currentCard', this.currentData()._id);
  103. },
  104. toggleMultiSelection(evt) {
  105. evt.stopPropagation();
  106. evt.preventDefault();
  107. MultiSelection.toggle(this.currentData()._id);
  108. },
  109. events() {
  110. return [{
  111. 'click .js-minicard': this.clickOnMiniCard,
  112. 'click .js-toggle-multi-selection': this.toggleMultiSelection,
  113. 'click .open-minicard-composer': this.scrollToBottom,
  114. submit: this.addCard,
  115. }];
  116. },
  117. }).register('listBody');
  118. let dropdownMenuIsOpened = false;
  119. BlazeComponent.extendComponent({
  120. template() {
  121. return 'addCardForm';
  122. },
  123. pressKey(evt) {
  124. // don't do anything if the drop down is showing
  125. if(dropDownIsOpened) {
  126. return;
  127. }
  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. onCreated() {
  160. dropDownIsOpened = false;
  161. },
  162. onRendered() {
  163. const $textarea = this.$('textarea');
  164. $textarea.textcomplete([
  165. // User mentions
  166. {
  167. match: /\B@(\w*)$/,
  168. search(term, callback) {
  169. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  170. callback($.map(currentBoard.members, (member) => {
  171. const username = Users.findOne(member.userId).username;
  172. return username.indexOf(term) === 0 ? username : null;
  173. }));
  174. },
  175. template(value) {
  176. return value;
  177. },
  178. replace(username) {
  179. return `@${username} `;
  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. const labelName = (!label.name || label.name === "") ? label.color : label.name;
  190. return labelName.indexOf(term) === 0 ? labelName : null;
  191. }));
  192. },
  193. template(value) {
  194. return value;
  195. },
  196. replace(label) {
  197. return `#${label} `;
  198. },
  199. index: 1,
  200. },
  201. ]);
  202. // customize hooks for dealing with the dropdowns
  203. $textarea.on({
  204. 'textComplete:show'() {
  205. dropDownIsOpened = true;
  206. },
  207. 'textComplete:hide'() {
  208. Tracker.afterFlush(() => {
  209. dropDownIsOpened = false;
  210. });
  211. },
  212. });
  213. },
  214. }).register('addCardForm');