package votorola.s.gwt.wic; // Copyright 2012, 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. import com.google.gwt.core.client.*; import com.google.gwt.dom.client.*; import votorola.g.web.gwt.*; import votorola.g.web.gwt.event.*; import votorola.s.gwt.stage.*; /** A controller that corrects links in the Wicket scene when the actor changes on stage. * Currently it corrects position-like URLs in 'href' and 'onclick' attributes of the * navigations tabs, which are expected to have an 'id' of 'navPile'. */ final class ActorLinker implements PropertyChangeHandler { /** Creates the {@linkplain #i() single instance} of ActorLinker. */ ActorLinker( final Stage stage ) // called by CountIn { navPile = Document.get().getElementById( "navPile" ); if( navPile == null ) assert false; else { GWTX.i().bus().addHandlerToSource( PropertyChange.TYPE, /*source*/stage, ActorLinker.this ); // no need to unregister, registry does not outlive this handler relink( stage.getActorName() ); // init } } /** The single instance of ActorLinker. */ static ActorLinker i() { return instance; } private static ActorLinker instance; { if( instance != null ) throw new IllegalStateException(); instance = ActorLinker.this; } // - P r o p e r t y - C h a n g e - H a n d l e r ------------------------------------ public void onPropertyChange( final PropertyChange e ) { if( !e.propertyName().startsWith( "actorName" )) return; relink( Stage.i().getActorName() ); } //// P r i v a t e /////////////////////////////////////////////////////////////////////// private static final JavaScriptObject JS = newJS(); // constant JavaScript constructions private static native JavaScriptObject newJS() /*-{ var JS = {}; JS.LINK_PATTERN = new RegExp( "^(.*[A-Z][a-z]*\\?)(.*p=[^']+)(.*)$" ); // page name ? params trailer // in 'onclick', params end in single quote (') JS.U_PARAMS_PATTERN = new RegExp( "^(.*)(&u=[^&]+)(.*)$" ); // leader &u=value trailer // requires '&' first prepended return JS; }-*/; private final Element navPile; private native void relink( final String actorName ) // native to use _hvvww_dom.nextElement /*-{ var dom = $wnd._hvvww_dom; var JS = @votorola.s.gwt.wic.ActorLinker::JS; var e = this.@votorola.s.gwt.wic.ActorLinker::navPile; var eBound = dom.nextElement( e, null, false ); // shallow for( ;; ) // each descendant of navPile { e = dom.nextElement( e, null, true ); // deep if( e == eBound ) break; var aName = 'href'; var aValue = e.getAttribute( aName ); if( !aValue ) { aName = 'onclick'; aValue = e.getAttribute( aName ); } if( !aValue ) continue; var linkMatch = JS.LINK_PATTERN.exec( aValue ); if( !linkMatch ) continue; var params = '&' + linkMatch[2]; // prepend '&' per U_PARAMS_PATTERN var uMatch = JS.U_PARAMS_PATTERN.exec( params ); if( uMatch ) params = uMatch[1] + uMatch[3]; // without old u if( actorName ) params += '&u=' + actorName; // add new u params = params.slice( 1 ); // undo prepend of '&' e.setAttribute( aName, linkMatch[1] + params + linkMatch[3] ); } }-*/; }