forked from mamonth/yam-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Router.js
192 lines (146 loc) · 4.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
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
/**
* App Router module
*
* Simple url routing system
*
* @example
*
* App.Route.add( /^sad\/?([0-9]+)?$/g, 'regular' )
* App.Route.add( 'sad/2', 'happy' );
*
* var res = App.Route.match( 'sad/2' );
*
* res = [
* happy:[],
* regular: [ 0: 2 ]
* ]
*
* @author Andrew Tereshko <[email protected]>
* @version 0.2.1
*/
define("app/Router", ["app/Hub", "app/Logger"], function(Hub, Logger) {
//"use strict";
$.Class.extend("app.Route",
/* @static */
{
_rules: [],
add: function( rule, name )
{
this._rules.push( new this( rule, name ) )
},
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;
},
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;
},
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()
{
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 '';
},
extractChunkURI: function(index) {
var uri = this.getCurrentPath(),
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;
},
buildParamsString: function( params )
{
return decodeURIComponent( $.param( params ) );
}
},
/* @prototype */
{
name: null,
rule: null,
init: function( rule, name )
{
this.name = name;
this.rule = rule;
},
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;
}
});
return app.Route;
});