checklists.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. let sortIndex;
  90. let checklistItemIndex;
  91. if (this.currentData().position === 'top') {
  92. sortIndex = Utils.calculateIndexData(null, card.firstChecklist()).base;
  93. checklistItemIndex = 0;
  94. } else {
  95. sortIndex = Utils.calculateIndexData(card.lastChecklist(), null).base;
  96. checklistItemIndex = -1;
  97. }
  98. if (title) {
  99. Checklists.insert({
  100. cardId,
  101. title,
  102. sort: sortIndex,
  103. });
  104. this.closeAllInlinedForms();
  105. setTimeout(() => {
  106. this.$('.add-checklist-item')
  107. .eq(checklistItemIndex)
  108. .click();
  109. }, 100);
  110. }
  111. },
  112. addChecklistItem(event) {
  113. event.preventDefault();
  114. const textarea = this.find('textarea.js-add-checklist-item');
  115. const newlineBecomesNewChecklistItem = this.find('input#toggleNewlineBecomesNewChecklistItem');
  116. const title = textarea.value.trim();
  117. const checklist = this.currentData().checklist;
  118. if (title) {
  119. let checklistItems = [title];
  120. if (newlineBecomesNewChecklistItem.checked) {
  121. checklistItems = title.split('\n').map(_value => _value.trim());
  122. }
  123. for (let checklistItem of checklistItems) {
  124. ChecklistItems.insert({
  125. title: checklistItem,
  126. checklistId: checklist._id,
  127. cardId: checklist.cardId,
  128. sort: Utils.calculateIndexData(checklist.lastItem()).base,
  129. });
  130. }
  131. }
  132. // We keep the form opened, empty it.
  133. textarea.value = '';
  134. textarea.focus();
  135. },
  136. canModifyCard() {
  137. return (
  138. Meteor.user() &&
  139. Meteor.user().isBoardMember() &&
  140. !Meteor.user().isCommentOnly() &&
  141. !Meteor.user().isWorker()
  142. );
  143. },
  144. deleteItem() {
  145. const checklist = this.currentData().checklist;
  146. const item = this.currentData().item;
  147. if (checklist && item && item._id) {
  148. ChecklistItems.remove(item._id);
  149. }
  150. },
  151. editChecklist(event) {
  152. event.preventDefault();
  153. const textarea = this.find('textarea.js-edit-checklist-item');
  154. const title = textarea.value.trim();
  155. const checklist = this.currentData().checklist;
  156. checklist.setTitle(title);
  157. },
  158. editChecklistItem(event) {
  159. event.preventDefault();
  160. const textarea = this.find('textarea.js-edit-checklist-item');
  161. const title = textarea.value.trim();
  162. const item = this.currentData().item;
  163. item.setTitle(title);
  164. },
  165. pressKey(event) {
  166. //If user press enter key inside a form, submit it
  167. //Unless the user is also holding down the 'shift' key
  168. if (event.keyCode === 13 && !event.shiftKey) {
  169. event.preventDefault();
  170. const $form = $(event.currentTarget).closest('form');
  171. $form.find('button[type=submit]').click();
  172. }
  173. },
  174. focusChecklistItem(event) {
  175. // If a new checklist is created, pre-fill the title and select it.
  176. const checklist = this.currentData().checklist;
  177. if (!checklist) {
  178. const textarea = event.target;
  179. textarea.value = capitalize(TAPi18n.__('r-checklist'));
  180. textarea.select();
  181. }
  182. },
  183. /** closes all inlined forms (checklist and checklist-item input fields) */
  184. closeAllInlinedForms() {
  185. this.$('.js-close-inlined-form').click();
  186. },
  187. events() {
  188. const events = {
  189. 'click #toggleHideCheckedItemsButton'() {
  190. Meteor.call('toggleHideCheckedItems');
  191. },
  192. };
  193. return [
  194. {
  195. ...events,
  196. 'click .js-open-checklist-details-menu': Popup.open('checklistActions'),
  197. 'submit .js-add-checklist': this.addChecklist,
  198. 'submit .js-edit-checklist-title': this.editChecklist,
  199. 'submit .js-add-checklist-item': this.addChecklistItem,
  200. 'submit .js-edit-checklist-item': this.editChecklistItem,
  201. 'click .js-convert-checklist-item-to-card': Popup.open('convertChecklistItemToCard'),
  202. 'click .js-delete-checklist-item': this.deleteItem,
  203. 'focus .js-add-checklist-item': this.focusChecklistItem,
  204. // add and delete checklist / checklist-item
  205. 'click .js-open-inlined-form': this.closeAllInlinedForms,
  206. keydown: this.pressKey,
  207. },
  208. ];
  209. },
  210. }).register('checklists');
  211. BlazeComponent.extendComponent({
  212. onCreated() {
  213. subManager.subscribe('board', Session.get('currentBoard'), false);
  214. this.selectedBoardId = new ReactiveVar(Session.get('currentBoard'));
  215. },
  216. boards() {
  217. return Boards.find(
  218. {
  219. archived: false,
  220. 'members.userId': Meteor.userId(),
  221. _id: { $ne: Meteor.user().getTemplatesBoardId() },
  222. },
  223. {
  224. sort: { sort: 1 /* boards default sorting */ },
  225. },
  226. );
  227. },
  228. swimlanes() {
  229. const board = Boards.findOne(this.selectedBoardId.get());
  230. return board.swimlanes();
  231. },
  232. aBoardLists() {
  233. const board = Boards.findOne(this.selectedBoardId.get());
  234. return board.lists();
  235. },
  236. events() {
  237. return [
  238. {
  239. 'change .js-select-boards'(event) {
  240. this.selectedBoardId.set($(event.currentTarget).val());
  241. subManager.subscribe('board', this.selectedBoardId.get(), false);
  242. },
  243. },
  244. ];
  245. },
  246. }).register('boardsSwimlanesAndLists');
  247. Template.checklists.helpers({
  248. checklists() {
  249. const card = Cards.findOne(this.cardId);
  250. const ret = card.checklists();
  251. return ret;
  252. },
  253. hideCheckedItems() {
  254. const currentUser = Meteor.user();
  255. if (currentUser) return currentUser.hasHideCheckedItems();
  256. return false;
  257. },
  258. });
  259. BlazeComponent.extendComponent({
  260. onRendered() {
  261. autosize(this.$('textarea.js-add-checklist-item'));
  262. },
  263. canModifyCard() {
  264. return (
  265. Meteor.user() &&
  266. Meteor.user().isBoardMember() &&
  267. !Meteor.user().isCommentOnly() &&
  268. !Meteor.user().isWorker()
  269. );
  270. },
  271. events() {
  272. return [
  273. {
  274. 'click a.fa.fa-copy'(event) {
  275. const $editor = this.$('textarea');
  276. const promise = Utils.copyTextToClipboard($editor[0].value);
  277. const $tooltip = this.$('.copied-tooltip');
  278. Utils.showCopied(promise, $tooltip);
  279. },
  280. }
  281. ];
  282. }
  283. }).register('addChecklistItemForm');
  284. BlazeComponent.extendComponent({
  285. events() {
  286. return [
  287. {
  288. 'click .js-delete-checklist': Popup.afterConfirm('checklistDelete', function () {
  289. Popup.back(2);
  290. const checklist = this.checklist;
  291. if (checklist && checklist._id) {
  292. Checklists.remove(checklist._id);
  293. }
  294. }),
  295. 'click .js-move-checklist': Popup.open('moveChecklist'),
  296. 'click .js-copy-checklist': Popup.open('copyChecklist'),
  297. }
  298. ]
  299. }
  300. }).register('checklistActionsPopup');
  301. BlazeComponent.extendComponent({
  302. onRendered() {
  303. autosize(this.$('textarea.js-edit-checklist-item'));
  304. },
  305. canModifyCard() {
  306. return (
  307. Meteor.user() &&
  308. Meteor.user().isBoardMember() &&
  309. !Meteor.user().isCommentOnly() &&
  310. !Meteor.user().isWorker()
  311. );
  312. },
  313. events() {
  314. return [
  315. {
  316. 'click a.fa.fa-copy'(event) {
  317. const $editor = this.$('textarea');
  318. const promise = Utils.copyTextToClipboard($editor[0].value);
  319. const $tooltip = this.$('.copied-tooltip');
  320. Utils.showCopied(promise, $tooltip);
  321. },
  322. }
  323. ];
  324. }
  325. }).register('editChecklistItemForm');
  326. Template.checklistItemDetail.helpers({
  327. canModifyCard() {
  328. return (
  329. Meteor.user() &&
  330. Meteor.user().isBoardMember() &&
  331. !Meteor.user().isCommentOnly() &&
  332. !Meteor.user().isWorker()
  333. );
  334. },
  335. hideCheckedItems() {
  336. const user = Meteor.user();
  337. if (user) return user.hasHideCheckedItems();
  338. return false;
  339. },
  340. });
  341. BlazeComponent.extendComponent({
  342. toggleItem() {
  343. const checklist = this.currentData().checklist;
  344. const item = this.currentData().item;
  345. if (checklist && item && item._id) {
  346. item.toggleItem();
  347. }
  348. },
  349. events() {
  350. return [
  351. {
  352. 'click .js-checklist-item .check-box-container': this.toggleItem,
  353. },
  354. ];
  355. },
  356. }).register('checklistItemDetail');
  357. /** Move Checklist Dialog */
  358. (class extends DialogWithBoardSwimlaneListCard {
  359. getDialogOptions() {
  360. const ret = Meteor.user().getMoveChecklistDialogOptions();
  361. return ret;
  362. }
  363. setDone(cardId, options) {
  364. Meteor.user().setMoveChecklistDialogOption(this.currentBoardId, options);
  365. this.data().checklist.move(cardId);
  366. }
  367. }).register('moveChecklistPopup');
  368. /** Copy Checklist Dialog */
  369. (class extends DialogWithBoardSwimlaneListCard {
  370. getDialogOptions() {
  371. const ret = Meteor.user().getCopyChecklistDialogOptions();
  372. return ret;
  373. }
  374. setDone(cardId, options) {
  375. Meteor.user().setCopyChecklistDialogOption(this.currentBoardId, options);
  376. this.data().checklist.copy(cardId);
  377. }
  378. }).register('copyChecklistPopup');