listHeader.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. });
  134. Template.listActionPopup.helpers({
  135. isBoardAdmin() {
  136. return ReactiveCache.getCurrentUser().isBoardAdmin();
  137. },
  138. isWipLimitEnabled() {
  139. return Template.currentData().getWipLimit('enabled');
  140. },
  141. isWatching() {
  142. return this.findWatcher(Meteor.userId());
  143. }
  144. });
  145. Template.listActionPopup.events({
  146. 'click .js-list-subscribe'() {},
  147. 'click .js-add-card.list-header-plus-bottom'(event) {
  148. const listDom = $(`#js-list-${this._id}`)[0];
  149. const listComponent = BlazeComponent.getComponentForElement(listDom);
  150. listComponent.openForm({
  151. position: 'bottom',
  152. });
  153. Popup.back();
  154. },
  155. 'click .js-set-list-width': Popup.open('setListWidth'),
  156. 'click .js-set-color-list': Popup.open('setListColor'),
  157. 'click .js-select-cards'() {
  158. const cardIds = this.allCards().map(card => card._id);
  159. MultiSelection.add(cardIds);
  160. Popup.back();
  161. },
  162. 'click .js-toggle-watch-list'() {
  163. const currentList = this;
  164. const level = currentList.findWatcher(Meteor.userId()) ? null : 'watching';
  165. Meteor.call('watch', 'list', currentList._id, level, (err, ret) => {
  166. if (!err && ret) Popup.back();
  167. });
  168. },
  169. 'click .js-close-list'(event) {
  170. event.preventDefault();
  171. this.archive();
  172. Popup.back();
  173. },
  174. 'click .js-set-wip-limit': Popup.open('setWipLimit'),
  175. 'click .js-more': Popup.open('listMore'),
  176. });
  177. BlazeComponent.extendComponent({
  178. applyWipLimit() {
  179. const list = Template.currentData();
  180. const limit = parseInt(
  181. Template.instance()
  182. .$('.wip-limit-value')
  183. .val(),
  184. 10,
  185. );
  186. if (limit < list.cards().length && !list.getWipLimit('soft')) {
  187. Template.instance()
  188. .$('.wip-limit-error')
  189. .click();
  190. } else {
  191. Meteor.call('applyWipLimit', list._id, limit);
  192. Popup.back();
  193. }
  194. },
  195. enableSoftLimit() {
  196. const list = Template.currentData();
  197. if (
  198. list.getWipLimit('soft') &&
  199. list.getWipLimit('value') < list.cards().length
  200. ) {
  201. list.setWipLimit(list.cards().length);
  202. }
  203. Meteor.call('enableSoftLimit', Template.currentData()._id);
  204. },
  205. enableWipLimit() {
  206. const list = Template.currentData();
  207. // Prevent user from using previously stored wipLimit.value if it is less than the current number of cards in the list
  208. if (
  209. !list.getWipLimit('enabled') &&
  210. list.getWipLimit('value') < list.cards().length
  211. ) {
  212. list.setWipLimit(list.cards().length);
  213. }
  214. Meteor.call('enableWipLimit', list._id);
  215. },
  216. isWipLimitSoft() {
  217. return Template.currentData().getWipLimit('soft');
  218. },
  219. isWipLimitEnabled() {
  220. return Template.currentData().getWipLimit('enabled');
  221. },
  222. wipLimitValue() {
  223. return Template.currentData().getWipLimit('value');
  224. },
  225. events() {
  226. return [
  227. {
  228. 'click .js-enable-wip-limit': this.enableWipLimit,
  229. 'click .wip-limit-apply': this.applyWipLimit,
  230. 'click .wip-limit-error': Popup.open('wipLimitError'),
  231. 'click .materialCheckBox': this.enableSoftLimit,
  232. },
  233. ];
  234. },
  235. }).register('setWipLimitPopup');
  236. Template.listMorePopup.events({
  237. 'click .js-delete': Popup.afterConfirm('listDelete', function() {
  238. Popup.back();
  239. const allCards = this.allCards();
  240. const allCardIds = _.pluck(allCards, '_id');
  241. // it's okay if the linked cards are on the same list
  242. if (
  243. ReactiveCache.getCards({
  244. $and: [
  245. { listId: { $ne: this._id } },
  246. { linkedId: { $in: allCardIds } },
  247. ],
  248. }).length === 0
  249. ) {
  250. allCardIds.map(_id => Cards.remove(_id));
  251. Lists.remove(this._id);
  252. } else {
  253. // TODO: Figure out more informative message.
  254. // Popup with a hint that the list cannot be deleted as there are
  255. // linked cards. We can adapt the query above so we can list the linked
  256. // cards.
  257. // Related:
  258. // client/components/cards/cardDetails.js about line 969
  259. // https://github.com/wekan/wekan/issues/2785
  260. const message = `${TAPi18n.__(
  261. 'delete-linked-cards-before-this-list',
  262. )} linkedId: ${
  263. this._id
  264. } at client/components/lists/listHeader.js and https://github.com/wekan/wekan/issues/2785`;
  265. alert(message);
  266. }
  267. Utils.goBoardId(this.boardId);
  268. }),
  269. });
  270. Template.listHeader.helpers({
  271. isBoardAdmin() {
  272. return ReactiveCache.getCurrentUser().isBoardAdmin();
  273. },
  274. });
  275. BlazeComponent.extendComponent({
  276. onCreated() {
  277. this.currentList = this.currentData();
  278. this.currentColor = new ReactiveVar(this.currentList.color);
  279. },
  280. colors() {
  281. return listsColors.map(color => ({ color, name: '' }));
  282. },
  283. isSelected(color) {
  284. if (this.currentColor.get() === null) {
  285. return color === 'white';
  286. } else {
  287. return this.currentColor.get() === color;
  288. }
  289. },
  290. events() {
  291. return [
  292. {
  293. 'click .js-palette-color'() {
  294. this.currentColor.set(this.currentData().color);
  295. },
  296. 'click .js-submit'() {
  297. this.currentList.setColor(this.currentColor.get());
  298. Popup.close();
  299. },
  300. 'click .js-remove-color'() {
  301. this.currentList.setColor(null);
  302. Popup.close();
  303. },
  304. },
  305. ];
  306. },
  307. }).register('setListColorPopup');
  308. BlazeComponent.extendComponent({
  309. applyListWidth() {
  310. const list = Template.currentData();
  311. const board = list.boardId;
  312. const width = parseInt(
  313. Template.instance()
  314. .$('.list-width-value')
  315. .val(),
  316. 10,
  317. );
  318. const constraint = parseInt(
  319. Template.instance()
  320. .$('.list-constraint-value')
  321. .val(),
  322. 10,
  323. );
  324. // FIXME(mark-i-m): where do we put constants?
  325. if (width < 100 || !width || constraint < 100 || !constraint) {
  326. Template.instance()
  327. .$('.list-width-error')
  328. .click();
  329. } else {
  330. Meteor.call('applyListWidth', board, list._id, width, constraint);
  331. Popup.back();
  332. }
  333. },
  334. listWidthValue() {
  335. const list = Template.currentData();
  336. const board = list.boardId;
  337. return ReactiveCache.getCurrentUser().getListWidth(board, list._id);
  338. },
  339. listConstraintValue() {
  340. const list = Template.currentData();
  341. const board = list.boardId;
  342. return ReactiveCache.getCurrentUser().getListConstraint(board, list._id);
  343. },
  344. isAutoWidth() {
  345. const boardId = Utils.getCurrentBoardId();
  346. const user = ReactiveCache.getCurrentUser();
  347. return user && user.isAutoWidth(boardId);
  348. },
  349. events() {
  350. return [
  351. {
  352. 'click .js-auto-width-board'() {
  353. dragscroll.reset();
  354. ReactiveCache.getCurrentUser().toggleAutoWidth(Utils.getCurrentBoardId());
  355. },
  356. 'click .list-width-apply': this.applyListWidth,
  357. 'click .list-width-error': Popup.open('listWidthError'),
  358. },
  359. ];
  360. },
  361. }).register('setListWidthPopup');