diff options
author | amadilsons <joao.amado.95@gmail.com> | 2017-09-27 14:42:18 +0200 |
---|---|---|
committer | amadilsons <joao.amado.95@gmail.com> | 2017-09-27 14:42:18 +0200 |
commit | 75f15f1be218bbfc4a06c0c57c4b1043091b5dc8 (patch) | |
tree | ef94de1a5a5ed5f975d00423075014987f6b6bc4 /models | |
parent | bbe37cfad83f261e2c24cca3b4f31baa0e818f58 (diff) | |
parent | eb945f26a3be316fb2ae4452d6db45a11f8b91d4 (diff) | |
download | wekan-75f15f1be218bbfc4a06c0c57c4b1043091b5dc8.tar.gz wekan-75f15f1be218bbfc4a06c0c57c4b1043091b5dc8.tar.bz2 wekan-75f15f1be218bbfc4a06c0c57c4b1043091b5dc8.zip |
Merge branch 'master' of https://github.com/wekan/wekan into issue881
Diffstat (limited to 'models')
-rw-r--r-- | models/activities.js | 4 | ||||
-rw-r--r-- | models/checklists.js | 65 |
2 files changed, 48 insertions, 21 deletions
diff --git a/models/activities.js b/models/activities.js index c732a940..4ddcfa72 100644 --- a/models/activities.js +++ b/models/activities.js @@ -119,6 +119,10 @@ if (Meteor.isServer) { const checklist = activity.checklist(); params.checklist = checklist.title; } + if (activity.checklistItemId) { + const checklistItem = activity.checklistItem(); + params.checklistItem = checklistItem.title; + } if (board) { const watchingUsers = _.pluck(_.where(board.watchers, {level: 'watching'}), 'userId'); const trackingUsers = _.pluck(_.where(board.watchers, {level: 'tracking'}), 'userId'); diff --git a/models/checklists.js b/models/checklists.js index 35ef8ae1..b90656e4 100644 --- a/models/checklists.js +++ b/models/checklists.js @@ -44,11 +44,6 @@ Checklists.attachSchema(new SimpleSchema({ type: Number, decimal: true, }, - newItemIndex: { - type: Number, - decimal: true, - defaultValue: 0, - }, })); const self = Checklists; @@ -57,16 +52,8 @@ Checklists.helpers({ itemCount() { return this.items.length; }, - getItems() { - return this.items.sort(function (itemA, itemB) { - if (itemA.sort < itemB.sort) { - return -1; - } - if (itemA.sort > itemB.sort) { - return 1; - } - return 0; - }); + getItemsSorted() { + return _.sortBy(this.items, 'sort'); }, finishedCount() { return this.items.filter((item) => { @@ -83,6 +70,16 @@ Checklists.helpers({ const items = self.findOne({_id : this._id}).items; 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({ @@ -112,14 +109,40 @@ Checklists.mutations({ }, //for items in checklist addItem(title) { - const itemCount = this.itemCount(); - const _id = `${this._id}${this.newItemIndex}`; + const _id = this.getNewItemId(); return { - $addToSet: { items: { _id, title, isFinished: false, sort: itemCount } }, - $set: { newItemIndex: this.newItemIndex + 1}, + $addToSet: { + 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) { + 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 } } }; }, editItem(itemId, title) { @@ -169,11 +192,11 @@ Checklists.mutations({ }, sortItems(itemIDs) { const validItems = []; - for (const itemID of itemIDs) { + itemIDs.forEach((itemID) => { if (this.getItem(itemID)) { validItems.push(this.itemIndex(itemID)); } - } + }); const modifiedValues = {}; for (let i = 0; i < validItems.length; i++) { modifiedValues[`items.${validItems[i]}.sort`] = i; |