diff --git a/HISTORY.md b/HISTORY.md index ea902dc..3099655 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -3,11 +3,17 @@ https://github.com/josdejong/jsoneditor -## not yet released, version 4.2.2 +## not yet released, version 5.0.0 - Replaced the PNG icon images with SVG. Thanks @1j01. - Implemented a new option `escapeUnicode`, which will show the hexadecimal unicode instead of the character itself. (See #93 and #230). +- Implemented method `getMode`. +- Implemented option `onModeChange(oldMode, newMode)`. +- Renamed options `change`, `editable`, `error` to respectively `onChange`, + `onEditable`, and `onError`. Old options are still working and give a + deprecation warning. +- JSONEditor new throws a warning in the console in case of unknown options. - Fixed #93 and #227: html codes like `&` not escaped. - Fixed #149: Memory leak when switching mode from/to `code` mode, web worker of Ace editor wasn't cleaned up. diff --git a/docs/api.md b/docs/api.md index 6809372..06bbd66 100644 --- a/docs/api.md +++ b/docs/api.md @@ -17,13 +17,16 @@ Constructs a new JSONEditor. - `{Object} ace` Provide a custom version of the [Ace editor](http://ace.c9.io/) and use this instead of the version that comes embedded with JSONEditor. Only applicable when `mode` is `code`. - - `{function} change` - Set a callback method triggered when the contents of the JSONEditor change. Called without parameters. Will only be triggered on changes made by the user, not in case of programmatic changes via the functions `set` or `setText`. - - `{function} editable` - Set a callback method to determine whether individual nodes are editable or read-only. Only applicable when option `mode` is `tree`. The callback is invoked as `editable(node)`, where `node` is an object `{field: string, value: string, path: string[]}`. The function must either return a boolean value to set both the nodes field and value editable or read-only, or return an object `{field: boolean, value: boolean}`. - - `{function} error` - Set a callback method triggered when an error occurs. Invoked with the error as first argument. The callback is only invoked - for errors triggered by a users action. + - `{function} onChange` + Set a callback function triggered when the contents of the JSONEditor change. Called without parameters. Will only be triggered on changes made by the user, not in case of programmatic changes via the functions `set` or `setText`. + - `{function} onEditable` + Set a callback function to determine whether individual nodes are editable or read-only. Only applicable when option `mode` is `tree`. The callback is invoked as `editable(node)`, where `node` is an object `{field: string, value: string, path: string[]}`. The function must either return a boolean value to set both the nodes field and value editable or read-only, or return an object `{field: boolean, value: boolean}`. + - `{function} onError` + Set a callback function triggered when an error occurs. Invoked with the error as first argument. The callback is only invoked + for errors triggered by a users action, like switching from code mode to tree mode or clicking the Format button whilst the editor doesn't contain valid JSON. + - `{function} onModeChange(newMode, oldMode)` + Set a callback function triggered right after the mode is changed by the user. Only applicable when + the mode can be changed by the user (i.e. when option `modes` is set). - `{boolean} escapeUnicode` If true, unicode characters are escaped and displayed as their code instead of the character. False by default. @@ -116,6 +119,15 @@ which can be the case when the editor is in mode `code` or `text`. - `{JSON} json` JSON data from the JSONEditor. +#### `JSONEditor.getMode()` + +Retrieve the current mode of the editor. + +*Returns:* + +- `{String} mode` + Current mode of the editor for example `tree` or `code`. + #### `JSONEditor.getName()` Retrieve the current field name of the root node. diff --git a/examples/03_switch_mode.html b/examples/03_switch_mode.html index 7a18445..f080ae2 100644 --- a/examples/03_switch_mode.html +++ b/examples/03_switch_mode.html @@ -43,8 +43,11 @@ var options = { mode: 'tree', modes: ['code', 'form', 'text', 'tree', 'view'], // allowed modes - error: function (err) { + onError: function (err) { alert(err.toString()); + }, + onModeChange: function (newMode, oldMode) { + console.log('Mode switched from', oldMode, 'to', newMode); } }; diff --git a/examples/05_custom_fields_editable.html b/examples/05_custom_fields_editable.html index 8637540..d8a5dae 100644 --- a/examples/05_custom_fields_editable.html +++ b/examples/05_custom_fields_editable.html @@ -1,7 +1,7 @@ - JSONEditor | Basic usage + JSONEditor | Custom editable fields @@ -28,7 +28,7 @@ var container = document.getElementById('jsoneditor'); var options = { - editable: function (node) { + onEditable: function (node) { // node is an object like: // { // field: 'FIELD', diff --git a/src/js/JSONEditor.js b/src/js/JSONEditor.js index 8d1800c..6d3650d 100644 --- a/src/js/JSONEditor.js +++ b/src/js/JSONEditor.js @@ -6,22 +6,24 @@ var util = require('./util'); * @constructor JSONEditor * @param {Element} container Container element * @param {Object} [options] Object with options. available options: - * {String} mode Editor mode. Available values: - * 'tree' (default), 'view', - * 'form', 'text', and 'code'. - * {function} change Callback method, triggered - * on change of contents - * {Boolean} search Enable search box. - * True by default - * Only applicable for modes - * 'tree', 'view', and 'form' - * {Boolean} history Enable history (undo/redo). - * True by default - * Only applicable for modes - * 'tree', 'view', and 'form' - * {String} name Field name for the root node. - * Only applicable for modes - * 'tree', 'view', and 'form' + * {String} mode Editor mode. Available values: + * 'tree' (default), 'view', + * 'form', 'text', and 'code'. + * {function} onChange Callback method, triggered + * on change of contents + * {function} onError Callback method, triggered + * when an error occurs + * {Boolean} search Enable search box. + * True by default + * Only applicable for modes + * 'tree', 'view', and 'form' + * {Boolean} history Enable history (undo/redo). + * True by default + * Only applicable for modes + * 'tree', 'view', and 'form' + * {String} name Field name for the root node. + * Only applicable for modes + * 'tree', 'view', and 'form' * {Number} indentation Number of indentation * spaces. 4 by default. * Only applicable for @@ -43,6 +45,40 @@ function JSONEditor (container, options, json) { 'Please install the newest version of your browser.'); } + if (options) { + // check for deprecated options + if (options.error) { + console.warn('Option "error" has been renamed to "onError"'); + options.onError = options.error; + delete options.error; + } + if (options.change) { + console.warn('Option "change" has been renamed to "onChange"'); + options.onChange = options.change; + delete options.change; + } + if (options.editable) { + console.warn('Option "editable" has been renamed to "onEditable"'); + options.onEditable = options.editable; + delete options.editable; + } + + // validate options + if (options) { + var VALID_OPTIONS = [ + 'ace', + 'onChange', 'onEditable', 'onError', 'onModeChange', + 'escapeUnicode', 'history', 'mode', 'modes', 'name', 'indentation', 'theme' + ]; + + Object.keys(options).forEach(function (option) { + if (VALID_OPTIONS.indexOf(option) === -1) { + console.warn('Unknown option "' + option + '". This option will be ignored'); + } + }); + } + } + if (arguments.length) { this._create(container, options, json); } @@ -145,10 +181,11 @@ JSONEditor.prototype.getName = function () { * 'text', and 'code'. */ JSONEditor.prototype.setMode = function (mode) { - var container = this.container, - options = util.extend({}, this.options), - data, - name; + var container = this.container; + var options = util.extend({}, this.options); + var oldMode = options.mode; + var data; + var name; options.mode = mode; var config = JSONEditor.modes[mode]; @@ -170,14 +207,18 @@ JSONEditor.prototype.setMode = function (mode) { try { config.load.call(this); } - catch (err) {} + catch (err) { + console.error(err); + } } - if (typeof options.onMode === 'function') { + if (typeof options.onModeChange === 'function' && mode !== oldMode) { try { - options.onMode(options.mode); + options.onModeChange(mode, oldMode); + } + catch (err) { + console.error(err); } - catch (err) {} } } catch (err) { @@ -189,6 +230,14 @@ JSONEditor.prototype.setMode = function (mode) { } }; +/** + * Get the current mode + * @return {string} + */ +JSONEditor.prototype.getMode = function () { + return this.options.mode; +}; + /** * Throw an error. If an error callback is configured in options.error, this * callback will be invoked. Else, a regular error is thrown. @@ -196,15 +245,8 @@ JSONEditor.prototype.setMode = function (mode) { * @private */ JSONEditor.prototype._onError = function(err) { - // TODO: onError is deprecated since version 2.2.0. cleanup some day - if (typeof this.onError === 'function') { - util.log('WARNING: JSONEditor.onError is deprecated. ' + - 'Use options.error instead.'); - this.onError(err); - } - - if (this.options && typeof this.options.error === 'function') { - this.options.error(err); + if (this.options && typeof this.options.onError === 'function') { + this.options.onError(err); } else { throw err; diff --git a/src/js/Node.js b/src/js/Node.js index badfb29..0c9051e 100644 --- a/src/js/Node.js +++ b/src/js/Node.js @@ -43,8 +43,8 @@ Node.prototype._updateEditability = function () { this.editable.field = this.editor.options.mode === 'tree'; this.editable.value = this.editor.options.mode !== 'view'; - if (this.editor.options.mode === 'tree' && (typeof this.editor.options.editable === 'function')) { - var editable = this.editor.options.editable({ + if (this.editor.options.mode === 'tree' && (typeof this.editor.options.onEditable === 'function')) { + var editable = this.editor.options.onEditable({ field: this.field, value: this.value, path: this.path() diff --git a/src/js/textmode.js b/src/js/textmode.js index 8485ee9..782cea7 100644 --- a/src/js/textmode.js +++ b/src/js/textmode.js @@ -16,20 +16,20 @@ var textmode = {}; * Create a text editor * @param {Element} container * @param {Object} [options] Object with options. available options: - * {String} mode Available values: - * "text" (default) - * or "code". - * {Number} indentation Number of indentation - * spaces. 2 by default. - * {function} change Callback method - * triggered on change - * {function} onMode Callback method - * triggered after setMode - * {Object} ace A custom instance of - * Ace editor. - * {boolean} escapeUnicode If true, unicode - * characters are escaped. - * false by default. + * {String} mode Available values: + * "text" (default) + * or "code". + * {Number} indentation Number of indentation + * spaces. 2 by default. + * {function} onChange Callback method + * triggered on change + * {function} onModeChange Callback method + * triggered after setMode + * {Object} ace A custom instance of + * Ace editor. + * {boolean} escapeUnicode If true, unicode + * characters are escaped. + * false by default. * @private */ textmode.create = function (container, options) { @@ -159,11 +159,9 @@ textmode.create = function (container, options) { }; this.menu.appendChild(poweredBy); - if (options.change) { + if (options.onChange) { // register onchange event - editor.on('change', function () { - options.change(); - }); + editor.on('change', options.onChange); } } else { @@ -174,18 +172,14 @@ textmode.create = function (container, options) { this.content.appendChild(textarea); this.textarea = textarea; - if (options.change) { + if (options.onChange) { // register onchange event if (this.textarea.oninput === null) { - this.textarea.oninput = function () { - options.change(); - } + this.textarea.oninput = options.onChange(); } else { // oninput is undefined. For IE8- - this.textarea.onchange = function () { - options.change(); - } + this.textarea.onchange = options.onChange(); } } } @@ -231,28 +225,6 @@ textmode._delete = function () { } }; -/** - * Throw an error. If an error callback is configured in options.error, this - * callback will be invoked. Else, a regular error is thrown. - * @param {Error} err - * @private - */ -textmode._onError = function(err) { - // TODO: onError is deprecated since version 2.2.0. cleanup some day - if (typeof this.onError === 'function') { - util.log('WARNING: JSONEditor.onError is deprecated. ' + - 'Use options.error instead.'); - this.onError(err); - } - - if (this.options && typeof this.options.error === 'function') { - this.options.error(err); - } - else { - throw err; - } -}; - /** * Compact the code in the formatter */ diff --git a/src/js/treemode.js b/src/js/treemode.js index 75888f6..fdf5cd4 100644 --- a/src/js/treemode.js +++ b/src/js/treemode.js @@ -12,16 +12,16 @@ var treemode = {}; * Create a tree editor * @param {Element} container Container element * @param {Object} [options] Object with options. available options: - * {String} mode Editor mode. Available values: - * 'tree' (default), 'view', - * and 'form'. - * {Boolean} search Enable search box. - * True by default - * {Boolean} history Enable history (undo/redo). - * True by default - * {function} change Callback method, triggered - * on change of contents - * {String} name Field name for the root node. + * {String} mode Editor mode. Available values: + * 'tree' (default), 'view', + * and 'form'. + * {Boolean} search Enable search box. + * True by default + * {Boolean} history Enable history (undo/redo). + * True by default + * {function} onChange Callback method, triggered + * on change of contents + * {String} name Field name for the root node. * {boolean} escapeUnicode If true, unicode * characters are escaped. * false by default. @@ -305,9 +305,9 @@ treemode._onAction = function (action, params) { } // trigger the onChange callback - if (this.options.change) { + if (this.options.onChange) { try { - this.options.change(); + this.options.onChange(); } catch (err) { util.log('Error in change callback: ', err); @@ -589,8 +589,8 @@ treemode._onUndo = function () { this.history.undo(); // trigger change callback - if (this.options.change) { - this.options.change(); + if (this.options.onChange) { + this.options.onChange(); } } }; @@ -605,8 +605,8 @@ treemode._onRedo = function () { this.history.redo(); // trigger change callback - if (this.options.change) { - this.options.change(); + if (this.options.onChange) { + this.options.onChange(); } } }; diff --git a/test/test_build.html b/test/test_build.html index fbead76..99738b6 100644 --- a/test/test_build.html +++ b/test/test_build.html @@ -42,7 +42,7 @@ options = { mode: 'tree', modes: ['code', 'form', 'text', 'tree', 'view'], // allowed modes - error: function (err) { + onError: function (err) { alert(err.toString()); }, indentation: 4,