Changeset 38925 for main


Ignore:
Timestamp:
2024-04-12T22:15:35+12:00 (4 weeks ago)
Author:
anupama
Message:

I can't get the webswing JavaScript to webswing communication to work. performAction doesn't appear to get called no matter what I try. Am I supposed to register it somewhere? The documentation doesn't mention this or how it's to be done.

Location:
main/trunk/greenstone3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/applet/GsdlCollageApplet/GsdlCollageApplet.java

    r38922 r38925  
    22
    33import org.webswing.toolkit.api.WebswingUtil;
     4import org.webswing.toolkit.api.action.WebActionEvent;
     5import org.webswing.toolkit.api.action.WebActionListener;
    46
    57//import java.applet.Applet;
     
    161163    //}
    162164
     165    // Handle getting called by webswing JavaScript:
     166    // when the user navigates away from the webswing app's web page, do cleanup
     167    // https://www.webswing.org/docs/23.2/integrate/jslink.html#invoking-java-from-javascript
     168    // https://vaadin.com/directory/component/webswing-vaadin1
     169    org.webswing.toolkit.api.WebswingUtil.getWebswingApi().addBrowserActionListener(new org.webswing.toolkit.api.action.WebActionListener() {
     170        @Override
     171        public void actionPerformed(org.webswing.toolkit.api.action.WebActionEvent actionEvent) {
     172            switch (actionEvent.getActionName()) {
     173            case "navigatingAway":
     174            // do cleanup
     175            System.err.println("@@@@ In Java collage addBrowserActionListener on navigatingAway - called");
     176            GsdlCollageApplet.this.stopRunning();
     177            break;
     178            case "testJavaCall":
     179            org.webswing.toolkit.api.WebswingUtil.getWebswingApi().sendActionEvent("javaToWebswingJSConsoleLog", "GsdlCollageApplet got TEST call !!", null);
     180            //WebswingUtil.getWebswingApi().sendActionEvent("@@@ javaToWebswingJSConsoleLog", "GsdlCollageApplet - testjavaCall handler got your message", null);
     181            }
     182        }
     183        });
     184   
    163185    this.addMouseListener(new CollageMouseAdapter());
    164186    }
     
    724746        d.width = collageApp.getWidth();
    725747        statusBar.setPreferredSize(d);
    726         //statusBar.setPreferredSize(new Dimension(collageApp.getWidth(), EXTRA_HEIGHT));   
     748        //statusBar.setPreferredSize(new Dimension(collageApp.getWidth(), EXTRA_HEIGHT));
    727749        }
    728750       
     
    737759        frame.addWindowListener(new WindowAdapter() {
    738760        public void windowClosing(WindowEvent e) {
     761            // WHY doesn't javascript console log unless I prefix webswing package name??
     762            //WebswingUtil.getWebswingApi().sendActionEvent("@@@ javaToWebswingJSConsoleLog", "GsdlCollageApplet - quitting now", null);
     763            org.webswing.toolkit.api.WebswingUtil.getWebswingApi().sendActionEvent("javaToWebswingJSConsoleLog", "GsdlCollageApplet about to QUIT !!", null);
     764           
    739765            collageApp.showStatus("Stopping threads");
    740766            System.err.println("\n\n*** Closing applet: stopping threads...");
    741767            collageApp.stopRunning();
     768           
    742769            //collageApp.stop();
    743770            //collageApp.destroy();
  • main/trunk/greenstone3/web/interfaces/default/transform/pages/webswing-collage.xsl

    r38890 r38925  
    8282    <xsl:template name="webswing-embed-collage">
    8383      <link rel="stylesheet" href="/webswing-server/css/style.css" />
    84 
    8584      <div id="webswing-collage" class="webswing-element" data-webswing-instance="webswingInstance0" style="width: 800px; height: 400px;">
    8685        <div id="loading" class="ws-modal-container">
     
    131130             window.open(url, '_blank');
    132131          }
     132        } else if (actionName == "javaToWebswingJSConsoleLog") {
     133            console.log("Got message from java:\n" + data);
    133134        }
    134135       
     
    201202  <script data-webswing-global-var="webswing">
    202203    <xsl:text disable-output-escaping="yes">
     204
     205      var doButtonClick = function() {
     206      console.log("button clicked");
     207          webswingInstance0.performAction({actionName: "testJavaCall"});
     208      }
     209      var unloaded = false;
    203210    (function (window, document) {
    204211        var loader = function () {
    205             var baseUrl = '/webswing-server/collage';
     212
     213        unloaded = false; // reset state, because we are reloading the webswing app
     214   
     215        var baseUrl = '/webswing-server/collage';
    206216            baseUrl = baseUrl.indexOf("/", baseUrl.length - 1) !== -1 ? baseUrl : (baseUrl + "/");
    207217            var xmlhttp = new XMLHttpRequest();
     
    218228            xmlhttp.send();
    219229        };
     230
     231    var navigatingAway = function () {
     232       console.log("*** navigatingAway called");
     233
     234       // Multiple eventlisteners are registered to call navigatingAway, as not all are
     235       // triggered in all contexts (desktop vs mobile vs older browsers)
     236       // https://developer.mozilla.org/en-US/docs/Web/API/Window/unload_event
     237       // We work with multiple eventlisteners to ensure we cleanup webswing collage app when
     238       // user navigates away from page. But we should only do the cleanup once, even if *all*
     239       // event handlers got triggered. We use the unloaded var to ensure we cleanup only once
     240       if(!unloaded) {
     241       if(typeof webswingInstance0.performAction === 'function') {
     242              //event.preventDefault(); // if listening for beforeunload event
     243              console.log("@@@@ Calling Java to stop running the webswing collage app");
     244              webswingInstance0.performAction({actionName: "navigatingAway"});
     245              unloaded = true;
     246          console.log("@@@@ unloaded");
     247          //alert("check log");
     248        } else {
     249            console.log("performAction method DOESN'T exist on the webswing object!");
     250            }
     251        } else {
     252            console.log("@@@@ already unloaded.");
     253        }
     254
     255    };
     256   
    220257        window.addEventListener ? window.addEventListener("load", loader, false) : window.attachEvent("onload", loader);
     258
     259    // https://developer.mozilla.org/en-US/docs/Web/API/Window/unload_event
     260    // unload has been deprecated, and so we try pagehide and visibilitychange events
     261    window.addEventListener ? window.addEventListener("unload", navigatingAway, false) : window.attachEvent("onunload", navigatingAway);
     262    window.addEventListener ? window.addEventListener("pagehide", navigatingAway, false) : window.attachEvent("onpagehide", navigatingAway);
     263    window.addEventListener ? window.addEventListener("visibilitychange", navigatingAway, false) : window.attachEvent("onvisibilitychange", navigatingAway);   
     264   
    221265    })(window, document);
    222266    </xsl:text>
    223267  </script>
    224 
    225 
    226 
     268 
     269 <button type="button" onclick="doButtonClick()">Click Me!</button>
    227270    </xsl:template>
    228271
Note: See TracChangeset for help on using the changeset viewer.