subtasks.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. import { FlowRouter } from 'meteor/ostrio:flow-router-extra';
  3. BlazeComponent.extendComponent({
  4. addSubtask(event) {
  5. event.preventDefault();
  6. const textarea = this.find('textarea.js-add-subtask-item');
  7. const title = textarea.value.trim();
  8. const cardId = this.currentData().cardId;
  9. const card = ReactiveCache.getCard(cardId);
  10. const sortIndex = -1;
  11. const crtBoard = ReactiveCache.getBoard(card.boardId);
  12. const targetBoard = crtBoard.getDefaultSubtasksBoard();
  13. const listId = targetBoard.getDefaultSubtasksListId();
  14. //Get the full swimlane data for the parent task.
  15. const parentSwimlane = ReactiveCache.getSwimlane({
  16. boardId: crtBoard._id,
  17. _id: card.swimlaneId,
  18. });
  19. //find the swimlane of the same name in the target board.
  20. const targetSwimlane = ReactiveCache.getSwimlane({
  21. boardId: targetBoard._id,
  22. title: parentSwimlane.title,
  23. });
  24. //If no swimlane with a matching title exists in the target board, fall back to the default swimlane.
  25. const swimlaneId =
  26. targetSwimlane === undefined
  27. ? targetBoard.getDefaultSwimline()._id
  28. : targetSwimlane._id;
  29. const nextCardNumber = targetBoard.getNextCardNumber();
  30. if (title) {
  31. const _id = Cards.insert({
  32. title,
  33. parentId: cardId,
  34. members: [],
  35. labelIds: [],
  36. customFields: [],
  37. listId,
  38. boardId: targetBoard._id,
  39. sort: sortIndex,
  40. swimlaneId,
  41. type: 'cardType-card',
  42. cardNumber: nextCardNumber
  43. });
  44. // In case the filter is active we need to add the newly inserted card in
  45. // the list of exceptions -- cards that are not filtered. Otherwise the
  46. // card will disappear instantly.
  47. // See https://github.com/wekan/wekan/issues/80
  48. Filter.addException(_id);
  49. setTimeout(() => {
  50. this.$('.add-subtask-item')
  51. .last()
  52. .click();
  53. }, 100);
  54. }
  55. textarea.value = '';
  56. textarea.focus();
  57. },
  58. deleteSubtask() {
  59. const subtask = this.currentData().subtask;
  60. if (subtask && subtask._id) {
  61. subtask.archive();
  62. }
  63. },
  64. isBoardAdmin() {
  65. return ReactiveCache.getCurrentUser().isBoardAdmin();
  66. },
  67. editSubtask(event) {
  68. event.preventDefault();
  69. const textarea = this.find('textarea.js-edit-subtask-item');
  70. const title = textarea.value.trim();
  71. const subtask = this.currentData().subtask;
  72. subtask.setTitle(title);
  73. },
  74. pressKey(event) {
  75. //If user press enter key inside a form, submit it
  76. //Unless the user is also holding down the 'shift' key
  77. if (event.keyCode === 13 && !event.shiftKey) {
  78. event.preventDefault();
  79. const $form = $(event.currentTarget).closest('form');
  80. $form.find('button[type=submit]').click();
  81. }
  82. },
  83. events() {
  84. return [
  85. {
  86. 'click .js-open-subtask-details-menu': Popup.open('subtaskActions'),
  87. 'submit .js-add-subtask': this.addSubtask,
  88. 'submit .js-edit-subtask-title': this.editSubtask,
  89. 'click .js-delete-subtask-item': this.deleteSubtask,
  90. keydown: this.pressKey,
  91. },
  92. ];
  93. },
  94. }).register('subtasks');
  95. BlazeComponent.extendComponent({
  96. // ...
  97. }).register('subtaskItemDetail');
  98. BlazeComponent.extendComponent({
  99. isBoardAdmin() {
  100. return ReactiveCache.getCurrentUser().isBoardAdmin();
  101. },
  102. events() {
  103. return [
  104. {
  105. 'click .js-view-subtask'(event) {
  106. if ($(event.target).hasClass('js-view-subtask')) {
  107. const subtask = this.currentData().subtask;
  108. const board = subtask.board();
  109. FlowRouter.go('card', {
  110. boardId: board._id,
  111. slug: board.slug,
  112. cardId: subtask._id,
  113. });
  114. }
  115. },
  116. 'click .js-delete-subtask' : Popup.afterConfirm('subtaskDelete', function () {
  117. Popup.back(2);
  118. const subtask = this.subtask;
  119. if (subtask && subtask._id) {
  120. subtask.archive();
  121. }
  122. }),
  123. }
  124. ]
  125. }
  126. }).register('subtaskActionsPopup');
  127. Template.editSubtaskItemForm.helpers({
  128. user() {
  129. return ReactiveCache.getUser(this.userId);
  130. },
  131. isBoardAdmin() {
  132. return ReactiveCache.getCurrentUser().isBoardAdmin();
  133. },
  134. });