Skip to content

Commit

Permalink
check if query API request or SPARQL and deal with it #6
Browse files Browse the repository at this point in the history
  • Loading branch information
mondoboia committed Dec 3, 2021
1 parent 6573da4 commit 339afd4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 28 deletions.
2 changes: 2 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"query_method": "sparql_endpoint",
"github": "",
"sparql_endpoint": "https://projects.dharc.unibo.it/musow/sparql",
"rest_api": "http://127.0.0.1:8081/musow/v1",
"color_code": "",
"logo_path": "",
"template_mode": "standard",
Expand Down Expand Up @@ -45,6 +46,7 @@
"query_method": "sparql_endpoint",
"github": "",
"sparql_endpoint": "https://query.wikidata.org/",
"rest_api": "",
"color_code": "",
"logo_path": "",
"template_mode": "alt",
Expand Down
70 changes: 42 additions & 28 deletions static/js/main.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,50 @@
window.onload = function () {
var sparqlEndpoint = data.sparql_endpoint;
if(data.count) {
data.count.forEach(element => {
var sparqlQuery = element.query;
var encoded = encodeURIComponent(sparqlQuery);
var label = element.label;/**/
$.ajax({
type: 'GET',
url: sparqlEndpoint + '?query=' + encoded,
headers: {Accept: 'application/sparql-results+json; charset=utf-8'},
success: function (returnedJson) {
for (i = 0; i < returnedJson.results.bindings.length; i++) {
var count = returnedJson.results.bindings[i].count.value;
// create div to set the column
var generalDiv = document.createElement("div");
generalDiv.className = "col-lg-4";
// create div to contain number and label
var countDiv = document.createElement("div");
countDiv.className = "card-body option-2b";
countDiv.appendChild(document.createTextNode(count));
generalDiv.appendChild(countDiv);
// create and append p for label
var labelP = document.createElement("p");
labelP.appendChild(document.createTextNode(label));
countDiv.appendChild(labelP);
var query = element.query;
// check if the query is an API request
if (query.startsWith('http')) {
alert('There is an API request.');
// $.ajax({
// type: 'GET',
// url: query,
// headers: {Accept: 'application/json'},
// success: function (returnedJson) {
// do things
// }
// }
} else {
// if it is a sparql query
var encoded = encodeURIComponent(query);
var sparqlEndpoint = data.sparql_endpoint;
var label = element.label;
$.ajax({
type: 'GET',
url: sparqlEndpoint + '?query=' + encoded,
headers: {Accept: 'application/sparql-results+json; charset=utf-8'},
success: function (returnedJson) {
for (i = 0; i < returnedJson.results.bindings.length; i++) {
var count = returnedJson.results.bindings[i].count.value;
// create div to set the column
var generalDiv = document.createElement("div");
generalDiv.className = "col-lg-4";
// create div to contain number and label
var countDiv = document.createElement("div");
countDiv.className = "card-body option-2b";
countDiv.appendChild(document.createTextNode(count));
generalDiv.appendChild(countDiv);
// create and append p for label
var labelP = document.createElement("p");
labelP.appendChild(document.createTextNode(label));
countDiv.appendChild(labelP);

// get container and append
var container = document.getElementById("count_container");
container.appendChild(generalDiv);
// get container and append
var container = document.getElementById("count_container");
container.appendChild(generalDiv);
}
}
}
})
})
}
})
} else {
alert("No COUNT found.");
Expand Down

0 comments on commit 339afd4

Please sign in to comment.