subtasks.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 = Cards.findOne(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 = Swimlanes.findOne({
  15. boardId: crtBoard._id,
  16. _id: card.swimlaneId,
  17. });
  18. //find the swimlane of the same name in the target board.
  19. const targetSwimlane = Swimlanes.findOne({
  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. editSubtask(event) {
  64. event.preventDefault();
  65. const textarea = this.find('textarea.js-edit-subtask-item');
  66. const title = textarea.value.trim();
  67. const subtask = this.currentData().subtask;
  68. subtask.setTitle(title);
  69. },
  70. pressKey(event) {
  71. //If user press enter key inside a form, submit it
  72. //Unless the user is also holding down the 'shift' key
  73. if (event.keyCode === 13 && !event.shiftKey) {
  74. event.preventDefault();
  75. const $form = $(event.currentTarget).closest('form');
  76. $form.find('button[type=submit]').click();
  77. }
  78. },
  79. events() {
  80. return [
  81. {
  82. 'click .js-open-subtask-details-menu': Popup.open('subtaskActions'),
  83. 'submit .js-add-subtask': this.addSubtask,
  84. 'submit .js-edit-subtask-title': this.editSubtask,
  85. 'click .js-delete-subtask-item': this.deleteSubtask,
  86. keydown: this.pressKey,
  87. },
  88. ];
  89. },
  90. }).register('subtasks');
  91. BlazeComponent.extendComponent({
  92. // ...
  93. }).register('subtaskItemDetail');
  94. BlazeComponent.extendComponent({
  95. events() {
  96. return [
  97. {
  98. 'click .js-view-subtask'(event) {
  99. if ($(event.target).hasClass('js-view-subtask')) {
  100. const subtask = this.currentData().subtask;
  101. const board = subtask.board();
  102. FlowRouter.go('card', {
  103. boardId: board._id,
  104. slug: board.slug,
  105. cardId: subtask._id,
  106. });
  107. }
  108. },
  109. 'click .js-delete-subtask' : Popup.afterConfirm('subtaskDelete', function () {
  110. Popup.back(2);
  111. const subtask = this.subtask;
  112. if (subtask && subtask._id) {
  113. subtask.archive();
  114. }
  115. }),
  116. }
  117. ]
  118. }
  119. }).register('subtaskActionsPopup');