diff options
author | Bart Visscher <bartv@thisnet.nl> | 2012-10-05 09:42:36 +0200 |
---|---|---|
committer | Bart Visscher <bartv@thisnet.nl> | 2012-10-05 09:42:36 +0200 |
commit | f3a211c03c3dd017e263ac5226a52eb62562d198 (patch) | |
tree | 4161ed0ab3b5fe695d08dc0d51df0a0ea02fe313 | |
parent | 167e9c1cc025ce920e48032ff5331fcb4f84de3d (diff) | |
download | nextcloud-server-f3a211c03c3dd017e263ac5226a52eb62562d198.tar.gz nextcloud-server-f3a211c03c3dd017e263ac5226a52eb62562d198.zip |
Implement routing on javascript side
-rw-r--r-- | core/js/router.js | 73 | ||||
-rw-r--r-- | core/routes.php | 3 | ||||
-rw-r--r-- | lib/base.php | 1 | ||||
-rw-r--r-- | lib/router.php | 18 | ||||
-rw-r--r-- | settings/js/users.js | 2 |
5 files changed, 96 insertions, 1 deletions
diff --git a/core/js/router.js b/core/js/router.js new file mode 100644 index 00000000000..8b66f5a05c5 --- /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 d396ddd6473..8d836816265 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 2d82d5a40fc..f7967329c29 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 da491e217fc..04a3d410060 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 81a3181ba59..1474ebcdd81 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); |