From 758d350f78d46e1884b14105800f8b6e302299e2 Mon Sep 17 00:00:00 2001 From: Maxime Quandalle Date: Tue, 16 Jun 2015 22:41:19 +0200 Subject: Fix #199 --- client/components/cards/labels.js | 100 +++++++++++++++++++++----------------- 1 file changed, 55 insertions(+), 45 deletions(-) (limited to 'client/components/cards/labels.js') diff --git a/client/components/cards/labels.js b/client/components/cards/labels.js index 36a35f82..731e6367 100644 --- a/client/components/cards/labels.js +++ b/client/components/cards/labels.js @@ -1,3 +1,49 @@ + +var labelColors; +Meteor.startup(function() { + labelColors = Boards.simpleSchema()._schema['labels.$.color'].allowedValues; +}); + +BlazeComponent.extendComponent({ + template: function() { + return 'formLabel'; + }, + + onCreated: function() { + this.currentColor = new ReactiveVar(this.data().color); + }, + + labels: function() { + return _.map(labelColors, function(color) { + return { color: color, name: '' }; + }); + }, + + isSelected: function(color) { + return this.currentColor.get() === color; + }, + + events: function() { + return [{ + 'click .js-palette-color': function() { + this.currentColor.set(this.currentData().color); + } + }]; + } +}).register('formLabel'); + +Template.createLabelPopup.helpers({ + // This is the default color for a new label. We search the first color that + // is not already used in the board (although it's not a problem if two + // labels have the same color). + defaultColor: function() { + var labels = this.labels || this.card.board().labels; + var usedColors = _.pluck(labels, 'color'); + var availableColors = _.difference(labelColors, usedColors); + return availableColors.length > 1 ? availableColors[0] : labelColors[0]; + } +}); + Template.cardLabelsPopup.events({ 'click .js-select-label': function(evt) { var cardId = Template.parentData(2).data._id; @@ -36,17 +82,18 @@ Template.createLabelPopup.events({ 'submit .create-label': function(evt, tpl) { var name = tpl.$('#labelName').val().trim(); var boardId = Session.get('currentBoard'); - var selectLabelDom = tpl.$('.js-palette-select').get(0); - var selectLabel = Blaze.getData(selectLabelDom); + var color = Blaze.getData(tpl.find('.fa-check')).color; + Boards.update(boardId, { $push: { labels: { _id: Random.id(6), name: name, - color: selectLabel.color + color: color } } }); + Popup.back(); evt.preventDefault(); } @@ -62,35 +109,23 @@ Template.editLabelPopup.events({ } } }); + Popup.back(2); }), 'submit .edit-label': function(evt, tpl) { + evt.preventDefault(); var name = tpl.$('#labelName').val().trim(); var boardId = Session.get('currentBoard'); var getLabel = Utils.getLabelIndex(boardId, this._id); - var selectLabelDom = tpl.$('.js-palette-select').get(0); - var selectLabel = Blaze.getData(selectLabelDom); - var $set = {}; + var color = Blaze.getData(tpl.find('.fa-check')).color; - // set label index + var $set = {}; $set[getLabel.key('name')] = name; + $set[getLabel.key('color')] = color; - // set color - $set[getLabel.key('color')] = selectLabel.color; - - // update Boards.update(boardId, { $set: $set }); - // return to the previous popup view trigger Popup.back(); - - evt.preventDefault(); - }, - 'click .js-select-label': function() { - Cards.remove(this.cardId); - - // redirect board - Utils.goBoardId(this.boardId); } }); @@ -99,28 +134,3 @@ Template.cardLabelsPopup.helpers({ return _.contains(Cards.findOne(cardId).labelIds, this._id); } }); - -var labelColors; -Meteor.startup(function() { - labelColors = Boards.simpleSchema()._schema['labels.$.color'].allowedValues; -}); - -Template.createLabelPopup.helpers({ - // This is the default color for a new label. We search the first color that - // is not already used in the board (although it's not a problem if two - // labels have the same color). - defaultColor: function() { - var labels = this.labels || this.card.board().labels; - var usedColors = _.pluck(labels, 'color'); - var availableColors = _.difference(labelColors, usedColors); - return availableColors.length > 1 ? availableColors[0] : labelColors[0]; - } -}); - -Template.formLabel.helpers({ - labels: function() { - return _.map(labelColors, function(color) { - return { color: color, name: '' }; - }); - } -}); -- cgit v1.2.3-1-g7c22