Commit 1ccf0d48 by Jay Zoldak

Merge branch 'feature/cas/manual-policy-merged' of github.com:MITx/mitx into…

Merge branch 'feature/cas/manual-policy-merged' of github.com:MITx/mitx into feature/cas/manual-policy-merged
parents 05f32f4b 8acbeec3
if (!CMS.Models['Settings']) CMS.Models.Settings = {}; if (!CMS.Models['Settings']) CMS.Models.Settings = {};
CMS.Models.Settings.Advanced = Backbone.Model.extend({ CMS.Models.Settings.Advanced = Backbone.Model.extend({
// the key for a newly added policy-- before the user has entered a key value
new_key : "__new_advanced_key__",
defaults: { defaults: {
// the properties are whatever the user types in (in addition to whatever comes originally from the server) // the properties are whatever the user types in (in addition to whatever comes originally from the server)
}, },
...@@ -11,7 +14,10 @@ CMS.Models.Settings.Advanced = Backbone.Model.extend({ ...@@ -11,7 +14,10 @@ CMS.Models.Settings.Advanced = Backbone.Model.extend({
validate: function (attrs) { validate: function (attrs) {
var errors = {}; var errors = {};
for (var key in attrs) { for (var key in attrs) {
if (_.contains(this.blacklistKeys, key)) { if (key === this.new_key || _.isEmpty(key)) {
errors[key] = "A key must be entered.";
}
else if (_.contains(this.blacklistKeys, key)) {
errors[key] = key + " is a reserved keyword or has another editor"; errors[key] = key + " is a reserved keyword or has another editor";
} }
} }
......
if (!CMS.Views['Settings']) CMS.Views.Settings = {}; if (!CMS.Views['Settings']) CMS.Views.Settings = {};
CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({
// the key for a newly added policy-- before the user has entered a key value
new_key : "__new_advanced_key__",
error_saving : "error_saving", error_saving : "error_saving",
successful_changes: "successful_changes", successful_changes: "successful_changes",
...@@ -54,6 +52,13 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({ ...@@ -54,6 +52,13 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({
return this; return this;
}, },
attachJSONEditor : function (textarea) { attachJSONEditor : function (textarea) {
// 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.
var siblings = $(textarea).siblings();
if (siblings.length >= 1 && $(siblings[0]).hasClass('CodeMirror')) {
return;
}
var self = this; var self = this;
CodeMirror.fromTextArea(textarea, { CodeMirror.fromTextArea(textarea, {
mode: "application/json", lineNumbers: false, lineWrapping: false, mode: "application/json", lineNumbers: false, lineWrapping: false,
...@@ -79,18 +84,20 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({ ...@@ -79,18 +84,20 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({
mirror.setValue(stringValue); mirror.setValue(stringValue);
} catch(quotedE) { } catch(quotedE) {
// TODO: validation error // TODO: validation error
console.log("Error with JSON, even after converting to String.") console.log("Error with JSON, even after converting to String.");
console.log(quotedE); console.log(quotedE);
JSONValue = undefined; JSONValue = undefined;
} }
} }
else { else {
// TODO: validation error // TODO: validation error
console.log("Error with JSON, but will not convert to String.") console.log("Error with JSON, but will not convert to String.");
console.log(e); console.log(e);
} }
} }
if (JSONValue !== undefined) { if (JSONValue !== undefined) {
// Is it OK to clear all validation errors? If we don't we get problems with errors overlaying.
self.clearValidationErrors();
self.model.set(key, JSONValue, {validate: true}); self.model.set(key, JSONValue, {validate: true});
} }
} }
...@@ -146,7 +153,7 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({ ...@@ -146,7 +153,7 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({
var key = $('.key', li$).attr('id'); var key = $('.key', li$).attr('id');
delete this.fieldToSelectorMap[key]; delete this.fieldToSelectorMap[key];
if (key !== this.new_key) { if (key !== this.model.new_key) {
this.model.deleteKeys.push(key); this.model.deleteKeys.push(key);
this.model.unset(key); this.model.unset(key);
} }
...@@ -182,9 +189,9 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({ ...@@ -182,9 +189,9 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({
listEle$.append(newEle); listEle$.append(newEle);
// disable the value entry until there's an acceptable key // disable the value entry until there's an acceptable key
$(newEle).find('.course-advanced-policy-value').addClass('disabled'); $(newEle).find('.course-advanced-policy-value').addClass('disabled');
this.fieldToSelectorMap[this.new_key] = this.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.new_key).closest('li').find('.json'); var policyValueDivs = this.$el.find('#' + this.model.new_key).closest('li').find('.json');
// only 1 but hey, let's take advantage of the context mechanism // only 1 but hey, let's take advantage of the context mechanism
_.each(policyValueDivs, this.attachJSONEditor, this); _.each(policyValueDivs, this.attachJSONEditor, this);
this.toggleNewButton(false); this.toggleNewButton(false);
...@@ -194,26 +201,29 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({ ...@@ -194,26 +201,29 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({
// 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 = $(event.currentTarget).closest('.key').attr('id');
var newKey = $(event.currentTarget).val(); // TODO: validation of keys with spaces. For now at least trim strings to remove initial and
// trailing whitespace
var newKey = $.trim($(event.currentTarget).val());
if (oldKey !== newKey) { if (oldKey !== newKey) {
// TODO: is it OK to erase other validation messages? // TODO: is it OK to erase other validation messages?
this.clearValidationErrors(); this.clearValidationErrors();
if (!this.validateKey(oldKey, newKey)) return; if (!this.validateKey(oldKey, newKey)) return;
if (this.model.has(newKey)) { // TODO: re-enable validation
var error = {}; // if (this.model.has(newKey)) {
error[oldKey] = 'You have already defined "' + newKey + '" in the manual policy definitions.'; // var error = {};
error[newKey] = "You tried to enter a duplicate of this key."; // error[oldKey] = 'You have already defined "' + newKey + '" in the manual policy definitions.';
this.model.trigger("error", this.model, error); // error[newKey] = "You tried to enter a duplicate of this key.";
return false; // this.model.trigger("error", this.model, error);
} // return false;
// }
// explicitly call validate to determine whether to proceed (relying on triggered error means putting continuation in the success // explicitly call validate to determine whether to proceed (relying on triggered error means putting continuation in the success
// method which is uglier I think?) // method which is uglier I think?)
var newEntryModel = {}; var newEntryModel = {};
// set the new key's value to the old one's // set the new key's value to the old one's
newEntryModel[newKey] = (oldKey === this.new_key ? '' : this.model.get(oldKey)); newEntryModel[newKey] = (oldKey === this.model.new_key ? '' : this.model.get(oldKey));
var validation = this.model.validate(newEntryModel); var validation = this.model.validate(newEntryModel);
if (validation) { if (validation) {
...@@ -231,7 +241,7 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({ ...@@ -231,7 +241,7 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({
delete this.fieldToSelectorMap[oldKey]; delete this.fieldToSelectorMap[oldKey];
if (oldKey !== this.new_key) { if (oldKey !== this.model.new_key) {
// mark the old key for deletion and delete from field maps // mark the old key for deletion and delete from field maps
this.model.deleteKeys.push(oldKey); this.model.deleteKeys.push(oldKey);
this.model.unset(oldKey) ; this.model.unset(oldKey) ;
...@@ -252,7 +262,9 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({ ...@@ -252,7 +262,9 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({
$(event.currentTarget).closest('li').replaceWith(newEle); $(event.currentTarget).closest('li').replaceWith(newEle);
// 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('#' + newKey).closest('li').find('.json'); var policyValueDivs = this.$el.find('#' + newKey).closest('li').find('.json');
// only 1 but hey, let's take advantage of the context mechanism
// 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); _.each(policyValueDivs, this.attachJSONEditor, this);
this.fieldToSelectorMap[newKey] = newKey; this.fieldToSelectorMap[newKey] = newKey;
...@@ -260,13 +272,15 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({ ...@@ -260,13 +272,15 @@ CMS.Views.Settings.Advanced = CMS.Views.ValidatingView.extend({
}, },
validateKey : function(oldKey, newKey) { validateKey : function(oldKey, newKey) {
// model validation can't handle malformed keys nor notice if 2 fields have same key; so, need to add that chk here // model validation can't handle malformed keys nor notice if 2 fields have same key; so, need to add that chk here
// TODO ensure there's no spaces or illegal chars // TODO ensure there's no spaces or illegal chars (note some checking for spaces currently done in model's
if (_.isEmpty(newKey)) { // validate method.
var error = {}; // if (_.isEmpty(newKey)) {
error[oldKey] = "Key cannot be an empty string"; // var error = {};
this.model.trigger("error", this.model, error); // error[oldKey] = "Key cannot be an empty string";
return false; // this.model.trigger("error", this.model, error);
} // return false;
else return true; // }
// else return true;
return true;
} }
}); });
\ No newline at end of file
...@@ -93,7 +93,7 @@ from contentstore import utils ...@@ -93,7 +93,7 @@ from contentstore import utils
<i class="ss-icon ss-symbolicons-block icon icon-warning">&#x26A0;</i> <i class="ss-icon ss-symbolicons-block icon icon-warning">&#x26A0;</i>
<p><strong>Note: </strong>Your changes will not take effect until you <strong>save your <p><strong>Note: </strong>Your changes will not take effect until you <strong>save your
progress</strong>.</p> progress</strong>. Be careful with syntax as validation is <strong>not implemented</strong>.</p>
</div> </div>
<div class="actions"> <div class="actions">
......
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