2013-07-31 19:21:02 +00:00
|
|
|
OC.router_base_url = OC.webroot + '/index.php';
|
2012-10-05 07:42:36 +00:00
|
|
|
OC.Router = {
|
2012-11-14 21:37:21 +00:00
|
|
|
// register your ajax requests to load after the loading of the routes
|
|
|
|
// has finished. otherwise you face problems with race conditions
|
|
|
|
registerLoadedCallback: function(callback){
|
2014-01-12 17:57:53 +00:00
|
|
|
if (!this.routes_request){
|
|
|
|
return;
|
|
|
|
}
|
2012-11-15 14:01:21 +00:00
|
|
|
this.routes_request.done(callback);
|
2012-11-14 21:37:21 +00:00
|
|
|
},
|
2014-01-12 17:57:53 +00:00
|
|
|
routes_request: !window.TESTING && $.ajax(OC.router_base_url + '/core/routes.json', {
|
2012-10-05 07:42:36 +00:00
|
|
|
dataType: 'json',
|
|
|
|
success: function(jsondata) {
|
2012-11-14 21:37:21 +00:00
|
|
|
if (jsondata.status === 'success') {
|
2012-10-05 07:42:36 +00:00
|
|
|
OC.Router.routes = jsondata.data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
generate:function(name, opt_params) {
|
|
|
|
if (!('routes' in this)) {
|
|
|
|
if(this.routes_request.state() != 'resolved') {
|
2012-11-14 22:55:37 +00:00
|
|
|
console.warn('To avoid race conditions, please register a callback');// wait
|
2012-10-05 07:42:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!(name in this.routes)) {
|
|
|
|
throw new Error('The route "' + name + '" does not exist.');
|
|
|
|
}
|
|
|
|
var route = this.routes[name];
|
|
|
|
var params = opt_params || {};
|
|
|
|
var unusedParams = $.extend(true, {}, params);
|
|
|
|
var url = '';
|
|
|
|
var optional = true;
|
|
|
|
$(route.tokens).each(function(i, token) {
|
|
|
|
if ('text' === token[0]) {
|
|
|
|
url = token[1] + url;
|
|
|
|
optional = false;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('variable' === token[0]) {
|
|
|
|
if (false === optional || !(token[3] in route.defaults)
|
|
|
|
|| ((token[3] in params) && params[token[3]] != route.defaults[token[3]])) {
|
|
|
|
var value;
|
|
|
|
if (token[3] in params) {
|
|
|
|
value = params[token[3]];
|
|
|
|
delete unusedParams[token[3]];
|
|
|
|
} else if (token[3] in route.defaults) {
|
|
|
|
value = route.defaults[token[3]];
|
|
|
|
} else if (optional) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
throw new Error('The route "' + name + '" requires the parameter "' + token[3] + '".');
|
|
|
|
}
|
|
|
|
|
|
|
|
var empty = true === value || false === value || '' === value;
|
|
|
|
|
|
|
|
if (!empty || !optional) {
|
|
|
|
url = token[1] + encodeURIComponent(value).replace(/%2F/g, '/') + url;
|
|
|
|
}
|
|
|
|
|
|
|
|
optional = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error('The token type "' + token[0] + '" is not supported.');
|
|
|
|
});
|
|
|
|
if (url === '') {
|
|
|
|
url = '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
unusedParams = $.param(unusedParams);
|
|
|
|
if (unusedParams.length > 0) {
|
|
|
|
url += '?'+unusedParams;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OC.router_base_url + url;
|
|
|
|
}
|
2014-01-12 17:57:53 +00:00
|
|
|
}
|