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)). 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]).
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).
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 |}).
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 ])