listHeader.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. import { TAPi18n } from '/imports/i18n';
  3. import dragscroll from '@wekanteam/dragscroll';
  4. let listsColors;
  5. Meteor.startup(() => {
  6. listsColors = Lists.simpleSchema()._schema.color.allowedValues;
  7. });
  8. BlazeComponent.extendComponent({
  9. canSeeAddCard() {
  10. const list = Template.currentData();
  11. return (
  12. (!list.getWipLimit('enabled') ||
  13. list.getWipLimit('soft') ||
  14. !this.reachedWipLimit()) &&
  15. !ReactiveCache.getCurrentUser().isWorker()
  16. );
  17. },
  18. isBoardAdmin() {
  19. return ReactiveCache.getCurrentUser().isBoardAdmin();
  20. },
  21. starred(check = undefined) {
  22. const list = Template.currentData();
  23. const status = list.isStarred();
  24. if (check === undefined) {
  25. // just check
  26. return status;
  27. } else {
  28. list.star(!status);
  29. return !status;
  30. }
  31. },
  32. collapsed(check = undefined) {
  33. const list = Template.currentData();
  34. const status = Utils.getListCollapseState(list);
  35. if (check === undefined) {
  36. // just check
  37. return status;
  38. } else {
  39. const next = typeof check === 'boolean' ? check : !status;
  40. Utils.setListCollapseState(list, next);
  41. return next;
  42. }
  43. },
  44. editTitle(event) {
  45. event.preventDefault();
  46. const newTitle = this.childComponents('inlinedForm')[0]
  47. .getValue()
  48. .trim();
  49. const list = this.currentData();
  50. if (newTitle) {
  51. list.rename(newTitle.trim());
  52. }
  53. },
  54. isWatching() {
  55. const list = this.currentData();
  56. return list.findWatcher(Meteor.userId());
  57. },
  58. limitToShowCardsCount() {
  59. const currentUser = ReactiveCache.getCurrentUser();
  60. if (currentUser) {
  61. return currentUser.getLimitToShowCardsCount();
  62. } else {
  63. return false;
  64. }
  65. },
  66. cardsCount() {
  67. const list = Template.currentData();
  68. let swimlaneId = '';
  69. if (Utils.boardView() === 'board-view-swimlanes')
  70. swimlaneId = this.parentComponent()
  71. .parentComponent()
  72. .data()._id;
  73. const ret = list.cards(swimlaneId).length;
  74. return ret;
  75. },
  76. reachedWipLimit() {
  77. const list = Template.currentData();
  78. return (
  79. list.getWipLimit('enabled') &&
  80. list.getWipLimit('value') <= list.cards().length
  81. );
  82. },
  83. exceededWipLimit() {
  84. const list = Template.currentData();
  85. return (
  86. list.getWipLimit('enabled') &&
  87. list.getWipLimit('value') < list.cards().length
  88. );
  89. },
  90. showCardsCountForList(count) {
  91. const limit = this.limitToShowCardsCount();
  92. return limit >= 0 && count >= limit;
  93. },
  94. cardsCountForListIsOne(count) {
  95. if (count === 1) {
  96. return TAPi18n.__('cards-count-one');
  97. } else {
  98. return TAPi18n.__('cards-count');
  99. }
  100. },
  101. events() {
  102. return [
  103. {
  104. 'click .js-list-star'(event) {
  105. event.preventDefault();
  106. this.starred(!this.starred());
  107. },
  108. 'click .js-collapse'(event) {
  109. event.preventDefault();
  110. this.collapsed(!this.collapsed());
  111. },
  112. 'click .js-open-list-menu': Popup.open('listAction'),
  113. 'click .js-add-card.list-header-plus-top'(event) {
  114. const listDom = $(event.target).parents(
  115. `#js-list-${this.currentData()._id}`,
  116. )[0];
  117. const listComponent = BlazeComponent.getComponentForElement(listDom);
  118. listComponent.openForm({
  119. position: 'top',
  120. });
  121. },
  122. 'click .js-unselect-list'() {
  123. Session.set('currentList', null);
  124. },
  125. submit: this.editTitle,
  126. },
  127. ];
  128. },
  129. }).register('listHeader');
  130. Template.listHeader.helpers({
  131. isBoardAdmin() {
  132. return ReactiveCache.getCurrentUser().isBoardAdmin();
  133. },
  134. numberFieldsSum() {
  135. const list = Template.currentData();
  136. if (!list) return 0;
  137. const boardId = Session.get('currentBoard');
  138. const fields = ReactiveCache.getCustomFields({
  139. boardIds: { $in: [boardId] },
  140. showSumAtTopOfList: true,
  141. type: 'number',
  142. });
  143. if (!fields || !fields.length) return 0;
  144. const cards = ReactiveCache.getCards({ listId: list._id, archived: false });
  145. let total = 0;
  146. if (cards && cards.length) {
  147. cards.forEach(card => {
  148. const cfs = (card.customFields || []);
  149. fields.forEach(field => {
  150. const cf = cfs.find(f => f && f._id === field._id);
  151. if (!cf || cf.value === null || cf.value === undefined) return;
  152. let v = cf.value;
  153. if (typeof v === 'string') {
  154. const parsed = parseFloat(v.replace(',', '.'));
  155. if (isNaN(parsed)) return;
  156. v = parsed;
  157. }
  158. if (typeof v === 'number' && isFinite(v)) {
  159. total += v;
  160. }
  161. });
  162. });
  163. }
  164. return total;
  165. },
  166. hasNumberFieldsSum() {
  167. const boardId = Session.get('currentBoard');
  168. const fields = ReactiveCache.getCustomFields({
  169. boardIds: { $in: [boardId] },
  170. showSumAtTopOfList: true,
  171. type: 'number',
  172. });
  173. return !!(fields && fields.length);
  174. },
  175. });
  176. Template.listActionPopup.helpers({
  177. isBoardAdmin() {
  178. return ReactiveCache.getCurrentUser().isBoardAdmin();
  179. },
  180. isWipLimitEnabled() {
  181. return Template.currentData().getWipLimit('enabled');
  182. },
  183. isWatching() {
  184. return this.findWatcher(Meteor.userId());
  185. }
  186. });
  187. Template.listActionPopup.events({
  188. 'click .js-list-subscribe'() {},
  189. 'click .js-add-card.list-header-plus-bottom'(event) {
  190. const listDom = $(`#js-list-${this._id}`)[0];
  191. const listComponent = BlazeComponent.getComponentForElement(listDom);
  192. listComponent.openForm({
  193. position: 'bottom',
  194. });
  195. Popup.back();
  196. },
  197. 'click .js-set-list-width': Popup.open('setListWidth'),
  198. 'click .js-set-color-list': Popup.open('setListColor'),
  199. 'click .js-select-cards'() {
  200. const cardIds = this.allCards().map(card => card._id);
  201. MultiSelection.add(cardIds);
  202. Popup.back();
  203. },
  204. 'click .js-toggle-watch-list'() {
  205. const currentList = this;
  206. const level = currentList.findWatcher(Meteor.userId()) ? null : 'watching';
  207. Meteor.call('watch', 'list', currentList._id, level, (err, ret) => {
  208. if (!err && ret) Popup.back();
  209. });
  210. },
  211. 'click .js-close-list'(event) {
  212. event.preventDefault();
  213. this.archive();
  214. Popup.back();
  215. },
  216. 'click .js-set-wip-limit': Popup.open('setWipLimit'),
  217. 'click .js-more': Popup.open('listMore'),
  218. });
  219. BlazeComponent.extendComponent({
  220. applyWipLimit() {
  221. const list = Template.currentData();
  222. const limit = parseInt(
  223. Template.instance()
  224. .$('.wip-limit-value')
  225. .val(),
  226. 10,
  227. );
  228. if (limit < list.cards().length && !list.getWipLimit('soft')) {
  229. Template.instance()
  230. .$('.wip-limit-error')
  231. .click();
  232. } else {
  233. Meteor.call('applyWipLimit', list._id, limit);
  234. Popup.back();
  235. }
  236. },
  237. enableSoftLimit() {
  238. const list = Template.currentData();
  239. if (
  240. list.getWipLimit('soft') &&
  241. list.getWipLimit('value') < list.cards().length
  242. ) {
  243. list.setWipLimit(list.cards().length);
  244. }
  245. Meteor.call('enableSoftLimit', Template.currentData()._id);
  246. },
  247. enableWipLimit() {
  248. const list = Template.currentData();
  249. // Prevent user from using previously stored wipLimit.value if it is less than the current number of cards in the list
  250. if (
  251. !list.getWipLimit('enabled') &&
  252. list.getWipLimit('value') < list.cards().length
  253. ) {
  254. list.setWipLimit(list.cards().length);
  255. }
  256. Meteor.call('enableWipLimit', list._id);
  257. },
  258. isWipLimitSoft() {
  259. return Template.currentData().getWipLimit('soft');
  260. },
  261. isWipLimitEnabled() {
  262. return Template.currentData().getWipLimit('enabled');
  263. },
  264. wipLimitValue() {
  265. return Template.currentData().getWipLimit('value');
  266. },
  267. events() {
  268. return [
  269. {
  270. 'click .js-enable-wip-limit': this.enableWipLimit,
  271. 'click .wip-limit-apply': this.applyWipLimit,
  272. 'click .wip-limit-error': Popup.open('wipLimitError'),
  273. 'click .materialCheckBox': this.enableSoftLimit,
  274. },
  275. ];
  276. },
  277. }).register('setWipLimitPopup');
  278. Template.listMorePopup.events({
  279. 'click .js-delete': Popup.afterConfirm('listDelete', function() {
  280. Popup.back();
  281. const allCards = this.allCards();
  282. const allCardIds = _.pluck(allCards, '_id');
  283. // it's okay if the linked cards are on the same list
  284. if (
  285. ReactiveCache.getCards({
  286. $and: [
  287. { listId: { $ne: this._id } },
  288. { linkedId: { $in: allCardIds } },
  289. ],
  290. }).length === 0
  291. ) {
  292. allCardIds.map(_id => Cards.remove(_id));
  293. Lists.remove(this._id);
  294. } else {
  295. // TODO: Figure out more informative message.
  296. // Popup with a hint that the list cannot be deleted as there are
  297. // linked cards. We can adapt the query above so we can list the linked
  298. // cards.
  299. // Related:
  300. // client/components/cards/cardDetails.js about line 969
  301. // https://github.com/wekan/wekan/issues/2785
  302. const message = `${TAPi18n.__(
  303. 'delete-linked-cards-before-this-list',
  304. )} linkedId: ${
  305. this._id
  306. } at client/components/lists/listHeader.js and https://github.com/wekan/wekan/issues/2785`;
  307. alert(message);
  308. }
  309. Utils.goBoardId(this.boardId);
  310. }),
  311. });
  312. Template.listHeader.helpers({
  313. isBoardAdmin() {
  314. return ReactiveCache.getCurrentUser().isBoardAdmin();
  315. },
  316. });
  317. BlazeComponent.extendComponent({
  318. onCreated() {
  319. this.currentList = this.currentData();
  320. this.currentColor = new ReactiveVar(this.currentList.color);
  321. },
  322. colors() {
  323. return listsColors.map(color => ({ color, name: '' }));
  324. },
  325. isSelected(color) {
  326. if (this.currentColor.get() === null) {
  327. return color === 'white';
  328. } else {
  329. return this.currentColor.get() === color;
  330. }
  331. },
  332. events() {
  333. return [
  334. {
  335. 'click .js-palette-color'() {
  336. this.currentColor.set(this.currentData().color);
  337. },
  338. 'click .js-submit'() {
  339. this.currentList.setColor(this.currentColor.get());
  340. Popup.close();
  341. },
  342. 'click .js-remove-color'() {
  343. this.currentList.setColor(null);
  344. Popup.close();
  345. },
  346. },
  347. ];
  348. },
  349. }).register('setListColorPopup');
  350. BlazeComponent.extendComponent({
  351. applyListWidth() {
  352. const list = Template.currentData();
  353. const board = list.boardId;
  354. const width = parseInt(
  355. Template.instance()
  356. .$('.list-width-value')
  357. .val(),
  358. 10,
  359. );
  360. const constraint = parseInt(
  361. Template.instance()
  362. .$('.list-constraint-value')
  363. .val(),
  364. 10,
  365. );
  366. // FIXME(mark-i-m): where do we put constants?
  367. if (width < 100 || !width || constraint < 100 || !constraint) {
  368. Template.instance()
  369. .$('.list-width-error')
  370. .click();
  371. } else {
  372. Meteor.call('applyListWidth', board, list._id, width, constraint);
  373. Popup.back();
  374. }
  375. },
  376. listWidthValue() {
  377. const list = Template.currentData();
  378. const board = list.boardId;
  379. return ReactiveCache.getCurrentUser().getListWidth(board, list._id);
  380. },
  381. listConstraintValue() {
  382. const list = Template.currentData();
  383. const board = list.boardId;
  384. return ReactiveCache.getCurrentUser().getListConstraint(board, list._id);
  385. },
  386. isAutoWidth() {
  387. const boardId = Utils.getCurrentBoardId();
  388. const user = ReactiveCache.getCurrentUser();
  389. return user && user.isAutoWidth(boardId);
  390. },
  391. events() {
  392. return [
  393. {
  394. 'click .js-auto-width-board'() {
  395. dragscroll.reset();
  396. ReactiveCache.getCurrentUser().toggleAutoWidth(Utils.getCurrentBoardId());
  397. },
  398. 'click .list-width-apply': this.applyListWidth,
  399. 'click .list-width-error': Popup.open('listWidthError'),
  400. },
  401. ];
  402. },
  403. }).register('setListWidthPopup');