001package votorola.a.web.wic; // Copyright 2012-2013, Michael Allan.  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE.
002
003import org.apache.wicket.AttributeModifier;
004import org.apache.wicket.markup.html.*;
005import org.apache.wicket.markup.html.basic.*;
006import org.apache.wicket.markup.html.panel.*;
007import votorola.a.*;
008import votorola.a.web.wic.*;
009import votorola.g.lang.*;
010
011
012/** Scripting for the Crossforum Theatre stage.  See <a
013  * href='../../../../context/diff/WP_D.css' target='_top'>WP_D.css</a> for an examples of
014  * the associated styling.
015  *
016  *     @see <a href='../../../../../../a/web/wic/WC_Stage.html'
017  *                                 target='_top'>WC_Stage.html</a>
018  */
019public @ThreadRestricted("wicket") final class WC_Stage extends Panel
020{
021
022
023    /** Constructs a WC_Stage.
024      *
025      *     @param moduleName the name of the GWT stage module, as for instance
026      *       "votorola.s.gwt.wic.DIn".
027      *     @param b the string builder that was returned from a previous call to
028      *       {@linkplain #appendLeader(VSession.User,VoteServer,VRequestCycle)
029      *       appendLeader}.  To it, you must already have appended a function
030      *       <code>voGWTConfig.s_gwt_stage_Stage_init</code> in order to initialize the
031      *       stage.  That function will be called in case of either {@linkplain
032      *       votorola.s.gwt.stage.TheatreInitializer#initFrom(Stage,boolean) initFrom} or
033      *       {@linkplain votorola.s.gwt.stage.TheatreInitializer#initTo(Stage) initTo}.
034      */
035    public WC_Stage( final String id, final String moduleName, final StringBuilder b,
036      final VRequestCycle cycle )
037    {
038        super( id );
039        setRenderBodyOnly( true );
040
041      // s_gwt_stage_Stage_initFrom
042      // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
043        b.append(
044           "_temp = function( rPending )"
045         + "{"
046         +    "var fThis = arguments.callee;"
047         +    "if( fThis.fWrapped ) fThis.fWrapped( rPending );" // admin's own config, if any
048         +    "voGWTConfig.s_gwt_stage_Stage_init();"
049         + "};"
050         + "_temp.fWrapped = voGWTConfig.s_gwt_stage_Stage_initFrom;"
051         + "voGWTConfig.s_gwt_stage_Stage_initFrom = _temp;"
052
053      // s_gwt_stage_Stage_initTo
054      // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
055         + "_temp = function( r, rPending )"
056         + "{"
057         +    "var fThis = arguments.callee;"
058         +    "if( fThis.fWrapped ) fThis.fWrapped( r, rPending );" // admin's own config, if any
059         +    "voGWTConfig.s_gwt_stage_Stage_init();"
060         + "};"
061         + "_temp.fWrapped = voGWTConfig.s_gwt_stage_Stage_initTo;"
062         + "voGWTConfig.s_gwt_stage_Stage_initTo = _temp;"
063
064      // - - -
065         + "delete _temp;"
066         + "\n/*]]>*/" );
067
068        final Label label = new Label( "gwtGlue", b.toString() );
069        label.setEscapeModelStrings( false );
070        add( label );
071
072        final WebMarkupContainer script = new WebMarkupContainer( "stageModule" );
073        script.add( AttributeModifier.replace( "src", cycle.gwtContextLocation()
074          + "/" + moduleName + "/" + moduleName + ".nocache.js" ));
075        add( script );
076    }
077
078
079
080   // ------------------------------------------------------------------------------------
081
082
083    /** Appends the leading portion of the script.
084      */
085    public static StringBuilder appendLeader( final VSession.User userOrNull,
086      final VoteServer vS, final VRequestCycle cycle )
087    {
088        //// meta properties (copied from the first page to implement this)
089        //// ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` `
090        //  final WebMarkupContainer meta = new WebMarkupContainer( "gwtProperty" );
091        //  meta.add( AttributeModifier.replace( "content", "locale=" + bunW.locale().toString() ));
092        //  add( meta );
093        ///// Here is the removed markup: <meta w:id='gwtProperty' name='gwt:property'/>.
094        ///// Using locale.useragent instead of this.  Both fail with Chrome as detailed
095        ///// in AMod.gwt.xml, but useragent seems a cleaner, more general solution.
096
097        final StringBuilder b = new StringBuilder( /*initial capacity, guess*/1000 );
098        b.append( "/*<![CDATA[*/\n"
099         + "if( !window.voGWTConfig ) voGWTConfig = {};" // admin set no config at all
100
101      // a
102      // ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` `
103         + "_temp = function()"
104         + "{"
105         +    "var fThis = arguments.callee;"
106         +    "if( fThis.fWrapped ) fThis.fWrapped();" // call admin's own config, if any
107         +    "if( !a_web_gwt_App_getServletContextLocation() )" // [1]
108         +    "{"
109         +       "a_web_gwt_App_setServletContextLocation( '" );
110        b.append( cycle.uriForContextPath().toASCIIString() ).append( "' );"
111         +    "}" );
112        if( userOrNull != null )
113        {
114            b.append( "a_web_gwt_App_setUsername( '" );
115            b.append( userOrNull.username() ).append( "' );" );
116        }
117        b.append(
118              "if( !a_web_gwt_PollwikiG_getLocation() )" // [1]
119         +    "{"
120         +       "a_web_gwt_PollwikiG_setLocation( '" ); // because default is unreliable
121        b.append( vS.pollwiki().uri().toASCIIString() ).append( "' );"
122         +    "}"
123         + "};"
124         + "_temp.fWrapped = voGWTConfig.a;"
125         + "voGWTConfig.a = _temp;" );
126        return b;
127
128        // [1] else obey the admin's setting, though the two should be identical
129    }
130
131
132}