/
Router.as
274 lines (265 loc) · 7.96 KB
/
Router.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
////////////////////////////////////////////////////////////////////////////////
//
// 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.DispatcherBead;
import org.apache.royale.core.IStrand;
import org.apache.royale.debugging.assert;
import org.apache.royale.core.IStatesObject;
import org.apache.royale.events.Event;
import org.apache.royale.core.IInitialViewApplication;
/**
* 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")]
/**
* 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
*/
public class Router extends DispatcherBead
{
public function Router()
{
}
/**
* Use this to automatically sync the state of the strand.
* This only works for the state property of the RouterState.
* It also assumes that the strand is an IStatesObject.
* For this to work correctly, it's usually assumed that the bead is attached to the application View
* @langversion 3.0
* @playerversion Flash 10.2
* @playerversion AIR 2.6
* @productversion Royale 0.9.7
*/
public var syncState:Boolean;
override public function set strand(value:IStrand):void
{
_strand = value;
COMPILE::JS
{
window.addEventListener("hashchange", hashChangeHandler);
initialTitle = document.title;
}
// If it's an Application, listen to applicationComplete
if(_strand is IInitialViewApplication)
listenOnStrand("applicationComplete",onInit);
//Otherwise listen to initComplete
else
listenOnStrand("initComplete",onInit);
}
private function onInit(event:Event):void
{
COMPILE::JS
{
if(location.hash)
{
hashChangeHandler();
}
}
}
private var initialTitle:String;
private function hashChangeHandler():void
{
parseHash();
if(syncState)
{
assert(_strand is IStatesObject,"syncState can only be used on IStatesObjects");
(_strand as IStatesObject).currentState = _routeState.state;
}
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 paths:Array = hash.split("/");
var statePart:String = paths.pop();
var splitParts:Array = statePart.split("?");
statePart = splitParts[0];
_routeState = new RouteState(statePart,document.title);
_routeState.path = paths;
_routeState.parameters = parseParameters(splitParts[1]);
}
}
private function parseParameters(query:String):Object
{
var urlVars:Object;
if(query){
var vars:Array = query.split("&");
if(vars.length){
urlVars = {};
}
for (var i:int=0;i<vars.length;i++) {
var pair:Array = vars[i].split("=");
urlVars[pair[0]] = pair[1] == undefined ? undefined : decodeURIComponent(pair[1]);
}
}
return urlVars;
}
private function buildHash():String
{
var hash:String = "#!";
if(_routeState.path && routeState.path.length){
hash += (_routeState.path.join("/") + "/");
}
if(_routeState.state){
hash += _routeState.state;
}
hash+= buildParameterString();
return hash;
}
private function buildParameterString():String{
var retVal:String = "";
if(_routeState.parameters){
retVal += "?";
for(var x:String in _routeState.parameters){
retVal += x;
if(_routeState.parameters[x] != undefined){
retVal += "=" + encodeURIComponent(_routeState.parameters[x]);
retVal += "&";
}
}
//remove trailing &
retVal = retVal.slice(0, -1);
}
return retVal;
}
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
{
window.history.pushState({"title":_routeState.title},_routeState.title,buildHash());
if(_routeState.title)
{
document.title = _routeState.title;
}
}
}
/**
* 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();
if(syncState)
{
assert(_strand is IStatesObject,"syncState can only be used on IStatesObjects");
(_strand as IStatesObject).currentState = _routeState.state;
}
dispatchEvent(new Event("stateChange"));
}
private function setTitle():void
{
COMPILE::JS
{
if(window.history.state){
document.title = window.history.state["title"];
} else {
document.title = initialTitle;
}
}
}
/**
* Goes forward in the history
* @langversion 3.0
* @playerversion Flash 10.2
* @playerversion AIR 2.6
* @productversion Royale 0.9.7
*/
public function forward():void{
COMPILE::JS
{
window.history.forward();
setTitle();
parseHash();
}
}
/**
* Goes backwards in the history
* @langversion 3.0
* @playerversion Flash 10.2
* @playerversion AIR 2.6
* @productversion Royale 0.9.7
*/
public function back():void{
COMPILE::JS
{
window.history.back();
setTitle();
parseHash();
}
}
/**
* Moved the specified number of steps (forward or backwards) in the history
* calling it with 0 or no value will reload the page.
* @langversion 3.0
* @playerversion Flash 10.2
* @playerversion AIR 2.6
* @productversion Royale 0.9.7
*/
public function go(steps:int=0):void{
COMPILE::JS
{
window.history.go(steps);
parseHash();
}
}
}
}