-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
275 lines (251 loc) · 10 KB
/
index.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
var $suggestions = $('<table>').addClass('table conditions').width(300).height(400).hide();
var suggestions_visible = false;
var $table = $('<table>');
var query;
var timer;
var map;
function createUrl(query) {
return 'http://data.plan4all.eu/sparql?default-graph-uri=&query=' + encodeURIComponent(query) + '&should-sponge=&format=application%2Fsparql-results%2Bjson&timeout=0&debug=on'
}
function createUrlForHtml(query) {
return 'http://data.plan4all.eu/sparql?default-graph-uri=&query=' + encodeURIComponent(query) + '&should-sponge=&format=text%2Fhtml&timeout=0&debug=on'
}
$(document).ready(function() {
$('body').append($suggestions);
$table.addClass('table');
})
$('#run').click(function() {
var squery = query;
$.ajax({
url: createUrlForHtml(query.replace("<extent>", $('#extent').val()))
}).done(function(r) {
$("#results").html(r);
$("#results table").addClass('table table-condensed');
})
});
$('#show_publish').click(function() {
var url = 'http://data.plan4all.eu/sparql?default-graph-uri=&query={0}&should-sponge=&format=application%2Fsparql-results%2Bjson&timeout=0&debug=on'.format(encodeURIComponent($('.generated_code').text()));
url = url.replace('%3Cextent%3E','<extent>');
$('#resource_locator').html(url);
$('#publish_dialog').modal('show');
});
function createRow(r, extra_row) {
var $combo = $('<select>').addClass('form-control property');
var $operator_dropdown = $('<select>').addClass('form-control');
$operator_dropdown.addClass('operator').append($('<option>').html('Equals')).append($('<option>').html('Contains'));
var $condition_input = $('<input>').addClass('form-control condition');
var $values_btn = $('<button>').addClass('btn btn-default').html('...');
var $condition_div = $('<div>').addClass('input-group').css('width', '281px').append(
$condition_input).append($('<span>').addClass('input-group-btn').append($values_btn));
var $del_btn = $('<button>').addClass('btn btn-danger').html('<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>');
$del_btn.click(function() {
$(this).parent().parent().remove();
});
$(r.results.bindings).each(function() {
var r = this;
$combo.append($('<option>').html(r.p.value));
})
var $tr = $('<tr>').addClass('condition_row').append(
$('<td></td>').append(extra_row ? '<span>AND </span>' : '').append($combo)
)
.append(
$('<td>').append($operator_dropdown)
).append(
$('<td>').append($condition_div)
).append(
$('<td>').append($del_btn)
)
$combo.val('http://www.w3.org/2000/01/rdf-schema#label');
$operator_dropdown.val('Contains');
$values_btn.click(suggestValues);
$condition_input.change(generateSparql);
$combo.change(generateSparql);
$operator_dropdown.change(generateSparql);
return $tr;
}
function refresh() {
$('#loader4suggestions').hide();
var query = 'SELECT DISTINCT ?p FROM <http://www.sdi4apps.eu/poi.rdf> WHERE {?s ?p ?o} ORDER BY ?p';
$.ajax({
url: createUrl(query)
}).done(function(r) {
var last_poi = '';
var last_date = '';
$table.append($('<tr>')
.append($('<th>').html('Filter field'))
.append($('<th>').html('Operator'))
.append($('<th>').html('Value'))
);
$table.append(createRow(r));
$('#loader4table').hide();
$('#table_container').html($table);
$('#table_container').append($('<p>').css('margin', '7px').append(
$('<button>').addClass('btn btn-primary').html('+').click(function() {
$table.append(createRow(r, true));
})))
$('.condition, .operator, .property').change(function() {
generateSparql();
});
});
}
function suggestValues() {
var $td = $(this).parent().parent().parent();
if (suggestions_visible) {
$suggestions.hide();
suggestions_visible = false;
} else {
$('#loader4suggestions').appendTo($td);
$('#loader4suggestions').show();
var property = $(".property option:selected", $(this).parent().parent().parent().parent()).text();
var query = 'SELECT DISTINCT ?o FROM <http://www.sdi4apps.eu/poi.rdf> WHERE {?s <' + property + '> ?o} LIMIT 20';
$.ajax({
url: createUrl(query)
}).done(function(r) {
$suggestions.html('');
$(r.results.bindings).each(function() {
var r = this;
$suggestions.append($('<tr>').append(
$('<td>').append($('<a href="#">').html(r.o.value).click(
function() {
$("input", $(this).parent().parent().parent().parent().parent()).val($(this).html())
generateSparql();
$suggestions.hide();
suggestions_visible = false;
}))
));
$('#loader4suggestions').hide();
})
$suggestions.appendTo($td);
$suggestions.show();
suggestions_visible = true;
})
}
}
function generateSparql() {
var conditions = [];
var criteria_ix = 0;
$('.condition_row', $table).each(function() {
var $tr = $(this);
if ($('.property', $tr).length > 0) {
var criteria_field = 'c' + (criteria_ix++);
var val = $('.condition', $tr).val();
var operator = $('.operator option:selected', $tr).text();
var property = $('.property option:selected', $tr).text();
if (operator == 'Equals' && val.indexOf('http') >= 0)
conditions.push(['?o ', '<' + property + '>', '<' + val + '>'].join(' '));
if (operator == 'Equals' && val.indexOf('http') == -1)
conditions.push("?o <" + property + "> ?" + criteria_field + ". FILTER(str(?" + criteria_field + ") = <" + val + ">)");
if (operator == 'Contains' && val.indexOf('http') >= 0)
conditions.push("?o <" + property + "> ?" + criteria_field + ". FILTER(regex(str(?" + criteria_field + "), <" + val + ">))");
if (operator == 'Contains' && val.indexOf('http') == -1)
conditions.push("?o <" + property + "> ?" + criteria_field + ". FILTER(regex(str(?" + criteria_field + "), \"" + val + "\"))");
}
})
var graph = $('#graph').val();
var geom = $('#geometry').val();
conditions.push('FILTER(isBlank(?geom) = false)');
conditions.push('<extent>');
var sub_query = 'SELECT ?o FROM <' + graph + '> WHERE {?o <' + geom + '> ?geom. ' + conditions.join('. ') + '} LIMIT 100';
query = 'SELECT ?o ?p ?s FROM <' + graph + '> WHERE { ?o ?p ?s. {' + sub_query + '}}';
$(".generated_code").text(query);
}
map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM(),
title: "OpenStreetMap",
base: true,
visible: true,
removable: false
})
],
view: new ol.View({
center: ol.proj.fromLonLat([12.41, 48.82]),
zoom: 4
})
});
map.getView().on('change:center', function(e) {
if (timer != null) clearTimeout(timer);
timer = setTimeout(function() {
extentChanged(map.getView().calculateExtent(map.getSize()));
}, 500);
});
map.getView().on('change:resolution', function(e) {
if (timer != null) clearTimeout(timer);
timer = setTimeout(function() {
extentChanged(map.getView().calculateExtent(map.getSize()));
}, 500);
});
function extentChanged(e) {
var b = ol.proj.transformExtent(e, map.getView().getProjection(), 'EPSG:4326');
$('#extent').val('FILTER(bif:st_intersects(bif:st_geomfromtext("BOX({0} {1}, {2} {3})"), ?geom)).'.format(b[0], b[1], b[2], b[3]));
}
if (!String.prototype.format) {
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined' ?
args[number] :
match;
});
};
}
generateSparql();
refresh();
$('#publish').click(function() {
//$.cookie("JSESSIONID", "1");
var contact = {
name: $('#first_name').val(),
surname: $('#last_name').val(),
organization: $('#organization').val(),
email: $('#email').val(),
adress: $('#address').val(),
city: $('#city').val()
}
var jobj = {
title: $('#title').val(),
token: $.cookie('JSESSIONID'),
description: $('#description').val(),
resource_locator: $('#resource_locator').val(),
keywords: $('#keywords').val(),
contactPoint: contact,
lineage: $('#lineage').val(),
extent: ol.proj.transformExtent(map.getView().calculateExtent(map.getSize()), map.getView().getProjection(), 'EPSG:4326'),
type: 'service',
serviceType: 'data',
issued: (new Date()).toISOString(),
language: 'en-US',
publisher: 'guest',
identifier: 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0,
v = c == 'x' ? r : r & 0x3 | 0x8;
return v.toString(16);
}),
charset: 'utf-8',
crs: 'EPSG:4326',
linkage: '',
format: 'application/sparql-results+json'
}
var sjson = JSON.stringify(jobj, null, 2);
/* $('#json_result').html(sjson);
$('#publish_json_dialog').modal('show');*/
$.ajax({
url: "/php/metadata/util/rest.php",
data: sjson,
cache: false,
method: 'POST',
async: false,
dataType: 'json',
success: function(e){
$('#publish_dialog').modal('hide');
$('#publish_result_success').modal('show');
console.log(e);
},
error: function(e){
$('#publish_dialog').modal('hide');
$('#publish_result_failure').modal('show');
console.log(e);
}
})
});