checklists.js 11 KB

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