wekan/client/components/cards/subtasks.js

148 lines
4.3 KiB
JavaScript
Raw Normal View History

import { ReactiveCache } from '/imports/reactiveCache';
2026-01-14 00:13:21 +02:00
import { FlowRouter } from 'meteor/ostrio:flow-router-extra';
Template.subtasks.events({
'click .js-open-subtask-details-menu': Popup.open('subtaskActions'),
'submit .js-add-subtask'(event, tpl) {
2018-06-18 23:25:56 +03:00
event.preventDefault();
const textarea = tpl.find('textarea.js-add-subtask-item');
2018-06-18 23:25:56 +03:00
const title = textarea.value.trim();
const cardId = Template.currentData().cardId;
const card = ReactiveCache.getCard(cardId);
2018-06-23 23:22:38 +03:00
const sortIndex = -1;
const crtBoard = ReactiveCache.getBoard(card.boardId);
2018-06-23 23:22:38 +03:00
const targetBoard = crtBoard.getDefaultSubtasksBoard();
const listId = targetBoard.getDefaultSubtasksListId();
//Get the full swimlane data for the parent task.
const parentSwimlane = ReactiveCache.getSwimlane({
boardId: crtBoard._id,
_id: card.swimlaneId,
});
//find the swimlane of the same name in the target board.
const targetSwimlane = ReactiveCache.getSwimlane({
boardId: targetBoard._id,
title: parentSwimlane.title,
});
//If no swimlane with a matching title exists in the target board, fall back to the default swimlane.
const swimlaneId =
targetSwimlane === undefined
? targetBoard.getDefaultSwimline()._id
: targetSwimlane._id;
2018-06-18 23:25:56 +03:00
2021-08-27 21:16:27 +02:00
const nextCardNumber = targetBoard.getNextCardNumber();
2018-06-18 23:25:56 +03:00
if (title) {
2018-06-23 23:22:38 +03:00
const _id = Cards.insert({
2018-06-18 23:25:56 +03:00
title,
2018-06-23 23:22:38 +03:00
parentId: cardId,
members: [],
labelIds: [],
customFields: [],
listId,
boardId: targetBoard._id,
sort: sortIndex,
swimlaneId,
2018-08-11 00:50:20 +02:00
type: 'cardType-card',
2021-08-27 21:16:27 +02:00
cardNumber: nextCardNumber
2018-06-18 23:25:56 +03:00
});
2018-06-25 23:12:20 +03:00
2018-06-23 23:22:38 +03:00
// In case the filter is active we need to add the newly inserted card in
// the list of exceptions -- cards that are not filtered. Otherwise the
// card will disappear instantly.
// See https://github.com/wekan/wekan/issues/80
Filter.addException(_id);
2018-06-18 23:25:56 +03:00
setTimeout(() => {
tpl.$('.add-subtask-item')
2019-06-28 12:52:09 -05:00
.last()
.click();
2018-06-18 23:25:56 +03:00
}, 100);
}
textarea.value = '';
textarea.focus();
},
'submit .js-edit-subtask-title'(event, tpl) {
event.preventDefault();
const textarea = tpl.find('textarea.js-edit-subtask-item');
const title = textarea.value.trim();
const subtask = Template.currentData().subtask;
subtask.setTitle(title);
},
async 'click .js-delete-subtask-item'() {
const subtask = Template.currentData().subtask;
if (subtask && subtask._id) {
await subtask.archive();
2018-06-18 23:25:56 +03:00
}
},
keydown(event) {
2018-06-18 23:25:56 +03:00
//If user press enter key inside a form, submit it
//Unless the user is also holding down the 'shift' key
if (event.keyCode === 13 && !event.shiftKey) {
event.preventDefault();
const $form = $(event.currentTarget).closest('form');
$form.find('button[type=submit]').click();
}
},
});
Template.subtasks.onCreated(function () {
this.toggleDeleteDialog = new ReactiveVar(false);
});
2018-06-18 23:25:56 +03:00
Template.subtasks.helpers({
isBoardAdmin() {
return ReactiveCache.getCurrentUser().isBoardAdmin();
},
toggleDeleteDialog() {
return Template.instance().toggleDeleteDialog;
2018-06-18 23:25:56 +03:00
},
});
2018-06-18 23:25:56 +03:00
Template.subtaskItemDetail.events({
async 'click .js-subtasks-item .check-box-unicode'() {
const item = Template.currentData().item;
if (item && item._id) {
await item.toggleItem();
}
},
});
Template.subtaskActionsPopup.helpers({
isBoardAdmin() {
return ReactiveCache.getCurrentUser().isBoardAdmin();
},
});
Template.subtaskActionsPopup.events({
'click .js-view-subtask'(event) {
if ($(event.target).hasClass('js-view-subtask')) {
const subtask = Template.currentData().subtask;
const board = subtask.board();
FlowRouter.go('card', {
boardId: board._id,
slug: board.slug,
cardId: subtask._id,
2026-03-09 02:25:13 -11:00
swimlaneId: subtask.swimlaneId,
listId: subtask.listId,
});
}
},
'click .js-delete-subtask' : Popup.afterConfirm('subtaskDelete', async function () {
Popup.back(2);
const subtask = this.subtask;
if (subtask && subtask._id) {
await subtask.archive();
}
}),
});
Template.editSubtaskItemForm.helpers({
user() {
return ReactiveCache.getUser(this.userId);
},
isBoardAdmin() {
return ReactiveCache.getCurrentUser().isBoardAdmin();
},
});