001package votorola.s.gwt.pollwiki; // 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 com.google.gwt.core.client.*;
004import com.google.gwt.regexp.shared.RegExp;
005import com.google.gwt.user.client.Window;
006import java.util.regex.*;
007import votorola.a.position.*;
008import votorola.a.voter.*;
009import votorola.a.web.gwt.*;
010import votorola.g.*;
011import votorola.g.web.gwt.*;
012import votorola.s.gwt.mediawiki.*;
013import votorola.s.gwt.stage.*;
014
015
016/** The GWT entry module for embedding the Crossforum Theatre {@linkplain
017  * votorola.s.gwt.stage.Stage stage} in pollwiki pages.  An instance of this class is
018  * automatically constructed and invoked per module definition <a href='../../../../../../s/gwt/pollwiki/PollwikiIn.gwt.xml'>PollwikiIn.gwt.xml</a>.
019  * It constructs a {@linkplain votorola.s.gwt.stage.StageV stage view} and ordinarily
020  * inserts it as the first child of the body element, as detailed for
021  * MediaWikiIn.{@linkplain MediaWikiIn#insertStageVTop(JavaScriptObject)
022  * insertStageVTop}.  To install it add the following to pollwiki page
023  * MediaWiki:Common.js:<pre
024  *
025 *>  // Crossforum Theatre stage.
026  *  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
027  *  // module.css and gwt.js must load before boot script nocache.js
028  *  // http://reluk.ca/project/votorola/_/javadoc/votorola/s/gwt/pollwiki/PollwikiIn.html
029  *
030  *    if( $.client.profile().name != 'msie' ) // all but IE [1]
031  *    {
032  *        var context = '<var>{@linkplain App#getServletContextLocation() CONTEXT}</var>';
033  *        document.write( "&lt;link href='" + context // [2]
034  *          + "/mediawiki/module.css' rel='stylesheet' type='text/css'/&gt;" );
035  *        document.write( "&lt;script src='" + context // [3]
036  *          + "/w/publicConfig/gwt.js' type='text/javascript'&gt;&lt;/script&gt;" );
037  *        document.write( "&lt;script src='" + context // [3]
038  *          + "/votorola.s.gwt.pollwiki.PollwikiIn/votorola.s.gwt.pollwiki.PollwikiIn.nocache.js'"
039  *          + " type='text/javascript'&gt;&lt;/script&gt;" );
040  *    }
041  *
042  *  //
043  *  // NOTES
044  *  //
045  *  //   [1] IE excluded by conditional compilation in MediaWiki 16 and earlier, thus:
046  *  //
047  *  //          /&#x2a;@cc_on @if( false ) @&#x2a;/
048  *  //          /&#x2a;@end @&#x2a;/
049  *  //
050  *  //       And by JQuery in MediaWiki 17 and later, thus:
051  *  //
052  *  //          if( $.client.profile().name != 'msie' )
053  *  //
054  *  //   [2] For MediaWiki 16 and earlier, would ordinarily use: importStylesheetURI( URL )
055  *  //       For MediaWiki 17 and later: mw.loader.load( URL, 'text/css' )
056  *  //       http://www.mediawiki.org/wiki/ResourceLoader/Migration_guide_%28users%29
057  *  //
058  *  //       But instead write to ensure CSS link element precedes script element, else layout
059  *  //       may be unstable: http://mail.zelea.com/list/votorola/2012-November/001540.html
060  *  //
061  *  //   [3] Scripts might instead be inject asynchronously using MediaWiki's mw.loader or
062  *  //       importScriptURI.  We are no longer using the old cross-site linker ('xs') that
063  *  //       writes to the document.  But asynchronous injection has not been tested yet.
064  *  //</pre>
065  *
066  *
067  * <h3>Working examples</h3><ul>
068  *
069  * <li><a href='http://reluk.ca/w/MediaWiki:Common.css' target='_top'>MediaWiki:Common.css</a></li>
070  * <li><a href='http://reluk.ca/w/MediaWiki:Common.js' target='_top'>MediaWiki:Common.js</a></li></ul>
071  */
072public final class PollwikiIn extends TheatreInitializer0 implements EntryPointS
073{
074
075
076   // - E n t r y - P o i n t ------------------------------------------------------------
077
078
079    public final void onModuleLoad()
080    {
081        MediaWikiIn.insertStageVTop( (JavaScriptObject)WindowX.js()._get("voGWTConfig") );
082        EntryPointS.U.schedule( PollwikiIn.this );
083        EntryPointS.U.execute(); // because all modules are now loaded
084        Stage.i().addInitializer( PollwikiIn.this ); // auto-removed
085
086      // Set username.
087      // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
088        final JavaScriptObject window = WindowX.js();
089        final String wgUserName = window._get( "wgUserName" );
090        if( wgUserName != null && !wgUserName.equals("null") )
091        {
092         // IDPair.toInternetAddress( wgUserName ); // test for proper mailish username
093         //// that is too difficult to emulate under GWT.  Only admin logs in under
094         //// non-username anyway, and only rarely.  Should be OK.
095         // App.setUsername( IDPair.normalUsername( wgUserName ));
096         //// always canonical, Mailish Username extension's login guarantees it
097            App.setUsername( wgUserName );
098        }
099    }
100
101
102
103   // - S c h e d u l e r . S c h e d u l e d - C o m m a n d ----------------------------
104
105
106    public void execute() { StageV.i().initEmbeddedDisplay(); }
107
108
109
110   // - T h e a t r e - I n i t i a l i z e r --------------------------------------------
111
112
113    public @Override void initFrom( final Stage stage, boolean _isReferencePending )
114    {
115        initAbsolutes( stage, /*isRestore*/true );
116    }
117
118
119
120    public @Override void initTo( final Stage stage ) { initAbsolutes( stage, /*isRestore*/false ); }
121
122
123
124    public @Override void initTo( final Stage stage, TheatrePage _referrer )
125    {
126        initAbsolutes( stage, /*isRestore*/false );
127    }
128
129
130
131//// P r i v a t e ///////////////////////////////////////////////////////////////////////
132
133
134    /** Effects initialization that is absolutely required, whether the stage is being
135      * restored (initFrom) or not (initTo).
136      */
137    private void initAbsolutes( final Stage stage, final boolean isRestore )
138    {
139        // Set default stage properties to match page.  Page categories are editable so
140        // their values may differ from those originally set and stored and now restored.
141
142        final JavaScriptObject window = WindowX.js();
143        final JsArrayString categories = window._get( "wgCategories" );
144        PageType pageType = PageType.other; // till proven otherwise
145        if( categories != null ) for( int c = categories.length() - 1; c >= 0; --c )
146        {
147            final String cat = categories.get( c );
148            if( "Position".equals(cat) || "Pipe".equals(cat)
149             || cat.endsWith(" pipe") ) // Component pipe, Office pipe, Supercomponent pipe, etc.
150            {
151                pageType = PageType.position;
152                break;
153            }
154            else if( "Poll".equals( cat ))
155            {
156                pageType = PageType.poll;
157                break;
158            }
159        }
160        if( pageType == PageType.poll )
161        {
162            final String pageName = window._get( "wgPageName" );
163            Stage.setDefaultPollName( pageName );
164        }
165        else if( "User".equals( window._get( "wgCanonicalNamespace" )))
166        {
167            final String pageName = window._get( "wgPageName" );
168            if( pageName != null )
169            {
170                if( pageType == PageType.position )
171                {
172                    if( isRestore && stage.getDefaultActorName() != null
173                      && stage.getDefaultPollName() != null ) return;
174                      // save match time, stored values should be correct
175
176                    final PositionID position = App.i().pollwiki().identifyAsPosition( pageName );
177                    if( position != null )
178                    {
179                        Stage.setDefaultActorName( position.personName() );
180                        Stage.setDefaultPollName( position.pollName() );
181                    }
182                }
183                else // other user page
184                {
185                    if( isRestore && stage.getDefaultActorName() != null
186                      && stage.getDefaultPollName() == null ) return;
187                      // save match time, stored values should be correct
188
189                    Stage.setDefaultPollName( null );
190                    final MatchResult m = MediaWiki.parsePageNameS( pageName );
191                    if( m != null )
192                    {
193                        Stage.setDefaultActorName( IDPair.normalUsername( m.group( 2 )));
194                    }
195                }
196            }
197        }
198    }
199
200
201
202   // ====================================================================================
203
204
205    private static enum PageType { other, poll, position }
206
207
208}