server/src/resources/assets/js/sparqlclient.js
changeset 386 c731ab9b934d
child 387 7fba86fa8604
equal deleted inserted replaced
385:f8200c5482ec 386:c731ab9b934d
       
     1 // sparql edit function
       
     2 
       
     3 // ASK, null
       
     4 const ASK_RESULT_FORMAT = [
       
     5     ["HTML", "text/html"]
       
     6 ];
       
     7 
       
     8 // SELECT
       
     9 const SELECT_RESULT_FORMAT = [
       
    10     ["HTML", "text/html"],
       
    11     ["SPARQL/CSV", "text/csv"],
       
    12     ["SPARQL/JSON", "application/sparql-results+json"],
       
    13     ["SPARQL/XML", "application/sparql-results+xml"],
       
    14     ["SPARQL/TSV", "text/tab-separated-values"],
       
    15     ["BINARY", "application/x-binary-rdf-results-table"]
       
    16 ];
       
    17 
       
    18 // DESCRIBE,CONSTRUCT
       
    19 const GRAPH_RESULT_FORMAT = [
       
    20     ["HTML", "text/html"],
       
    21     ["N-Triples", "application/n-triples"],
       
    22     ["RDF/XML", "application/rdf+xml"],
       
    23     ["Turtle", "text/turtle"],
       
    24     ["N3", "text/n3"],
       
    25     ["RDF/JSON", "application/rdf+json"],
       
    26     ["TriG", "application/trig"],
       
    27     ["N-Quads", "application/n-quads"],
       
    28     ["BinaryRDF", "application/x-binary-rdf"],
       
    29     ["TriX", "application/trix"],
       
    30     ["JSON-LD", "application/ld+json"]
       
    31 ];
       
    32 
       
    33 function setOutputFormatSelect(selectElt, optionsList) {
       
    34     var keys = $.map(optionsList, function(o) { return o[0];});
       
    35     var selectedKey = $(":selected",selectElt).text();
       
    36     if(!selectedKey || $.inArray(selectedKey, keys) === -1) {
       
    37         selectedKey = "HTML";
       
    38     }
       
    39     selectElt.empty();
       
    40     var innerHtml = "";
       
    41     $.each(optionsList, function(i, o) {
       
    42         innerHtml += "<option value=\""+o[1]+"\""+ ((o[0]==selectedKey)?" selected=\"selected\" ":"") +">"+o[0]+"<options>";
       
    43     });
       
    44 
       
    45     selectElt.append(innerHtml);
       
    46 }
       
    47 
       
    48 var currentQueryType = null;
       
    49 
       
    50 function updateQueryType(queryType) {
       
    51     var resultFormats = ASK_RESULT_FORMAT;
       
    52     if(currentQueryType != queryType) {
       
    53         if(queryType == "SELECT") {
       
    54             resultFormats = SELECT_RESULT_FORMAT;
       
    55         } else if (queryType == "DESCRIBE" || queryType == "CONSTRUCT") {
       
    56             resultFormats = GRAPH_RESULT_FORMAT;
       
    57         }
       
    58         setOutputFormatSelect($("#format"),resultFormats);
       
    59         currentQueryType = queryType;
       
    60     }
       
    61 }
       
    62 
       
    63 function getSubmitState(editor) {
       
    64     var parseErrors = $(".parseErrorIcon").length;
       
    65     return editor.getQueryMode() === 'query' && parseErrors == 0 && !/^\s*$/.test(editor.getValue());
       
    66 }
       
    67 
       
    68 function initSparqlEditor() {
       
    69     var yasqe = YASQE.fromTextArea($('#query').get(0), {
       
    70         sparql: {
       
    71             showQueryButton: false,
       
    72             //endpoint: "{{ route('sparql_proxy') }}",
       
    73             //requestMethod: "GET",
       
    74             //acceptHeaderGraph: "application/rdf+json,/;q=0.9"
       
    75             //acceptHeaderGraph: "text/turtle,/;q=0.9",
       
    76             //acceptHeaderSelect: "application/x-turtle,/;q=0.9",
       
    77         }
       
    78     });
       
    79     yasqe.on("update", function(instance) {
       
    80         var queryType = instance.getQueryType();
       
    81         updateQueryType(queryType);
       
    82         var submitState = getSubmitState(instance);
       
    83         $("#submit-query-form").prop('disabled', !submitState );
       
    84         $("#format").prop('disabled', !submitState );
       
    85         $("#timeout").prop('disabled', !submitState );
       
    86     });
       
    87 
       
    88     $("#query-form").submit(function() {
       
    89         return getSubmitState(yasqe);
       
    90     });
       
    91 
       
    92     $("#limits-choices a").click(function(e) {
       
    93         e.preventDefault();
       
    94         $('#limit').val($(e.target).text());
       
    95     });
       
    96 
       
    97     // var yasr = YASR($('#results').get(0), {
       
    98     //     getUsedPrefixes: yasqe.getPrefixesFromQuery,
       
    99     //     useGoogleCharts: false,
       
   100     //     outputPlugins: ["table", "error", "boolean", "rawResponse", "pivot"],
       
   101     // });
       
   102     // yasqe.options.sparql.callbacks.complete = yasr.setResponse;
       
   103 }