-
Notifications
You must be signed in to change notification settings - Fork 0
/
Router.js
170 lines (133 loc) · 4 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
(function(ns) {
"use strict";
/**
* Simple url routing system.
* @name wader.Router
* @class Wader Router
* @author Andrew Tereshko <[email protected]>
* @version 0.3
* @example
*
* Wader.Route.add(/^sad\/?([0-9]+)?$/g, "regular");
* Wader.Route.add("sad/2", "happy");
*
* var res = Wader.Route.match("sad/2");
*
* res = [
* happy:[],
* regular: [0: 2]
* ]
*/
$.Class.extend("wader.Router", {
/** @lends wader.Router */
_rules: [],
/**
* @param {RegExp} rule
* @param {String} name
* @return {undefined}
*/
add: function (rule, name) {
this._rules.push(new this(rule, name));
},
/**
* @param {String} path
* @return {Hash}
*/
match: function (path) {
if (!path) {
path = this.getCurrentPath();
}
var result = {}, rulesCount = this._rules.length;
for (var i = 0; i < rulesCount; i++) {
if (result[this._rules[i].name] === undefined) {
var params = this._rules[i].match(path);
if (params !== false) {
result[this._rules[i].name] = params;
}
}
}
return result;
},
/**
* @return {String}
*/
getCurrentPath: function () {
var path = window.History.getState().url.replace( /^https?:\/\/[^\/]+\//, '');
path = path.replace(/\?([^#\?]+)?/g, '');
path = path.replace(/(#|#!).*/, "/");
// Remove slash duplicates
path = path.replace(/\/+/, '/');
return path;
},
/**
* @return {String[]}
*/
getParams: function () {
var params = {}, hash;
var hashes = this.getParamsString().split('&');
var i = hashes.length;
while (i--) {
if (!hashes[i]) continue;
hash = hashes[i].split('=');
if (!hash[0]) continue;
params[hash[0]] = hash[1] || '';
}
return params;
},
/**
* @return {String}
*/
getParamsString: function () {
var paramsPos = window.History.getState().url.indexOf('?');
if (paramsPos > 0 && paramsPos + 1 < window.History.getState().url.length) {
return window.History.getState().url.slice(paramsPos + 1);
}
return '';
},
/**
* @param {String[]} params
* @return {String}
*/
buildParamsString: function (params) {
var paramsString = '';
for (variable in params) {
paramsString = paramsString + (paramsString.length ? '&' : '') + variable + '=' + params[variable];
}
return paramsString;
}
},
/** @lends wader.Router# */
{
name: null,
rule: null,
/**
* @param {RegExp} rule
* @param {String} name
* @return {undefined}
*/
init: function (rule, name) {
this.name = name;
this.rule = rule;
},
/**
* @param {String} path
* @return {String[]}
*/
match: function (path) {
var params = false, matches, key;
if (this.rule instanceof RegExp) {
matches = this.rule.exec(path);
if (matches) {
params = [];
for (key = 1; key <= matches.length; key++) {
params.push(matches[key]);
}
}
} else if (path == this.rule) {
params = [];
}
return params;
}
});
if (ns !== wader) ns.Router = wader.Router;
})(window.WADER_NS || window);