This is an automated email from the ASF dual-hosted git repository.

harbs pushed a commit to branch feature/router
in repository https://gitbox.apache.org/repos/asf/royale-asjs.git

commit 7f72ead004090c986f50f13ae31d667acbc08124
Author: Harbs <[email protected]>
AuthorDate: Mon Feb 10 10:09:15 2020 +0200

    Router changes
---
 .../TourDeJewel/src/main/royale/MainContent.mxml   |  9 ++-
 .../projects/Basic/src/main/resources/defaults.css |  5 ++
 .../org/apache/royale/routing/IPathRouteBead.as    | 28 +++++++
 .../{RouteToComponent.as => PathRouteBead.as}      | 91 ++++++----------------
 .../org/apache/royale/routing/RouteToComponent.as  | 58 +-------------
 .../org/apache/royale/routing/RouteToState.as      | 34 ++------
 .../royale/org/apache/royale/routing/Router.as     |  2 +
 7 files changed, 71 insertions(+), 156 deletions(-)

diff --git a/examples/royale/TourDeJewel/src/main/royale/MainContent.mxml 
b/examples/royale/TourDeJewel/src/main/royale/MainContent.mxml
index 04e5c32..85d4167 100644
--- a/examples/royale/TourDeJewel/src/main/royale/MainContent.mxml
+++ b/examples/royale/TourDeJewel/src/main/royale/MainContent.mxml
@@ -39,8 +39,9 @@ limitations under the License.
             
             private function setNavSelection():void {
                 var routeState:RouteState = router.routeState;
-                if(routeState.state){
-                    setNavFromHash(routeState.state);
+                var baseName:String = routeState.getBaseName();
+                if(baseName){
+                    setNavFromHash(baseName);
                 } else {
                     goToHome();
                 }
@@ -89,7 +90,7 @@ limitations under the License.
                 currentNavOption = (event.target as Navigation).selectedItem 
as NavigationLinkVO;
                 
                 router.routeState.title = currentNavOption.label;
-                router.routeState.state = currentNavOption.hash;
+                router.routeState.path = currentNavOption.hash;
                 router.setState();
                 
                 navigateTo();
@@ -108,7 +109,7 @@ limitations under the License.
                 currentNavOption = currentNavOption.selectedChild;
                 
                 router.routeState.title = currentNavOption.label;
-                router.routeState.state = currentNavOption.hash;
+                router.routeState.path = currentNavOption.hash;
                 router.setState();
                 
                 navigateTo();
diff --git a/frameworks/projects/Basic/src/main/resources/defaults.css 
b/frameworks/projects/Basic/src/main/resources/defaults.css
index ae835f4..02804fb 100644
--- a/frameworks/projects/Basic/src/main/resources/defaults.css
+++ b/frameworks/projects/Basic/src/main/resources/defaults.css
@@ -733,6 +733,11 @@ CascadingMenuItemRenderer
        flex-grow: 1;
 }
 
+Router
+{
+       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/IPathRouteBead.as
 
b/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/IPathRouteBead.as
new file mode 100644
index 0000000..07ea0fa
--- /dev/null
+++ 
b/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/IPathRouteBead.as
@@ -0,0 +1,28 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  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
+{
+  /**
+   * Marker interface for a bead which parses the path part of path part of a 
route
+   */
+  public interface IPathRouteBead
+  {
+    
+  }
+}
\ No newline at end of file
diff --git 
a/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/RouteToComponent.as
 
b/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/PathRouteBead.as
similarity index 59%
copy from 
frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/RouteToComponent.as
copy to 
frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/PathRouteBead.as
index 0c1c8ba..0eb84ec 100644
--- 
a/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/RouteToComponent.as
+++ 
b/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/PathRouteBead.as
@@ -19,28 +19,20 @@
 package org.apache.royale.routing
 {
   import org.apache.royale.core.Bead;
-  import org.apache.royale.core.IStrand;
   import org.apache.royale.events.ValueEvent;
-  import org.apache.royale.core.IParent;
-  import org.apache.royale.core.IChild;
-  [DefaultProperty("routes")]
-  /**
-   * RouteToComponent is a bead designed for Router
-   * One or more routes should be assigned to the bead
-   */
-  public class RouteToComponent extends Bead
+  import org.apache.royale.core.IStrand;
+
+  public class PathRouteBead extends Bead implements IPathRouteBead
   {
-    public function RouteToComponent()
+    public function PathRouteBead()
     {
       
     }
 
-    public var routes:Array;
-
     /**
      * @royaleignorecoercion org.apache.royale.routing.Router
      */
-    private function get host():Router{
+    protected function get host():Router{
       return _strand as Router
     }
     override public function set strand(value:IStrand):void
@@ -48,9 +40,24 @@ package org.apache.royale.routing
       _strand = value;
       listenOnStrand("hashNeeded",hashNeeded);
       listenOnStrand("hashReceived",hashReceived);
-      listenOnStrand("stateChange",stateChanged)
     }
-    private function hashNeeded(ev:ValueEvent):void
+
+    protected function hashReceived(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("?")
+      if(index == -1)
+        index = hash.indexOf("#");
+      
+      if(index != -1)
+        hash = hash.slice(0,index);
+      
+      host.routeState.path = hash;
+    }
+
+    protected function hashNeeded(ev:ValueEvent):void
     {
       var hash:String = ev.value;
       var trailing:String = "";
@@ -75,59 +82,5 @@ package org.apache.royale.routing
 
     }
 
-    private function hashReceived(ev:ValueEvent):void
-    {
-      var hash:String = ev.value;
-      var trailing:String = "";
-      // if we have parameters, we don't care if we also have an anchor
-      var delim:String = ""
-      var index:int = hash.indexOf("?")
-      if(index == -1)
-        index = hash.indexOf("#");
-      
-      if(index != -1)
-        hash = hash.slice(0,index);
-      
-      host.routeState.path = hash;
-    }
-
-    /**
-     * @royaleignorecoercion org.apache.royale.core.IParent
-     */
-    private function stateChanged():void
-    {
-      var default:ComponentRoute;
-      // apply routes
-      if(routes)
-      {
-        var baseName:String = host.routeState.getBaseName();
-
-        for(var i:int=0;i<routes.length;i++){
-          var route:ComponentRoute = routes[i];
-          if(route.defaultRoute)
-            default = route;
-          if(route.baseName == baseName)
-          {
-            addComponent(route);
-            return;
-          }
-        }
-        if(default)
-        {
-          addComponent(default);
-        }
-      }      
-    }
-    private function addComponent(route:ComponentRoute):void{
-      var parent:IParent = route.parent || host.host as IParent;
-      while(parent.numElements > 0)
-        parent.removeElement(parent.getElementAt(0));
-      
-      var comp:IChild = new route.component();
-      parent.addElement(comp);
-      if(route.title)
-        host.routeState.title = route.title;
-
-    }
   }
 }
\ No newline at end of file
diff --git 
a/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/RouteToComponent.as
 
b/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/RouteToComponent.as
index 0c1c8ba..2c52c13 100644
--- 
a/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/RouteToComponent.as
+++ 
b/frameworks/projects/Basic/src/main/royale/org/apache/royale/routing/RouteToComponent.as
@@ -18,17 +18,15 @@
 
////////////////////////////////////////////////////////////////////////////////
 package org.apache.royale.routing
 {
-  import org.apache.royale.core.Bead;
-  import org.apache.royale.core.IStrand;
-  import org.apache.royale.events.ValueEvent;
-  import org.apache.royale.core.IParent;
   import org.apache.royale.core.IChild;
+  import org.apache.royale.core.IParent;
+  import org.apache.royale.core.IStrand;
   [DefaultProperty("routes")]
   /**
    * RouteToComponent is a bead designed for Router
    * One or more routes should be assigned to the bead
    */
-  public class RouteToComponent extends Bead
+  public class RouteToComponent extends PathRouteBead
   {
     public function RouteToComponent()
     {
@@ -37,59 +35,11 @@ package org.apache.royale.routing
 
     public var routes:Array;
 
-    /**
-     * @royaleignorecoercion org.apache.royale.routing.Router
-     */
-    private function get host():Router{
-      return _strand as Router
-    }
     override public function set strand(value:IStrand):void
     {
-      _strand = value;
-      listenOnStrand("hashNeeded",hashNeeded);
-      listenOnStrand("hashReceived",hashReceived);
+      super.strand = value;
       listenOnStrand("stateChange",stateChanged)
     }
-    private function hashNeeded(ev:ValueEvent):void
-    {
-      var hash:String = ev.value;
-      var trailing:String = "";
-      var delim:String = ""
-      if(hash.indexOf("?") != -1)
-        delim = "?"
-
-      else if(hash.indexOf("#") != -1)
-        delim = "#"
-      if(delim)
-      {
-        trailing = hash.slice(hash.indexOf(delim));
-        hash = hash.slice(0,hash.indexOf(delim));
-      }
-      // no bead added the path yet
-      if(!hash)
-      {
-        hash = host.routeState.path;
-      }
-      
-      ev.value = hash + trailing;
-
-    }
-
-    private function hashReceived(ev:ValueEvent):void
-    {
-      var hash:String = ev.value;
-      var trailing:String = "";
-      // if we have parameters, we don't care if we also have an anchor
-      var delim:String = ""
-      var index:int = hash.indexOf("?")
-      if(index == -1)
-        index = hash.indexOf("#");
-      
-      if(index != -1)
-        hash = hash.slice(0,index);
-      
-      host.routeState.path = hash;
-    }
 
     /**
      * @royaleignorecoercion org.apache.royale.core.IParent
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 6a6fcd3..334da2f 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
@@ -18,31 +18,22 @@
 
////////////////////////////////////////////////////////////////////////////////
 package org.apache.royale.routing
 {
-  import org.apache.royale.core.Bead;
+  import org.apache.royale.core.IStatesObject;
   import org.apache.royale.core.IStrand;
+  import org.apache.royale.debugging.assert;
   import org.apache.royale.events.ValueEvent;
-  import org.apache.royale.core.IStatesObject;
   import org.apache.royale.utils.callLater;
-  import org.apache.royale.debugging.assert;
 
-  public class RouteToState extends Bead
+  public class RouteToState extends PathRouteBead
   {
     public function RouteToState()
     {
       
     }
-    /**
-     * @royaleignorecoercion org.apache.royale.routing.Router
-     */
-    private function get host():Router{
-      return _strand as Router
-    }
 
     override public function set strand(value:IStrand):void
     {
-      _strand = value;
-      listenOnStrand("hashNeeded",hashNeeded);
-      listenOnStrand("hashReceived",hashReceived);
+      super.strand = value;
       listenOnStrand("stateChange",stateChanged)
       // attach state change event async to allow adding the parent strand 
after this is added.
       callLater(attachStateEvent);
@@ -63,7 +54,7 @@ package org.apache.royale.routing
       host.routeState.path = getStateComponent().currentState;
       host.setState();
     }
-    private function hashNeeded(ev:ValueEvent):void
+    override protected function hashNeeded(ev:ValueEvent):void
     {
       var hash:String = ev.value;
       // don't overwrite path, parameters and anchor
@@ -85,21 +76,6 @@ package org.apache.royale.routing
       
       ev.value = parentPath + getStateComponent().currentState + trailing;
     }
-    private function hashReceived(ev:ValueEvent):void
-    {
-      var hash:String = ev.value;
-      var trailing:String = "";
-      // if we have parameters, we don't care if we also have an anchor
-      var delim:String = ""
-      var index:int = hash.indexOf("?")
-      if(index == -1)
-        index = hash.indexOf("#");
-      
-      if(index != -1)
-        hash = hash.slice(0,index);
-      
-      host.routeState.path = hash;
-    }
     private var settingState:Boolean;
     /**
      * @royaleignorecoercion org.apache.royale.core.IStatesObject
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 05ad347..e6f713d 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
@@ -28,6 +28,7 @@ package org.apache.royale.routing
   import org.apache.royale.core.IMXMLDocument;
   import org.apache.royale.utils.MXMLDataInterpreter;
   import org.apache.royale.utils.sendStrandEvent;
+  import org.apache.royale.utils.loadBeadFromValuesManager;
   [DefaultProperty("beads")]
     /**
      *  Dispatched when the state is changed.
@@ -63,6 +64,7 @@ package org.apache.royale.routing
                public function set strand(value:IStrand):void
                {       
                        _strand = value;
+      loadBeadFromValuesManager(IPathRouteBead, "iPathRouteBead", this);
                        COMPILE::JS
                        {
                                window.addEventListener("hashchange", 
hashChangeHandler);

Reply via email to