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