diff --git a/docs/api.md b/docs/api.md
index 0c60ea7..c01b39f 100644
--- a/docs/api.md
+++ b/docs/api.md
@@ -157,15 +157,15 @@ Constructs a new JSONEditor.
Indicate the KeyCodes for trigger confirm completion, by default those keys are: [39, 35, 9] which are the code for [right, end, tab]
- - `{Function} getOptions (text: string, path: string[], input: string)`
+ - `{Function} getOptions (editor: object, text: string, path: string, input: string)`
This function will return your possible options for create the autocomplete selection, you can control dynamically which options you want to display according to the current active editing node.
*Parameters:*
-
- - `text` : The text in the current node part. (basically the text that the user is editing)
- - `path` : The document json object that is being edited.
- - `input` : Can be "field" or "value" depending if the user is editing a field name or a value of a node.
+ - `editor` : The editor instance object that the node belongs to.
+ - `text` : The text in the current node part. (basically the text that the user is editing)
+ - `path` : The json path of the node that is being edited.
+ - `input` : Can be "field" or "value" depending if the user is editing a field name or a value of a node.
*Returns:*
diff --git a/examples/12_autocomplete_dynamic.html b/examples/12_autocomplete_dynamic.html
index c3cd8ae..8dbd2ed 100644
--- a/examples/12_autocomplete_dynamic.html
+++ b/examples/12_autocomplete_dynamic.html
@@ -35,9 +35,9 @@
modes: ['text', 'tree'],
autocomplete: {
applyTo:['value'],
- getOptions: function (text, path) {
+ getOptions: function (editor, text) {
return new Promise(function (resolve, reject) {
- var options = extractUniqueWords(path);
+ var options = extractUniqueWords(editor.get());
if (options.length > 0) resolve(options); else reject();
});
}
diff --git a/examples/13_autocomplete_advanced.html b/examples/13_autocomplete_advanced.html
index fcf4292..28793d2 100644
--- a/examples/13_autocomplete_advanced.html
+++ b/examples/13_autocomplete_advanced.html
@@ -1,4 +1,4 @@
-
+
JSONEditor | Advanced Auto Complete
@@ -38,19 +38,20 @@
autocomplete: {
confirmKeys: [39, 35, 9, 190], // Confirm Autocomplete Keys: [right, end, tab, '.'] // By default are only [right, end, tab]
- getOptions: function (text, path, input) {
+ getOptions: function (editor, text, path, input) {
if (!text.startsWith(activationChar) || input !== 'value') return [];
var data = {};
var startFrom = 0;
var lastPoint = text.lastIndexOf('.');
+ var jsonObj = editor.get();
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;
+ data = jsonpath.query(jsonObj, '$.' + 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;
+ data = jsonObj;
var optionsStr = YaskON.stringify(data, null, activationChar);
var options = optionsStr.split('\n');
diff --git a/src/js/Node.js b/src/js/Node.js
index 6566300..66cb4a2 100644
--- a/src/js/Node.js
+++ b/src/js/Node.js
@@ -374,12 +374,30 @@ Node.prototype.getLevel = function() {
/**
* Get path of the root node till the current node
+ * @return {String} Returns a jsonpath of the current node in the form: node1.child1.child2, node1.array1[0].child, etc.
+ */
+Node.prototype.getNodeJsonPath = function () {
+ var path = this.parent ? this.parent.getNodePath() : [];
+ path.push(this);
+ var strPath = '';
+ path.forEach(function (node) {
+ strPath += !node.parent
+ ? '$' // do not add an (optional) field name of the root node
+ : (node.parent.type != 'array')
+ ? '.' + node.field
+ : '[' + node.index + ']';
+ });
+ return strPath;
+};
+
+/**
+ * Get jsonpath of the current node
* @return {Node[]} Returns an array with nodes
*/
-Node.prototype.getNodePath = function() {
- var path = this.parent ? this.parent.getNodePath() : [];
- path.push(this);
- return path;
+Node.prototype.getNodePath = function () {
+ var path = this.parent ? this.parent.getNodePath() : [];
+ path.push(this);
+ return path;
};
/**
diff --git a/src/js/treemode.js b/src/js/treemode.js
index d3737b4..68632ce 100644
--- a/src/js/treemode.js
+++ b/src/js/treemode.js
@@ -1115,7 +1115,7 @@ treemode._onKeyDown = function (event) {
// Activate autocomplete
setTimeout(function (hnode, element) {
if (element.innerText.length > 0) {
- var result = this.options.autocomplete.getOptions(element.innerText, editor.get(), jsonElementType);
+ var result = this.options.autocomplete.getOptions(hnode.editor, element.innerText, hnode.getNodeJsonPath(), jsonElementType);
if (typeof result.then === 'function') {
// probably a promise
if (result.then(function (obj) {