Merge branch 'GhassenRjab-feature/keep-state-checklist' into devel

Keep state of checklist items when moved to another checklist.
Thanks to GhassenRjab ! Related #876
This commit is contained in:
Lauri Ojansivu 2017-09-24 16:45:01 +03:00
commit d974311d9b
5 changed files with 61 additions and 40 deletions

View file

@ -7,7 +7,8 @@ This release adds the following new features:
and fixes the following bugs: and fixes the following bugs:
* [Checklist items are lost when moving items to another checklist](https://github.com/wekan/wekan/pull/1240). * [Checklist items are lost when moving items to another checklist](https://github.com/wekan/wekan/pull/1240);
* [Keep state of checklist items when moved to another checklist](https://github.com/wekan/wekan/pull/1242).
Thanks to GitHub users GhassenRjab, umbertooo and xet7 for their contributions. Thanks to GitHub users GhassenRjab, umbertooo and xet7 for their contributions.

View file

@ -47,7 +47,7 @@ template(name="editChecklistItemForm")
template(name="checklistItems") template(name="checklistItems")
.checklist-items.js-checklist-items .checklist-items.js-checklist-items
each item in checklist.getItems each item in checklist.getItemsSorted
+inlinedForm(classNames="js-edit-checklist-item" item = item checklist = checklist) +inlinedForm(classNames="js-edit-checklist-item" item = item checklist = checklist)
+editChecklistItemForm(type = 'item' item = item checklist = checklist) +editChecklistItemForm(type = 'item' item = item checklist = checklist)
else else

View file

@ -20,24 +20,26 @@ function initSorting(items) {
}); });
items.sortable('cancel'); items.sortable('cancel');
const formerParent = ui.item.parents('.js-checklist-items'); const formerParent = ui.item.parents('.js-checklist-items');
let checklist = Blaze.getData(parent.get(0)).checklist; const checklist = Blaze.getData(parent.get(0)).checklist;
const oldChecklist = Blaze.getData(formerParent.get(0)).checklist; const oldChecklist = Blaze.getData(formerParent.get(0)).checklist;
if (oldChecklist._id !== checklist._id) { if (oldChecklist._id !== checklist._id) {
const currentItem = Blaze.getData(ui.item.get(0)).item; const currentItem = Blaze.getData(ui.item.get(0)).item;
for (let i = 0; i < orderedItems.length; i++) { for (let i = 0; i < orderedItems.length; i++) {
let itemId = orderedItems[i]; const itemId = orderedItems[i];
if (itemId !== currentItem._id) continue; if (itemId !== currentItem._id) continue;
checklist.addItem(currentItem.title); const newItem = {
checklist = Checklists.findOne({_id: checklist._id}); _id: checklist.getNewItemId(),
itemId = checklist._id + (checklist.newItemIndex - 1); title: currentItem.title,
if (currentItem.finished) { sort: i,
checklist.finishItem(itemId); isFinished: currentItem.isFinished,
} };
orderedItems[i] = itemId; checklist.addFullItem(newItem);
oldChecklist.removeItem(currentItem._id); orderedItems[i] = currentItem._id;
} oldChecklist.removeItem(itemId);
} }
} else {
checklist.sortItems(orderedItems); checklist.sortItems(orderedItems);
}
}, },
}); });
} }

View file

@ -44,11 +44,6 @@ Checklists.attachSchema(new SimpleSchema({
type: Number, type: Number,
decimal: true, decimal: true,
}, },
newItemIndex: {
type: Number,
decimal: true,
defaultValue: 0,
},
})); }));
const self = Checklists; const self = Checklists;
@ -57,16 +52,8 @@ Checklists.helpers({
itemCount() { itemCount() {
return this.items.length; return this.items.length;
}, },
getItems() { getItemsSorted() {
return this.items.sort(function (itemA, itemB) { return _.sortBy(this.items, 'sort');
if (itemA.sort < itemB.sort) {
return -1;
}
if (itemA.sort > itemB.sort) {
return 1;
}
return 0;
});
}, },
finishedCount() { finishedCount() {
return this.items.filter((item) => { return this.items.filter((item) => {
@ -83,6 +70,16 @@ Checklists.helpers({
const items = self.findOne({_id : this._id}).items; const items = self.findOne({_id : this._id}).items;
return _.pluck(items, '_id').indexOf(itemId); return _.pluck(items, '_id').indexOf(itemId);
}, },
getNewItemId() {
const itemCount = this.itemCount();
let idx = 0;
if (itemCount > 0) {
const lastId = this.items[itemCount - 1]._id;
const lastIdSuffix = lastId.substr(this._id.length);
idx = parseInt(lastIdSuffix, 10) + 1;
}
return `${this._id}${idx}`;
},
}); });
Checklists.allow({ Checklists.allow({
@ -112,14 +109,40 @@ Checklists.mutations({
}, },
//for items in checklist //for items in checklist
addItem(title) { addItem(title) {
const itemCount = this.itemCount(); const _id = this.getNewItemId();
const _id = `${this._id}${this.newItemIndex}`;
return { return {
$addToSet: { items: { _id, title, isFinished: false, sort: itemCount } }, $addToSet: {
$set: { newItemIndex: this.newItemIndex + 1}, items: {
_id, title,
isFinished: false,
sort: this.itemCount(),
},
},
}; };
}, },
addFullItem(item) {
const itemsUpdate = {};
this.items.forEach(function(iterItem, index) {
if (iterItem.sort >= item.sort) {
itemsUpdate[`items.${index}.sort`] = iterItem.sort + 1;
}
});
if (!_.isEmpty(itemsUpdate)) {
self.direct.update({ _id: this._id }, { $set: itemsUpdate });
}
return { $addToSet: { items: item } };
},
removeItem(itemId) { removeItem(itemId) {
const item = this.getItem(itemId);
const itemsUpdate = {};
this.items.forEach(function(iterItem, index) {
if (iterItem.sort > item.sort) {
itemsUpdate[`items.${index}.sort`] = iterItem.sort - 1;
}
});
if (!_.isEmpty(itemsUpdate)) {
self.direct.update({ _id: this._id }, { $set: itemsUpdate });
}
return { $pull: { items: { _id: itemId } } }; return { $pull: { items: { _id: itemId } } };
}, },
editItem(itemId, title) { editItem(itemId, title) {
@ -169,11 +192,11 @@ Checklists.mutations({
}, },
sortItems(itemIDs) { sortItems(itemIDs) {
const validItems = []; const validItems = [];
for (const itemID of itemIDs) { itemIDs.forEach((itemID) => {
if (this.getItem(itemID)) { if (this.getItem(itemID)) {
validItems.push(this.itemIndex(itemID)); validItems.push(this.itemIndex(itemID));
} }
} });
const modifiedValues = {}; const modifiedValues = {};
for (let i = 0; i < validItems.length; i++) { for (let i = 0; i < validItems.length; i++) {
modifiedValues[`items.${validItems[i]}.sort`] = i; modifiedValues[`items.${validItems[i]}.sort`] = i;

View file

@ -136,12 +136,7 @@ Migrations.add('add-sort-checklists', () => {
if (!checklist.hasOwnProperty('sort')) { if (!checklist.hasOwnProperty('sort')) {
Checklists.direct.update( Checklists.direct.update(
checklist._id, checklist._id,
{ { $set: { sort: index } },
$set: {
sort: index,
newItemIndex: checklist.items.length,
},
},
noValidate noValidate
); );
} }