From 125c9c13c1343334e769836a0cc4cd05aaab2c26 Mon Sep 17 00:00:00 2001 From: cahrens Date: Sun, 12 May 2013 15:45:31 -0400 Subject: [PATCH] Got clearing of fields working. --- cms/static/js/models/metadata_model.js | 41 ++++++- cms/static/js/views/metadata_editor_view.js | 115 ++++++++++++++++++++ cms/static/js/views/metadata_number_view.js | 14 --- cms/static/js/views/metadata_option_view.js | 37 ------- cms/static/js/views/metadata_string_view.js | 33 ------ cms/templates/base.html | 3 - 6 files changed, 155 insertions(+), 88 deletions(-) delete mode 100644 cms/static/js/views/metadata_number_view.js delete mode 100644 cms/static/js/views/metadata_option_view.js delete mode 100644 cms/static/js/views/metadata_string_view.js diff --git a/cms/static/js/models/metadata_model.js b/cms/static/js/models/metadata_model.js index 4349fa7187..4aa10f57d1 100644 --- a/cms/static/js/models/metadata_model.js +++ b/cms/static/js/models/metadata_model.js @@ -4,10 +4,49 @@ CMS.Models.Metadata = Backbone.Model.extend({ defaults: { "display_name": null, - "value" : null + "value" : null, + "explicitly_set": null, + "default_value" : null + }, + + initialize: function() { + this.original_value = this.get('value'); + this.original_explicitly_set = this.get('explicitly_set'); }, getOriginalValue: function() { + return this.originalValue; + }, + + isModified : function() { + if (!this.get('explicitly_set') && !this.original_explicitly_set) { + return false; + } + if (this.get('explicitly_set') && this.original_explicitly_set) { + return this.get('value') !== this.original_value; + } + return true; + }, + + isExplicitlySet: function() { + return this.get('explicitly_set'); + }, + + getDisplayValue : function () { return this.get('value'); + }, + + getValue: function() { + return this.get('explicitly_set') ? this.get('value') : null; + }, + + setValue: function (value) { + this.set('explicitly_set', true); + this.set('value', value); + }, + + clear: function() { + this.set('explicitly_set', false); + this.set('value', this.get('default_value')); } }); diff --git a/cms/static/js/views/metadata_editor_view.js b/cms/static/js/views/metadata_editor_view.js index 4e35deba9f..6c2081977c 100644 --- a/cms/static/js/views/metadata_editor_view.js +++ b/cms/static/js/views/metadata_editor_view.js @@ -49,3 +49,118 @@ CMS.Views.Metadata.Editor = Backbone.View.extend({ return modified_values; } }); + +CMS.Views.Metadata.AbstractEditor = Backbone.View.extend({ + + initialize : function() { + var self = this; + var templateName = this.getTemplateName(); + this.uniqueId = _.uniqueId(templateName + "_"); + window.templateLoader.loadRemoteTemplate(templateName, + "/static/client_templates/" + templateName + ".html", + function (raw_template) { + self.template = _.template(raw_template); + self.$el.append(self.template({model: self.model, uniqueId: self.uniqueId})); + self.render(); + } + ); + }, + + getTemplateName : function () {}, + + getValueFromEditor : function () {}, + + setValueInEditor : function (value) {}, + + updateModel: function () { + this.model.setValue(this.getValueFromEditor()); + this.render(); + }, + + clear: function () { + this.model.clear(); + this.render(); + }, + + showClearButton: function() { + if (!this.$el.hasClass('is-set')) { + this.$el.addClass('is-set'); + // TODO: can we use toggleclass? + this.$el.find('.setting-clear').removeClass('inactive'); + this.$el.find('.setting-clear').addClass('active'); + } + }, + + render: function () { + if (!this.template) return; + + this.setValueInEditor(this.model.getDisplayValue()); + + if (this.model.isExplicitlySet()) { + this.showClearButton(); + } + else { + this.$el.removeClass('is-set'); + // TODO: can we use toggleclass? + this.$el.find('.setting-clear').addClass('inactive'); + this.$el.find('.setting-clear').removeClass('active'); + } + }, + + + modified: function () { + return this.model.isModified(); + }, + + getValue: function() { + return this.model.getValue(); + } +}); + +CMS.Views.Metadata.String = CMS.Views.Metadata.AbstractEditor.extend({ + + events : { + "change input" : "updateModel", + "keypress .setting-input" : "showClearButton" , + "click .setting-clear" : "clear" + }, + + getTemplateName : function () { + return "metadata_string_entry"; + }, + + getValueFromEditor : function () { + var val = this.$el.find('#' + this.uniqueId).val(); +// TODO: not sure this is necessary. Trying to support empty value (""). + return val ? val : ""; + }, + + setValueInEditor : function (value) { + this.$el.find('input').val(value); + } +}); + +CMS.Views.Metadata.Option = CMS.Views.Metadata.AbstractEditor.extend({ + + events : { + "change select" : "updateModel", + "click .setting-clear" : "clear" + }, + + getTemplateName : function () { + return "metadata_option_entry"; + }, + + getValueFromEditor : function () { + return this.$el.find('#' + this.uniqueId).find(":selected").text(); + }, + + setValueInEditor : function (value) { + $('#' + this.uniqueId + " option").filter(function() { + return $(this).text() === value; + }).prop('selected', true); + } +}); + + + diff --git a/cms/static/js/views/metadata_number_view.js b/cms/static/js/views/metadata_number_view.js deleted file mode 100644 index a1be53eafa..0000000000 --- a/cms/static/js/views/metadata_number_view.js +++ /dev/null @@ -1,14 +0,0 @@ -if (!CMS.Views['Metadata']) CMS.Views.Metadata = {}; - -CMS.Views.Metadata.Number = CMS.Views.Metadata.String.extend({ - - getValue: function() { - var stringVal = this.$el.find('#' + this.uniqueId).val(); - if (this.isInteger) { - return parseInt(stringVal) - } - else { - return parseFloat(stringVal) - } - } -}); \ No newline at end of file diff --git a/cms/static/js/views/metadata_option_view.js b/cms/static/js/views/metadata_option_view.js deleted file mode 100644 index 28c49fb3d4..0000000000 --- a/cms/static/js/views/metadata_option_view.js +++ /dev/null @@ -1,37 +0,0 @@ -if (!CMS.Views['Metadata']) CMS.Views.Metadata = {}; - -CMS.Views.Metadata.Option = Backbone.View.extend({ - - // Model class ... - events : { - }, - - initialize : function() { - var self = this; - this.uniqueId = _.uniqueId('metadata_option_entry_'); - // instantiates an editor template for each update in the collection - window.templateLoader.loadRemoteTemplate("metadata_option_entry", - "/static/client_templates/metadata_option_entry.html", - function (raw_template) { - self.template = _.template(raw_template); - self.$el.append(self.template({model: self.model, uniqueId: self.uniqueId})); - $('#' + self.uniqueId + " option").filter(function() { - return $(this).text() === self.model.get('value'); - }).prop('selected', true); - if (self.model.get('explicitly_set')) { - self.$el.addClass('is-set'); - self.$el.find('#'+self.uniqueId + " .setting-clear").addClass('active'); - } - - } - ); - }, - - modified: function () { - return this.getValue() !== this.model.getOriginalValue(); - }, - - getValue: function() { - return this.$el.find('#' + this.uniqueId).find(":selected").text(); - } -}); diff --git a/cms/static/js/views/metadata_string_view.js b/cms/static/js/views/metadata_string_view.js deleted file mode 100644 index 465b75ba8b..0000000000 --- a/cms/static/js/views/metadata_string_view.js +++ /dev/null @@ -1,33 +0,0 @@ -if (!CMS.Views['Metadata']) CMS.Views.Metadata = {}; - -CMS.Views.Metadata.String = Backbone.View.extend({ - - // Model class ... - events : { - }, - - initialize : function() { - var self = this; - this.uniqueId = _.uniqueId('metadata_string_entry_'); - // instantiates an editor template for each update in the collection - window.templateLoader.loadRemoteTemplate("metadata_string_entry", - "/static/client_templates/metadata_string_entry.html", - function (raw_template) { - self.template = _.template(raw_template); - self.$el.append(self.template({model: self.model, uniqueId: self.uniqueId})); - if (self.model.get('explicitly_set')) { - self.$el.addClass('is-set'); - self.$el.find('#'+self.uniqueId + " .setting-clear").addClass('active'); - } - } - ); - }, - - modified: function () { - return this.getValue() !== this.model.getOriginalValue(); - }, - - getValue: function() { - return this.$el.find('#' + this.uniqueId).val(); - } -}); diff --git a/cms/templates/base.html b/cms/templates/base.html index 6649affefb..88bbc56677 100644 --- a/cms/templates/base.html +++ b/cms/templates/base.html @@ -52,9 +52,6 @@ - - -