Implement routing on javascript side

This commit is contained in:
Bart Visscher 2012-10-05 09:42:36 +02:00
parent 167e9c1cc0
commit f3a211c03c
5 changed files with 96 additions and 1 deletions

73
core/js/router.js Normal file
View File

@ -0,0 +1,73 @@
OC.router_base_url = OC.webroot + '/index.php/',
OC.Router = {
routes_request: $.ajax(OC.router_base_url + 'core/routes.json', {
dataType: 'json',
success: function(jsondata) {
if (jsondata.status == 'success') {
OC.Router.routes = jsondata.data;
}
}
}),
generate:function(name, opt_params) {
if (!('routes' in this)) {
if(this.routes_request.state() != 'resolved') {
alert('wait');// wait
}
}
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;
}
};

View File

@ -28,6 +28,9 @@ $this->create('core_ajax_vcategories_delete', '/core/ajax/vcategories/delete.php
->actionInclude('core/ajax/vcategories/delete.php'); ->actionInclude('core/ajax/vcategories/delete.php');
$this->create('core_ajax_vcategories_edit', '/core/ajax/vcategories/edit.php') $this->create('core_ajax_vcategories_edit', '/core/ajax/vcategories/edit.php')
->actionInclude('core/ajax/vcategories/edit.php'); ->actionInclude('core/ajax/vcategories/edit.php');
// Routing
$this->create('core_ajax_routes', '/core/routes.json')
->action('OC_Router', 'JSRoutes');
// Not specifically routed // Not specifically routed
$this->create('app_css', '/apps/{app}/{file}') $this->create('app_css', '/apps/{app}/{file}')

View File

@ -253,6 +253,7 @@ class OC{
OC_Util::addScript( "config" ); OC_Util::addScript( "config" );
//OC_Util::addScript( "multiselect" ); //OC_Util::addScript( "multiselect" );
OC_Util::addScript('search', 'result'); OC_Util::addScript('search', 'result');
OC_Util::addScript('router');
if( OC_Config::getValue( 'installed', false )) { if( OC_Config::getValue( 'installed', false )) {
if( OC_Appconfig::getValue( 'core', 'backgroundjobs_mode', 'ajax' ) == 'ajax' ) { if( OC_Appconfig::getValue( 'core', 'backgroundjobs_mode', 'ajax' ) == 'ajax' ) {

View File

@ -94,4 +94,22 @@ class OC_Router {
{ {
return $this->getGenerator()->generate($name, $parameters, $absolute); return $this->getGenerator()->generate($name, $parameters, $absolute);
} }
public static function JSRoutes()
{
// TODO: http caching
$routes = array();
$router = OC::getRouter();
$root = $router->getCollection('root');
foreach($root->all() as $name => $route) {
$compiled_route = $route->compile();
$defaults = $route->getDefaults();
unset($defaults['action']);
$routes[$name] = array(
'tokens' => $compiled_route->getTokens(),
'defaults' => $defaults,
);
}
OCP\JSON::success ( array( 'data' => $routes ) );
}
} }

View File

@ -130,7 +130,7 @@ var UserList={
if (typeof UserList.offset === 'undefined') { if (typeof UserList.offset === 'undefined') {
UserList.offset = $('tbody tr').length; UserList.offset = $('tbody tr').length;
} }
$.get(OC.filePath('settings', 'ajax', 'userlist'), { offset: UserList.offset }, function(result) { $.get(OC.Router.generate('settings_ajax_userlist', { offset: UserList.offset }), function(result) {
if (result.status === 'success') { if (result.status === 'success') {
$.each(result.data, function(index, user) { $.each(result.data, function(index, user) {
var tr = UserList.add(user.name, user.groups, user.subadmin, user.quota, false); var tr = UserList.add(user.name, user.groups, user.subadmin, user.quota, false);