Prepare to create card from template

This commit is contained in:
Andrés Manelli 2019-02-23 16:36:29 +01:00
parent 7a6afb8aea
commit 0fec711545
7 changed files with 57 additions and 60 deletions

View file

@ -459,26 +459,9 @@ BlazeComponent.extendComponent({
}, },
}).register('boardsAndLists'); }).register('boardsAndLists');
function cloneCheckList(_id, checklist) {
'use strict';
const checklistId = checklist._id;
checklist.cardId = _id;
checklist._id = null;
const newChecklistId = Checklists.insert(checklist);
ChecklistItems.find({checklistId}).forEach(function(item) {
item._id = null;
item.checklistId = newChecklistId;
item.cardId = _id;
ChecklistItems.insert(item);
});
}
Template.copyCardPopup.events({ Template.copyCardPopup.events({
'click .js-done'() { 'click .js-done'() {
const card = Cards.findOne(Session.get('currentCard')); const card = Cards.findOne(Session.get('currentCard'));
const oldId = card._id;
card._id = null;
const lSelect = $('.js-select-lists')[0]; const lSelect = $('.js-select-lists')[0];
card.listId = lSelect.options[lSelect.selectedIndex].value; card.listId = lSelect.options[lSelect.selectedIndex].value;
const slSelect = $('.js-select-swimlanes')[0]; const slSelect = $('.js-select-swimlanes')[0];
@ -493,38 +476,13 @@ Template.copyCardPopup.events({
if (title) { if (title) {
card.title = title; card.title = title;
card.coverId = ''; card.coverId = '';
const _id = Cards.insert(card); const _id = card.copy();
// In case the filter is active we need to add the newly inserted card in // 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 // the list of exceptions -- cards that are not filtered. Otherwise the
// card will disappear instantly. // card will disappear instantly.
// See https://github.com/wekan/wekan/issues/80 // See https://github.com/wekan/wekan/issues/80
Filter.addException(_id); Filter.addException(_id);
// copy checklists
let cursor = Checklists.find({cardId: oldId});
cursor.forEach(function() {
cloneCheckList(_id, arguments[0]);
});
// copy subtasks
cursor = Cards.find({parentId: oldId});
cursor.forEach(function() {
'use strict';
const subtask = arguments[0];
subtask.parentId = _id;
subtask._id = null;
/* const newSubtaskId = */ Cards.insert(subtask);
});
// copy card comments
cursor = CardComments.find({cardId: oldId});
cursor.forEach(function () {
'use strict';
const comment = arguments[0];
comment.cardId = _id;
comment._id = null;
CardComments.insert(comment);
});
Popup.close(); Popup.close();
} }
}, },
@ -561,9 +519,8 @@ Template.copyChecklistToManyCardsPopup.events({
Filter.addException(_id); Filter.addException(_id);
// copy checklists // copy checklists
let cursor = Checklists.find({cardId: oldId}); Checklists.find({cardId: oldId}).forEach((ch) => {
cursor.forEach(function() { ch.copy(_id);
cloneCheckList(_id, arguments[0]);
}); });
// copy subtasks // copy subtasks
@ -577,13 +534,8 @@ Template.copyChecklistToManyCardsPopup.events({
}); });
// copy card comments // copy card comments
cursor = CardComments.find({cardId: oldId}); CardComments.find({cardId: oldId}).forEach((cmt) => {
cursor.forEach(function () { cmt.copy(_id);
'use strict';
const comment = arguments[0];
comment.cardId = _id;
comment._id = null;
CardComments.insert(comment);
}); });
} }
Popup.close(); Popup.close();

View file

@ -96,11 +96,9 @@ template(name="searchCardPopup")
label {{_ 'boards'}}: label {{_ 'boards'}}:
.link-board-wrapper .link-board-wrapper
select.js-select-boards select.js-select-boards
option(value="")
each boards each boards
if $eq _id currentBoard._id option(value="{{_id}}") {{title}}
option(value="{{_id}}" selected) {{_ 'current'}}
else
option(value="{{_id}}") {{title}}
form.js-search-term-form form.js-search-term-form
input(type="text" name="searchTerm" placeholder="{{_ 'search-example'}}" autofocus) input(type="text" name="searchTerm" placeholder="{{_ 'search-example'}}" autofocus)
.list-body.js-perfect-scrollbar.search-card-results .list-body.js-perfect-scrollbar.search-card-results

View file

@ -456,6 +456,7 @@ BlazeComponent.extendComponent({
archived: false, archived: false,
linkedId: {$nin: ownCardsIds}, linkedId: {$nin: ownCardsIds},
_id: {$nin: ownCardsIds}, _id: {$nin: ownCardsIds},
type: {$nin: ['template-card']},
}); });
}, },
@ -523,16 +524,16 @@ BlazeComponent.extendComponent({
}, },
onCreated() { onCreated() {
const isTemplateSearch = $(Popup._getTopStack().openerElement).hasClass('js-search-template'); this.isTemplateSearch = $(Popup._getTopStack().openerElement).hasClass('js-search-template');
let board = {}; let board = {};
if (isTemplateSearch) { if (this.isTemplateSearch) {
board = Boards.findOne(Meteor.user().profile.templatesBoardId); board = Boards.findOne(Meteor.user().profile.templatesBoardId);
} else { } else {
// Prefetch first non-current board id // Prefetch first non-current board id
board = Boards.findOne({ board = Boards.findOne({
archived: false, archived: false,
'members.userId': Meteor.userId(), 'members.userId': Meteor.userId(),
_id: {$ne: Session.get('currentBoard')}, _id: {$nin: [Session.get('currentBoard'), Meteor.user().profile.templatesBoardId]},
}); });
} }
if (!board) { if (!board) {

View file

@ -473,6 +473,8 @@ Boards.helpers({
if (this.isTemplatesBoard()) { if (this.isTemplatesBoard()) {
query.type = 'template-card'; query.type = 'template-card';
query.archived = false; query.archived = false;
} else {
query.type = {$nin: ['template-card']};
} }
const projection = { limit: 10, sort: { createdAt: -1 } }; const projection = { limit: 10, sort: { createdAt: -1 } };

View file

@ -67,6 +67,12 @@ CardComments.allow({
}); });
CardComments.helpers({ CardComments.helpers({
copy(newCardId) {
this.cardId = newCardId;
this._id = null;
CardComments.insert(this);
},
user() { user() {
return Users.findOne(this.userId); return Users.findOne(this.userId);
}, },

View file

@ -272,6 +272,31 @@ Cards.allow({
}); });
Cards.helpers({ Cards.helpers({
copy() {
const oldId = this._id;
this._id = null;
const _id = Cards.insert(this);
// copy checklists
Checklists.find({cardId: oldId}).forEach((ch) => {
ch.copy(_id);
});
// copy subtasks
Cards.find({parentId: oldId}).forEach((subtask) => {
subtask.parentId = _id;
subtask._id = null;
Cards.insert(subtask);
});
// copy card comments
CardComments.find({cardId: oldId}).forEach((cmt) => {
cmt.copy(_id);
});
return _id;
},
list() { list() {
return Lists.findOne(this.listId); return Lists.findOne(this.listId);
}, },

View file

@ -48,6 +48,19 @@ Checklists.attachSchema(new SimpleSchema({
})); }));
Checklists.helpers({ Checklists.helpers({
copy(newCardId) {
const oldChecklistId = this._id;
this._id = null;
this.cardId = newCardId;
const newChecklistId = Checklists.insert(this);
ChecklistItems.find({checklistId: oldChecklistId}).forEach((item) => {
item._id = null;
item.checklistId = newChecklistId;
item.cardId = newCardId;
ChecklistItems.insert(item);
});
},
itemCount() { itemCount() {
return ChecklistItems.find({ checklistId: this._id }).count(); return ChecklistItems.find({ checklistId: this._id }).count();
}, },