started to add SPARQL query GUI
[myslice.git] / portal / static / js / omn.js
1 var yasr = YASR(document.getElementById("yasr"), {
2         //this way, the URLs in the results are prettified using the defined prefixes in the query
3         getUsedPrefixes: yasqe.getPrefixesFromQuery
4 });
5
6 YASQE.defaults.sparql.showQueryButton = true;
7 YASQE.defaults.sparql.endpoint = "http://lod.fed4fire.eu/sparql";
8 YASQE.defaults.sparql.callbacks.success =  function(data){console.log("success", data);};
9 YASQE.defaults.sparql.callbacks.complete = yasr.setResponse;
10 YASQE.defaults.value = "SELECT ?name ?am ?endpoint WHERE {\n  ?infra <http://open-multinet.info/ontology/omn#hasService> ?am ;\n         rdfs:label ?name . \n  ?am rdf:type <http://open-multinet.info/ontology/omn-domain-geni-fire#AMService> ;\n      <http://open-multinet.info/ontology/omn#hasEndpoint> ?endpoint .\n} LIMIT 100"
11
12 /**
13  * We use most of the default settings for the property and class autocompletion types. This includes:
14  * -  the pre/post processing of tokens
15  * -  detecting whether we are in a valid autocompletion position
16  * -  caching of the suggestion list. These are cached for a period of a month on the client side.
17  */
18
19 var getAutocompletionsArrayFromCsv = function(csvString) {
20         var completionsArray = [];
21         csvString.split("\n").splice(1).forEach(function(url) {//remove first line, as this one contains the projection variable
22                 completionsArray.push(url.substring(1, url.length-1));//remove quotes
23         });
24         return completionsArray;
25 }
26
27
28
29 var customPropertyCompleter = function(yasqe) {
30         //we use several functions from the regular property autocompleter (this way, we don't have to re-define code such as determining whether we are in a valid autocompletion position)
31         var returnObj = {
32                 isValidCompletionPosition: function(){return YASQE.Autocompleters.properties.isValidCompletionPosition(yasqe)},
33                 preProcessToken: function(token) {return YASQE.Autocompleters.properties.preProcessToken(yasqe, token)},
34                 postProcessToken: function(token, suggestedString) {return YASQE.Autocompleters.properties.postProcessToken(yasqe, token, suggestedString)}
35         };
36
37         //In this case we assume the properties will fit in memory. So, turn on bulk loading, which will make autocompleting a lot faster
38         returnObj.bulk = true;
39         returnObj.async = true;
40
41         //and, as everything is in memory, enable autoShowing the completions
42         returnObj.autoShow = true;
43
44         returnObj.persistent = "customProperties";//this will store the sparql results in the client-cache for a month.
45         returnObj.get = function(token, callback) {
46                 //all we need from these parameters is the last one: the callback to pass the array of completions to
47                 var sparqlQuery = "PREFIX void: <http://rdfs.org/ns/void#>\n" +
48                 "PREFIX ds: <http://bio2rdf.org/bio2rdf.dataset_vocabulary:>\n" +
49                 "SELECT DISTINCT *\n" +
50                 " { [] void:subset [\n" +
51                 "   void:linkPredicate ?property;\n" +
52                 "  ]\n" +
53                 "} ORDER BY ?property";
54                 $.ajax({
55                         data: {query: sparqlQuery},
56                         url: YASQE.defaults.sparql.endpoint,
57                         headers: {Accept: "text/csv"},//ask for csv. Simple, and uses less bandwidth
58                         success: function(data) {
59                                 callback(getAutocompletionsArrayFromCsv(data));
60                         }
61                 });
62         };
63         return returnObj;
64 };
65 //now register our new autocompleter
66 YASQE.registerAutocompleter('customPropertyCompleter', customPropertyCompleter);
67
68
69 //excellent, now do the same for the classes
70 var customClassCompleter = function(yasqe) {
71         var returnObj = {
72                 isValidCompletionPosition: function(){return YASQE.Autocompleters.classes.isValidCompletionPosition(yasqe)},
73                 preProcessToken: function(token) {return YASQE.Autocompleters.classes.preProcessToken(yasqe, token)},
74                 postProcessToken: function(token, suggestedString) {return YASQE.Autocompleters.classes.postProcessToken(yasqe, token, suggestedString)}
75         };
76         returnObj.bulk = true;
77         returnObj.async = true;
78         returnObj.autoShow = true;
79         returnObj.get = function(token, callback) {
80                 var sparqlQuery = "PREFIX void: <http://rdfs.org/ns/void#>\n" +
81                 "PREFIX ds: <http://bio2rdf.org/bio2rdf.dataset_vocabulary:>\n" +
82                 "SELECT *\n" +
83                 "{ [] void:subset [\n" +
84                 "       a ds:Dataset-Type-Count;\n" +
85                 "       void:class ?type\n"+
86                 "   ]\n" +
87                 "} ORDER BY ?type";
88                 $.ajax({
89                         data: {query: sparqlQuery},
90                         url: YASQE.defaults.sparql.endpoint,
91                         headers: {Accept: "text/csv"},//ask for csv. Simple, and uses less bandwidth
92                         success: function(data) {
93                                 callback(getAutocompletionsArrayFromCsv(data));
94                         }
95                 });
96         };
97         return returnObj;
98 };
99 YASQE.registerAutocompleter('customClassCompleter', customClassCompleter);
100
101 //And, to make sure we don't use the other property and class autocompleters, overwrite the default enabled completers
102 YASQE.defaults.autocompleters = ['customClassCompleter', 'customPropertyCompleter'];
103
104
105 //finally, initialize YASQE
106 var yasqe = YASQE(document.getElementById("yasqe"));