View source with formatted comments or as raw
    1/*  Part of ClioPatria
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2013-2018, VU University Amsterdam
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(yasgui,
   36          [ has_yasgui/0
   37          ]).   38:- use_module(library(http/http_dispatch)).   39:- use_module(library(http/html_write)).   40:- use_module(library(http/js_write)).   41:- use_module(library(http/http_server_files)).   42:- use_module(library(http/html_head)).   43
   44:- use_module(api(json)).               % get /json/prefixes
   45
   46:- http_handler(yasgui('index.html'), yasgui_editor, [id(yasgui)]).   47:- http_handler(yasqe(.), serve_files_in_directory(yasqe), [prefix]).   48:- http_handler(yasr(.), serve_files_in_directory(yasr), [prefix]).   49
   50%!  yasgui_editor(+Request)
   51%
   52%   HTTP handler that presents the YASGUI SPARQL editor.
   53
   54yasgui_editor(_Request) :-
   55    has_yasgui,
   56    !,
   57    reply_html_page(
   58        cliopatria(plain),
   59        title('YASGUI SPARQL Editor'),
   60        \yasgui_page).
   61yasgui_editor(_Request) :-
   62    reply_html_page(cliopatria(default),
   63                    title('No YASQE/YASR installed'),
   64                    \no_yasgui).
   65
   66
   67%!  has_yasgui is semidet.
   68%
   69%   True if the YASGUI SPARQL editor is installed.
   70
   71has_yasgui :-
   72    Options = [ access(read), file_errors(fail) ],
   73    absolute_file_name(yasqe('yasqe.bundled.min.js'), _, Options),
   74    absolute_file_name(yasr('yasr.bundled.min.js'), _, Options).
   75
   76
   77yasgui_page -->
   78    { http_link_to_id(sparql_query, [], SparqlLocation),
   79      http_link_to_id(json_prefixes, [], JSONPrefixes),
   80      http_link_to_id(list_resource, [], ListResource)
   81    },
   82    html_requires(yasqe('yasqe.bundled.min.js')),
   83    html_requires(yasqe('yasqe.min.css')),
   84    html_requires(yasr('yasr.bundled.min.js')),
   85    html_requires(yasr('yasr.min.css')),
   86    html([ div(id(yasqe), []),
   87           div(id(yasr), [])
   88         ]),
   89
   90    js_script({|javascript(SparqlLocation, JSONPrefixes, ListResource)||
   91                   window.onload=function(){
   92  var yasqe = YASQE(document.getElementById("yasqe"), {
   93                                 sparql: { endpoint: SparqlLocation,
   94                                           showQueryButton: true
   95                                         }
   96                             });
   97
   98  var serverPrefixes;                   // TBD: re-fetch if out-of-date?
   99
  100  function usedPrefixes() {
  101    var prefixmap = yasqe.getPrefixesFromQuery();
  102    if ( serverPrefixes ) {
  103      for(var key in serverPrefixes) {
  104        var yasrKey = key+":";
  105        if ( !prefixmap[yasrKey] )
  106          prefixmap[yasrKey] = serverPrefixes[key];
  107      }
  108    }
  109    return prefixmap;
  110  }
  111
  112  YASR.plugins.table.defaults.callbacks.onCellClick = function(td, event) {
  113    var href = YASR.$(td).find("a").attr("href");
  114
  115    if (href) {
  116      window.location = ListResource + "?r=" + encodeURIComponent(href);
  117      event.preventDefault();
  118    }
  119  };
  120
  121  var yasr = {};
  122
  123  YASQE.$.ajax({ url: JSONPrefixes,
  124           dataType: "json",
  125           contentType: 'application/json',
  126           success: function(data, status) {
  127                        serverPrefixes = data;
  128                    },
  129           complete: function() {
  130                        yasr = YASR(document.getElementById("yasr"), {
  131                          getUsedPrefixes: usedPrefixes
  132                        });
  133                     }
  134         });
  135
  136  /**
  137  * Set some of the hooks to link YASR and YASQE
  138  */
  139
  140  yasqe.options.sparql.callbacks.success = function(data, textStatus, xhr) {
  141    yasr.setResponse({response: data, contentType: xhr.getResponseHeader("Content-Type")});
  142  };
  143
  144  yasqe.options.sparql.callbacks.error = function(xhr, textStatus, errorThrown) {
  145    var exceptionMsg = textStatus + " (response status code " + xhr.status + ")";
  146    if (errorThrown && errorThrown.length)
  147      exceptionMsg += ": " + errorThrown;
  148    yasr.setResponse({exception: exceptionMsg});
  149  };
  150};
  151              |}).
  152
  153
  154%!  no_yasgui//
  155%
  156%   Display a message indicating the user how to install YASQE/YASR
  157
  158no_yasgui -->
  159    { absolute_file_name(cliopatria(.), CD0,
  160                         [ file_type(directory),
  161                           access(read)
  162                         ]),
  163      prolog_to_os_filename(CD0, ClioHome)
  164    },
  165    html_requires(pldoc),
  166    html([ h1('YASGUI (YASQE and YASR) is not installed'),
  167           p([ 'Please run the following command in the ClioPatria ',
  168               'installation directory "~w" to install YASQE/YASR.'-[ClioHome]
  169             ]),
  170           pre(class(code),
  171               [ 'git submodule update --init web/yasqe web/yasr'
  172               ])
  173         ])