forked from mhausenblas/sparqlbin.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsparqlbin.js
163 lines (141 loc) · 4.87 KB
/
sparqlbin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
var SPARQLBIN_SERVICE_PORT = 6969;
var SPARQLBIN_EXECUTEQUERY_PATH = "execute";
var SPARQLBIN_SHAREQUERY_PATH = "share";
var SPARQLBIN_PERMALINK_PATH = "q";
var baseURLSPARQLBin = window.location.href;
$(document).ready(function(){
var currentURL = window.location.href;
// check if we're not in standalone mode (on the server) and hence have to monkey patch the base URL
if(baseURLSPARQLBin.indexOf(":" + SPARQLBIN_SERVICE_PORT) == -1) {
// baseURLSPARQLBin = "http://" + window.location.host + ":" + SPARQLBIN_SERVICE_PORT + "/";
baseURLSPARQLBin = "http://" + window.location.host + "/";
}
else {
baseURLSPARQLBin = "http://" + window.location.host + "/";
}
if(currentURL.indexOf("#") != -1){ // we have a paste entry
$.getJSON(baseURLSPARQLBin + SPARQLBIN_PERMALINK_PATH + "/" + currentURL.substring(currentURL.indexOf("#") + 1), function(data) {
$("#endpoint").val(data.endpoint);
$("#query").val(data.querystr);
addMetadata(data.timestamp, currentURL);
});
}
$("#execute").click(function(event){
executeQuery();
});
$("#share").click(function(event){
shareQuery();
});
$("#validate").click(function(event){
validateQuery();
});
});
// shares a SPARQL query using the SPARQLBin web service
function shareQuery() {
var data = { endpoint : "", query : "" };
// some input validation
if($("#endpoint").val().substring(0, "http://".length) == "http://" && $("#query").val() != "") {
data.endpoint = escape($("#endpoint").val());
data.query = $("#query").val();
}
else {
alert("Hey, you haven't provided valid input. Check your query and/or endpoint please and try again.");
return;
}
$.ajax({
type: "POST",
url: baseURLSPARQLBin + SPARQLBIN_SHAREQUERY_PATH,
data: data,
dataType : "json",
success: function(d){
if(d) {
// $("#permalink").html("You can now share your query via this <a href='" + "#" + d.entryid + "'>permalink</a>.");
// $("#results").slideDown('200');
window.location = "#" + d.entryid;
addMetadata("Now", "#" + d.entryid);
}
},
error: function(msg){
$("#out").html("<p>There was a problem sharing the query:</p><code>" + msg.responseText + "</cdoe>");
$("#results").slideDown('200');
}
});
}
// executes a SPARQL query using the SPARQLBin web service
function executeQuery() {
var data = { endpoint : "", query : "" };
// some input validation
if($("#endpoint").val().substring(0, "http://".length) == "http://" && $("#query").val() != "") {
data.endpoint = escape($("#endpoint").val());
data.query = $("#query").val();
}
else {
alert("Hey, you haven't provided valid input. Check your query and/or endpoint please and try again.");
return;
}
$("#results").slideDown('200');
$.ajax({
type: "POST",
url: baseURLSPARQLBin + SPARQLBIN_EXECUTEQUERY_PATH,
data: data,
dataType : "json",
success: function(d){
if(d) {
renderResults(d);
}
},
error: function(msg){
$("#out").html("<p>There was a problem executing the query:</p><code>" + msg.responseText + "</cdoe>");
}
});
}
// validates a SPARQL query using sparql.org via the SPARQLBin web service
function validateQuery() {
var validationURL = "http://sparql.org/validate/query?query=" + escape($("#query").val());
var validationResult = $("<iframe />", {
id: "validationresults",
src: validationURL,
width: "100%",
height: "20em"
});
$("#results").slideDown('200');
$("#out").html("<p style='font-size: 100%; margin: 0 0 .5em 0'>Validation results provided by <a href='http://sparql.org/' title='SPARQLer - An RDF Query Server' target='_blank'>sparql.org</a>:</p>");
$("#out").append(validationResult);
}
// renders the results
function renderResults(data){
var vars = Array();
var buffer = "";
$("#out").html("");
// we had a SELECT query - render results as table
if(data.head.vars) {
buffer = "<table><tr>";
for(rvar in data.head.vars) { // table head with vars
buffer += "<th>" + data.head.vars[rvar] + "</th>";
}
buffer += "</tr>";
for(entry in data.results.bindings) { // iterate over rows
if(entry%2) buffer += "<tr>";
else buffer += "<tr class='invrow'>";
for(rvar in data.head.vars) { // iterate over columns per row
var col = data.head.vars[rvar];
var result = data.results.bindings[entry][col].value;
if(data.results.bindings[entry][col].type == 'uri') // if the result is an URI
buffer += "<td><a href='" + result + "' target='_blank'>" + result + "</a></td>";
else
buffer += "<td>" + result + "</td>";
}
buffer += "</tr>";
}
buffer += "</table>";
}
// we had a ASK query - render results as single value
if(data.boolean) {
buffer = "<p style='font-size:140%'>" + data.boolean + "</p>";
}
$("#out").append(buffer);
}
function addMetadata(timestamp, permalink){
$("#metadata").html("");
$("#query-container").prepend("<div id='metadata'>Last update: " + timestamp + " | <a href='" + permalink + "'>Permalink</a></div>");
}