listHeader.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. let listsColors;
  2. Meteor.startup(() => {
  3. listsColors = Lists.simpleSchema()._schema.color.allowedValues;
  4. });
  5. BlazeComponent.extendComponent({
  6. canSeeAddCard() {
  7. const list = Template.currentData();
  8. return (
  9. (!list.getWipLimit('enabled') ||
  10. list.getWipLimit('soft') ||
  11. !this.reachedWipLimit()) &&
  12. !Meteor.user().isWorker()
  13. );
  14. },
  15. isBoardAdmin() {
  16. return Meteor.user().isBoardAdmin();
  17. },
  18. starred(check = undefined) {
  19. const list = Template.currentData();
  20. const status = list.isStarred();
  21. if (check === undefined) {
  22. // just check
  23. return status;
  24. } else {
  25. list.star(!status);
  26. return !status;
  27. }
  28. },
  29. editTitle(event) {
  30. event.preventDefault();
  31. const newTitle = this.childComponents('inlinedForm')[0]
  32. .getValue()
  33. .trim();
  34. const list = this.currentData();
  35. if (newTitle) {
  36. list.rename(newTitle.trim());
  37. }
  38. },
  39. isWatching() {
  40. const list = this.currentData();
  41. return list.findWatcher(Meteor.userId());
  42. },
  43. limitToShowCardsCount() {
  44. const currentUser = Meteor.user();
  45. if (currentUser) {
  46. return Meteor.user().getLimitToShowCardsCount();
  47. } else {
  48. return false;
  49. }
  50. },
  51. cardsCount() {
  52. const list = Template.currentData();
  53. let swimlaneId = '';
  54. if (Utils.boardView() === 'board-view-swimlanes')
  55. swimlaneId = this.parentComponent()
  56. .parentComponent()
  57. .data()._id;
  58. return list.cards(swimlaneId).count();
  59. },
  60. reachedWipLimit() {
  61. const list = Template.currentData();
  62. return (
  63. list.getWipLimit('enabled') &&
  64. list.getWipLimit('value') <= list.cards().count()
  65. );
  66. },
  67. showCardsCountForList(count) {
  68. const limit = this.limitToShowCardsCount();
  69. return limit > 0 && count > limit;
  70. },
  71. events() {
  72. return [
  73. {
  74. 'click .js-list-star'(event) {
  75. event.preventDefault();
  76. this.starred(!this.starred());
  77. },
  78. 'click .js-open-list-menu': Popup.open('listAction'),
  79. 'click .js-add-card'(event) {
  80. const listDom = $(event.target).parents(
  81. `#js-list-${this.currentData()._id}`,
  82. )[0];
  83. const listComponent = BlazeComponent.getComponentForElement(listDom);
  84. listComponent.openForm({
  85. position: 'top',
  86. });
  87. },
  88. 'click .js-unselect-list'() {
  89. Session.set('currentList', null);
  90. },
  91. submit: this.editTitle,
  92. },
  93. ];
  94. },
  95. }).register('listHeader');
  96. Template.listHeader.helpers({
  97. showDesktopDragHandles() {
  98. currentUser = Meteor.user();
  99. if (currentUser) {
  100. return (currentUser.profile || {}).showDesktopDragHandles;
  101. } else if (window.localStorage.getItem('showDesktopDragHandles')) {
  102. return true;
  103. } else {
  104. return false;
  105. }
  106. },
  107. });
  108. Template.listActionPopup.helpers({
  109. isWipLimitEnabled() {
  110. return Template.currentData().getWipLimit('enabled');
  111. },
  112. isWatching() {
  113. return this.findWatcher(Meteor.userId());
  114. },
  115. });
  116. Template.listActionPopup.events({
  117. 'click .js-list-subscribe'() {},
  118. 'click .js-set-color-list': Popup.open('setListColor'),
  119. 'click .js-select-cards'() {
  120. const cardIds = this.allCards().map(card => card._id);
  121. MultiSelection.add(cardIds);
  122. Popup.close();
  123. },
  124. 'click .js-toggle-watch-list'() {
  125. const currentList = this;
  126. const level = currentList.findWatcher(Meteor.userId()) ? null : 'watching';
  127. Meteor.call('watch', 'list', currentList._id, level, (err, ret) => {
  128. if (!err && ret) Popup.close();
  129. });
  130. },
  131. 'click .js-close-list'(event) {
  132. event.preventDefault();
  133. this.archive();
  134. Popup.close();
  135. },
  136. 'click .js-set-wip-limit': Popup.open('setWipLimit'),
  137. 'click .js-more': Popup.open('listMore'),
  138. });
  139. BlazeComponent.extendComponent({
  140. applyWipLimit() {
  141. const list = Template.currentData();
  142. const limit = parseInt(
  143. Template.instance()
  144. .$('.wip-limit-value')
  145. .val(),
  146. 10,
  147. );
  148. if (limit < list.cards().count() && !list.getWipLimit('soft')) {
  149. Template.instance()
  150. .$('.wip-limit-error')
  151. .click();
  152. } else {
  153. Meteor.call('applyWipLimit', list._id, limit);
  154. Popup.back();
  155. }
  156. },
  157. enableSoftLimit() {
  158. const list = Template.currentData();
  159. if (
  160. list.getWipLimit('soft') &&
  161. list.getWipLimit('value') < list.cards().count()
  162. ) {
  163. list.setWipLimit(list.cards().count());
  164. }
  165. Meteor.call('enableSoftLimit', Template.currentData()._id);
  166. },
  167. enableWipLimit() {
  168. const list = Template.currentData();
  169. // Prevent user from using previously stored wipLimit.value if it is less than the current number of cards in the list
  170. if (
  171. !list.getWipLimit('enabled') &&
  172. list.getWipLimit('value') < list.cards().count()
  173. ) {
  174. list.setWipLimit(list.cards().count());
  175. }
  176. Meteor.call('enableWipLimit', list._id);
  177. },
  178. isWipLimitSoft() {
  179. return Template.currentData().getWipLimit('soft');
  180. },
  181. isWipLimitEnabled() {
  182. return Template.currentData().getWipLimit('enabled');
  183. },
  184. wipLimitValue() {
  185. return Template.currentData().getWipLimit('value');
  186. },
  187. events() {
  188. return [
  189. {
  190. 'click .js-enable-wip-limit': this.enableWipLimit,
  191. 'click .wip-limit-apply': this.applyWipLimit,
  192. 'click .wip-limit-error': Popup.open('wipLimitError'),
  193. 'click .materialCheckBox': this.enableSoftLimit,
  194. },
  195. ];
  196. },
  197. }).register('setWipLimitPopup');
  198. Template.listMorePopup.events({
  199. 'click .js-delete': Popup.afterConfirm('listDelete', function() {
  200. Popup.close();
  201. // TODO how can we avoid the fetch call?
  202. const allCards = this.allCards().fetch();
  203. const allCardIds = _.pluck(allCards, '_id');
  204. // it's okay if the linked cards are on the same list
  205. if (
  206. Cards.find({
  207. $and: [
  208. { listId: { $ne: this._id } },
  209. { linkedId: { $in: allCardIds } },
  210. ],
  211. }).count() === 0
  212. ) {
  213. allCardIds.map(_id => Cards.remove(_id));
  214. Lists.remove(this._id);
  215. } else {
  216. // TODO: Figure out more informative message.
  217. // Popup with a hint that the list cannot be deleted as there are
  218. // linked cards. We can adapt the query above so we can list the linked
  219. // cards.
  220. // Related:
  221. // client/components/cards/cardDetails.js about line 969
  222. // https://github.com/wekan/wekan/issues/2785
  223. const message = `${TAPi18n.__(
  224. 'delete-linked-cards-before-this-list',
  225. )} linkedId: ${
  226. this._id
  227. } at client/components/lists/listHeader.js and https://github.com/wekan/wekan/issues/2785`;
  228. alert(message);
  229. }
  230. Utils.goBoardId(this.boardId);
  231. }),
  232. });
  233. BlazeComponent.extendComponent({
  234. onCreated() {
  235. this.currentList = this.currentData();
  236. this.currentColor = new ReactiveVar(this.currentList.color);
  237. },
  238. colors() {
  239. return listsColors.map(color => ({ color, name: '' }));
  240. },
  241. isSelected(color) {
  242. return this.currentColor.get() === color;
  243. },
  244. events() {
  245. return [
  246. {
  247. 'click .js-palette-color'() {
  248. this.currentColor.set(this.currentData().color);
  249. },
  250. 'click .js-submit'() {
  251. this.currentList.setColor(this.currentColor.get());
  252. Popup.close();
  253. },
  254. 'click .js-remove-color'() {
  255. this.currentList.setColor(null);
  256. Popup.close();
  257. },
  258. },
  259. ];
  260. },
  261. }).register('setListColorPopup');