You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@royale.apache.org by ha...@apache.org on 2020/04/20 14:36:23 UTC
[royale-asjs] branch develop updated: HashRouter and BrowserRouter
This is an automated email from the ASF dual-hosted git repository.
harbs pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/royale-asjs.git
The following commit(s) were added to refs/heads/develop by this push:
new bdaa1b0 HashRouter and BrowserRouter
bdaa1b0 is described below
commit bdaa1b0bc5f54423aac7bafda363f512236977a1
Author: Harbs <ha...@in-tools.com>
AuthorDate: Mon Apr 20 17:36:06 2020 +0300
HashRouter and BrowserRouter
---
.../Basic/src/main/resources/basic-manifest.xml | 2 +
.../projects/Basic/src/main/resources/defaults.css | 10 +
.../org/apache/royale/routing/BrowserRouter.as | 247 ++++++++++++++++++
.../royale/org/apache/royale/routing/HashRouter.as | 257 +++++++++++++++++++
.../org/apache/royale/routing/PathRouteBead.as | 11 +-
.../org/apache/royale/routing/RouteToParameters.as | 8 +-
.../org/apache/royale/routing/RouteToState.as | 2 +-
.../royale/org/apache/royale/routing/Router.as | 280 +++++++++++----------
.../org/apache/royale/routing/SetRouteTitle.as | 4 +-
9 files changed, 670 insertions(+), 151 deletions(-)
diff --git a/frameworks/projects/Basic/src/main/resources/basic-manifest.xml b/frameworks/projects/Basic/src/main/resources/basic-manifest.xml
index 589f0e0..c95858d 100644
--- a/frameworks/projects/Basic/src/main/resources/basic-manifest.xml
+++ b/frameworks/projects/Basic/src/main/resources/basic-manifest.xml
@@ -289,6 +289,8 @@
<component id="SetRouteTitle" class="org.apache.royale.routing.SetRouteTitle"/>
<component id="RouteTitleLookup" class="org.apache.royale.routing.RouteTitleLookup"/>
<component id="Router" class="org.apache.royale.routing.Router"/>
+ <component id="HashRouter" class="org.apache.royale.routing.HashRouter"/>
+ <component id="BrowserRouter" class="org.apache.royale.routing.BrowserRouter"/>
<component id="UIGraphicsBase" class="org.apache.royale.display.UIGraphicsBase"/>
diff --git a/frameworks/projects/Basic/src/main/resources/defaults.css b/frameworks/projects/Basic/src/main/resources/defaults.css
index 1fc23b6..f8009a9 100644
--- a/frameworks/projects/Basic/src/main/resources/defaults.css
+++ b/frameworks/projects/Basic/src/main/resources/defaults.css
@@ -763,6 +763,16 @@ Router
IPathRouteBead: ClassReference("org.apache.royale.routing.PathRouteBead");
}
+HashRouter
+{
+ IPathRouteBead: ClassReference("org.apache.royale.routing.PathRouteBead");
+}
+
+BrowserRouter
+{
+ IPathRouteBead: ClassReference("org.apache.royale.routing.PathRouteBead");
+}
+
TextInput
{
border: 1px solid #808080;
diff --git a/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/BrowserRouter.as b/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/BrowserRouter.as
new file mode 100644
index 0000000..d7b9085
--- /dev/null
+++ b/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/BrowserRouter.as
@@ -0,0 +1,247 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+package org.apache.royale.routing
+{
+ import org.apache.royale.core.IStrand;
+ import org.apache.royale.events.Event;
+ import org.apache.royale.core.IInitialViewApplication;
+ import org.apache.royale.core.Strand;
+ import org.apache.royale.core.IBead;
+ import org.apache.royale.events.IEventDispatcher;
+ import org.apache.royale.events.ValueEvent;
+ import org.apache.royale.core.IMXMLDocument;
+ import org.apache.royale.utils.MXMLDataInterpreter;
+ import org.apache.royale.utils.sendStrandEvent;
+ import org.apache.royale.utils.loadBeadFromValuesManager;
+ import org.apache.royale.utils.callLater;
+ [DefaultProperty("beads")]
+ /**
+ * Dispatched when the state is changed.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion Royale 0.9.8
+ */
+ [Event(name="stateChange", type="org.apache.royale.events.Event")]
+
+ /**
+ * Dispatched when bindings are initialized
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion Royale 0.9.8
+ */
+ [Event(name="initBindings", type="org.apache.royale.events.Event")]
+
+ /**
+ * Router is a bead which automatically handles browsing history.
+ * It could be attached to any strand, but typically it would be attached to Application or View
+ * Listen to stateChange events to handle changes to browsing history and use setState and renderState for modifying the history.
+ * The state of the router can be modified before committing the state changes.
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion Royale 0.9.8
+ */
+ public class BrowserRouter extends Strand implements IBead, IMXMLDocument
+ {
+ public function BrowserRouter()
+ {
+
+ }
+
+ private var _basePath:String = "/"
+
+ /**
+ * The base path of the application.
+ * If the appllication is being loaded from somewhere other than the domain root, this must be specified.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion Royale 0.9.8
+ */
+ public function get basePath():String
+ {
+ return _basePath;
+ }
+
+ public function set basePath(value:String):void
+ {
+ // the basePath must always start and end with a slash
+ if(value.indexOf("/") != 0){
+ value = "/" + value;
+ }
+ if(value.charAt(value.length -1) != "/"){
+ value += "/";
+ }
+ _basePath = value;
+ }
+ public function get host():IStrand
+ {
+ return _strand;
+ }
+
+ private var _strand:IStrand;
+ public function set strand(value:IStrand):void
+ {
+ _strand = value;
+ loadBeadFromValuesManager(IPathRouteBead, "iPathRouteBead", this);
+
+ // wait until the app is initialized. Calling onInit async soves this problem
+ callLater(onInit);
+ }
+
+ /**
+ * Helper function to attach event listener without the need for casting
+ * @royaleignorecoercion org.apache.royale.events.IEventDispatcher
+ */
+ protected function listenOnStrand(eventType:String,handler:Function,capture:Boolean=false):void
+ {
+ (_strand as IEventDispatcher).addEventListener(eventType, handler, capture);
+ }
+
+ protected function onInit(event:Event):void
+ {
+ if(beads)
+ {
+ for each (var bead:IBead in beads)
+ addBead(bead);
+ }
+ // needed for binding in MXML
+ dispatchEvent(new Event("initBindings"));
+
+ COMPILE::JS
+ {
+ locationChangeHandler();
+ }
+ }
+
+ private function locationChangeHandler():void
+ {
+ parseLocation();
+ dispatchEvent(new Event("stateChange"));
+ }
+
+ private function parseLocation():void
+ {
+ //TODO SWF implementation
+ COMPILE::JS
+ {
+ var host:String = location.host;
+ var path:String = location.href.slice(host.length+1);// slice off the host and the leading slash
+ var ev:ValueEvent = new ValueEvent("urlReceived",path);
+ dispatchEvent(ev);
+ }
+ }
+
+ private var _routeState:RouteState;
+
+ public function get routeState():RouteState
+ {
+ if(!_routeState){
+ _routeState = new RouteState();
+ }
+ return _routeState;
+ }
+
+ public function set routeState(value:RouteState):void
+ {
+ _routeState = value;
+ }
+ /**
+ * Commits the current state to the browsing history
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion Royale 0.9.8
+ */
+ public function setState():void
+ {
+ COMPILE::JS
+ {
+ var path:String = basePath;
+ var ev:ValueEvent = new ValueEvent("urlNeeded","");
+ dispatchEvent(ev);
+ var stateEv:ValueEvent = new ValueEvent("stateNeeded",{});
+ dispatchEvent(stateEv);
+ if(!ev.defaultPrevented)
+ {
+ path += ev.value;
+ window.history.pushState(stateEv.value,_routeState.title,path);
+ sendStrandEvent(this,"stateSet");
+ }
+ }
+ }
+ /**
+ * Same as setState, but also notifies of the state change
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion Royale 0.9.8
+ */
+ public function renderState():void
+ {
+ setState();
+ dispatchEvent(new Event("stateChange"));
+ }
+
+ private var _mxmlDescriptor:Array;
+ private var _mxmlDocument:Object = this;
+
+ /**
+ * @copy org.apache.royale.core.Application#MXMLDescriptor
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion Royale 0.8
+ */
+ public function get MXMLDescriptor():Array
+ {
+ return _mxmlDescriptor;
+ }
+
+ /**
+ * @private
+ */
+ public function setMXMLDescriptor(document:Object, value:Array):void
+ {
+ _mxmlDocument = document;
+ _mxmlDescriptor = value;
+ }
+
+ /**
+ * @copy org.apache.royale.core.Application#generateMXMLAttributes()
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion Royale 0.8
+ */
+ public function generateMXMLAttributes(data:Array):void
+ {
+ MXMLDataInterpreter.generateMXMLProperties(this, data);
+ }
+
+
+ }
+}
\ No newline at end of file
diff --git a/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/HashRouter.as b/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/HashRouter.as
new file mode 100644
index 0000000..ed322f0
--- /dev/null
+++ b/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/HashRouter.as
@@ -0,0 +1,257 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+package org.apache.royale.routing
+{
+ import org.apache.royale.core.IStrand;
+ import org.apache.royale.events.Event;
+ import org.apache.royale.core.IInitialViewApplication;
+ import org.apache.royale.core.Strand;
+ import org.apache.royale.core.IBead;
+ import org.apache.royale.events.IEventDispatcher;
+ import org.apache.royale.events.ValueEvent;
+ import org.apache.royale.core.IMXMLDocument;
+ import org.apache.royale.utils.MXMLDataInterpreter;
+ import org.apache.royale.utils.sendStrandEvent;
+ import org.apache.royale.utils.loadBeadFromValuesManager;
+ import org.apache.royale.utils.callLater;
+ [DefaultProperty("beads")]
+ /**
+ * Dispatched when the state is changed.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion Royale 0.9.8
+ */
+ [Event(name="stateChange", type="org.apache.royale.events.Event")]
+
+ /**
+ * Dispatched when bindings are initialized
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion Royale 0.9.8
+ */
+ [Event(name="initBindings", type="org.apache.royale.events.Event")]
+
+ /**
+ * HashRouter is a bead which automatically handles browsing history.
+ * It could be attached to any strand, but typically it would be attached to Application or View
+ * Listen to stateChange events to handle changes to browsing history and use setState and renderState for modifying the history.
+ * The state of the router can be modified before committing the state changes.
+ * The difference between HashRouter and BrowserRouter is that HashRouter uses the location hash which is only the section from "#" and on.
+ * BrowserRouter uses path routes which are indistinguishable from normal urls.
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion Royale 0.9.8
+ */
+ public class HashRouter extends Strand implements IBead, IMXMLDocument
+ {
+ public function HashRouter()
+ {
+
+ }
+
+ private var _useHashBang:Boolean = true;
+
+ /**
+ * Using a hashbang (<code>!#</code>) lets the application be indexed by search engines.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion Royale 0.9.8
+ */
+ public function get useHashBang():Boolean
+ {
+ return _useHashBang;
+ }
+
+ public function set useHashBang(value:Boolean):void
+ {
+ _useHashBang = value;
+ }
+
+ public function get host():IStrand
+ {
+ return _strand;
+ }
+
+ private var _strand:IStrand;
+ public function set strand(value:IStrand):void
+ {
+ _strand = value;
+ loadBeadFromValuesManager(IPathRouteBead, "iPathRouteBead", this);
+ COMPILE::JS
+ {
+ window.addEventListener("hashchange", hashChangeHandler);
+ }
+
+ // wait until the app is initialized. Calling onInit async soves this problem
+ callLater(onInit);
+ }
+
+ /**
+ * Helper function to attach event listener without the need for casting
+ * @royaleignorecoercion org.apache.royale.events.IEventDispatcher
+ */
+ protected function listenOnStrand(eventType:String,handler:Function,capture:Boolean=false):void
+ {
+ (_strand as IEventDispatcher).addEventListener(eventType, handler, capture);
+ }
+
+ protected function onInit(event:Event):void
+ {
+ if(beads)
+ {
+ for each (var bead:IBead in beads)
+ addBead(bead);
+ }
+ // needed for binding in MXML
+ dispatchEvent(new Event("initBindings"));
+
+ COMPILE::JS
+ {
+ if(location.hash)
+ {
+ hashChangeHandler();
+ }
+ else// if there's no hash we should still dispatch a stateChange event so the beads can set defaults
+ {
+ dispatchEvent(new Event("stateChange"));
+ }
+ }
+ }
+
+ private function hashChangeHandler():void
+ {
+ parseHash();
+ dispatchEvent(new Event("stateChange"));
+ }
+
+ private function parseHash():void
+ {
+ //TODO SWF implementation
+ COMPILE::JS
+ {
+ var hash:String = location.hash;
+ var index:int = 0;
+ if(hash.indexOf("!")==1){
+ index = 1;
+ }
+ hash = hash.slice(index+1);
+ var ev:ValueEvent = new ValueEvent("urlReceived",hash);
+ dispatchEvent(ev);
+ }
+ }
+
+ private var _routeState:RouteState;
+
+ public function get routeState():RouteState
+ {
+ if(!_routeState){
+ _routeState = new RouteState();
+ }
+ return _routeState;
+ }
+
+ public function set routeState(value:RouteState):void
+ {
+ _routeState = value;
+ }
+ /**
+ * Commits the current state to the browsing history
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion Royale 0.9.8
+ */
+ public function setState():void
+ {
+ COMPILE::JS
+ {
+ var hash:String = useHashBang ? "#!" : "#";
+ var ev:ValueEvent = new ValueEvent("urlNeeded","");
+ dispatchEvent(ev);
+ var stateEv:ValueEvent = new ValueEvent("stateNeeded",{});
+ dispatchEvent(stateEv);
+ if(!ev.defaultPrevented)
+ {
+ hash += ev.value;
+ window.history.pushState(stateEv.value,_routeState.title,hash);
+ sendStrandEvent(this,"stateSet");
+ }
+ }
+ }
+ /**
+ * Same as setState, but also notifies of the state change
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion Royale 0.9.8
+ */
+ public function renderState():void
+ {
+ setState();
+ dispatchEvent(new Event("stateChange"));
+ }
+
+ private var _mxmlDescriptor:Array;
+ private var _mxmlDocument:Object = this;
+
+ /**
+ * @copy org.apache.royale.core.Application#MXMLDescriptor
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion Royale 0.8
+ */
+ public function get MXMLDescriptor():Array
+ {
+ return _mxmlDescriptor;
+ }
+
+ /**
+ * @private
+ */
+ public function setMXMLDescriptor(document:Object, value:Array):void
+ {
+ _mxmlDocument = document;
+ _mxmlDescriptor = value;
+ }
+
+ /**
+ * @copy org.apache.royale.core.Application#generateMXMLAttributes()
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion Royale 0.8
+ */
+ public function generateMXMLAttributes(data:Array):void
+ {
+ MXMLDataInterpreter.generateMXMLProperties(this, data);
+ }
+
+
+ }
+}
\ No newline at end of file
diff --git a/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/PathRouteBead.as b/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/PathRouteBead.as
index 0eb84ec..a041813 100644
--- a/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/PathRouteBead.as
+++ b/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/PathRouteBead.as
@@ -38,16 +38,17 @@ package org.apache.royale.routing
override public function set strand(value:IStrand):void
{
_strand = value;
- listenOnStrand("hashNeeded",hashNeeded);
- listenOnStrand("hashReceived",hashReceived);
+ listenOnStrand("urlNeeded",urlNeeded);
+ listenOnStrand("urlReceived",urlReceived);
}
- protected function hashReceived(ev:ValueEvent):void
+ protected function urlReceived(ev:ValueEvent):void
{
var hash:String = ev.value;
// if we have parameters, we don't care if we also have an anchor
var delim:String = ""
- var index:int = hash.indexOf("?")
+ var index:int = hash.indexOf("?");
+ // if not found then we need to check for an anchor
if(index == -1)
index = hash.indexOf("#");
@@ -57,7 +58,7 @@ package org.apache.royale.routing
host.routeState.path = hash;
}
- protected function hashNeeded(ev:ValueEvent):void
+ protected function urlNeeded(ev:ValueEvent):void
{
var hash:String = ev.value;
var trailing:String = "";
diff --git a/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/RouteToParameters.as b/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/RouteToParameters.as
index ee725ae..0c6d0b9 100644
--- a/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/RouteToParameters.as
+++ b/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/RouteToParameters.as
@@ -38,11 +38,11 @@ package org.apache.royale.routing
override public function set strand(value:IStrand):void
{
_strand = value;
- listenOnStrand("hashNeeded",hashNeeded);
- listenOnStrand("hashReceived",hashReceived);
+ listenOnStrand("urlNeeded",urlNeeded);
+ listenOnStrand("urlReceived",urlReceived);
listenOnStrand("stateChange",stateChanged)
}
- private function hashNeeded(ev:ValueEvent):void
+ private function urlNeeded(ev:ValueEvent):void
{
var hash:String = ev.value;
var paramStr:String = buildParameterString();
@@ -57,7 +57,7 @@ package org.apache.royale.routing
}
- private function hashReceived(ev:ValueEvent):void
+ private function urlReceived(ev:ValueEvent):void
{
var hash:String = ev.value;
var index:int = hash.indexOf("?");
diff --git a/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/RouteToState.as b/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/RouteToState.as
index a548842..6f3c463 100644
--- a/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/RouteToState.as
+++ b/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/RouteToState.as
@@ -55,7 +55,7 @@ package org.apache.royale.routing
host.routeState.path = getStateComponent().currentState;
host.setState();
}
- override protected function hashNeeded(ev:ValueEvent):void
+ override protected function urlNeeded(ev:ValueEvent):void
{
var hash:String = ev.value;
// don't overwrite path, parameters and anchor
diff --git a/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/Router.as b/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/Router.as
index 1304f61..55335a5 100644
--- a/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/Router.as
+++ b/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/Router.as
@@ -31,166 +31,168 @@ package org.apache.royale.routing
import org.apache.royale.utils.loadBeadFromValuesManager;
import org.apache.royale.utils.callLater;
[DefaultProperty("beads")]
- /**
- * Dispatched when the state is changed.
- *
- * @langversion 3.0
- * @playerversion Flash 10.2
- * @playerversion AIR 2.6
- * @productversion Royale 0.9.7
- */
- [Event(name="stateChange", type="org.apache.royale.events.Event")]
-
- /**
- * Dispatched when bindings are initialized
- *
- * @langversion 3.0
- * @playerversion Flash 10.2
- * @playerversion AIR 2.6
- * @productversion Royale 0.9.7
- */
- [Event(name="initBindings", type="org.apache.royale.events.Event")]
-
- /**
- * Router is a bead which automatically handles browsing history.
- * It could be attached to any strand, but typically it would be attached to Application or View
- * Listen to stateChange events to handle changes to browsing history and use setState and renderState for modifying the history.
- * The state of the router can be modified before committing the state changes.
- * @langversion 3.0
- * @playerversion Flash 10.2
- * @playerversion AIR 2.6
- * @productversion Royale 0.9.7
- */
+ /**
+ * Dispatched when the state is changed.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion Royale 0.9.7
+ */
+ [Event(name="stateChange", type="org.apache.royale.events.Event")]
+
+ /**
+ * Dispatched when bindings are initialized
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion Royale 0.9.7
+ */
+ [Event(name="initBindings", type="org.apache.royale.events.Event")]
+
+ /**
+ * Router is deprecated. Please use HashRouter or BrowserRouter instead.
+ * Router is a bead which automatically handles browsing history.
+ * It could be attached to any strand, but typically it would be attached to Application or View
+ * Listen to stateChange events to handle changes to browsing history and use setState and renderState for modifying the history.
+ * The state of the router can be modified before committing the state changes.
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion Royale 0.9.7
+ * @deprecated
+ */
public class Router extends Strand implements IBead, IMXMLDocument
{
- public function Router()
- {
+ public function Router()
+ {
- }
+ }
- public function get host():IStrand
- {
- return _strand;
- }
+ public function get host():IStrand
+ {
+ return _strand;
+ }
- private var _strand:IStrand;
+ private var _strand:IStrand;
public function set strand(value:IStrand):void
{
_strand = value;
- loadBeadFromValuesManager(IPathRouteBead, "iPathRouteBead", this);
+ loadBeadFromValuesManager(IPathRouteBead, "iPathRouteBead", this);
COMPILE::JS
{
window.addEventListener("hashchange", hashChangeHandler);
}
- // wait until the app is initialized. Calling onInit async soves this problem
- callLater(onInit);
+ // wait until the app is initialized. Calling onInit async soves this problem
+ callLater(onInit);
}
- /**
- * Helper function to attach event listener without the need for casting
- * @royaleignorecoercion org.apache.royale.events.IEventDispatcher
- */
- protected function listenOnStrand(eventType:String,handler:Function,capture:Boolean=false):void
- {
- (_strand as IEventDispatcher).addEventListener(eventType, handler, capture);
- }
-
- protected function onInit(event:Event):void
- {
- if(beads)
- {
- for each (var bead:IBead in beads)
- addBead(bead);
- }
- // needed for binding in MXML
- dispatchEvent(new Event("initBindings"));
-
- COMPILE::JS
- {
- if(location.hash)
- {
- hashChangeHandler();
- }
- else// if there's no hash we should still dispatch a stateChange event so the beads can set defaults
- {
- dispatchEvent(new Event("stateChange"));
- }
- }
- }
+ /**
+ * Helper function to attach event listener without the need for casting
+ * @royaleignorecoercion org.apache.royale.events.IEventDispatcher
+ */
+ protected function listenOnStrand(eventType:String,handler:Function,capture:Boolean=false):void
+ {
+ (_strand as IEventDispatcher).addEventListener(eventType, handler, capture);
+ }
+
+ protected function onInit(event:Event):void
+ {
+ if(beads)
+ {
+ for each (var bead:IBead in beads)
+ addBead(bead);
+ }
+ // needed for binding in MXML
+ dispatchEvent(new Event("initBindings"));
+
+ COMPILE::JS
+ {
+ if(location.hash)
+ {
+ hashChangeHandler();
+ }
+ else// if there's no hash we should still dispatch a stateChange event so the beads can set defaults
+ {
+ dispatchEvent(new Event("stateChange"));
+ }
+ }
+ }
private function hashChangeHandler():void
{
- parseHash();
+ parseHash();
dispatchEvent(new Event("stateChange"));
}
- private function parseHash():void
- {
- //TODO SWF implementation
- COMPILE::JS
- {
- var hash:String = location.hash;
- var index:int = 0;
- if(hash.indexOf("!")==1){
- index = 1;
- }
- hash = hash.slice(index+1);
- var ev:ValueEvent = new ValueEvent("hashReceived",hash);
- dispatchEvent(ev);
- }
- }
-
- private var _routeState:RouteState;
-
- public function get routeState():RouteState
- {
- if(!_routeState){
- _routeState = new RouteState();
- }
- return _routeState;
- }
-
- public function set routeState(value:RouteState):void
- {
- _routeState = value;
- }
- /**
- * Commits the current state to the browsing history
- * @langversion 3.0
- * @playerversion Flash 10.2
- * @playerversion AIR 2.6
- * @productversion Royale 0.9.7
- */
- public function setState():void
- {
- COMPILE::JS
- {
- var hash:String = "#!";
- var ev:ValueEvent = new ValueEvent("hashNeeded","");
- dispatchEvent(ev);
- var stateEv:ValueEvent = new ValueEvent("stateNeeded",{});
- dispatchEvent(stateEv);
- if(!ev.defaultPrevented)
- {
- hash += ev.value;
- window.history.pushState(stateEv.value,_routeState.title,hash);
- sendStrandEvent(this,"stateSet");
- }
- }
- }
- /**
- * Same as setState, but also notifies of the state change
- * @langversion 3.0
- * @playerversion Flash 10.2
- * @playerversion AIR 2.6
- * @productversion Royale 0.9.7
- */
- public function renderState():void
- {
- setState();
- dispatchEvent(new Event("stateChange"));
- }
+ private function parseHash():void
+ {
+ //TODO SWF implementation
+ COMPILE::JS
+ {
+ var hash:String = location.hash;
+ var index:int = 0;
+ if(hash.indexOf("!")==1){
+ index = 1;
+ }
+ hash = hash.slice(index+1);
+ var ev:ValueEvent = new ValueEvent("urlReceived",hash);
+ dispatchEvent(ev);
+ }
+ }
+
+ private var _routeState:RouteState;
+
+ public function get routeState():RouteState
+ {
+ if(!_routeState){
+ _routeState = new RouteState();
+ }
+ return _routeState;
+ }
+
+ public function set routeState(value:RouteState):void
+ {
+ _routeState = value;
+ }
+ /**
+ * Commits the current state to the browsing history
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion Royale 0.9.7
+ */
+ public function setState():void
+ {
+ COMPILE::JS
+ {
+ var hash:String = "#!";
+ var ev:ValueEvent = new ValueEvent("urlNeeded","");
+ dispatchEvent(ev);
+ var stateEv:ValueEvent = new ValueEvent("stateNeeded",{});
+ dispatchEvent(stateEv);
+ if(!ev.defaultPrevented)
+ {
+ hash += ev.value;
+ window.history.pushState(stateEv.value,_routeState.title,hash);
+ sendStrandEvent(this,"stateSet");
+ }
+ }
+ }
+ /**
+ * Same as setState, but also notifies of the state change
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion Royale 0.9.7
+ */
+ public function renderState():void
+ {
+ setState();
+ dispatchEvent(new Event("stateChange"));
+ }
private var _mxmlDescriptor:Array;
private var _mxmlDocument:Object = this;
diff --git a/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/SetRouteTitle.as b/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/SetRouteTitle.as
index 924f132..081fd05 100644
--- a/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/SetRouteTitle.as
+++ b/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/SetRouteTitle.as
@@ -43,9 +43,9 @@ package org.apache.royale.routing
initialTitle = document.title;
}
listenOnStrand("stateSet",handleStateSet);
- listenOnStrand("hashReceived",hashReceived);
+ listenOnStrand("urlReceived",urlReceived);
}
- private function hashReceived(ev:ValueEvent):void
+ private function urlReceived(ev:ValueEvent):void
{
setTitle();
}