listHeader.js 9.1 KB

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