From f3f845dde85d2a82e68ee1792793c31a0a874fe3 Mon Sep 17 00:00:00 2001 From: Jim Martens Date: Tue, 12 Sep 2017 10:36:42 +0200 Subject: Added support for sorted checklist items --- models/checklists.js | 57 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 9 deletions(-) (limited to 'models') diff --git a/models/checklists.js b/models/checklists.js index 2521412f..35ef8ae1 100644 --- a/models/checklists.js +++ b/models/checklists.js @@ -17,6 +17,10 @@ Checklists.attachSchema(new SimpleSchema({ 'items.$.title': { type: String, }, + 'items.$.sort': { + type: Number, + decimal: true, + }, 'items.$.isFinished': { type: Boolean, defaultValue: false, @@ -36,12 +40,34 @@ Checklists.attachSchema(new SimpleSchema({ } }, }, + sort: { + type: Number, + decimal: true, + }, + newItemIndex: { + type: Number, + decimal: true, + defaultValue: 0, + }, })); +const self = Checklists; + 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; + }); + }, finishedCount() { return this.items.filter((item) => { return item.isFinished; @@ -54,7 +80,8 @@ Checklists.helpers({ return _.findWhere(this.items, { _id }); }, itemIndex(itemId) { - return _.pluck(this.items, '_id').indexOf(itemId); + const items = self.findOne({_id : this._id}).items; + return _.pluck(items, '_id').indexOf(itemId); }, }); @@ -86,14 +113,11 @@ Checklists.mutations({ //for items in checklist addItem(title) { 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; - } - const _id = `${this._id}${idx}`; - return { $addToSet: { items: { _id, title, isFinished: false } } }; + const _id = `${this._id}${this.newItemIndex}`; + return { + $addToSet: { items: { _id, title, isFinished: false, sort: itemCount } }, + $set: { newItemIndex: this.newItemIndex + 1}, + }; }, removeItem(itemId) { return { $pull: { items: { _id: itemId } } }; @@ -143,6 +167,21 @@ Checklists.mutations({ } return {}; }, + sortItems(itemIDs) { + const validItems = []; + for (const itemID of itemIDs) { + 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) { -- cgit v1.2.3-1-g7c22