7 Replies Latest reply on Apr 10, 2013 5:10 PM by David Schlenk

    Force portlet mode change to rerender entire page?

    David Schlenk Newbie

      I'm trying to port a portlet that uses jQuery to prepopulate some date fields in the edit mode of a portlet. jQuery's document.ready isn't fired once view mode has been seen once as it seems like the mode switch is happening without a full page load, probably using some fancy ajax or whatnot. Is there any way to force mode switches to do full page loads? If not, can I somehow fire a javascript method after the mode change render completes?

        • 1. Re: Force portlet mode change to rerender entire page?
          Vu Viet Phuong Novice

          Yes we use ajax to change to portlet mode if you use the default UI of GateIn

          - To run your JS after an change portlet mode by ajax: there are several ways:

                   - Use the standard portlet extra markup header --> add <script> element to the html header

                   - Put the <script> in your portlet template

                   - others: use GateIn api: org.exoplatform.web.application.JavascriptManager --> this class help to add script to the page

          - To switch portlet mode that refresh the page, you need to build portlet url that help to change the portlet mode (pls look at the portlet api spec) then use the GateIn extension machanism (you can find examples in GateIn source code), override UIPortlet.gtmpl template --> replace GateIn default "change portlet mode" URL by yours

          • 2. Re: Force portlet mode change to rerender entire page?
            David Schlenk Newbie

            1) I tried adding the script via doHeaders, but it seems to be loaded before jQuery gets loaded via the AMD module loading process as I get an error in the console about $ not being defined.

             

            2) Adding the script directly to the jsp behaves the same. Besides, I'd rather not clutter up my portlet jsp with script definitions.

             

            3) I can't figure out what maven dependency I need to get access to the JavascriptManager class.

             

            4) Ain't nobody got time for that!

            • 3. Re: Force portlet mode change to rerender entire page?
              Vu Viet Phuong Novice

              - So the first issue: run portlet JS after ajax updated is resolved , right ? Now you want to use jQuery provide by GateIn. Yes, build-in jQuery of GateIn comes in JS module format, if you want to use it, your script need to be AMD too. Here is the document https://docs.jboss.org/author/display/GTNPORTAL36/JavaScript+in+GateIn

               

              - maven dependency for JavascriptManager class:

                <dependency>

                  <groupId>org.gatein.portal</groupId>

                  <artifactId>exo.portal.component.web.resources</artifactId>

                </dependency>

              the repository is here

              • 4. Re: Force portlet mode change to rerender entire page?
                David Schlenk Newbie

                No, I still haven't figured out a way to fire a javascript function/callback after the mode change finishes. I am already using the GateIn JS AMD module configuration for my script. The problem seems to be twofold:

                1. If I run something inside a jQuery $(document).ready declaration in my AMD script, it won't fire again after the mode changes since it isn't a full page reload, just an AJAX call that replaces content.
                2. If I try to use one of the approaches you suggested above, I can't tell GateIn that I want to use the inbuilt jQuery version because you can only define a portlet as having a module OR a script, not both, and 1 and only 1 of whichever you choose. The recipe in the documentation for making jQuery globally available requires the use of a script in the portlet declaration in gatein-resources.xml. I need my script to be a module because it has dependencies on other AMD modules. I guess I could add a contentless dummy portlet that exposes jQuery in the window and add that to the page, but that seems like a terrible hack to me.

                 

                I'll try using the JavascripManager and report back on that.

                • 5. Re: Force portlet mode change to rerender entire page?
                  David Schlenk Newbie

                  The only examples I can find for the use of JavascriptManager are inside groovy templates, which I'm not using. They access some magic object _ctx to get the JavascriptManager instance and I don't know what that object is or how to access it from my portlet class. Or do I just construct a new one? Are there secret docs on how to use JavascriptManager somewhere?

                  • 6. Re: Force portlet mode change to rerender entire page?
                    Vu Viet Phuong Novice

                    sorry for the late answer, about JavascriptManager, you can use this:

                         JavascriptManager jsMan = WebuiRequestContext.<WebuiRequestContext>getCurrentInstance().getJavascriptManager();

                          jsMan.addCustomizedOnLoadScript(...);

                    you will need this maven dependency:

                         <dependency>

                           <groupId>org.gatein.portal</groupId>

                           <artifactId>exo.portal.webui.framework</artifactId>

                         </dependency>

                     

                    GateIn JS loading support is very flexible, you can still call for both scripts, and modules when rendering portlet. Let say we have scripts and modules defined in gatein-resources.xml

                         <scripts>

                              <name>foo</name>

                               ....

                         </scripts>


                         <module>

                              <name>bar</name>

                              ....

                         </module>

                    When the portlet is renderred:

                      public void render(RenderRequest req, RenderResponse resp) throws PortletException, IOException {

                        resp.setProperty("org.gatein.javascript.dependency", "SHARED/foo");

                        resp.addProperty("org.gatein.javascript.dependency", "SHARED/bar");

                      }

                    That way no need to depends on GateIn specific dependencies.

                    • 7. Re: Force portlet mode change to rerender entire page?
                      David Schlenk Newbie

                      I ended up using the addProperty to RenderResponse method and it works perfectly. Perhaps the schema for gatein-resources.xml could be changed in the next revision to be as flexible as this is?