diff --git a/core/js/router.js b/core/js/router.js new file mode 100644 index 0000000000..8b66f5a05c --- /dev/null +++ b/core/js/router.js @@ -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; + } +}; diff --git a/core/routes.php b/core/routes.php index d396ddd647..8d83681626 100644 --- a/core/routes.php +++ b/core/routes.php @@ -28,6 +28,9 @@ $this->create('core_ajax_vcategories_delete', '/core/ajax/vcategories/delete.php ->actionInclude('core/ajax/vcategories/delete.php'); $this->create('core_ajax_vcategories_edit', '/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 $this->create('app_css', '/apps/{app}/{file}') diff --git a/lib/base.php b/lib/base.php index 2d82d5a40f..f7967329c2 100644 --- a/lib/base.php +++ b/lib/base.php @@ -253,6 +253,7 @@ class OC{ OC_Util::addScript( "config" ); //OC_Util::addScript( "multiselect" ); OC_Util::addScript('search', 'result'); + OC_Util::addScript('router'); if( OC_Config::getValue( 'installed', false )) { if( OC_Appconfig::getValue( 'core', 'backgroundjobs_mode', 'ajax' ) == 'ajax' ) { diff --git a/lib/router.php b/lib/router.php index da491e217f..04a3d41006 100644 --- a/lib/router.php +++ b/lib/router.php @@ -94,4 +94,22 @@ class OC_Router { { 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 ) ); + } } diff --git a/settings/js/users.js b/settings/js/users.js index 81a3181ba5..1474ebcdd8 100644 --- a/settings/js/users.js +++ b/settings/js/users.js @@ -130,7 +130,7 @@ var UserList={ if (typeof UserList.offset === 'undefined') { 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') { $.each(result.data, function(index, user) { var tr = UserList.add(user.name, user.groups, user.subadmin, user.quota, false);