listHeader.js 8.0 KB

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