-
Notifications
You must be signed in to change notification settings - Fork 12
/
router.js
155 lines (142 loc) · 5.6 KB
/
router.js
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
/*******************************************************************************
********************************************************************************
** **
** Copyright (c) 2012 Catch.com, Inc. **
** **
** Licensed 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. **
** **
********************************************************************************
*******************************************************************************/
goog.provide('mvc.Router');
goog.require('goog.History');
goog.require('goog.array');
goog.require('goog.events');
goog.require('goog.history.Html5History');
/**
* @constructor
* @extends {goog.events.EventTarget}
*
* @param {boolean=} opt_noFragment set to true to hide fragment when using
* HTML5 history.
* @param {string=} opt_blankPage url to a blank page - needed if HTML5 is not
* available and you don't want to show the fragment.
* @param {HTMLInputElement=} opt_input The hidden input element to be used to
* store the history token. If not provided, a hidden input element will
* be created using document.write.
* @param {HTMLIFrameElement=} opt_iframe The hidden iframe that will be used by
* IE for pushing history state changes, or by all browsers if opt_noFragment
* is true. If not provided, a hidden iframe element will be created using
* document.write.
*/
mvc.Router = function(opt_noFragment, opt_blankPage, opt_input, opt_iframe) {
goog.base(this);
this.history_ = goog.history.Html5History.isSupported() ?
new goog.history.Html5History() :
new goog.History(!!(opt_blankPage && opt_noFragment), opt_blankPage,
opt_input, opt_iframe);
if (this.history_.setUseFragment)
this.history_.setUseFragment(!opt_noFragment);
goog.events.listen(this.history_, goog.history.EventType.NAVIGATE,
this.onChange_, false, this);
this.routes_ = [];
this.currentFragment_ = "";
this.history_.setEnabled(true);
};
goog.inherits(mvc.Router, goog.events.EventTarget);
/**
* router event types
*/
mvc.Router.EventType = {
/*
* event to trigger when route is about to change.
*/
ROUTE_EXPIRED: "routeExpired"
}
/**
* pass through the fragment for the URL
*
* @param {string} fragment to set for the history token.
*/
mvc.Router.prototype.navigate = function(fragment) {
this.history_.setToken(fragment);
};
/**
* returns current routed fragment
* @return {string} routed fragment.
*/
mvc.Router.prototype.getFragment = function() {
return this.currentFragment_;
}
/**
* define route as string or regex. /:abc/ will pass "abc" through as an
* argument. *abc/def will pass through all after the * as an argument
*
* @param {string|RegExp} route to watch for.
* @param {function(string, ...[string])} fn should take in the token and any captured strings.
* @param {Object=} opt_context Object in whose context the function is to be
* called (the global scope if none).
*/
mvc.Router.prototype.route = function(route, fn, opt_context) {
if (goog.isString(route))
route = new RegExp('^' + goog.string.regExpEscape(route)
.replace(/\\:\w+/g, '([a-zA-Z0-9._-]+)')
.replace(/\\\*/g, '(.*)')
.replace(/\\\[/g, '(')
.replace(/\\\]/g, ')?')
.replace(/\\\{/g, '(?:')
.replace(/\\\}/g, ')?') + '$');
var completeRoute = {
route: route,
callback: fn,
context: opt_context
};
//this.runRouteIfMatches_(completeRoute, this.currentFragment_);
this.routes_.push(completeRoute);
};
/**
* run route callback if route regexp matches fragment
* @param {Object} route Route object with context and route regexp.
* @param {string} fragment URI fragment to match with.
*/
mvc.Router.prototype.runRouteIfMatches_ = function(route, fragment) {
var args = route.route.exec(fragment);
if (args) {
route.callback.apply(route.context, args);
return true;
}
return false;
}
/**
* @private
*/
mvc.Router.prototype.onChange_ = function() {
var fragment = this.history_.getToken();
if (fragment != this.currentFragment_) {
this.dispatchEvent({
type: mvc.Router.EventType.ROUTE_EXPIRED,
previous: this.currentFragment_,
current: fragment
});
this.currentFragment_ = fragment;
goog.array.find(this.routes_ || [], function(route) {
return this.runRouteIfMatches_(route, fragment);
}, this);
}
};
mvc.Router.prototype.checkRoutes = function() {
var fragment = this.history_.getToken();
this.currentFragment_ = fragment;
goog.array.find(this.routes_ || [], function(route) {
return this.runRouteIfMatches_(route, fragment);
}, this);
}