listHeader.js 7.7 KB

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