-
Notifications
You must be signed in to change notification settings - Fork 2
/
Router.js
304 lines (242 loc) · 7.8 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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/**
* App Router module
*
* Simple url routing system
*
* @example
*
* yam.Route.add( /^sad\/?([0-9]+)?$/g, 'regular' )
* yam.Route.add( 'sad/2', 'happy' );
*
* var res = yam.Route.match( 'sad/2' );
*
* res = [
* happy:[],
* regular: [ 0: 2 ]
* ]
*
* @author Andrew Tereshko <[email protected]>
* @version 0.2.1
*/
define( ['./Hub', './Logger', './State'], function( Hub, Logger, State ) {
//"use strict";
/**
* @class yam.Route
* @extends jQuery.Class
*/
$.Class.extend('yam.Route',
/** @static **/
{
_rules: [],
/**
* Add rule
*
* @param rule
* @param name
*/
add: function( rule, name )
{
this._rules.push( new this( rule, name ) )
},
/**
* Match path with rules, return matched
*
* @param path
* @returns {object}
*/
match: function( path )
{
if( !path ) {
path = State.current().location;
}
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;
},
/**
* Get current state path
*
* @deprecated since 0.3.2, now yam.State.current().location must be used
*
* @returns {String}
*/
getCurrentPath: function() {
return State.current().location;
},
/**
* Retrieve and parse GET parameters string
*
* @returns {Object}
*/
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;
var sc = hash[0].indexOf('['), cs = hash[0].indexOf(']');
if( sc > 0 && cs > 0 ) {
var paramName = hash[0].substr( 0, sc );
if( hash[0].charAt( sc+1 ) == ']' ) {
if( !params[ paramName ] ) { params[ paramName ] = [] }
params[ paramName ].push( hash[1] );
}
else {
if( !params[ paramName ] ) { params[ paramName ] = {} }
var paramPart = hash[0].substr( sc + 1, cs - sc - 1 );
params[ paramName ][ paramPart ] = hash[1];
}
}
else {
params[ hash[0] ] = hash[1] || '';
}
}
return params;
},
getParamsString: function()
{
// @TODO cut off History depency
var url = window.History.getState().url, paramsPos = url.indexOf('?');
if( paramsPos > 0 && paramsPos + 1 < url.length )
{
return url.slice( paramsPos + 1 );
}
return '';
},
/**
* @deprecated since 0.3.2, when History and browser related methods was removed
*
* @param index
* @returns {String}
*/
extractChunkURI: function( index ) {
var uri = State.current().location,
chunk = undefined;
if(!isNaN(index)) {
if(typeof uri === 'string' && uri.length > 0) {
if(uri.indexOf('/') >= 0) {
var chunks = uri.split('/');
if(chunks.length >= parseInt(index)) {
chunk = chunks[parseInt(index)];
}
}
}
}
return chunk;
},
/**
* Construct GET parameters string from object.
*
* @param params
* @returns {string}
*/
buildParamsString: function (data, traditional, noArrayKeys) {
var s = [],
rbracket = /\[\]$/,
r20 = /%20/g,
encode = function (value, encoded) {
return encoded ? value : encodeURIComponent(value);
},
add = function (key, value, keyEncoded) {
if( $.isFunction(value) ) return;
if( typeof value == 'undefined' || value === null ) value = '';
s[s.length] = encode(key, keyEncoded) + '=' + encode(value);
},
buildParams = function (key, data, traditional, add, keyEncoded) {
if (jQuery.isArray(data)) {
data.forEach( function ( v, i ) {
if (traditional || rbracket.test(key)) {
add(key, v);
} else {
buildParams(encode(key, keyEncoded) + '[' + (!noArrayKeys && (typeof v === "object" || jQuery.isArray(v)) ? i : '') + ']', v, traditional, add, true);
}
});
} else if (data !== null && typeof data === 'object') {
for( name in data ){
if( name.match( /jQuery[0-9]+/ig ) ) return;
buildParams(encode(key, keyEncoded) + '[' + encode(name) + ']', data[name], traditional, add, true);
};
} else {
add(key, data, keyEncoded);
}
};
// Set traditional to true for jQuery <= 1.3.2 behavior.
if (traditional === undefined) {
traditional = $.ajaxSettings.traditional;
}
if ( $.isArray(data) || (data.jquery && !$.isPlainObject(data))) {
$.each(data, function () {
add(this.name, this.value);
});
} else {
for( key in data){
buildParams(key, data[key], traditional, add);
}
}
return s.join('&').replace(r20, '+');
}
},
/** @prototype **/
{
/**
* Route name
*/
name: null,
/**
* Route rule ( can be string or regexp )
*/
rule: null,
/**
* Constructor
*
* @param rule
* @param name
*/
init: function( rule, name )
{
this.name = name;
this.rule = rule;
},
/**
* Match self rule with path.
*
* @param path
* @returns {boolean}
*/
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++ )
{
if( matches[ key ] !== undefined ) params.push( matches[ key ] );
}
}
}
else if ( path == this.rule )
{
params = [];
}
return params;
}
});
//backward compatibility
if( undefined === window.app ) window.app = {};
window.app.Route = yam.Route;
return yam.Route;
});