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 /core | |
parent | 167e9c1cc025ce920e48032ff5331fcb4f84de3d (diff) | |
download | nextcloud-server-f3a211c03c3dd017e263ac5226a52eb62562d198.tar.gz nextcloud-server-f3a211c03c3dd017e263ac5226a52eb62562d198.zip |
Implement routing on javascript side
Diffstat (limited to 'core')
-rw-r--r-- | core/js/router.js | 73 | ||||
-rw-r--r-- | core/routes.php | 3 |
2 files changed, 76 insertions, 0 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}') |