listHeader.js 7.4 KB

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