listHeader.js 11 KB

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