Closed ffritsche closed 4 years ago
Creating a new completer (based on a different autocompleter), involves calling Yasqe.forkAutocompleter
now.
You can call that function with the autocompleter name that you want to copy from as first argument, and the properties that you want to overwrite as second argument.
For the list of allowed autocompleter properties, see https://github.com/TriplyDB/Yasgui/blob/5e5a12aa4d07337cba4301629ae8ca4a96ed6dde/packages/yasqe/src/autocompleters/index.ts#L9-L18
I.e., a simple example for creating a new class autocompleter would be:
Yasqe.forkAutocompleter('class', {
name: "customClassCompleterObj",
get: function(yasqe, token) {
return Promise.resolve(['https://ex1.org', 'https://ex2.org'])
}
})
You can use this same approach for adding custom prefix and property completers. If you have any suggestions for documentation improvements, feel free to mention them here, preferably with a PR!
ps. You'd also need to manually disable the original class completer by changing the Yasqe.defaults.completers
setting. But, I'm thinking about doing that by default in the future when forking an autocompleter
Thanks, her is my solution:
Yasgui.defaults.requestConfig.endpoint = "http://ks2:8890/sparql";
Yasqe.forkAutocompleter('class', {
name: "customClassCompleterObj",
autoShow: true,
bulk: true,
async: true,
//persistenceId: "customClassCompleterObj",
get: function(yasqe, token) {
var sparqlQuery = "SELECT distinct ?x WHERE {?x rdfs:subClassOf []}";
var classes = [];
$.ajax({
data: {
query: sparqlQuery
},
url: Yasgui.defaults.requestConfig.endpoint,
async: false,
headers: {
Accept: "application/sparql-results+json"
},
success: function (data) {
data.results.bindings.forEach(function (b) {
classes.push(b.x.value);
});
}
});
return Promise.resolve(classes);
}
})
Yasqe.forkAutocompleter('property', {
name: "customPropertyCompleterObj",
autoShow: true,
bulk: true,
async: true,
// persistenceId: "customPropertyCompleterObj",
get: function(yasqe, token) {
//all we need from these parameters is the last one: the callback to pass the array of completions to
var sparqlQuery = "SELECT distinct ?x WHERE { ?x a ?y. ?y rdfs:subClassOf* rdf:Property}";
var properties = [];
$.ajax({
data: {
query: sparqlQuery
},
url: Yasgui.defaults.requestConfig.endpoint,
async: false,
headers: {
Accept: "application/sparql-results+json"
},
success: function (data) {
data.results.bindings.forEach(function (b) {
properties.push(b.x.value);
});
}
});
return Promise.resolve(properties);
}
})
Yasqe.forkAutocompleter('prefixes', {
name: "customPrefixCompleterObj",
autoShow: true,
bulk: true,
async: true,
persistenceId: "customPrefixCompleterObj",
get: function(yasqe, token) {
var prefixes = [];
$.ajax({
dataType: "json",
url: "prefixlist.json",
async: false,
success: function (data) {
entries = Object.entries(data);
entries.forEach(entry => prefixes.push(entry[0]+": <"+entry[1]+">"));
}
});
return Promise.resolve(prefixes);
}
})
//And, to make sure we don't use the other property and class autocompleters, overwrite the default enabled completers
Yasqe.defaults.autocompleters = ['customPrefixCompleterObj', 'customClassCompleterObj', 'customPropertyCompleterObj', 'variables'];
Im try to write an autocompleter for classes and properties.
I just cant get it to work and I cant find good examples for the new version of Yasgui. Also is there an way to extend the prefixlist? There was a option "fetchFrom" to set the url for the json prefixlist.
Thanks