listBody.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. // Parse for @user and #label mentions, stripping them from the title
  34. // and applying the appropriate users and labels to the card instead.
  35. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  36. // Find all @-mentioned usernames, collect a list of their IDs and strip
  37. // their mention out of the title.
  38. let foundUserIds = []; // eslint-disable-line prefer-const
  39. currentBoard.members.forEach((member) => {
  40. const username = Users.findOne(member.userId).username;
  41. if (title.indexOf(`@${username}`) !== -1) {
  42. foundUserIds.push(member.userId);
  43. title = title.replace(`@${username}`, '');
  44. }
  45. });
  46. // Find all #-mentioned labels (based on their colour or name), collect a
  47. // list of their IDs, and strip their mention out of the title.
  48. let foundLabelIds = []; // eslint-disable-line prefer-const
  49. currentBoard.labels.forEach((label) => {
  50. const labelName = (!label.name || label.name === '')
  51. ? label.color : label.name;
  52. if (title.indexOf(`#${labelName}`) !== -1) {
  53. foundLabelIds.push(label._id);
  54. title = title.replace(`#${labelName}`, '');
  55. }
  56. });
  57. if ($.trim(title)) {
  58. const _id = Cards.insert({
  59. title,
  60. listId: this.data()._id,
  61. boardId: this.data().board()._id,
  62. labelIds: foundLabelIds,
  63. members: foundUserIds,
  64. sort: sortIndex,
  65. });
  66. // In case the filter is active we need to add the newly inserted card in
  67. // the list of exceptions -- cards that are not filtered. Otherwise the
  68. // card will disappear instantly.
  69. // See https://github.com/wekan/wekan/issues/80
  70. Filter.addException(_id);
  71. // We keep the form opened, empty it, and scroll to it.
  72. textarea.val('').focus();
  73. if (position === 'bottom') {
  74. this.scrollToBottom();
  75. }
  76. }
  77. },
  78. scrollToBottom() {
  79. const container = this.firstNode();
  80. $(container).animate({
  81. scrollTop: container.scrollHeight,
  82. });
  83. },
  84. clickOnMiniCard(evt) {
  85. if (MultiSelection.isActive() || evt.shiftKey) {
  86. evt.stopImmediatePropagation();
  87. evt.preventDefault();
  88. const methodName = evt.shiftKey ? 'toggleRange' : 'toggle';
  89. MultiSelection[methodName](this.currentData()._id);
  90. // If the card is already selected, we want to de-select it.
  91. // XXX We should probably modify the minicard href attribute instead of
  92. // overwriting the event in case the card is already selected.
  93. } else if (Session.equals('currentCard', this.currentData()._id)) {
  94. evt.stopImmediatePropagation();
  95. evt.preventDefault();
  96. Utils.goBoardId(Session.get('currentBoard'));
  97. }
  98. },
  99. cardIsSelected() {
  100. return Session.equals('currentCard', this.currentData()._id);
  101. },
  102. toggleMultiSelection(evt) {
  103. evt.stopPropagation();
  104. evt.preventDefault();
  105. MultiSelection.toggle(this.currentData()._id);
  106. },
  107. events() {
  108. return [{
  109. 'click .js-minicard': this.clickOnMiniCard,
  110. 'click .js-toggle-multi-selection': this.toggleMultiSelection,
  111. 'click .open-minicard-composer': this.scrollToBottom,
  112. submit: this.addCard,
  113. }];
  114. },
  115. }).register('listBody');
  116. let dropdownMenuIsOpened = false;
  117. BlazeComponent.extendComponent({
  118. template() {
  119. return 'addCardForm';
  120. },
  121. pressKey(evt) {
  122. // Don't do anything if the drop down is showing
  123. if (dropdownMenuIsOpened) {
  124. return;
  125. }
  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. onCreated() {
  158. dropdownMenuIsOpened = false;
  159. },
  160. onRendered() {
  161. const $textarea = this.$('textarea');
  162. const currentBoard = Boards.findOne(Session.get('currentBoard'));
  163. $textarea.textcomplete([
  164. // User mentions
  165. {
  166. match: /\B@(\w*)$/,
  167. search(term, callback) {
  168. callback($.map(currentBoard.members, (member) => {
  169. const username = Users.findOne(member.userId).username;
  170. return username.indexOf(term) === 0 ? username : null;
  171. }));
  172. },
  173. template(value) {
  174. return value;
  175. },
  176. replace(username) {
  177. return `@${username} `;
  178. },
  179. index: 1,
  180. },
  181. // Labels
  182. {
  183. match: /\B#(\w*)$/,
  184. search(term, callback) {
  185. callback($.map(currentBoard.labels, (label) => {
  186. const labelName = (!label.name || label.name === '')
  187. ? label.color
  188. : label.name;
  189. return labelName.indexOf(term) === 0 ? labelName : null;
  190. }));
  191. },
  192. template(value) {
  193. // XXX the following is duplicated from editor.js and should be
  194. // abstracted to keep things DRY
  195. // add a "colour badge" in front of the label name
  196. // but first, get the colour's name from its value
  197. const colorName = currentBoard.labels.find((label) => {
  198. return value === label.name || value === label.color;
  199. }).color;
  200. const valueSpan = (colorName === value)
  201. ? `<span class="quiet">${value}</span>`
  202. : value;
  203. return (colorName && colorName !== '')
  204. ? `<div class="minicard-label card-label-${colorName}"
  205. title="${value}"></div> ${valueSpan}`
  206. : value;
  207. },
  208. replace(label) {
  209. return `#${label} `;
  210. },
  211. index: 1,
  212. },
  213. ]);
  214. // customize hooks for dealing with the dropdowns
  215. $textarea.on({
  216. 'textComplete:show'() {
  217. dropdownMenuIsOpened = true;
  218. },
  219. 'textComplete:hide'() {
  220. Tracker.afterFlush(() => {
  221. dropdownMenuIsOpened = false;
  222. });
  223. },
  224. });
  225. EscapeActions.register('textcomplete',
  226. () => {},
  227. () => dropdownMenuIsOpened
  228. );
  229. },
  230. }).register('addCardForm');