Some updates in the examples (indentation, link to jsonpath library instead of including it)
This commit is contained in:
parent
02555b8510
commit
0aed6e481e
|
@ -19,36 +19,32 @@
|
|||
}
|
||||
</style>
|
||||
|
||||
<!--<link href="./css/darktheme.css" rel="stylesheet" type="text/css">-->
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<p>
|
||||
This example demonstrates how to autocomplete works, options available are "apple","cranberry","raspberry","pie"
|
||||
This example demonstrates how to autocomplete works, options available are: 'apple','cranberry','raspberry','pie', 'mango', 'mandarine', 'melon', 'appleton'.
|
||||
</p>
|
||||
|
||||
<div id="jsoneditor"></div>
|
||||
|
||||
<script>
|
||||
|
||||
// create the editor
|
||||
var container = document.getElementById('jsoneditor');
|
||||
var options = {
|
||||
modes: ['text', 'tree'],
|
||||
autocomplete: {
|
||||
applyTo:['field','value'], // This indicates the autocomplete is going to be applied to json properties names and values.
|
||||
getOptions: function () {
|
||||
return ["apple","cranberry","raspberry","pie", "mango", "mandarine", "melon", "appleton"];
|
||||
}
|
||||
modes: ['text', 'tree'],
|
||||
autocomplete: {
|
||||
getOptions: function () {
|
||||
return ['apple', 'cranberry', 'raspberry', 'pie', 'mango', 'mandarine', 'melon', 'appleton'];
|
||||
}
|
||||
}
|
||||
};
|
||||
var json = {
|
||||
'array': [{'field1':'v1', 'field2':'v2'}, 2, 3],
|
||||
'boolean': true,
|
||||
'null': null,
|
||||
'number': 123,
|
||||
'object': {'a': 'b', 'c': 'd'},
|
||||
'string': 'Hello World'
|
||||
'array': [{'field1':'v1', 'field2':'v2'}, 2, 3],
|
||||
'boolean': true,
|
||||
'null': null,
|
||||
'number': 123,
|
||||
'object': {'a': 'b', 'c': 'd'},
|
||||
'string': 'Hello World'
|
||||
};
|
||||
var editor = new JSONEditor(container, options, json);
|
||||
</script>
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<head>
|
||||
<title>JSONEditor | Auto Complete</title>
|
||||
|
||||
<title>JSONEditor | Dynamic Auto Complete</title>
|
||||
|
||||
<link href="../dist/jsoneditor.css" rel="stylesheet" type="text/css">
|
||||
<script src="../dist/jsoneditor.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
|
||||
|
@ -20,50 +20,46 @@
|
|||
}
|
||||
</style>
|
||||
|
||||
<!--<link href="./css/darktheme.css" rel="stylesheet" type="text/css">-->
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<p>
|
||||
This example demonstrates how to autocomplete works, options available are dynamics and consist in all the strings found in the json
|
||||
This example demonstrates how to autocomplete works, options available are dynamics and consist in all the strings found in the json
|
||||
</p>
|
||||
|
||||
<div id="jsoneditor"></div>
|
||||
|
||||
<script>
|
||||
|
||||
// create the editor
|
||||
var container = document.getElementById('jsoneditor');
|
||||
var options = {
|
||||
modes: ['text', 'tree'],
|
||||
autocomplete: {
|
||||
applyTo:['value'],
|
||||
getOptions: function (text, path) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
var options = extractUniqueWords(path);
|
||||
if (options.length > 0) resolve(options); else reject();
|
||||
});
|
||||
}
|
||||
modes: ['text', 'tree'],
|
||||
autocomplete: {
|
||||
applyTo:['value'],
|
||||
getOptions: function (text, path) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
var options = extractUniqueWords(path);
|
||||
if (options.length > 0) resolve(options); else reject();
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
// ...
|
||||
|
||||
function extractUniqueWords (json) {
|
||||
return _.uniq(_.flatMapDeep(json, function (value, key) {
|
||||
return _.isObject(value)
|
||||
? [key]
|
||||
: [key, String(value)]
|
||||
}))
|
||||
}
|
||||
// helper function to extract all unique words in the keys and values of a JSON object
|
||||
function extractUniqueWords (json) {
|
||||
return _.uniq(_.flatMapDeep(json, function (value, key) {
|
||||
return _.isObject(value)
|
||||
? [key]
|
||||
: [key, String(value)]
|
||||
}))
|
||||
}
|
||||
|
||||
var json = {
|
||||
'array': [{'field1':'v1', 'field2':'v2'}, 2, 3],
|
||||
'boolean': true,
|
||||
'null': null,
|
||||
'number': 123,
|
||||
'object': {'a': 'b', 'c': 'd'},
|
||||
'string': 'Hello World'
|
||||
'array': [{'field1':'v1', 'field2':'v2'}, 2, 3],
|
||||
'boolean': true,
|
||||
'null': null,
|
||||
'number': 123,
|
||||
'object': {'a': 'b', 'c': 'd'},
|
||||
'string': 'Hello World'
|
||||
};
|
||||
var editor = new JSONEditor(container, options, json);
|
||||
</script>
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<head>
|
||||
<title>JSONEditor | Auto Complete</title>
|
||||
|
||||
<title>JSONEditor | Advanced Auto Complete</title>
|
||||
|
||||
<meta charset="UTF-8">
|
||||
<link href="../dist/jsoneditor.css" rel="stylesheet" type="text/css">
|
||||
<script src="../dist/jsoneditor.js"></script>
|
||||
<script src="jsonpath.min.js"></script>
|
||||
|
||||
<script src="https://unpkg.com/jsonpath@0.2.11/jsonpath.min.js"></script>
|
||||
|
||||
<style type="text/css">
|
||||
#jsoneditor {
|
||||
|
@ -20,84 +20,83 @@
|
|||
}
|
||||
</style>
|
||||
|
||||
<!--<link href="./css/darktheme.css" rel="stylesheet" type="text/css">-->
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<p>
|
||||
This example demonstrates how to autocomplete works with an ActivationChar option, press "*" in any value and continue with autocompletion.
|
||||
The autocomplete returns the posible jsonpaths of the existing json document.
|
||||
This example demonstrates how to autocomplete works with an ActivationChar option, press "*" in any value and continue with autocompletion.
|
||||
The autocomplete returns the posible jsonpaths of the existing json document, for example <code>*object.a</code>.
|
||||
</p>
|
||||
|
||||
<div id="jsoneditor"></div>
|
||||
|
||||
<script>
|
||||
// create the editor
|
||||
var container = document.getElementById('jsoneditor');
|
||||
var activationChar = '*';
|
||||
var container = document.getElementById('jsoneditor');
|
||||
var activationChar = '*';
|
||||
var options = {
|
||||
modes: ['text', 'tree'],
|
||||
autocomplete: {
|
||||
confirmKeys: [39, 35, 9, 190], // Confirm Autocomplete Keys: [right, end, tab, '.'] // By default are only [right, end, tab]
|
||||
|
||||
getOptions: function (text, path, input) {
|
||||
if (!text.startsWith(activationChar) || input != 'value') return [];
|
||||
var data = {};
|
||||
var startFrom = 0;
|
||||
var lastPoint = text.lastIndexOf('.');
|
||||
if ((lastPoint > 0) && (text.length > 1)) {
|
||||
data = jsonpath.query(path, '$.' + text.substring(activationChar.length, lastPoint));
|
||||
if (data.length > 0) data = data[0]; else data = {};
|
||||
// Indicate that autocompletion should start after the . (ignoring the first part)
|
||||
startFrom = text.lastIndexOf('.') + 1;
|
||||
}
|
||||
else
|
||||
data = path;
|
||||
modes: ['text', 'tree'],
|
||||
autocomplete: {
|
||||
confirmKeys: [39, 35, 9, 190], // Confirm Autocomplete Keys: [right, end, tab, '.'] // By default are only [right, end, tab]
|
||||
|
||||
var optionsStr = YaskON.stringify(data, null, activationChar);
|
||||
var options = optionsStr.split("\n");
|
||||
return { startFrom: startFrom, options: options };
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var YaskON = {
|
||||
// Return first level json paths by the node 'o'
|
||||
stringify: function (o, prefix, activationChar) {
|
||||
prefix = prefix || '';
|
||||
switch (typeof o) {
|
||||
case 'object':
|
||||
var output = "";
|
||||
if (Array.isArray(o)) {
|
||||
o.forEach(function (e, index) {
|
||||
output += activationChar + prefix + '[' + index + ']' + '\n';
|
||||
}.bind(this));
|
||||
return output;
|
||||
}
|
||||
output = "";
|
||||
for (var k in o) {
|
||||
if (o.hasOwnProperty(k)) {
|
||||
if (prefix == "") output += this.stringify(o[k], k, activationChar);
|
||||
}
|
||||
}
|
||||
if (prefix != "") output += activationChar + prefix + '\n'
|
||||
return output;
|
||||
case 'function':
|
||||
return "";
|
||||
default:
|
||||
return prefix + '\n';
|
||||
}
|
||||
getOptions: function (text, path, input) {
|
||||
if (!text.startsWith(activationChar) || input !== 'value') return [];
|
||||
var data = {};
|
||||
var startFrom = 0;
|
||||
var lastPoint = text.lastIndexOf('.');
|
||||
if ((lastPoint > 0) && (text.length > 1)) {
|
||||
data = jsonpath.query(path, '$.' + text.substring(activationChar.length, lastPoint));
|
||||
if (data.length > 0) data = data[0]; else data = {};
|
||||
// Indicate that autocompletion should start after the . (ignoring the first part)
|
||||
startFrom = text.lastIndexOf('.') + 1;
|
||||
}
|
||||
};
|
||||
else
|
||||
data = path;
|
||||
|
||||
var json = {
|
||||
'array': [{ 'field1': 'v1', 'field2': 'v2' }, 2, 3],
|
||||
'boolean': true,
|
||||
'null': null,
|
||||
'number': 123,
|
||||
'object': { 'a': 'b', 'c': 'd' },
|
||||
'string': 'Hello World'
|
||||
};
|
||||
var optionsStr = YaskON.stringify(data, null, activationChar);
|
||||
var options = optionsStr.split('\n');
|
||||
return { startFrom: startFrom, options: options };
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// helper function to auto complete paths of a JSON object
|
||||
var YaskON = {
|
||||
// Return first level json paths by the node 'o'
|
||||
stringify: function (o, prefix, activationChar) {
|
||||
prefix = prefix || '';
|
||||
switch (typeof o) {
|
||||
case 'object':
|
||||
var output = '';
|
||||
if (Array.isArray(o)) {
|
||||
o.forEach(function (e, index) {
|
||||
output += activationChar + prefix + '[' + index + ']' + '\n';
|
||||
}.bind(this));
|
||||
return output;
|
||||
}
|
||||
output = '';
|
||||
for (var k in o) {
|
||||
if (o.hasOwnProperty(k)) {
|
||||
if (prefix === '') output += this.stringify(o[k], k, activationChar);
|
||||
}
|
||||
}
|
||||
if (prefix !== '') output += activationChar + prefix + '\n'
|
||||
return output;
|
||||
case 'function':
|
||||
return '';
|
||||
default:
|
||||
return prefix + '\n';
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var json = {
|
||||
'array': [{ 'field1': 'v1', 'field2': 'v2' }, 2, 3],
|
||||
'boolean': true,
|
||||
'null': null,
|
||||
'number': 123,
|
||||
'object': { 'a': 'b', 'c': 'd' },
|
||||
'string': 'Hello World'
|
||||
};
|
||||
var editor = new JSONEditor(container, options, json);
|
||||
</script>
|
||||
</body>
|
||||
|
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue