From 7259b269b0bc9b63611c3a3fd0db27732042b8be Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Mon, 26 May 2014 19:02:00 +0800 Subject: [PATCH 1/3] JSLint (global and jslint declaration, single use strict closure to avoid repeat or non-function "use strict" statements; curly braces, upper-case (and CamelCase) constructors per JS/JSLint convention; no need for else after previous if(s) return; define var out of loops; comment out unused function param, no assignments within statement; rmv need for continue (though jslint can be configured to allow) --- src/core.js | 103 +++++++++++++++++++++++++++++----------------------- 1 file changed, 58 insertions(+), 45 deletions(-) diff --git a/src/core.js b/src/core.js index f5804b213..7f329f92f 100644 --- a/src/core.js +++ b/src/core.js @@ -1,26 +1,32 @@ -var JSONEditor = function(element,options) { +/*globals $extend, $each, requestAnimationFrame*/ +/*jslint vars:true*/ + +var JSONEditor = (function () {'use strict'; + +function JSONEditor (element,options) { options = $extend({},JSONEditor.defaults.options,options||{}); this.element = element; this.options = options; this.init(); -}; +} + JSONEditor.prototype = { init: function() { var self = this; this.ready = false; - var theme_class = JSONEditor.defaults.themes[this.options.theme || JSONEditor.defaults.theme]; - if(!theme_class) throw "Unknown theme " + (this.options.theme || JSONEditor.defaults.theme); + var ThemeClass = JSONEditor.defaults.themes[this.options.theme || JSONEditor.defaults.theme]; + if(!ThemeClass) {throw "Unknown theme " + (this.options.theme || JSONEditor.defaults.theme);} this.schema = this.options.schema; - this.theme = new theme_class(); + this.theme = new ThemeClass(); this.template = this.options.template; this.uuid = 0; this.__data = {}; - var icon_class = JSONEditor.defaults.iconlibs[this.options.iconlib || JSONEditor.defaults.iconlib]; - if(icon_class) this.iconlib = new icon_class(); + var IconClass = JSONEditor.defaults.iconlibs[this.options.iconlib || JSONEditor.defaults.iconlib]; + if(IconClass) {this.iconlib = new IconClass();} this.root_container = this.theme.getContainer(); this.element.appendChild(this.root_container); @@ -33,13 +39,13 @@ JSONEditor.prototype = { }); this.validator.ready(function(expanded) { - if(self.ready) return; + if(self.ready) {return;} self.schema = expanded; // Create the root editor - var editor_class = self.getEditorClass(self.schema); - self.root = self.createEditor(editor_class, { + var EditorClass = self.getEditorClass(self.schema); + self.root = self.createEditor(EditorClass, { jsoneditor: self, schema: self.schema, container: self.root_container, @@ -47,7 +53,7 @@ JSONEditor.prototype = { }); // Starting data - if(self.options.startval) self.root.setValue(self.options.startval); + if(self.options.startval) {self.root.setValue(self.options.startval);} self.ready = true; @@ -61,31 +67,29 @@ JSONEditor.prototype = { }); }, getValue: function() { - if(!this.ready) throw "JSON Editor not ready yet. Listen for 'ready' event before getting the value"; + if(!this.ready) {throw "JSON Editor not ready yet. Listen for 'ready' event before getting the value";} return this.root.getValue(); }, setValue: function(value) { - if(!this.ready) throw "JSON Editor not ready yet. Listen for 'ready' event before setting the value"; + if(!this.ready) {throw "JSON Editor not ready yet. Listen for 'ready' event before setting the value";} this.root.setValue(value); return this; }, validate: function(value) { - if(!this.ready) throw "JSON Editor not ready yet. Listen for 'ready' event before validating"; + if(!this.ready) {throw "JSON Editor not ready yet. Listen for 'ready' event before validating";} // Custom value if(arguments.length === 1) { return this.validator.validate(arguments[1]); } // Current value (use cached result) - else { - return this.validation_results; - } + return this.validation_results; }, destroy: function() { - if(this.destroyed) return; - if(!this.ready) return; + if(this.destroyed) {return;} + if(!this.ready) {return;} this.schema = null; this.options = null; @@ -114,9 +118,11 @@ JSONEditor.prototype = { this.callbacks = this.callbacks || {}; this.callbacks[event] = this.callbacks[event] || []; var newcallbacks = []; - for(var i=0; i Date: Mon, 26 May 2014 19:34:51 +0800 Subject: [PATCH 2/3] Full set of jslint directives --- src/core.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core.js b/src/core.js index 7f329f92f..176a4f082 100644 --- a/src/core.js +++ b/src/core.js @@ -1,5 +1,5 @@ /*globals $extend, $each, requestAnimationFrame*/ -/*jslint vars:true*/ +/*jslint vars:true, nomen: true, plusplus: true, white: true*/ var JSONEditor = (function () {'use strict'; From 146a62f7c81b77058e26cf10fe9569c4fa22d09c Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Tue, 27 May 2014 07:37:24 +0800 Subject: [PATCH 3/3] document indenting, indicate unused params, spaces after commas and before keywords like function/if; strange rule to have space between semicolon and curly brace --- src/core.js | 184 ++++++++++++++++++++++++++-------------------------- 1 file changed, 93 insertions(+), 91 deletions(-) diff --git a/src/core.js b/src/core.js index 176a4f082..41efacd42 100644 --- a/src/core.js +++ b/src/core.js @@ -1,48 +1,49 @@ /*globals $extend, $each, requestAnimationFrame*/ -/*jslint vars:true, nomen: true, plusplus: true, white: true*/ +/*jslint white:true, indent:2, vars:true, nomen:true, plusplus:true, unparam:false */ -var JSONEditor = (function () {'use strict'; +var JSONEditor = (function () { +'use strict'; -function JSONEditor (element,options) { - options = $extend({},JSONEditor.defaults.options,options||{}); +function JSONEditor(element, options) { + options = $extend({}, JSONEditor.defaults.options, options || {}); this.element = element; this.options = options; this.init(); } JSONEditor.prototype = { - init: function() { + init: function () { var self = this; - + this.ready = false; var ThemeClass = JSONEditor.defaults.themes[this.options.theme || JSONEditor.defaults.theme]; - if(!ThemeClass) {throw "Unknown theme " + (this.options.theme || JSONEditor.defaults.theme);} - + if (!ThemeClass) {throw "Unknown theme " + (this.options.theme || JSONEditor.defaults.theme); } + this.schema = this.options.schema; this.theme = new ThemeClass(); this.template = this.options.template; this.uuid = 0; this.__data = {}; - + var IconClass = JSONEditor.defaults.iconlibs[this.options.iconlib || JSONEditor.defaults.iconlib]; - if(IconClass) {this.iconlib = new IconClass();} + if (IconClass) {this.iconlib = new IconClass(); } this.root_container = this.theme.getContainer(); this.element.appendChild(this.root_container); - this.validator = new JSONEditor.Validator(this.schema,{ + this.validator = new JSONEditor.Validator(this.schema, { ajax: this.options.ajax, refs: this.options.refs, no_additional_properties: this.options.no_additional_properties, required_by_default: this.options.required_by_default }); - - this.validator.ready(function(expanded) { - if(self.ready) {return;} - + + this.validator.ready(function (expanded) { + if (self.ready) {return; } + self.schema = expanded; - + // Create the root editor var EditorClass = self.getEditorClass(self.schema); self.root = self.createEditor(EditorClass, { @@ -53,12 +54,12 @@ JSONEditor.prototype = { }); // Starting data - if(self.options.startval) {self.root.setValue(self.options.startval);} + if (self.options.startval) {self.root.setValue(self.options.startval); } self.ready = true; // Fire ready event asynchronously - requestAnimationFrame(function() { + requestAnimationFrame(function () { self.validation_results = self.validator.validate(self.root.getValue()); self.root.showValidationErrors(self.validation_results); self.trigger('ready'); @@ -66,31 +67,31 @@ JSONEditor.prototype = { }); }); }, - getValue: function() { - if(!this.ready) {throw "JSON Editor not ready yet. Listen for 'ready' event before getting the value";} + getValue: function () { + if (!this.ready) {throw "JSON Editor not ready yet. Listen for 'ready' event before getting the value"; } return this.root.getValue(); }, - setValue: function(value) { - if(!this.ready) {throw "JSON Editor not ready yet. Listen for 'ready' event before setting the value";} + setValue: function (value) { + if (!this.ready) {throw "JSON Editor not ready yet. Listen for 'ready' event before setting the value"; } this.root.setValue(value); return this; }, - validate: function(value) { - if(!this.ready) {throw "JSON Editor not ready yet. Listen for 'ready' event before validating";} - + validate: function (value) { + if (!this.ready) {throw "JSON Editor not ready yet. Listen for 'ready' event before validating"; } + // Custom value - if(arguments.length === 1) { + if (arguments.length === 1) { return this.validator.validate(arguments[1]); } // Current value (use cached result) return this.validation_results; }, - destroy: function() { - if(this.destroyed) {return;} - if(!this.ready) {return;} - + destroy: function () { + if (this.destroyed) {return; } + if (!this.ready) {return; } + this.schema = null; this.options = null; this.root.destroy(); @@ -104,30 +105,29 @@ JSONEditor.prototype = { this.__data = null; this.ready = false; this.element.innerHTML = ''; - + this.destroyed = true; }, - on: function(event, callback) { + on: function (event, callback) { this.callbacks = this.callbacks || {}; this.callbacks[event] = this.callbacks[event] || []; this.callbacks[event].push(callback); }, - off: function(event, callback) { + off: function (event, callback) { // Specific callback - if(event && callback) { + if (event && callback) { this.callbacks = this.callbacks || {}; this.callbacks[event] = this.callbacks[event] || []; var newcallbacks = []; var i; - for(i=0; i