Commit cc63f9c2 by cahrens

Unique IDs, other cleanup.

parent 4b850f4d
<li class="field-group course-advanced-policy-list-item"> <li class="field-group course-advanced-policy-list-item">
<div class="field text key" id="<%= (_.isEmpty(key) ? '__new_advanced_key__' : key) %>"> <div class="field text key" id="<%= (_.isEmpty(key) ? '__new_advanced_key__' : key) %>">
<label for="course-advanced-policy-key">Policy Key:</label> <label for="<%= keyUniqueId %>">Policy Key:</label>
<input type="text" class="short" id="course-advanced-policy-key" value="<%= key %>" /> <input type="text" class="short policy-key" id="<%= keyUniqueId %>" value="<%= key %>" />
<span class="tip tip-stacked">Keys are case sensitive and cannot contain spaces or start with a number</span> <span class="tip tip-stacked">Keys are case sensitive and cannot contain spaces or start with a number</span>
</div> </div>
<div class="field text value"> <div class="field text value">
<label for="course-advanced-policy-value">Policy Value:</label> <label for="<%= valueUniqueId %>">Policy Value:</label>
<textarea class="json text" id="course-advanced-policy-value"><%= value %></textarea> <textarea class="json text" id="<%= valueUniqueId %>"><%= value %></textarea>
</div> </div>
<div class="actions"> <div class="actions">
......
...@@ -7,12 +7,10 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({ ...@@ -7,12 +7,10 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({
// Model class is CMS.Models.Settings.Advanced // Model class is CMS.Models.Settings.Advanced
events : { events : {
'click .delete-button' : "deleteEntry", 'click .delete-button' : "deleteEntry",
// 'click .save-button' : "saveView", TODO: put back once Advanced is not in tab view
// 'click .cancel-button' : "revertView", with current code, must attach listener in initialize method
'click .new-button' : "addEntry", 'click .new-button' : "addEntry",
// update model on changes // update model on changes
'change #course-advanced-policy-key' : "updateKey", 'change .policy-key' : "updateKey",
'keydown #course-advanced-policy-key' : "showSaveCancelButtons" 'keydown .policy-key' : "showSaveCancelButtons"
// TODO enable/disable save based on validation (currently enabled whenever there are changes) // TODO enable/disable save based on validation (currently enabled whenever there are changes)
}, },
initialize : function() { initialize : function() {
...@@ -43,7 +41,7 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({ ...@@ -43,7 +41,7 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({
var self = this; var self = this;
_.each(_.sortBy(_.keys(this.model.attributes), _.identity), _.each(_.sortBy(_.keys(this.model.attributes), _.identity),
function(key) { function(key) {
listEle$.append(self.template({ key : key, value : JSON.stringify(self.model.get(key), null, 4)})); listEle$.append(self.getTemplate(key, self.model.get(key)));
self.fieldToSelectorMap[key] = key; self.fieldToSelectorMap[key] = key;
}); });
var policyValues = listEle$.find('.json'); var policyValues = listEle$.find('.json');
...@@ -54,8 +52,7 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({ ...@@ -54,8 +52,7 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({
attachJSONEditor : function (textarea) { attachJSONEditor : function (textarea) {
// Since we are allowing duplicate keys at the moment, it is possible that we will try to attach // Since we are allowing duplicate keys at the moment, it is possible that we will try to attach
// JSON Editor to a value that already has one. Therefore only attach if no CodeMirror peer exists. // JSON Editor to a value that already has one. Therefore only attach if no CodeMirror peer exists.
var siblings = $(textarea).siblings(); if ( $(textarea).siblings().hasClass('CodeMirror')) {
if (siblings.length >= 1 && $(siblings[0]).hasClass('CodeMirror')) {
return; return;
} }
...@@ -185,10 +182,8 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({ ...@@ -185,10 +182,8 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({
}, },
addEntry : function() { addEntry : function() {
var listEle$ = this.$el.find('.course-advanced-policy-list'); var listEle$ = this.$el.find('.course-advanced-policy-list');
var newEle = this.template({ key : "", value : JSON.stringify("")}); var newEle = this.getTemplate("", "");
listEle$.append(newEle); listEle$.append(newEle);
// disable the value entry until there's an acceptable key
$(newEle).find('.course-advanced-policy-value').addClass('disabled');
this.fieldToSelectorMap[this.model.new_key] = this.model.new_key; this.fieldToSelectorMap[this.model.new_key] = this.model.new_key;
// need to re-find b/c replaceWith seems to copy rather than use the specific ele instance // need to re-find b/c replaceWith seems to copy rather than use the specific ele instance
var policyValueDivs = this.$el.find('#' + this.model.new_key).closest('li').find('.json'); var policyValueDivs = this.$el.find('#' + this.model.new_key).closest('li').find('.json');
...@@ -198,9 +193,10 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({ ...@@ -198,9 +193,10 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({
this.showSaveCancelButtons(); this.showSaveCancelButtons();
}, },
updateKey : function(event) { updateKey : function(event) {
var parentElement = $(event.currentTarget).closest('.key');
// old key: either the key as in the model or new_key. // old key: either the key as in the model or new_key.
// That is, it doesn't change as the val changes until val is accepted. // That is, it doesn't change as the val changes until val is accepted.
var oldKey = $(event.currentTarget).closest('.key').attr('id'); var oldKey = parentElement.attr('id');
// TODO: validation of keys with spaces. For now at least trim strings to remove initial and // TODO: validation of keys with spaces. For now at least trim strings to remove initial and
// trailing whitespace // trailing whitespace
var newKey = $.trim($(event.currentTarget).val()); var newKey = $.trim($(event.currentTarget).val());
...@@ -257,15 +253,8 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({ ...@@ -257,15 +253,8 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({
this.model.deleteKeys.splice(wasDeleting, 1); this.model.deleteKeys.splice(wasDeleting, 1);
} }
// update gui (sets all the ids etc) // Update the ID to the new value.
var newEle = this.template({key : newKey, value : JSON.stringify(this.model.get(newKey)) }); parentElement.attr('id', newKey);
$(event.currentTarget).closest('li').replaceWith(newEle);
// need to re-find b/c replaceWith seems to copy rather than use the specific ele instance
var policyValueDivs = this.$el.find('#' + newKey).closest('li').find('.json');
// Because we are not dis-allowing duplicate key definitions, we may get back more than one
// div. But attachJSONEditor will only attach editor if it is not already defined.
_.each(policyValueDivs, this.attachJSONEditor, this);
this.fieldToSelectorMap[newKey] = newKey; this.fieldToSelectorMap[newKey] = newKey;
} }
...@@ -282,5 +271,10 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({ ...@@ -282,5 +271,10 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({
// } // }
// else return true; // else return true;
return true; return true;
},
getTemplate: function (key, value) {
return this.template({ key : key, value : JSON.stringify(value, null, 4),
keyUniqueId: _.uniqueId('policy_key_'), valueUniqueId: _.uniqueId('policy_value_')});
} }
}); });
\ No newline at end of file
...@@ -54,11 +54,11 @@ editor.render(); ...@@ -54,11 +54,11 @@ editor.render();
<article class="content-primary" role="main"> <article class="content-primary" role="main">
<form id="settings_advanced" class="settings-advanced" method="post" action=""> <form id="settings_advanced" class="settings-advanced" method="post" action="">
<div class="message message-status confirm is-shown"> <div class="message message-status confirm">
Your policy changes have been saved. Your policy changes have been saved.
</div> </div>
<div class="message message-status error is-shown"> <div class="message message-status error">
There was an error saving your information. Please see below. There was an error saving your information. Please see below.
</div> </div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment