listHeader.js 8.2 KB

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