View source with raw comments or as raw
    1/*  Part of ClioPatria
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@cs.vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (C): 2013-2014 VU University Amsterdam
    7
    8    This program is free software; you can redistribute it and/or
    9    modify it under the terms of the GNU General Public License
   10    as published by the Free Software Foundation; either version 2
   11    of the License, or (at your option) any later version.
   12
   13    This program is distributed in the hope that it will be useful,
   14    but WITHOUT ANY WARRANTY; without even the implied warranty of
   15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16    GNU General Public License for more details.
   17
   18    You should have received a copy of the GNU General Public
   19    License along with this library; if not, write to the Free Software
   20    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   21
   22    As a special exception, if you link this library with other files,
   23    compiled with a Free Software compiler, to produce an executable, this
   24    library does not by itself cause the resulting executable to be covered
   25    by the GNU General Public License. This exception does not however
   26    invalidate any other reasons why the executable file might be covered by
   27    the GNU General Public License.
   28*/
   29
   30:- module(yasgui,
   31	  [ has_yasgui/0
   32	  ]).   33:- use_module(library(http/http_dispatch)).   34:- use_module(library(http/html_write)).   35:- use_module(library(http/js_write)).   36:- use_module(library(http/http_server_files)).   37:- use_module(library(http/html_head)).   38
   39:- use_module(api(json)).		% get /json/prefixes
   40
   41:- http_handler(yasgui('index.html'), yasgui_editor, [id(yasgui)]).   42:- http_handler(yasqe(.), serve_files_in_directory(yasqe), [prefix]).   43:- http_handler(yasr(.), serve_files_in_directory(yasr), [prefix]).
 yasgui_editor(+Request)
HTTP handler that presents the YASGUI SPARQL editor.
   49yasgui_editor(_Request) :-
   50	has_yasgui, !,
   51	reply_html_page(
   52	    cliopatria(plain),
   53	    title('YASGUI SPARQL Editor'),
   54	    \yasgui_page).
   55yasgui_editor(_Request) :-
   56	reply_html_page(cliopatria(default),
   57			title('No YASQE/YASR installed'),
   58			\no_yasgui).
 has_yasgui is semidet
True if the YASGUI SPARQL editor is installed.
   65has_yasgui :-
   66	Options = [ access(read), file_errors(fail) ],
   67	absolute_file_name(yasqe('yasqe.bundled.min.js'), _, Options),
   68	absolute_file_name(yasr('yasr.bundled.min.js'), _, Options).
   69
   70
   71yasgui_page -->
   72	{ http_link_to_id(sparql_query, [], SparqlLocation),
   73	  http_link_to_id(json_prefixes, [], JSONPrefixes),
   74	  http_link_to_id(list_resource, [], ListResource)
   75	},
   76	html_requires(yasqe('yasqe.bundled.min.js')),
   77	html_requires(yasqe('yasqe.min.css')),
   78	html_requires(yasr('yasr.bundled.min.js')),
   79	html_requires(yasr('yasr.min.css')),
   80	html([ div(id(yasqe), []),
   81	       div(id(yasr), [])
   82	     ]),
   83
   84	js_script({|javascript(SparqlLocation, JSONPrefixes, ListResource)||
   85		   window.onload=function(){
   86  var yasqe = YASQE(document.getElementById("yasqe"), {
   87				 sparql: { endpoint: SparqlLocation,
   88					   showQueryButton: true
   89					 }
   90			     });
   91
   92  var serverPrefixes;			// TBD: re-fetch if out-of-date?
   93
   94  function usedPrefixes() {
   95    var prefixmap = yasqe.getPrefixesFromQuery();
   96    if ( serverPrefixes ) {
   97      for(var key in serverPrefixes) {
   98	var yasrKey = key+":";
   99        if ( !prefixmap[yasrKey] )
  100	  prefixmap[yasrKey] = serverPrefixes[key];
  101      }
  102    }
  103    return prefixmap;
  104  }
  105
  106  YASR.plugins.table.defaults.callbacks.onCellClick = function(td, event) {
  107    var href = YASR.$(td).find("a").attr("href");
  108
  109    if (href) {
  110      window.location = ListResource + "?r=" + encodeURIComponent(href);
  111      event.preventDefault();
  112    }
  113  };
  114
  115  var yasr = {};
  116
  117  YASQE.$.ajax({ url: JSONPrefixes,
  118	   dataType: "json",
  119	   contentType: 'application/json',
  120	   success: function(data, status) {
  121			serverPrefixes = data;
  122		    },
  123	   complete: function() {
  124			yasr = YASR(document.getElementById("yasr"), {
  125			  getUsedPrefixes: usedPrefixes
  126			});
  127		     }
  128	 });
  129
  130  /**
  131  * Set some of the hooks to link YASR and YASQE
  132  */
  133
  134  yasqe.options.sparql.callbacks.success = function(data, textStatus, xhr) {
  135    yasr.setResponse({response: data, contentType: xhr.getResponseHeader("Content-Type")});
  136  };
  137
  138  yasqe.options.sparql.callbacks.error = function(xhr, textStatus, errorThrown) {
  139    var exceptionMsg = textStatus + " (response status code " + xhr.status + ")";
  140    if (errorThrown && errorThrown.length)
  141      exceptionMsg += ": " + errorThrown;
  142    yasr.setResponse({exception: exceptionMsg});
  143  };
  144};
  145		   |}).
 no_yasgui//
Display a message indicating the user how to install YASQE/YASR
  152no_yasgui -->
  153	{ absolute_file_name(cliopatria(.), CD0,
  154			     [ file_type(directory),
  155			       access(read)
  156			     ]),
  157	  prolog_to_os_filename(CD0, ClioHome)
  158	},
  159	html_requires(pldoc),
  160	html([ h1('YASGUI (YASQE and YASR) is not installed'),
  161	       p([ 'Please run the following command in the ClioPatria ',
  162		   'installation directory "~w" to install YASQE/YASR.'-[ClioHome]
  163		 ]),
  164	       pre(class(code),
  165		   [ 'git submodule update --init web/yasqe web/yasr'
  166		   ])
  167	     ])