-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathampersand-router-query-parameters.js
302 lines (267 loc) · 10.3 KB
/
ampersand-router-query-parameters.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
var classExtend = require('ampersand-class-extend');
var Events = require('backbone-events-standalone');
var ampHistory = require('./ampersand-history-query-parameters');
var _ = require('underscore');
// Routers map faux-URLs to actions, and fire events when routes are
// matched. Creating a new one sets its `routes` hash, if not set statically.
var Router = module.exports = function (options) {
options || (options = {});
this.history = options.history || ampHistory;
if (options.routes) this.routes = options.routes;
this._bindRoutes();
this.initialize.apply(this, arguments);
};
// Cached regular expressions for matching named param parts and splatted
// parts of route strings.
var queryStringParam = /^\?(.*)/;
var optionalParam = /\((.*?)\)/g;
var namedParam = /(\(\?)?:\w+/g;
var splatParam = /\*\w+/g;
var escapeRegExp = /[\-{}\[\]+?.,\\\^$|#\s]/g;
var namesPattern = /[\:\*]([^\:\?\/]+)/g;
Router.arrayValueSplit = '|';
// Set up all inheritable **Backbone.Router** properties and methods.
_.extend(Router.prototype, Events, {
initialize: function (options) {
this.encodedSplatParts = options && options.encodedSplatParts;
},
route: function (route, name, callback) {
if (!_.isRegExp(route)) route = this._routeToRegExp(route);
if (_.isFunction(name)) {
callback = name;
name = '';
}
if (!callback) callback = this[name];
var router = this;
this.history.route(route, function (fragment) {
var args = router._extractParameters(route, fragment);
if (router.execute(callback, args, name) !== false) {
router.trigger.apply(router, ['route:' + name].concat(args));
router.trigger('route', name, args);
router.history.trigger('route', router, name, args);
}
});
return this;
},
// Execute a route handler with the provided parameters. This is an
// excellent place to do pre-route setup or post-route cleanup.
execute: function (callback, args, name) {
if (callback) callback.apply(this, args);
},
// Simple proxy to `ampHistory` to save a fragment into the history.
navigate: function (fragment, options) {
this.history.navigate(fragment, options);
return this;
},
// Helper for doing `internal` redirects without adding to history
// and thereby breaking backbutton functionality.
redirectTo: function (newUrl) {
this.navigate(newUrl, {replace: true, trigger: true});
},
// Bind all defined routes to `history`. We have to reverse the
// order of the routes here to support behavior where the most general
// routes can be defined at the bottom of the route map.
_bindRoutes: function () {
if (!this.routes) return;
this.routes = _.result(this, 'routes');
var route, routes = Object.keys(this.routes);
while ((route = routes.pop()) != null) {
this.route(route, this.routes[route]);
}
},
_routeToRegExp: function (route) {
var splatMatch = (splatParam.exec(route) || {index: -1}),
namedMatch = (namedParam.exec(route) || {index: -1}),
paramNames = route.match(namesPattern) || [];
route = route.replace(escapeRegExp, '\\$&')
.replace(optionalParam, '(?:$1)?')
.replace(namedParam, function (match, optional) {
return optional ? match : '([^\\/\\?]+)';
})
// `[^??]` is hacking around a regular expression bug under iOS4.
// If only `[^?]` is used then paths like signin/photos will fail
// while paths with `?` anywhere, like `signin/photos?`, will succeed.
.replace(splatParam, '([^??]*?)');
route += '(\\?.*)?';
var rtn = new RegExp('^' + route + '$');
// use the rtn value to hold some parameter data
if (splatMatch.index >= 0) {
// there is a splat
if (namedMatch >= 0) {
// negative value will indicate there is a splat match before any named matches
rtn.splatMatch = splatMatch.index - namedMatch.index;
} else {
rtn.splatMatch = -1;
}
}
// Map and remove any trailing ')' character that has been caught up in regex matching
rtn.paramNames = _.map(paramNames, function (name) { return name.replace(/\)$/, '').substring(1); });
rtn.namedParameters = this.namedParameters;
return rtn;
},
/**
* Given a route, and a URL fragment that it matches, return the array of
* extracted parameters.
*/
_extractParameters: function (route, fragment) {
var params = route.exec(fragment).slice(1),
namedParams = {};
if (params.length > 0 && !params[params.length - 1]) {
// remove potential invalid data from query params match
params.splice(params.length - 1, 1);
}
// do we have an additional query string?
var match = params.length && params[params.length - 1] && params[params.length - 1].match(queryStringParam);
if (match) {
var queryString = match[1];
var data = {};
if (queryString) {
var self = this;
iterateQueryString(queryString, function (name, value) {
self._setParamValue(name, value, data);
});
}
params[params.length - 1] = data;
_.extend(namedParams, data);
}
// decode params
var length = params.length;
if (route.splatMatch && this.encodedSplatParts) {
if (route.splatMatch < 0) {
// splat param is first
return params;
} else {
length = length - 1;
}
}
for (var i=0; i<length; i++) {
if (_.isString(params[i])) {
params[i] = parseParams(params[i]);
if (route.paramNames && route.paramNames.length >= i-1) {
namedParams[route.paramNames[i]] = params[i];
}
}
}
return (Router.namedParameters || route.namedParameters) ? [namedParams] : params;
},
/**
* Set the parameter value on the data hash
*/
_setParamValue: function (key, value, data) {
// use '.' to define hash separators
key = key.replace('[]', '');
key = key.replace('%5B%5D', '');
var parts = key.split('.');
var _data = data;
for (var i = 0; i < parts.length; i++) {
var part = parts[i];
if (i === parts.length - 1) {
// set the value
_data[part] = this._decodeParamValue(value, _data[part]);
} else {
_data = _data[part] = _data[part] || {};
}
}
},
/**
* Decode an individual parameter value (or list of values)
* @param value the complete value
* @param currentValue the currently known value (or list of values)
*/
_decodeParamValue: function (value, currentValue) {
// '|' will indicate an array. Array with 1 value is a=|b - multiple values can be a=b|c
var splitChar = Router.arrayValueSplit;
if (splitChar && value.indexOf(splitChar) >= 0) {
var values = value.split(splitChar);
// clean it up
for (var i = values.length - 1; i >= 0; i--) {
if (!values[i]) {
values.splice(i, 1);
} else {
values[i] = parseParams(values[i]);
}
}
return values;
}
value = parseParams(value);
if (!currentValue) {
return value;
} else if (_.isArray(currentValue)) {
currentValue.push(value);
return currentValue;
} else {
return [currentValue, value];
}
},
/**
* Return the route fragment with queryParameters serialized to query parameter string
*/
toFragment: function (route, queryParameters) {
if (queryParameters) {
if (!_.isString(queryParameters)) {
queryParameters = toQueryString(queryParameters);
}
if (queryParameters) {
route += '?' + queryParameters;
}
}
return route;
}
});
/**
* Serialize the val hash to query parameters and return it. Use the namePrefix to prefix all param names (for recursion)
*/
function toQueryString(val, namePrefix) {
var splitChar = Router.arrayValueSplit;
function encodeSplit(val) { return String(val).replace(splitChar, encodeURIComponent(splitChar)); }
if (!val) {
return '';
}
namePrefix = namePrefix || '';
var rtn = [];
_.each(val, function (_val, name) {
name = namePrefix + name;
if (_.isString(_val) || _.isNumber(_val) || _.isBoolean(_val) || _.isDate(_val)) {
// primitive type
if (_val != null) {
rtn.push(name + '=' + encodeSplit(encodeURIComponent(_val)));
}
} else if (_.isArray(_val)) {
// arrays use Backbone.Router.arrayValueSplit separator
var str = '';
for (var i = 0; i < _val.length; i++) {
var param = _val[i];
if (param != null) {
str += splitChar + encodeSplit(param);
}
}
if (str) {
rtn.push(name + '=' + str);
}
} else {
// dig into hash
var result = toQueryString(_val, name + '.');
if (result) {
rtn.push(result);
}
}
});
return rtn.join('&');
}
function parseParams(value) {
// decodeURIComponent doesn't touch '+'
try {
return decodeURIComponent(value.replace(/\+/g, ' '));
} catch (err) {
// Failover to whatever was passed if we get junk data
return value;
}
}
function iterateQueryString(queryString, callback) {
var keyValues = queryString.split('&');
_.each(keyValues, function (keyValue) {
var arr = keyValue.split('=');
callback(arr.shift(), arr.join('='));
});
}
Router.extend = classExtend;