Renamed options `change`, `editable`, `error` to respectively `onChange`, `onEditable`, and `onError`. Implemented `onModeChange` and `getMode`.

This commit is contained in:
jos 2015-12-25 11:38:41 +01:00
parent 75994c8035
commit de314b897e
9 changed files with 145 additions and 110 deletions

View File

@ -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.

View File

@ -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.

View File

@ -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);
}
};

View File

@ -1,7 +1,7 @@
<!DOCTYPE HTML>
<html>
<head>
<title>JSONEditor | Basic usage</title>
<title>JSONEditor | Custom editable fields</title>
<link href="../dist/jsoneditor.css" rel="stylesheet" type="text/css">
<script src="../dist/jsoneditor.js"></script>
@ -28,7 +28,7 @@
var container = document.getElementById('jsoneditor');
var options = {
editable: function (node) {
onEditable: function (node) {
// node is an object like:
// {
// field: 'FIELD',

View File

@ -9,8 +9,10 @@ var util = require('./util');
* {String} mode Editor mode. Available values:
* 'tree' (default), 'view',
* 'form', 'text', and 'code'.
* {function} change Callback method, triggered
* {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
@ -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;

View File

@ -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()

View File

@ -21,9 +21,9 @@ var textmode = {};
* or "code".
* {Number} indentation Number of indentation
* spaces. 2 by default.
* {function} change Callback method
* {function} onChange Callback method
* triggered on change
* {function} onMode Callback method
* {function} onModeChange Callback method
* triggered after setMode
* {Object} ace A custom instance of
* Ace editor.
@ -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
*/

View File

@ -19,7 +19,7 @@ var treemode = {};
* True by default
* {Boolean} history Enable history (undo/redo).
* True by default
* {function} change Callback method, triggered
* {function} onChange Callback method, triggered
* on change of contents
* {String} name Field name for the root node.
* {boolean} escapeUnicode If true, unicode
@ -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();
}
}
};

View File

@ -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,