mirror of
https://github.com/wekan/wekan.git
synced 2025-04-24 14:08:31 -04:00
Update Checklist model to keep checklist item state when moved
- Remove newItemIndex field (no longer needed) - Use Underscore.js to sort checklist items - Add helper method to generate new Items Ids so we can use it in model and in client - Add addFullItem method to add an item from object - Update sort when items are being added or removed
This commit is contained in:
parent
6e5d9f9bb6
commit
28eca2a11f
1 changed files with 44 additions and 21 deletions
|
@ -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;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue