From 339afd437846ead36b83d0ba06bcf5f971f7fb83 Mon Sep 17 00:00:00 2001 From: mondoboia Date: Fri, 3 Dec 2021 12:13:55 +0100 Subject: [PATCH] check if query API request or SPARQL and deal with it #6 --- config.json | 2 ++ static/js/main.js | 70 ++++++++++++++++++++++++++++------------------- 2 files changed, 44 insertions(+), 28 deletions(-) diff --git a/config.json b/config.json index 97da8cf..6705671 100644 --- a/config.json +++ b/config.json @@ -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", @@ -45,6 +46,7 @@ "query_method": "sparql_endpoint", "github": "", "sparql_endpoint": "https://query.wikidata.org/", + "rest_api": "", "color_code": "", "logo_path": "", "template_mode": "alt", diff --git a/static/js/main.js b/static/js/main.js index a2c1b57..0197b2e 100644 --- a/static/js/main.js +++ b/static/js/main.js @@ -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.");