subtasks.js 4.2 KB

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