diff options
Diffstat (limited to 'models/boards.js')
-rw-r--r-- | models/boards.js | 46 |
1 files changed, 33 insertions, 13 deletions
diff --git a/models/boards.js b/models/boards.js index 4baec280..98d6ec77 100644 --- a/models/boards.js +++ b/models/boards.js @@ -92,12 +92,16 @@ Boards.helpers({ return _.where(this.members, {isActive: true}); }, + getLabel(name, color) { + return _.findWhere(this.labels, { name, color }); + }, + labelIndex(labelId) { - return _.indexOf(_.pluck(this.labels, '_id'), labelId); + return _.pluck(this.labels, '_id').indexOf(labelId); }, memberIndex(memberId) { - return _.indexOf(_.pluck(this.members, 'userId'), memberId); + return _.pluck(this.members, 'userId').indexOf(memberId); }, absoluteUrl() { @@ -107,6 +111,14 @@ Boards.helpers({ colorClass() { return `board-color-${this.color}`; }, + + // XXX currently mutations return no value so we have an issue when using addLabel in import + // XXX waiting on https://github.com/mquandalle/meteor-collection-mutations/issues/1 to remove... + pushLabel(name, color) { + const _id = Random.id(6); + Boards.direct.update(this._id, { $push: {labels: { _id, name, color }}}); + return _id; + }, }); Boards.mutations({ @@ -131,18 +143,26 @@ Boards.mutations({ }, addLabel(name, color) { - const _id = Random.id(6); - return { $push: {labels: { _id, name, color }}}; + // If label with the same name and color already exists we don't want to + // create another one because they would be indistinguishable in the UI + // (they would still have different `_id` but that is not exposed to the + // user). + if (!this.getLabel(name, color)) { + const _id = Random.id(6); + return { $push: {labels: { _id, name, color }}}; + } }, editLabel(labelId, name, color) { - const labelIndex = this.labelIndex(labelId); - return { - $set: { - [`labels.${labelIndex}.name`]: name, - [`labels.${labelIndex}.color`]: color, - }, - }; + if (!this.getLabel(name, color)) { + const labelIndex = this.labelIndex(labelId); + return { + $set: { + [`labels.${labelIndex}.name`]: name, + [`labels.${labelIndex}.color`]: color, + }, + }; + } }, removeLabel(labelId) { @@ -259,7 +279,7 @@ Boards.before.insert((userId, doc) => { // Handle labels const colors = Boards.simpleSchema()._schema['labels.$.color'].allowedValues; const defaultLabelsColors = _.clone(colors).splice(0, 6); - doc.labels = _.map(defaultLabelsColors, (color) => { + doc.labels = defaultLabelsColors.map((color) => { return { color, _id: Random.id(6), @@ -307,7 +327,7 @@ if (Meteor.isServer) { { boardId: doc._id }, { $pull: { - labels: removedLabelId, + labelIds: removedLabelId, }, }, { multi: true } |