diff --git a/src/core.js b/src/core.js index f5804b213..41efacd42 100644 --- a/src/core.js +++ b/src/core.js @@ -1,45 +1,52 @@ -var JSONEditor = function(element,options) { - options = $extend({},JSONEditor.defaults.options,options||{}); +/*globals $extend, $each, requestAnimationFrame*/ +/*jslint white:true, indent:2, vars:true, nomen:true, plusplus:true, unparam:false */ + +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() { + 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); - 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 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,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'); @@ -60,33 +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) - else { - return this.validation_results; - } + 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(); @@ -100,28 +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 = []; - for(var i=0; i