001package votorola.s.gwt.mediawiki; // Copyright 2012-2014, 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.JavaScriptObject;
004import com.google.gwt.dom.client.*;
005import com.google.gwt.user.client.Window;
006import com.google.gwt.user.client.ui.RootPanel;
007import votorola.a.voter.*;
008import votorola.a.web.gwt.*;
009import votorola.g.web.gwt.*;
010import votorola.s.gwt.stage.*;
011
012
013/** The GWT entry module for embedding the Crossforum Theatre {@linkplain
014  * votorola.s.gwt.stage.Stage stage} in MediaWiki pages.  An instance of this class is
015  * automatically constructed and invoked per module definition <a href='../../../../../../s/gwt/mediawiki/MediaWikiIn.gwt.xml'>MediaWikiIn.gwt.xml</a>.
016  *
017  * <h3>Configuration</h3>
018  *
019  * <p>The following configuration properties are specific to this entry module.  These
020  * are properties of global JavaScript object {@linkplain GWTConfig window.voGWTConfig}.
021  * The short name (SHORT) of each is fully qualified as
022  * "window.voGWTConfig.s_gwt_mediawiki_SHORT".</p>
023  *
024  * <table class='definition' style='margin-left:1em'>
025  *     <tr>
026  *         <th class='key'>Short name</th>
027  *         <th>Form</th>
028  *         <th>Value</th>
029  *         <th>Default</th>
030  *         </tr>
031  *     <tr><td class='key'>toTop</td>
032  *
033  *         <td>boolean</td>
034  *
035  *         <td>Specify true for {@linkplain #insertStageVTop(JavaScriptObject) top
036  *         insertion} of the stage.  Otherwise the stage is placed as for
037  *         <a href='#draftInject'>remote drafting</a>.</td>
038  *
039  *         <td>false</td>
040  *
041  *         </tr>
042  *     </table>
043  *
044  * <h3>Injection</h3>
045  *
046  * <p>Injection embeds the stage in a web page.  Common patterns of injection are
047  * documented in the sections below.</p>
048  *
049  * <h4 id='univeralInject'>Universal injection</h4>
050  *
051  * This constructs a {@linkplain votorola.s.gwt.stage.StageV stage view} and ordinarily
052  * inserts it as the first child of the body element on every page, as detailed for
053  * {@linkplain #insertStageVTop(JavaScriptObject) insertStageVTop}.  To install it, add
054  * the following to pollwiki page MediaWiki:Common.js or your
055  * <a href='http://www.mediawiki.org/wiki/Manual:Interface/JavaScript#Personal_scripts' target='_top'>personal script</a>/<a href='http://www.mediawiki.org/wiki/Manual:$wgAllowUserJs' target='_top'>user script</a>,
056  * substituting the correct value for <var>CONTEXT</var>:<pre>
057  *
058  *  // Crossforum Theatre stage.
059  *  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
060  *  // module.css and gwt.js must load before boot script nocache.js
061  *  // http://reluk.ca/project/votorola/_/javadoc/votorola/s/gwt/mediawiki/MediaWikiIn.html
062  *
063  *    if( $.client.profile().name != 'msie' ) // all but IE [1]
064  *    {
065  *        voInject = function()  // implicitly declared so deletable below
066  *        {
067  *            if( !window.voGWTConfig ) voGWTConfig = {};
068  *            else if( window.voGWTConfig.s_gwt_mediawiki_isInjected ) return;
069  *              // guard against double injection by both admin and user scripts
070  *
071  *            window.voGWTConfig.s_gwt_mediawiki_isInjected = true;
072  *            window.voGWTConfig.s_gwt_mediawiki_toTop = true;
073  *            var context = '<var>{@linkplain App#getServletContextLocation() CONTEXT}</var>';
074  *            document.write( "&lt;link href='" + context // [2]
075  *              + "/mediawiki/module.css' rel='stylesheet' type='text/css'/&gt;" );
076  *            document.write( "&lt;script src='" + context // [3]
077  *              + "/w/publicConfig/gwt.js' type='text/javascript'>&lt;/script&gt;" );
078  *            document.write( "&lt;script src='" + context // [3]
079  *              + "/votorola.s.gwt.mediawiki.MediaWikiIn/votorola.s.gwt.mediawiki.MediaWikiIn.nocache.js'"
080  *              + " type='text/javascript'&gt;&lt;/script&gt;" );
081  *        }
082  *        voInject();
083  *        delete voInject; // clean up
084  *    }
085  *
086  *  //
087  *  // NOTES
088  *  //
089  *  //   [1] IE excluded by conditional compilation in MediaWiki 16 and earlier, thus:
090  *  //
091  *  //          /&#x2a;@cc_on @if( false ) @&#x2a;/
092  *  //          /&#x2a;@end @&#x2a;/
093  *  //
094  *  //       And by JQuery in MediaWiki 17 and later, thus:
095  *  //
096  *  //          if( $.client.profile().name != 'msie' )
097  *  //
098  *  //   [2] For MediaWiki 16 and earlier, would ordinarily use: importStylesheetURI( URL )
099  *  //       For MediaWiki 17 and later: mw.loader.load( URL, 'text/css' )
100  *  //       http://www.mediawiki.org/wiki/ResourceLoader/Migration_guide_%28users%29
101  *  //
102  *  //       But instead write to ensure CSS link element precedes script element, else layout
103  *  //       may be unstable: http://mail.zelea.com/list/votorola/2012-November/001540.html
104  *  //
105  *  //   [3] Scripts might instead be injected asynchronously using MediaWiki's mw.loader or
106  *  //       importScriptURI.  We are no longer using the old cross-site linker ('xs') that
107  *  //       writes to the document.  But asynchronous injection has not been tested yet.
108  *  //</pre>
109  *
110  * <h4 id='draftInject'>Selective injection for remote drafting</h4>
111  *
112  * This will ordinarily inject the stage only in remote draft pages.  It locates
113  * <code>&lt;<var>ELEMENT</var> id='bodyContent'/&gt;</code> and inserts a newly
114  * constructed {@linkplain StageV stage view} as its first child, which will normally
115  * render just beneath the tabs and other navigation controls of the wiki (or if
116  * <var>ELEMENT</var> cannot be located, then it defaults to {@linkplain
117  * #insertStageVTop(JavaScriptObject) top insertion}).  To install it add the following
118  * to MediaWiki:Common.js or your
119  * <a href='http://www.mediawiki.org/wiki/Manual:Interface/JavaScript#Personal_scripts' target='_top'>personal script</a>/<a href='http://www.mediawiki.org/wiki/Manual:$wgAllowUserJs' target='_top'>user script</a>,
120  * substituting the correct value for <var>CONTEXT</var>:<pre>
121  *
122  *  // Crossforum Theatre stage for remote drafting.
123  *  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
124  *  // module.css and gwt.js must load before boot script nocache.js
125  *  // http://reluk.ca/project/votorola/_/javadoc/votorola/s/gwt/mediawiki/MediaWikiIn.html
126  *
127  *    if( $.client.profile().name != 'msie' ) // all but IE [1]
128  *    {
129  *        voInject = function()  // implicitly declared so deletable below
130  *        {
131  *            var toInject = false;
132  *            for( var c = wgCategories.length - 1; c &gt;=0; --c )
133  *            {
134  *                var category = wgCategories[c];
135  *                if( category == 'Remote draft' )
136  *                {
137  *                    toInject = true; // inject regardless for a remote draft page
138  *                    break;
139  *                }
140  *            }
141  *            if( toInject )
142  *            {
143  *                if( !window.voGWTConfig ) voGWTConfig = {};
144  *                else if( window.voGWTConfig.s_gwt_mediawiki_isInjected ) return;
145  *                  // guard against double injection by both admin and user scripts
146  *
147  *                window.voGWTConfig.s_gwt_mediawiki_isInjected = true;
148  *                var context = '<var>{@linkplain App#getServletContextLocation() CONTEXT}</var>';
149  *                document.write( "&lt;link href='" + context // [2]
150  *                  + "/mediawiki/module.css' rel='stylesheet' type='text/css'/&gt;" );
151  *                document.write( "&lt;script src='" + context // [3]
152  *                  + "/w/publicConfig/gwt.js' type='text/javascript'&gt;&lt;/script&gt;" );
153  *                document.write( "&lt;script src='" + context // [3]
154  *                  + "/votorola.s.gwt.mediawiki.MediaWikiIn/votorola.s.gwt.mediawiki.MediaWikiIn.nocache.js'"
155  *                  + " type='text/javascript'&gt;&lt;/script&gt;" );
156  *            }
157  *        }
158  *        voInject();
159  *        delete voInject; // clean up
160  *    }
161  *
162  *  //
163  *  // NOTES
164  *  //
165  *  //   (same as farther above)
166  *  //</pre>
167  *
168  * <p>As mentioned, this will ordinarily inject the stage only in remote draft pages.
169  * You may create such pages with the help of the template provided
170  * in the working examples farther below.</p>
171  *
172  * <p>Note that the username of the logged in Votorola/Outcast user is not currently
173  * detectable in MediaWiki pages external to the pollwiki.  This means that user-oriented
174  * links such as "my draft" in the {@linkplain votorola.s.gwt.stage.link.LinkTrackV link
175  * track} will not be properly disabled when they are redundant, e.g. when "my draft" is
176  * already being shown.</p>
177  *
178  * <h3>Working examples</h3>
179  *
180  * The only site deploying this entry module (at time of writing) is Metagov, which
181  * deploys it in non-standard fashion via its plug-in framework.  See:<ul>
182  *
183  * <li><a href='http://metagovernment.org/wiki/MediaWiki:Common.js' target='_top'>MediaWiki:Common.js</a></li>
184  * <li><a href='http://metagovernment.tuxfamily.org/plug/plug.js' target='_top'>plug.js</a></li>
185  * <li><a href='http://metagovernment.org/wiki/Category:Remote_draft' target='_top'>Category:Remote draft</a></li>
186  * <li><a href='http://metagovernment.org/wiki/Template:Remote_draft' target='_top'>Template:Remote draft</a></li></ul>
187  */
188public final @GWTConfig class MediaWikiIn extends TheatreInitializer0 implements EntryPointS
189{
190
191
192    /** Constructs a {@linkplain StageV stage view} and ordinarily inserts it as the first
193      * child of the body element.  Alternatively the page author may specify the
194      * insertion point as {@linkplain StageIn#insertStageV() documented for StageIn}.
195      */
196    public static void insertStageVTop( final JavaScriptObject voGWTConfig )
197    {
198        StageIn.insertStageV();
199        final Document doc = Document.get();
200
201      // Adjust styling to accomodate stage, which may affect layout.
202      // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
203        final Element gW;
204        if( "monobook".equals( WindowX.js()._get("skin") ))
205        {
206            gW = doc.getElementById( "globalWrapper" );
207            if( gW == null )
208            {
209                Window.alert( "unable to layout stage, no element ID 'globalWrapper'" );
210                return;
211            }
212        }
213        else // might be vector, which removes that element, so create our own
214        {
215            gW = doc.createDivElement();
216            final Element stageV = doc.getElementById( "StageV-top" );
217            final Element body = doc.getBody();
218            for( Node node = body.getFirstChild(); node != null; )
219            {
220                final Node next = node.getNextSibling();
221                move:
222                {
223                    if( Element.is( node ))
224                    {
225                        final Element e = node.cast();
226                        if( e.equals( stageV )) break move; // leave this above gW
227
228                        final String id = e.getId();
229                        if( id.equals("mgSwitchPopperSensor")
230                         || id.equals("mgSwitchWindow") )  break move; // leave these above gW
231                    }
232                    gW.appendChild( node );
233                }
234                node = next;
235            }
236            body.appendChild( gW );
237        }
238        // The stage view element is now assured of being on top, followed by gW which
239        // contains all other content.  Next make gW a containing block by giving it a
240        // relative postition.  This ensures that all of MediaWiki's positioned elements
241        // (#pLogo and such) are situated relative to gW, and therefore sit properly
242        // beneath the stage instead of overlapping it.
243        gW.getStyle().setPosition( Style.Position.RELATIVE );
244    }
245
246
247
248   // - E n t r y - P o i n t ------------------------------------------------------------
249
250
251    public void onModuleLoad()
252    {
253        final JavaScriptObject voGWTConfig = WindowX.js()._get( "voGWTConfig" );
254        if( voGWTConfig._getBoolean( "s_gwt_mediawiki_toTop" )) insertStageVTop( voGWTConfig );
255        else
256        {
257            // Expect the wiki in this case to inject the stage script in some pages only,
258            // so place the stage beneath the wiki's own nav controls, thus keeping those
259            // controls in a steady position across all pages, staged or not.
260            final RootPanel parent = RootPanel.get( "bodyContent" );
261            if( parent != null ) parent.insert( new StageV( /*toDisplay*/true ), 0 );
262              // toDisplay because stabilization not yet implemented for this kind of embedding
263            else insertStageVTop( voGWTConfig ); // fallback
264        }
265
266      // - - -
267        EntryPointS.U.schedule( MediaWikiIn.this );
268        EntryPointS.U.execute(); // because all modules are now loaded
269    }
270
271
272
273   // - 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 ----------------------------
274
275
276    public void execute()
277    {
278        StageV.i().initEmbeddedDisplay();
279        Stage.i().addInitializer( MediaWikiIn.this ); // auto-removed
280    }
281
282
283
284   // - T h e a t r e - I n i t i a l i z e r --------------------------------------------
285
286
287    public @Override void initFrom( final Stage s, boolean _rPending ) { initAbsolutes( s ); }
288
289
290
291    public @Override void initTo( final Stage s ) { initAbsolutes( s ); }
292
293
294
295    public @Override void initTo( final Stage s, TheatrePage _r ) { initAbsolutes( s ); }
296
297
298
299//// P r i v a t e ///////////////////////////////////////////////////////////////////////
300
301
302    /** Effects initialization that is absolutely required whether the stage is being
303      * restored (initFrom) or not (initTo).
304      */
305    private void initAbsolutes( final Stage stage )
306    {
307        // Set default properties from voDraft markers.  The markers are editable so their
308        // values may differ from those originally set and stored and now restored.
309        final Document doc = Document.get();
310        Element marker;
311        marker = doc.getElementById( "voDraft-poll" );
312        if( marker != null ) Stage.setDefaultPollName( marker.getTitle() );
313        marker = doc.getElementById( "voDraft-author" );
314        if( marker != null )
315        {
316            Stage.setDefaultActorName( IDPair.normalUsername( marker.getTitle() ));
317        }
318    }
319
320
321}