checklists.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. import Cards from '/models/cards';
  2. import Boards from '/models/boards';
  3. const subManager = new SubsManager();
  4. const { calculateIndexData, capitalize } = Utils;
  5. function initSorting(items) {
  6. items.sortable({
  7. tolerance: 'pointer',
  8. helper: 'clone',
  9. items: '.js-checklist-item:not(.placeholder)',
  10. connectWith: '.js-checklist-items',
  11. appendTo: 'parent',
  12. distance: 7,
  13. placeholder: 'checklist-item placeholder',
  14. scroll: true,
  15. start(evt, ui) {
  16. ui.placeholder.height(ui.helper.height());
  17. EscapeActions.clickExecute(evt.target, 'inlinedForm');
  18. },
  19. stop(evt, ui) {
  20. const parent = ui.item.parents('.js-checklist-items');
  21. const checklistId = Blaze.getData(parent.get(0)).checklist._id;
  22. let prevItem = ui.item.prev('.js-checklist-item').get(0);
  23. if (prevItem) {
  24. prevItem = Blaze.getData(prevItem).item;
  25. }
  26. let nextItem = ui.item.next('.js-checklist-item').get(0);
  27. if (nextItem) {
  28. nextItem = Blaze.getData(nextItem).item;
  29. }
  30. const nItems = 1;
  31. const sortIndex = calculateIndexData(prevItem, nextItem, nItems);
  32. const checklistDomElement = ui.item.get(0);
  33. const checklistData = Blaze.getData(checklistDomElement);
  34. const checklistItem = checklistData.item;
  35. items.sortable('cancel');
  36. checklistItem.move(checklistId, sortIndex.base);
  37. },
  38. });
  39. }
  40. BlazeComponent.extendComponent({
  41. onRendered() {
  42. const self = this;
  43. self.itemsDom = this.$('.js-checklist-items');
  44. initSorting(self.itemsDom);
  45. self.itemsDom.mousedown(function(evt) {
  46. evt.stopPropagation();
  47. });
  48. function userIsMember() {
  49. return Meteor.user() && Meteor.user().isBoardMember();
  50. }
  51. // Disable sorting if the current user is not a board member
  52. self.autorun(() => {
  53. const $itemsDom = $(self.itemsDom);
  54. if ($itemsDom.data('uiSortable') || $itemsDom.data('sortable')) {
  55. $(self.itemsDom).sortable('option', 'disabled', !userIsMember());
  56. if (Utils.isMiniScreenOrShowDesktopDragHandles()) {
  57. $(self.itemsDom).sortable({
  58. handle: 'span.fa.checklistitem-handle',
  59. });
  60. }
  61. }
  62. });
  63. },
  64. canModifyCard() {
  65. return (
  66. Meteor.user() &&
  67. Meteor.user().isBoardMember() &&
  68. !Meteor.user().isCommentOnly() &&
  69. !Meteor.user().isWorker()
  70. );
  71. },
  72. }).register('checklistDetail');
  73. BlazeComponent.extendComponent({
  74. addChecklist(event) {
  75. event.preventDefault();
  76. const textarea = this.find('textarea.js-add-checklist-item');
  77. const title = textarea.value.trim();
  78. let cardId = this.currentData().cardId;
  79. const card = Cards.findOne(cardId);
  80. if (card.isLinked()) cardId = card.linkedId;
  81. if (title) {
  82. Checklists.insert({
  83. cardId,
  84. title,
  85. sort: card.checklists().count(),
  86. });
  87. this.closeAllInlinedForms();
  88. setTimeout(() => {
  89. this.$('.add-checklist-item')
  90. .last()
  91. .click();
  92. }, 100);
  93. }
  94. },
  95. addChecklistItem(event) {
  96. event.preventDefault();
  97. const textarea = this.find('textarea.js-add-checklist-item');
  98. const title = textarea.value.trim();
  99. const checklist = this.currentData().checklist;
  100. if (title) {
  101. ChecklistItems.insert({
  102. title,
  103. checklistId: checklist._id,
  104. cardId: checklist.cardId,
  105. sort: Utils.calculateIndexData(checklist.lastItem()).base,
  106. });
  107. }
  108. // We keep the form opened, empty it.
  109. textarea.value = '';
  110. textarea.focus();
  111. },
  112. canModifyCard() {
  113. return (
  114. Meteor.user() &&
  115. Meteor.user().isBoardMember() &&
  116. !Meteor.user().isCommentOnly() &&
  117. !Meteor.user().isWorker()
  118. );
  119. },
  120. deleteItem() {
  121. const checklist = this.currentData().checklist;
  122. const item = this.currentData().item;
  123. if (checklist && item && item._id) {
  124. ChecklistItems.remove(item._id);
  125. }
  126. },
  127. editChecklist(event) {
  128. event.preventDefault();
  129. const textarea = this.find('textarea.js-edit-checklist-item');
  130. const title = textarea.value.trim();
  131. const checklist = this.currentData().checklist;
  132. checklist.setTitle(title);
  133. },
  134. editChecklistItem(event) {
  135. event.preventDefault();
  136. const textarea = this.find('textarea.js-edit-checklist-item');
  137. const title = textarea.value.trim();
  138. const item = this.currentData().item;
  139. item.setTitle(title);
  140. },
  141. pressKey(event) {
  142. //If user press enter key inside a form, submit it
  143. //Unless the user is also holding down the 'shift' key
  144. if (event.keyCode === 13 && !event.shiftKey) {
  145. event.preventDefault();
  146. const $form = $(event.currentTarget).closest('form');
  147. $form.find('button[type=submit]').click();
  148. }
  149. },
  150. focusChecklistItem(event) {
  151. // If a new checklist is created, pre-fill the title and select it.
  152. const checklist = this.currentData().checklist;
  153. if (!checklist) {
  154. const textarea = event.target;
  155. textarea.value = capitalize(TAPi18n.__('r-checklist'));
  156. textarea.select();
  157. }
  158. },
  159. /** closes all inlined forms (checklist and checklist-item input fields) */
  160. closeAllInlinedForms() {
  161. this.$('.js-close-inlined-form').click();
  162. },
  163. events() {
  164. const events = {
  165. 'click #toggleHideCheckedItemsButton'() {
  166. Meteor.call('toggleHideCheckedItems');
  167. },
  168. };
  169. return [
  170. {
  171. ...events,
  172. 'click .js-open-checklist-details-menu': Popup.open('checklistActions'),
  173. 'submit .js-add-checklist': this.addChecklist,
  174. 'submit .js-edit-checklist-title': this.editChecklist,
  175. 'submit .js-add-checklist-item': this.addChecklistItem,
  176. 'submit .js-edit-checklist-item': this.editChecklistItem,
  177. 'click .js-convert-checklist-item-to-card': Popup.open('convertChecklistItemToCard'),
  178. 'click .js-delete-checklist-item': this.deleteItem,
  179. 'focus .js-add-checklist-item': this.focusChecklistItem,
  180. // add and delete checklist / checklist-item
  181. 'click .js-open-inlined-form': this.closeAllInlinedForms,
  182. keydown: this.pressKey,
  183. },
  184. ];
  185. },
  186. }).register('checklists');
  187. BlazeComponent.extendComponent({
  188. onCreated() {
  189. subManager.subscribe('board', Session.get('currentBoard'), false);
  190. this.selectedBoardId = new ReactiveVar(Session.get('currentBoard'));
  191. },
  192. boards() {
  193. return Boards.find(
  194. {
  195. archived: false,
  196. 'members.userId': Meteor.userId(),
  197. _id: { $ne: Meteor.user().getTemplatesBoardId() },
  198. },
  199. {
  200. sort: { sort: 1 /* boards default sorting */ },
  201. },
  202. );
  203. },
  204. swimlanes() {
  205. const board = Boards.findOne(this.selectedBoardId.get());
  206. return board.swimlanes();
  207. },
  208. aBoardLists() {
  209. const board = Boards.findOne(this.selectedBoardId.get());
  210. return board.lists();
  211. },
  212. events() {
  213. return [
  214. {
  215. 'change .js-select-boards'(event) {
  216. this.selectedBoardId.set($(event.currentTarget).val());
  217. subManager.subscribe('board', this.selectedBoardId.get(), false);
  218. },
  219. },
  220. ];
  221. },
  222. }).register('boardsSwimlanesAndLists');
  223. Template.checklists.helpers({
  224. checklists() {
  225. const card = Cards.findOne(this.cardId);
  226. const ret = card.checklists();
  227. return ret;
  228. },
  229. hideCheckedItems() {
  230. const currentUser = Meteor.user();
  231. if (currentUser) return currentUser.hasHideCheckedItems();
  232. return false;
  233. },
  234. });
  235. BlazeComponent.extendComponent({
  236. onRendered() {
  237. autosize(this.$('textarea.js-add-checklist-item'));
  238. },
  239. canModifyCard() {
  240. return (
  241. Meteor.user() &&
  242. Meteor.user().isBoardMember() &&
  243. !Meteor.user().isCommentOnly() &&
  244. !Meteor.user().isWorker()
  245. );
  246. },
  247. events() {
  248. return [
  249. {
  250. 'click a.fa.fa-copy'(event) {
  251. const $editor = this.$('textarea');
  252. const promise = Utils.copyTextToClipboard($editor[0].value);
  253. const $tooltip = this.$('.copied-tooltip');
  254. Utils.showCopied(promise, $tooltip);
  255. },
  256. }
  257. ];
  258. }
  259. }).register('addChecklistItemForm');
  260. BlazeComponent.extendComponent({
  261. events() {
  262. return [
  263. {
  264. 'click .js-delete-checklist' : Popup.afterConfirm('checklistDelete', function () {
  265. Popup.back(2);
  266. const checklist = this.checklist;
  267. if (checklist && checklist._id) {
  268. Checklists.remove(checklist._id);
  269. }
  270. }),
  271. 'click .js-move-checklist' : Popup.open('moveChecklist'),
  272. }
  273. ]
  274. }
  275. }).register('checklistActionsPopup');
  276. BlazeComponent.extendComponent({
  277. onRendered() {
  278. autosize(this.$('textarea.js-edit-checklist-item'));
  279. },
  280. canModifyCard() {
  281. return (
  282. Meteor.user() &&
  283. Meteor.user().isBoardMember() &&
  284. !Meteor.user().isCommentOnly() &&
  285. !Meteor.user().isWorker()
  286. );
  287. },
  288. events() {
  289. return [
  290. {
  291. 'click a.fa.fa-copy'(event) {
  292. const $editor = this.$('textarea');
  293. const promise = Utils.copyTextToClipboard($editor[0].value);
  294. const $tooltip = this.$('.copied-tooltip');
  295. Utils.showCopied(promise, $tooltip);
  296. },
  297. }
  298. ];
  299. }
  300. }).register('editChecklistItemForm');
  301. Template.checklistItemDetail.helpers({
  302. canModifyCard() {
  303. return (
  304. Meteor.user() &&
  305. Meteor.user().isBoardMember() &&
  306. !Meteor.user().isCommentOnly() &&
  307. !Meteor.user().isWorker()
  308. );
  309. },
  310. hideCheckedItems() {
  311. const user = Meteor.user();
  312. if (user) return user.hasHideCheckedItems();
  313. return false;
  314. },
  315. });
  316. BlazeComponent.extendComponent({
  317. toggleItem() {
  318. const checklist = this.currentData().checklist;
  319. const item = this.currentData().item;
  320. if (checklist && item && item._id) {
  321. item.toggleItem();
  322. }
  323. },
  324. events() {
  325. return [
  326. {
  327. 'click .js-checklist-item .check-box-container': this.toggleItem,
  328. },
  329. ];
  330. },
  331. }).register('checklistItemDetail');
  332. BlazeComponent.extendComponent({
  333. onCreated() {
  334. const boardId = Utils.getCurrentBoardId();
  335. subManager.subscribe('board', boardId, false);
  336. // subManager.subscribe('swimlane', swimlaneId, false);
  337. // subManager.subscribe('list', listId, false);
  338. // subManager.subscribe('card', cardId, false);
  339. this.selectedBoardId = new ReactiveVar(boardId);
  340. this.selectedSwimlaneId = new ReactiveVar('');
  341. this.selectedListId = new ReactiveVar('');
  342. this.selectedCardId = new ReactiveVar('');
  343. this.setMoveChecklistDialogOption(boardId);
  344. },
  345. /** set the last confirmed dialog field values
  346. * @param boardId the current board id
  347. */
  348. setMoveChecklistDialogOption(boardId) {
  349. this.moveChecklistDialogOption = {
  350. 'boardId' : "",
  351. 'swimlaneId' : "",
  352. 'listId' : "",
  353. 'cardId': "",
  354. }
  355. let currentOptions = Meteor.user().getMoveChecklistDialogOptions();
  356. if (currentOptions && boardId && currentOptions[boardId]) {
  357. this.moveChecklistDialogOption = currentOptions[boardId];
  358. }
  359. const board = Boards.findOne(boardId);
  360. try {
  361. const swimlaneId = board.swimlanes().fetch()[0]._id;
  362. this.selectedSwimlaneId.set(swimlaneId);
  363. } catch (e) {}
  364. try {
  365. const listId = board.lists().fetch()[0];
  366. this.selectedListId.set(listId);
  367. } catch (e) {}
  368. const cardId = Utils.getCurrentCardId();
  369. this.selectedCardId.set(cardId);
  370. },
  371. /** returns if the board id was the last confirmed one
  372. * @param boardId check this board id
  373. * @return if the board id was the last confirmed one
  374. */
  375. isMoveChecklistDialogOptionBoardId(boardId) {
  376. let ret = this.moveChecklistDialogOption.boardId == boardId;
  377. return ret;
  378. },
  379. /** returns if the swimlane id was the last confirmed one
  380. * @param swimlaneId check this swimlane id
  381. * @return if the swimlane id was the last confirmed one
  382. */
  383. isMoveChecklistDialogOptionSwimlaneId(swimlaneId) {
  384. let ret = this.moveChecklistDialogOption.swimlaneId == swimlaneId;
  385. return ret;
  386. },
  387. /** returns if the list id was the last confirmed one
  388. * @param listId check this list id
  389. * @return if the list id was the last confirmed one
  390. */
  391. isMoveChecklistDialogOptionListId(listId) {
  392. let ret = this.moveChecklistDialogOption.listId == listId;
  393. return ret;
  394. },
  395. /** returns if the card id was the last confirmed one
  396. * @param cardId check this card id
  397. * @return if the card id was the last confirmed one
  398. */
  399. isMoveChecklistDialogOptionCardId(cardId) {
  400. let ret = this.moveChecklistDialogOption.cardId == cardId;
  401. return ret;
  402. },
  403. boards() {
  404. return Boards.find(
  405. {
  406. archived: false,
  407. 'members.userId': Meteor.userId(),
  408. _id: { $ne: Meteor.user().getTemplatesBoardId() },
  409. },
  410. {
  411. sort: { sort: 1 },
  412. },
  413. );
  414. },
  415. swimlanes() {
  416. const board = Boards.findOne(this.selectedBoardId.get());
  417. return board.swimlanes();
  418. },
  419. lists() {
  420. const board = Boards.findOne(this.selectedBoardId.get());
  421. return board.lists();
  422. },
  423. cards() {
  424. const list = Lists.findOne(this.selectedListId.get());
  425. const ret = list.cards(this.selectedSwimlaneId.get());
  426. return ret;
  427. },
  428. events() {
  429. return [
  430. {
  431. 'click .js-done'() {
  432. const boardSelect = this.$('.js-select-boards')[0];
  433. const boardId = boardSelect.options[boardSelect.selectedIndex].value;
  434. const listSelect = this.$('.js-select-lists')[0];
  435. const listId = listSelect.options[listSelect.selectedIndex].value;
  436. const swimlaneSelect = this.$('.js-select-swimlanes')[0];
  437. const swimlaneId = swimlaneSelect.options[swimlaneSelect.selectedIndex].value;
  438. const cardSelect = this.$('.js-select-cards')[0];
  439. const cardId = cardSelect.options[cardSelect.selectedIndex].value;
  440. const options = {
  441. 'boardId' : boardId,
  442. 'swimlaneId' : swimlaneId,
  443. 'listId' : listId,
  444. 'cardId': cardId,
  445. }
  446. Meteor.user().setMoveChecklistDialogOption(boardId, options);
  447. this.data().checklist.move(cardId);
  448. Popup.back(2);
  449. },
  450. 'change .js-select-boards'(event) {
  451. const boardId = $(event.currentTarget).val();
  452. subManager.subscribe('board', boardId, false);
  453. this.setMoveChecklistDialogOption(boardId);
  454. this.selectedBoardId.set(boardId);
  455. },
  456. 'change .js-select-swimlanes'(event) {
  457. this.selectedSwimlaneId.set($(event.currentTarget).val());
  458. },
  459. 'change .js-select-lists'(event) {
  460. this.selectedListId.set($(event.currentTarget).val());
  461. },
  462. },
  463. ];
  464. },
  465. }).register('moveChecklistPopup');