107 lines
3.8 KiB
HTML
107 lines
3.8 KiB
HTML
<!DOCTYPE HTML>
|
|
<html>
|
|
<head>
|
|
<title>JSONEditor | Auto Complete</title>
|
|
|
|
<link href="../dist/jsoneditor.css" rel="stylesheet" type="text/css">
|
|
<script src="../dist/jsoneditor.js"></script>
|
|
|
|
|
|
<style type="text/css">
|
|
#jsoneditor {
|
|
width: 500px;
|
|
height: 500px;
|
|
}
|
|
|
|
p {
|
|
width: 500px;
|
|
font-family: "DejaVu Sans", sans-serif;
|
|
}
|
|
</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.
|
|
</p>
|
|
|
|
<div id="jsoneditor"></div>
|
|
|
|
<script>
|
|
// create the editor
|
|
var container = document.getElementById('jsoneditor');
|
|
var options = {
|
|
modes: ['text', 'tree'],
|
|
autocomplete: {
|
|
config: {
|
|
confirmKeys: [39, 35, 9, 190] // Confirm Autocomplete Keys: [right, end, tab, '.'] // By default are only [right, end, tab]
|
|
},
|
|
activationChar: '*', // ActivationChar indicate if the value starts with this Char then autocompletion will be activated. ('' will be ignored).
|
|
applyTo:['value'],
|
|
getOptions: function (autocomplete, node, text) {
|
|
|
|
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 data = {};
|
|
autocomplete.startFrom = 0;
|
|
var lastPoint = text.lastIndexOf('.');
|
|
if ((lastPoint > 0) && (text.length > 1)) {
|
|
var fnode = node.editor.node.findNode('.' + text.substring(this.activationChar.length, lastPoint));
|
|
if (fnode && typeof fnode.getValue() == 'object') {
|
|
data = fnode.getValue();
|
|
// Indicate that autocompletion should start after the . (ignoring the first part)
|
|
autocomplete.startFrom = text.lastIndexOf('.') + 1;
|
|
}
|
|
}
|
|
else
|
|
data = node.editor.get();
|
|
|
|
var optionsStr = YaskON.stringify(data, null, this.activationChar);
|
|
var options = optionsStr.split("\n");
|
|
|
|
return options;
|
|
}
|
|
}
|
|
};
|
|
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>
|
|
</html>
|