checklists.js 11 KB

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