diff options
author | Lauri Ojansivu <x@xet7.org> | 2018-03-20 22:58:22 +0200 |
---|---|---|
committer | Lauri Ojansivu <x@xet7.org> | 2018-03-20 22:58:22 +0200 |
commit | a7855f3f582d193b0a6dbd70b5da6f59384f9520 (patch) | |
tree | 8bb0c9d19a23dfb374a251241ba83f8e7b448bae /models | |
parent | 90f3f7ba163ef5d015bc473946b02c23e263a011 (diff) | |
parent | ac19904ee86541165b0017dcaaee33629904806b (diff) | |
download | wekan-a7855f3f582d193b0a6dbd70b5da6f59384f9520.tar.gz wekan-a7855f3f582d193b0a6dbd70b5da6f59384f9520.tar.bz2 wekan-a7855f3f582d193b0a6dbd70b5da6f59384f9520.zip |
Merge branch 'andresmanelli-checklistItems' into devel
Diffstat (limited to 'models')
-rw-r--r-- | models/activities.js | 2 | ||||
-rw-r--r-- | models/cards.js | 2 | ||||
-rw-r--r-- | models/checklistItems.js | 95 | ||||
-rw-r--r-- | models/checklists.js | 172 |
4 files changed, 106 insertions, 165 deletions
diff --git a/models/activities.js b/models/activities.js index bd33303a..3f1d28ae 100644 --- a/models/activities.js +++ b/models/activities.js @@ -42,7 +42,7 @@ Activities.helpers({ return Checklists.findOne(this.checklistId); }, checklistItem() { - return Checklists.findOne(this.checklistId).getItem(this.checklistItemId); + return ChecklistItems.findOne(this.checklistItemId); }, }); diff --git a/models/cards.js b/models/cards.js index 544afca5..8fd15488 100644 --- a/models/cards.js +++ b/models/cards.js @@ -155,7 +155,7 @@ Cards.helpers({ }, checklists() { - return Checklists.find({cardId: this._id}, {sort: {createdAt: 1}}); + return Checklists.find({cardId: this._id}, {sort: { sort: 1 } }); }, checklistItemCount() { diff --git a/models/checklistItems.js b/models/checklistItems.js new file mode 100644 index 00000000..3c01d476 --- /dev/null +++ b/models/checklistItems.js @@ -0,0 +1,95 @@ +ChecklistItems = new Mongo.Collection('checklistItems'); + +ChecklistItems.attachSchema(new SimpleSchema({ + title: { + type: String, + }, + sort: { + type: Number, + decimal: true, + }, + isFinished: { + type: Boolean, + defaultValue: false, + }, + checklistId: { + type: String, + }, + cardId: { + type: String, + }, +})); + +ChecklistItems.allow({ + insert(userId, doc) { + return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId)); + }, + update(userId, doc) { + return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId)); + }, + remove(userId, doc) { + return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId)); + }, + fetch: ['userId', 'cardId'], +}); + +ChecklistItems.before.insert((userId, doc) => { + if (!doc.userId) { + doc.userId = userId; + } +}); + +// Mutations +ChecklistItems.mutations({ + setTitle(title) { + return { $set: { title } }; + }, + toggleItem() { + return { $set: { isFinished: !this.isFinished } }; + }, + move(checklistId, sortIndex) { + const cardId = Checklists.findOne(checklistId).cardId; + const mutatedFields = { + cardId, + checklistId, + sort: sortIndex, + }; + + return {$set: mutatedFields}; + }, +}); + +// Activities helper +function itemCreation(userId, doc) { + const card = Cards.findOne(doc.cardId); + const boardId = card.boardId; + Activities.insert({ + userId, + activityType: 'addChecklistItem', + cardId: doc.cardId, + boardId, + checklistId: doc.checklistId, + checklistItemId: doc._id, + }); +} + +function itemRemover(userId, doc) { + Activities.remove({ + checklistItemId: doc._id, + }); +} + +// Activities +if (Meteor.isServer) { + Meteor.startup(() => { + ChecklistItems._collection._ensureIndex({ checklistId: 1 }); + }); + + ChecklistItems.after.insert((userId, doc) => { + itemCreation(userId, doc); + }); + + ChecklistItems.after.remove((userId, doc) => { + itemRemover(userId, doc); + }); +} diff --git a/models/checklists.js b/models/checklists.js index 7eb0a24f..637e280c 100644 --- a/models/checklists.js +++ b/models/checklists.js @@ -7,24 +7,6 @@ Checklists.attachSchema(new SimpleSchema({ title: { type: String, }, - items: { - type: [Object], - defaultValue: [], - }, - 'items.$._id': { - type: String, - }, - 'items.$.title': { - type: String, - }, - 'items.$.sort': { - type: Number, - decimal: true, - }, - 'items.$.isFinished': { - type: Boolean, - defaultValue: false, - }, finishedAt: { type: Date, optional: true, @@ -46,40 +28,28 @@ Checklists.attachSchema(new SimpleSchema({ }, })); -const self = Checklists; - Checklists.helpers({ itemCount() { - return this.items.length; + return ChecklistItems.find({ checklistId: this._id }).count(); }, - getItemsSorted() { - return _.sortBy(this.items, 'sort'); + items() { + return ChecklistItems.find(Filter.mongoSelector({ + checklistId: this._id, + }), { sort: ['sort'] }); }, finishedCount() { - return this.items.filter((item) => { - return item.isFinished; - }).length; + return ChecklistItems.find({ + checklistId: this._id, + isFinished: true, + }).count(); }, isFinished() { return 0 !== this.itemCount() && this.itemCount() === this.finishedCount(); }, - getItem(_id) { - return _.findWhere(this.items, { _id }); - }, itemIndex(itemId) { 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({ @@ -103,108 +73,9 @@ Checklists.before.insert((userId, doc) => { }); Checklists.mutations({ - //for checklist itself setTitle(title) { return { $set: { title } }; }, - //for items in checklist - addItem(title) { - const _id = this.getNewItemId(); - return { - $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) { - if (this.getItem(itemId)) { - const itemIndex = this.itemIndex(itemId); - return { - $set: { - [`items.${itemIndex}.title`]: title, - }, - }; - } - return {}; - }, - finishItem(itemId) { - if (this.getItem(itemId)) { - const itemIndex = this.itemIndex(itemId); - return { - $set: { - [`items.${itemIndex}.isFinished`]: true, - }, - }; - } - return {}; - }, - resumeItem(itemId) { - if (this.getItem(itemId)) { - const itemIndex = this.itemIndex(itemId); - return { - $set: { - [`items.${itemIndex}.isFinished`]: false, - }, - }; - } - return {}; - }, - toggleItem(itemId) { - const item = this.getItem(itemId); - if (item) { - const itemIndex = this.itemIndex(itemId); - return { - $set: { - [`items.${itemIndex}.isFinished`]: !item.isFinished, - }, - }; - } - return {}; - }, - sortItems(itemIDs) { - const validItems = []; - 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; - } - return { - $set: modifiedValues, - }; - }, }); if (Meteor.isServer) { @@ -222,30 +93,6 @@ if (Meteor.isServer) { }); }); - //TODO: so there will be no activity for adding item into checklist, maybe will be implemented in the future. - // The future is now - Checklists.after.update((userId, doc, fieldNames, modifier) => { - if (fieldNames.includes('items')) { - if (modifier.$addToSet) { - Activities.insert({ - userId, - activityType: 'addChecklistItem', - cardId: doc.cardId, - boardId: Cards.findOne(doc.cardId).boardId, - checklistId: doc._id, - checklistItemId: modifier.$addToSet.items._id, - }); - } else if (modifier.$pull) { - const activity = Activities.findOne({ - checklistItemId: modifier.$pull.items._id, - }); - if (activity) { - Activities.remove(activity._id); - } - } - } - }); - Checklists.before.remove((userId, doc) => { const activities = Activities.find({ checklistId: doc._id }); if (activities) { @@ -256,7 +103,6 @@ if (Meteor.isServer) { }); } -//CARD COMMENT REST API if (Meteor.isServer) { JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/checklists', function (req, res) { try { |