source: main/trunk/greenstone3/web/interfaces/default/transform/layouts/webswing-collage.xsl@ 38932

Last change on this file since 38932 was 38932, checked in by anupama, 5 weeks ago

Intermediate commit. Moved the webswing collage code back out of classifier.xsl and now into layouts/webswing-collage.xsl. classifier.xsl imports layouts/webswing-collage.xsl so it can call the necessary XSL template. layouts/webswing-collage.xsl is therefore not quite the same as pages/webswing-collage.xsl (which will eventually be removed, as we no longer want an extra page for the webswing separate from the actually classifier GS3 built for us), though that's where the effective code is originally taken from.

File size: 10.4 KB
Line 
1<?xml version="1.0" encoding="UTF-8"?>
2<xsl:stylesheet version="1.0"
3 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
4 xmlns:java="http://xml.apache.org/xslt/java"
5 xmlns:util="xalan://org.greenstone.gsdl3.util.XSLTUtil"
6 xmlns:gslib="http://www.greenstone.org/skinning"
7 xmlns:gsf="http://www.greenstone.org/greenstone3/schema/ConfigFormat"
8 extension-element-prefixes="java util"
9 exclude-result-prefixes="java util gsf">
10
11 <xsl:template name="webswing-embed-collage">
12 <link rel="stylesheet" href="/webswing-server/css/style.css" />
13 <div id="webswing-collage" class="webswing-element" data-webswing-instance="webswingInstance0" style="width: 800px; height: 400px;">
14 <div id="loading" class="ws-modal-container">
15 <div class="ws-login">
16 <div class="ws-login-content">
17 <div class="ws-spinner">
18 <div class="ws-spinner-dot-1"><xsl:comment>filler</xsl:comment></div>
19 <div class="ws-spinner-dot-2"><xsl:comment>filler</xsl:comment></div>
20 </div>
21 </div>
22 </div>
23 </div>
24 </div>
25
26 <gsf:script>
27 // https://www.webswing.org/docs/23.2/configure/applet.html
28 // https://www.webswing.org/docs/23.2/configure/swing.html
29 // https://www.webswing.org/docs/23.2/integrate/javascript-api?_h=customArgs%2Cargs#usage-with-customization-and-options
30 // https://www.webswing.org/docs/23.2/integrate/embed.html
31 // https://www.webswing.org/docs/20.1/integrate/urlparams.html
32 // https://www.webswing.org/docs/23.2/integrate/urlparams.html
33 // https://www.webswing.org/docs/20.1/integrate/customize.html
34 // https://www.webswing.org/docs/2.7/integrate/embed.html
35
36 var webswingInstance0 = {
37 options: {
38 autoStart: true,
39 //appletParams: getParam('appletParams'),
40 //appletParams: {"collection":"smallbea", "library":"library"},
41 connectionUrl:'/webswing-server/collage',
42
43 customization: function(injector) {
44 injector.services.base.handleActionEvent = function(actionName, data, binaryData) {
45 //console.log("WebSwing actionEvent callback handler: called with actionName = " + actionName);
46
47 if (actionName === "openURL") {
48 var url = data;
49 // check if a target tab/window name has been specified
50 // TODO: Any better way of passing > 1 string between Java and JavaScript?
51 var index = url.indexOf(" - ");
52 if (index !== -1) {
53 var target = url.substring(index+3); // skip past " - " to get target name
54 url = url.substring(0, index);
55 // Note that target window name is not the same as target window title
56 // https://stackoverflow.com/questions/8051811/how-to-show-window-title-using-window-open
57 window.open(url, target);
58 } else {
59 window.open(url, '_blank');
60 }
61 } else if (actionName == "javaToWebswingJSConsoleLog") {
62 console.log("Got message from java:\n" + data);
63 }
64
65 }
66 }
67
68
69 }
70 };
71
72
73 // The applet jar files can just remain in web/applet where they are compiled up
74
75 if(!webswingInstance0.options.args) {
76 webswingInstance0.options.args="";
77 }
78
79 //https://stackoverflow.com/questions/25203124/how-to-get-base-url-with-jquery-or-javascript
80 var baseURL = window.location.origin+window.location.pathname;
81 // webswingInstance0.options.args += "\"" + baseURL+ "?a=a&amp;rt=d&amp;s=GsdlCollageApplet&amp;c=smallbea\"";
82 var servlet_index = baseURL.indexOf("/"+gs.xsltParams.library_name);
83 if(servlet_index > 0) {
84 baseURL = baseURL.substring(0, servlet_index+1);
85 }
86 var baseClassifier = "CL3";
87 //var gs3CollImgPath = gs.xsltParams.library_name + "/sites/" + gs.xsltParams.site_name + "/collect/" + gs.cgiParams.c;
88 var gs3CollBrowsePath = gs.xsltParams.library_name + "/collection/" + gs.cgiParams.c + "/browse/" + baseClassifier;
89
90 webswingInstance0.options.args += "--baseurl \"" + baseURL + "\"";
91 //webswingInstance0.options.args += " --gsdlversion 3";
92 webswingInstance0.options.args += " --collection " + gs.cgiParams.c;
93 webswingInstance0.options.args += " --library " + gs.xsltParams.library_name;
94 webswingInstance0.options.args += " --documentroot greenstone3";
95 webswingInstance0.options.args += " --hrefMustHave " + gs3CollBrowsePath;
96 webswingInstance0.options.args += " --imageMustNotHave \"interfaces/\"";
97 webswingInstance0.options.args += " --verbosity " + 3;
98 webswingInstance0.options.args += " --imageType " + "\".jpg%.png\"";
99 webswingInstance0.options.args += " --classifier \"" + baseClassifier + ".1\""; // "CL3.1"
100 webswingInstance0.options.args += " --maxDepth 500 --maxDisplay 25 --refreshDelay 1500 --bgcolor \"#96c29a\"";
101
102 // Control the width and height of the Java application launched with webswing
103 // by passing width and height params set to the attributes of the webswing element
104 // https://stackoverflow.com/questions/21851633/get-height-from-style-attribute
105 var w = document.getElementById("webswing-collage").style.width;
106 var suffixIndex = 0;
107 if(w) {
108 suffixIndex = w.indexOf("px");
109 if(suffixIndex>0) {
110 w = w.substring(0, suffixIndex);
111 }
112 webswingInstance0.options.args += " --width " + w;
113 }
114 var h = document.getElementById("webswing-collage").style.height;
115 if(h) {
116 suffixIndex = h.indexOf("px");
117 if(suffixIndex>0) {
118 h = h.substring(0, suffixIndex);
119 }
120 webswingInstance0.options.args += " --height " + h;
121 }
122
123 //console.log("args: " + webswingInstance0.options.args);
124
125 // Allow sending user events to swing application
126 // https://www.webswing.org/docs/20.2/integrate/embed.html
127 //webswingInstance0.setControl(true);
128
129 function getParam(name) {
130 name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
131 var results = new RegExp("[\\?&amp;]" + name + "=([^&amp;#]*)").exec(location.href);
132 return results == null ? null : decodeURIComponent(results[1]);
133 }
134 </gsf:script>
135
136
137 <script data-webswing-global-var="webswing">
138 <xsl:text disable-output-escaping="yes">
139
140 var unloaded = false;
141
142 (function (window, document) {
143 var loader = function () {
144
145 unloaded = false; // reset state, because we are reloading the webswing app
146
147 var baseUrl = '/webswing-server/collage';
148 baseUrl = baseUrl.indexOf("/", baseUrl.length - 1) !== -1 ? baseUrl : (baseUrl + "/");
149 var xmlhttp = new XMLHttpRequest();
150 xmlhttp.onreadystatechange = function () {
151 if (xmlhttp.readyState == XMLHttpRequest.DONE) {
152 var version = xmlhttp.status == 200 ? xmlhttp.responseText : "undefined";
153 var script = document.createElement("script"),
154 tag = document.getElementsByTagName("script")[0];
155 script.src = baseUrl + "javascript/webswing-embed.js?version=" + version;
156 tag.parentNode.insertBefore(script, tag);
157 }
158 };
159 xmlhttp.open("GET", baseUrl + "rest/version", true);
160 xmlhttp.send();
161 };
162
163 var navigatingAway = function () {
164 console.log("*** navigatingAway called");
165
166 // Multiple eventlisteners are registered to call navigatingAway, as not all are
167 // triggered in all contexts (desktop vs mobile vs older browsers)
168 // https://developer.mozilla.org/en-US/docs/Web/API/Window/unload_event
169 // We work with multiple eventlisteners to ensure we cleanup webswing collage app when
170 // user navigates away from page. But we should only do the cleanup once, even if *all*
171 // event handlers got triggered. We use the unloaded var to ensure we cleanup only once
172 if(!unloaded) {
173 if(typeof webswingInstance0.kill === 'function') { // it should exist
174
175 console.log("@@@ Asking webswing to stop the collage application");
176 webswingInstance0.kill();
177 unloaded = true; // do not unload again, if multiple listeners call navigatingAway() callback function
178 console.log("@@@@ unloaded");
179 } // else cannot call method that does not exist
180 } else {
181 console.log("@@@@ already unloaded.");
182 }
183
184 };
185 window.addEventListener ? window.addEventListener("load", loader, false) : window.attachEvent("onload", loader);
186
187
188 // When the user navigates away from this page or reloads it, we want to
189 // shutdown the webswing Java application.
190
191 // https://developer.mozilla.org/en-US/docs/Web/API/Window/unload_event
192 // unload is discouraged as it is not always supported (some mobile devices),
193 // so we listen for pagehide events too. Visibilitychange events happen on minimising win
194 // or another tab getting focus. So do not stop the app on mere visibilitychange.
195 // https://dev.to/amersikira/top-3-ways-to-easily-detect-when-a-user-leaves-a-page-using-javascript-2ce4
196
197 // Pagehide is also fired when user presses back button: https://developer.mozilla.org/en-US/docs/Web/API/Window/pagehide_event
198 // https://stackoverflow.com/questions/15925251/trigger-an-event-when-user-navigates-away
199
200 // Order of registering listeners may matter and be useful:
201 // https://stackoverflow.com/questions/31852179/javascript-event-listeners-firing-order
202 // https://stackoverflow.com/questions/2706109/are-javascript-dom-event-handlers-called-in-order-of-registration
203 // https://medium.com/@olofbaage/javascript-essentials-all-you-need-to-know-about-event-listeners-8ed889bffb8d
204 // Event support detection
205 // https://stackoverflow.com/questions/158673/onbeforeunload-support-detection
206 // https://stackoverflow.com/questions/2877393/detecting-support-for-a-given-javascript-event
207 // http://perfectionkills.com/detecting-event-support-without-browser-sniffing
208
209 // Does onUnload not leave enough time to get webswing to shutdown? OnBeforeUnload works
210 // https://caniuse.com/?search=beforeunload
211 window.addEventListener ? window.addEventListener("beforeunload", navigatingAway, false) : window.attachEvent("onbeforeunload", navigatingAway);
212 window.addEventListener ? window.addEventListener("pagehide", navigatingAway, false) : window.attachEvent("onpagehide", navigatingAway);
213 //window.addEventListener ? window.addEventListener("visibilitychange", navigatingAway, false) : window.attachEvent("onvisibilitychange", navigatingAway);
214
215 })(window, document);
216 </xsl:text>
217 </script>
218
219 </xsl:template>
220
221</xsl:stylesheet>
Note: See TracBrowser for help on using the repository browser.