listHeader.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. exceededWipLimit() {
  68. const list = Template.currentData();
  69. return (
  70. list.getWipLimit('enabled') &&
  71. list.getWipLimit('value') < list.cards().count()
  72. );
  73. },
  74. showCardsCountForList(count) {
  75. const limit = this.limitToShowCardsCount();
  76. return limit > 0 && count > limit;
  77. },
  78. events() {
  79. return [
  80. {
  81. 'click .js-list-star'(event) {
  82. event.preventDefault();
  83. this.starred(!this.starred());
  84. },
  85. 'click .js-open-list-menu': Popup.open('listAction'),
  86. 'click .js-add-card'(event) {
  87. const listDom = $(event.target).parents(
  88. `#js-list-${this.currentData()._id}`,
  89. )[0];
  90. const listComponent = BlazeComponent.getComponentForElement(listDom);
  91. listComponent.openForm({
  92. position: 'top',
  93. });
  94. },
  95. 'click .js-unselect-list'() {
  96. Session.set('currentList', null);
  97. },
  98. submit: this.editTitle,
  99. },
  100. ];
  101. },
  102. }).register('listHeader');
  103. Template.listHeader.helpers({
  104. isBoardAdmin() {
  105. return Meteor.user().isBoardAdmin();
  106. },
  107. showDesktopDragHandles() {
  108. currentUser = Meteor.user();
  109. if (currentUser) {
  110. return (currentUser.profile || {}).showDesktopDragHandles;
  111. } else if (window.localStorage.getItem('showDesktopDragHandles')) {
  112. return true;
  113. } else {
  114. return false;
  115. }
  116. },
  117. });
  118. Template.listActionPopup.helpers({
  119. isBoardAdmin() {
  120. return Meteor.user().isBoardAdmin();
  121. },
  122. isWipLimitEnabled() {
  123. return Template.currentData().getWipLimit('enabled');
  124. },
  125. isWatching() {
  126. return this.findWatcher(Meteor.userId());
  127. },
  128. });
  129. Template.listActionPopup.events({
  130. 'click .js-list-subscribe'() {},
  131. 'click .js-set-color-list': Popup.open('setListColor'),
  132. 'click .js-select-cards'() {
  133. const cardIds = this.allCards().map(card => card._id);
  134. MultiSelection.add(cardIds);
  135. Popup.close();
  136. },
  137. 'click .js-toggle-watch-list'() {
  138. const currentList = this;
  139. const level = currentList.findWatcher(Meteor.userId()) ? null : 'watching';
  140. Meteor.call('watch', 'list', currentList._id, level, (err, ret) => {
  141. if (!err && ret) Popup.close();
  142. });
  143. },
  144. 'click .js-close-list'(event) {
  145. event.preventDefault();
  146. this.archive();
  147. Popup.close();
  148. },
  149. 'click .js-set-wip-limit': Popup.open('setWipLimit'),
  150. 'click .js-more': Popup.open('listMore'),
  151. });
  152. BlazeComponent.extendComponent({
  153. applyWipLimit() {
  154. const list = Template.currentData();
  155. const limit = parseInt(
  156. Template.instance()
  157. .$('.wip-limit-value')
  158. .val(),
  159. 10,
  160. );
  161. if (limit < list.cards().count() && !list.getWipLimit('soft')) {
  162. Template.instance()
  163. .$('.wip-limit-error')
  164. .click();
  165. } else {
  166. Meteor.call('applyWipLimit', list._id, limit);
  167. Popup.back();
  168. }
  169. },
  170. enableSoftLimit() {
  171. const list = Template.currentData();
  172. if (
  173. list.getWipLimit('soft') &&
  174. list.getWipLimit('value') < list.cards().count()
  175. ) {
  176. list.setWipLimit(list.cards().count());
  177. }
  178. Meteor.call('enableSoftLimit', Template.currentData()._id);
  179. },
  180. enableWipLimit() {
  181. const list = Template.currentData();
  182. // Prevent user from using previously stored wipLimit.value if it is less than the current number of cards in the list
  183. if (
  184. !list.getWipLimit('enabled') &&
  185. list.getWipLimit('value') < list.cards().count()
  186. ) {
  187. list.setWipLimit(list.cards().count());
  188. }
  189. Meteor.call('enableWipLimit', list._id);
  190. },
  191. isWipLimitSoft() {
  192. return Template.currentData().getWipLimit('soft');
  193. },
  194. isWipLimitEnabled() {
  195. return Template.currentData().getWipLimit('enabled');
  196. },
  197. wipLimitValue() {
  198. return Template.currentData().getWipLimit('value');
  199. },
  200. events() {
  201. return [
  202. {
  203. 'click .js-enable-wip-limit': this.enableWipLimit,
  204. 'click .wip-limit-apply': this.applyWipLimit,
  205. 'click .wip-limit-error': Popup.open('wipLimitError'),
  206. 'click .materialCheckBox': this.enableSoftLimit,
  207. },
  208. ];
  209. },
  210. }).register('setWipLimitPopup');
  211. Template.listMorePopup.events({
  212. 'click .js-delete': Popup.afterConfirm('listDelete', function() {
  213. Popup.close();
  214. // TODO how can we avoid the fetch call?
  215. const allCards = this.allCards().fetch();
  216. const allCardIds = _.pluck(allCards, '_id');
  217. // it's okay if the linked cards are on the same list
  218. if (
  219. Cards.find({
  220. $and: [
  221. { listId: { $ne: this._id } },
  222. { linkedId: { $in: allCardIds } },
  223. ],
  224. }).count() === 0
  225. ) {
  226. allCardIds.map(_id => Cards.remove(_id));
  227. Lists.remove(this._id);
  228. } else {
  229. // TODO: Figure out more informative message.
  230. // Popup with a hint that the list cannot be deleted as there are
  231. // linked cards. We can adapt the query above so we can list the linked
  232. // cards.
  233. // Related:
  234. // client/components/cards/cardDetails.js about line 969
  235. // https://github.com/wekan/wekan/issues/2785
  236. const message = `${TAPi18n.__(
  237. 'delete-linked-cards-before-this-list',
  238. )} linkedId: ${
  239. this._id
  240. } at client/components/lists/listHeader.js and https://github.com/wekan/wekan/issues/2785`;
  241. alert(message);
  242. }
  243. Utils.goBoardId(this.boardId);
  244. }),
  245. });
  246. Template.listHeader.helpers({
  247. isBoardAdmin() {
  248. return Meteor.user().isBoardAdmin();
  249. },
  250. });
  251. BlazeComponent.extendComponent({
  252. onCreated() {
  253. this.currentList = this.currentData();
  254. this.currentColor = new ReactiveVar(this.currentList.color);
  255. },
  256. colors() {
  257. return listsColors.map(color => ({ color, name: '' }));
  258. },
  259. isSelected(color) {
  260. if (this.currentColor.get() === null) {
  261. return color === 'white';
  262. } else {
  263. return this.currentColor.get() === color;
  264. }
  265. },
  266. events() {
  267. return [
  268. {
  269. 'click .js-palette-color'() {
  270. this.currentColor.set(this.currentData().color);
  271. },
  272. 'click .js-submit'() {
  273. this.currentList.setColor(this.currentColor.get());
  274. Popup.close();
  275. },
  276. 'click .js-remove-color'() {
  277. this.currentList.setColor(null);
  278. Popup.close();
  279. },
  280. },
  281. ];
  282. },
  283. }).register('setListColorPopup');