listHeader.js 7.7 KB

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