checklists.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. import { TAPi18n } from '/imports/i18n';
  2. import Cards from '/models/cards';
  3. import Boards from '/models/boards';
  4. import { DialogWithBoardSwimlaneListCard } from '/client/lib/dialogWithBoardSwimlaneListCard';
  5. const subManager = new SubsManager();
  6. const { calculateIndexData, capitalize } = Utils;
  7. function initSorting(items) {
  8. items.sortable({
  9. tolerance: 'pointer',
  10. helper: 'clone',
  11. items: '.js-checklist-item:not(.placeholder)',
  12. connectWith: '.js-checklist-items',
  13. appendTo: 'parent',
  14. distance: 7,
  15. placeholder: 'checklist-item placeholder',
  16. scroll: true,
  17. start(evt, ui) {
  18. ui.placeholder.height(ui.helper.height());
  19. EscapeActions.clickExecute(evt.target, 'inlinedForm');
  20. },
  21. stop(evt, ui) {
  22. const parent = ui.item.parents('.js-checklist-items');
  23. const checklistId = Blaze.getData(parent.get(0)).checklist._id;
  24. let prevItem = ui.item.prev('.js-checklist-item').get(0);
  25. if (prevItem) {
  26. prevItem = Blaze.getData(prevItem).item;
  27. }
  28. let nextItem = ui.item.next('.js-checklist-item').get(0);
  29. if (nextItem) {
  30. nextItem = Blaze.getData(nextItem).item;
  31. }
  32. const nItems = 1;
  33. const sortIndex = calculateIndexData(prevItem, nextItem, nItems);
  34. const checklistDomElement = ui.item.get(0);
  35. const checklistData = Blaze.getData(checklistDomElement);
  36. const checklistItem = checklistData.item;
  37. items.sortable('cancel');
  38. checklistItem.move(checklistId, sortIndex.base);
  39. },
  40. });
  41. }
  42. BlazeComponent.extendComponent({
  43. onRendered() {
  44. const self = this;
  45. self.itemsDom = this.$('.js-checklist-items');
  46. initSorting(self.itemsDom);
  47. self.itemsDom.mousedown(function (evt) {
  48. evt.stopPropagation();
  49. });
  50. function userIsMember() {
  51. return Meteor.user() && Meteor.user().isBoardMember();
  52. }
  53. // Disable sorting if the current user is not a board member
  54. self.autorun(() => {
  55. const $itemsDom = $(self.itemsDom);
  56. if ($itemsDom.data('uiSortable') || $itemsDom.data('sortable')) {
  57. $(self.itemsDom).sortable('option', 'disabled', !userIsMember());
  58. if (Utils.isTouchScreenOrShowDesktopDragHandles()) {
  59. $(self.itemsDom).sortable({
  60. handle: 'span.fa.checklistitem-handle',
  61. });
  62. }
  63. }
  64. });
  65. },
  66. canModifyCard() {
  67. return (
  68. Meteor.user() &&
  69. Meteor.user().isBoardMember() &&
  70. !Meteor.user().isCommentOnly() &&
  71. !Meteor.user().isWorker()
  72. );
  73. },
  74. /** returns the finished percent of the checklist */
  75. finishedPercent() {
  76. const ret = this.data().checklist.finishedPercent();
  77. return ret;
  78. },
  79. }).register('checklistDetail');
  80. BlazeComponent.extendComponent({
  81. addChecklist(event) {
  82. event.preventDefault();
  83. const textarea = this.find('textarea.js-add-checklist-item');
  84. const title = textarea.value.trim();
  85. let cardId = this.currentData().cardId;
  86. const card = Cards.findOne(cardId);
  87. //if (card.isLinked()) cardId = card.linkedId;
  88. if (card.isLinkedCard()) cardId = card.linkedId;
  89. if (title) {
  90. Checklists.insert({
  91. cardId,
  92. title,
  93. sort: card.checklists().count(),
  94. });
  95. this.closeAllInlinedForms();
  96. setTimeout(() => {
  97. this.$('.add-checklist-item')
  98. .last()
  99. .click();
  100. }, 100);
  101. }
  102. },
  103. addChecklistItem(event) {
  104. event.preventDefault();
  105. const textarea = this.find('textarea.js-add-checklist-item');
  106. const newlineBecomesNewChecklistItem = this.find('input#toggleNewlineBecomesNewChecklistItem');
  107. const title = textarea.value.trim();
  108. const checklist = this.currentData().checklist;
  109. if (title) {
  110. let checklistItems = [title];
  111. if (newlineBecomesNewChecklistItem.checked) {
  112. checklistItems = title.split('\n').map(_value => _value.trim());
  113. }
  114. for (let checklistItem of checklistItems) {
  115. ChecklistItems.insert({
  116. title: checklistItem,
  117. checklistId: checklist._id,
  118. cardId: checklist.cardId,
  119. sort: Utils.calculateIndexData(checklist.lastItem()).base,
  120. });
  121. }
  122. }
  123. // We keep the form opened, empty it.
  124. textarea.value = '';
  125. textarea.focus();
  126. },
  127. canModifyCard() {
  128. return (
  129. Meteor.user() &&
  130. Meteor.user().isBoardMember() &&
  131. !Meteor.user().isCommentOnly() &&
  132. !Meteor.user().isWorker()
  133. );
  134. },
  135. deleteItem() {
  136. const checklist = this.currentData().checklist;
  137. const item = this.currentData().item;
  138. if (checklist && item && item._id) {
  139. ChecklistItems.remove(item._id);
  140. }
  141. },
  142. editChecklist(event) {
  143. event.preventDefault();
  144. const textarea = this.find('textarea.js-edit-checklist-item');
  145. const title = textarea.value.trim();
  146. const checklist = this.currentData().checklist;
  147. checklist.setTitle(title);
  148. },
  149. editChecklistItem(event) {
  150. event.preventDefault();
  151. const textarea = this.find('textarea.js-edit-checklist-item');
  152. const title = textarea.value.trim();
  153. const item = this.currentData().item;
  154. item.setTitle(title);
  155. },
  156. pressKey(event) {
  157. //If user press enter key inside a form, submit it
  158. //Unless the user is also holding down the 'shift' key
  159. if (event.keyCode === 13 && !event.shiftKey) {
  160. event.preventDefault();
  161. const $form = $(event.currentTarget).closest('form');
  162. $form.find('button[type=submit]').click();
  163. }
  164. },
  165. focusChecklistItem(event) {
  166. // If a new checklist is created, pre-fill the title and select it.
  167. const checklist = this.currentData().checklist;
  168. if (!checklist) {
  169. const textarea = event.target;
  170. textarea.value = capitalize(TAPi18n.__('r-checklist'));
  171. textarea.select();
  172. }
  173. },
  174. /** closes all inlined forms (checklist and checklist-item input fields) */
  175. closeAllInlinedForms() {
  176. this.$('.js-close-inlined-form').click();
  177. },
  178. events() {
  179. const events = {
  180. 'click #toggleHideCheckedItemsButton'() {
  181. Meteor.call('toggleHideCheckedItems');
  182. },
  183. };
  184. return [
  185. {
  186. ...events,
  187. 'click .js-open-checklist-details-menu': Popup.open('checklistActions'),
  188. 'submit .js-add-checklist': this.addChecklist,
  189. 'submit .js-edit-checklist-title': this.editChecklist,
  190. 'submit .js-add-checklist-item': this.addChecklistItem,
  191. 'submit .js-edit-checklist-item': this.editChecklistItem,
  192. 'click .js-convert-checklist-item-to-card': Popup.open('convertChecklistItemToCard'),
  193. 'click .js-delete-checklist-item': this.deleteItem,
  194. 'focus .js-add-checklist-item': this.focusChecklistItem,
  195. // add and delete checklist / checklist-item
  196. 'click .js-open-inlined-form': this.closeAllInlinedForms,
  197. keydown: this.pressKey,
  198. },
  199. ];
  200. },
  201. }).register('checklists');
  202. BlazeComponent.extendComponent({
  203. onCreated() {
  204. subManager.subscribe('board', Session.get('currentBoard'), false);
  205. this.selectedBoardId = new ReactiveVar(Session.get('currentBoard'));
  206. },
  207. boards() {
  208. return Boards.find(
  209. {
  210. archived: false,
  211. 'members.userId': Meteor.userId(),
  212. _id: { $ne: Meteor.user().getTemplatesBoardId() },
  213. },
  214. {
  215. sort: { sort: 1 /* boards default sorting */ },
  216. },
  217. );
  218. },
  219. swimlanes() {
  220. const board = Boards.findOne(this.selectedBoardId.get());
  221. return board.swimlanes();
  222. },
  223. aBoardLists() {
  224. const board = Boards.findOne(this.selectedBoardId.get());
  225. return board.lists();
  226. },
  227. events() {
  228. return [
  229. {
  230. 'change .js-select-boards'(event) {
  231. this.selectedBoardId.set($(event.currentTarget).val());
  232. subManager.subscribe('board', this.selectedBoardId.get(), false);
  233. },
  234. },
  235. ];
  236. },
  237. }).register('boardsSwimlanesAndLists');
  238. Template.checklists.helpers({
  239. checklists() {
  240. const card = Cards.findOne(this.cardId);
  241. const ret = card.checklists();
  242. return ret;
  243. },
  244. hideCheckedItems() {
  245. const currentUser = Meteor.user();
  246. if (currentUser) return currentUser.hasHideCheckedItems();
  247. return false;
  248. },
  249. });
  250. BlazeComponent.extendComponent({
  251. onRendered() {
  252. autosize(this.$('textarea.js-add-checklist-item'));
  253. },
  254. canModifyCard() {
  255. return (
  256. Meteor.user() &&
  257. Meteor.user().isBoardMember() &&
  258. !Meteor.user().isCommentOnly() &&
  259. !Meteor.user().isWorker()
  260. );
  261. },
  262. events() {
  263. return [
  264. {
  265. 'click a.fa.fa-copy'(event) {
  266. const $editor = this.$('textarea');
  267. const promise = Utils.copyTextToClipboard($editor[0].value);
  268. const $tooltip = this.$('.copied-tooltip');
  269. Utils.showCopied(promise, $tooltip);
  270. },
  271. }
  272. ];
  273. }
  274. }).register('addChecklistItemForm');
  275. BlazeComponent.extendComponent({
  276. events() {
  277. return [
  278. {
  279. 'click .js-delete-checklist': Popup.afterConfirm('checklistDelete', function () {
  280. Popup.back(2);
  281. const checklist = this.checklist;
  282. if (checklist && checklist._id) {
  283. Checklists.remove(checklist._id);
  284. }
  285. }),
  286. 'click .js-move-checklist': Popup.open('moveChecklist'),
  287. 'click .js-copy-checklist': Popup.open('copyChecklist'),
  288. }
  289. ]
  290. }
  291. }).register('checklistActionsPopup');
  292. BlazeComponent.extendComponent({
  293. onRendered() {
  294. autosize(this.$('textarea.js-edit-checklist-item'));
  295. },
  296. canModifyCard() {
  297. return (
  298. Meteor.user() &&
  299. Meteor.user().isBoardMember() &&
  300. !Meteor.user().isCommentOnly() &&
  301. !Meteor.user().isWorker()
  302. );
  303. },
  304. events() {
  305. return [
  306. {
  307. 'click a.fa.fa-copy'(event) {
  308. const $editor = this.$('textarea');
  309. const promise = Utils.copyTextToClipboard($editor[0].value);
  310. const $tooltip = this.$('.copied-tooltip');
  311. Utils.showCopied(promise, $tooltip);
  312. },
  313. }
  314. ];
  315. }
  316. }).register('editChecklistItemForm');
  317. Template.checklistItemDetail.helpers({
  318. canModifyCard() {
  319. return (
  320. Meteor.user() &&
  321. Meteor.user().isBoardMember() &&
  322. !Meteor.user().isCommentOnly() &&
  323. !Meteor.user().isWorker()
  324. );
  325. },
  326. hideCheckedItems() {
  327. const user = Meteor.user();
  328. if (user) return user.hasHideCheckedItems();
  329. return false;
  330. },
  331. });
  332. BlazeComponent.extendComponent({
  333. toggleItem() {
  334. const checklist = this.currentData().checklist;
  335. const item = this.currentData().item;
  336. if (checklist && item && item._id) {
  337. item.toggleItem();
  338. }
  339. },
  340. events() {
  341. return [
  342. {
  343. 'click .js-checklist-item .check-box-container': this.toggleItem,
  344. },
  345. ];
  346. },
  347. }).register('checklistItemDetail');
  348. /** Move Checklist Dialog */
  349. (class extends DialogWithBoardSwimlaneListCard {
  350. getDialogOptions() {
  351. const ret = Meteor.user().getMoveChecklistDialogOptions();
  352. return ret;
  353. }
  354. setDone(cardId, options) {
  355. Meteor.user().setMoveChecklistDialogOption(this.currentBoardId, options);
  356. this.data().checklist.move(cardId);
  357. }
  358. }).register('moveChecklistPopup');
  359. /** Copy Checklist Dialog */
  360. (class extends DialogWithBoardSwimlaneListCard {
  361. getDialogOptions() {
  362. const ret = Meteor.user().getCopyChecklistDialogOptions();
  363. return ret;
  364. }
  365. setDone(cardId, options) {
  366. Meteor.user().setCopyChecklistDialogOption(this.currentBoardId, options);
  367. this.data().checklist.copy(cardId);
  368. }
  369. }).register('copyChecklistPopup');