diff options
author | Thomas Müller <thomas.mueller@tmit.eu> | 2014-03-02 22:30:24 +0100 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2014-03-02 22:30:24 +0100 |
commit | 1291303c5a312fad9f01fbaf22cc21e3b9b3675d (patch) | |
tree | c1e46efc7589c3101a8c3806ecc3112c8f3e2849 /core | |
parent | 88c9ae129e7dcc0e10aee682c8d4fa31b93b614c (diff) | |
download | nextcloud-server-1291303c5a312fad9f01fbaf22cc21e3b9b3675d.tar.gz nextcloud-server-1291303c5a312fad9f01fbaf22cc21e3b9b3675d.zip |
Replace OC.Router.generate() with OC.generateUrl()
Diffstat (limited to 'core')
-rw-r--r-- | core/js/jquery.avatar.js | 41 | ||||
-rw-r--r-- | core/js/js.js | 31 | ||||
-rw-r--r-- | core/js/router.js | 81 | ||||
-rw-r--r-- | core/js/tags.js | 18 | ||||
-rw-r--r-- | core/routes.php | 2 |
5 files changed, 55 insertions, 118 deletions
diff --git a/core/js/jquery.avatar.js b/core/js/jquery.avatar.js index 02a40c088b4..381c42d9dbb 100644 --- a/core/js/jquery.avatar.js +++ b/core/js/jquery.avatar.js @@ -75,31 +75,32 @@ var $div = this; - OC.Router.registerLoadedCallback(function() { - var url = OC.Router.generate('core_avatar_get', {user: user, size: size})+'?requesttoken='+oc_requesttoken; - $.get(url, function(result) { - if (typeof(result) === 'object') { - if (!hidedefault) { - if (result.data && result.data.displayname) { - $div.imageplaceholder(user, result.data.displayname); - } else { - $div.imageplaceholder(user); - } + var url = OC.generateUrl( + '/avatar/{user}/{size}?requesttoken={requesttoken}', + {user: user, size: size, requesttoken: oc_requesttoken}); + + $.get(url, function(result) { + if (typeof(result) === 'object') { + if (!hidedefault) { + if (result.data && result.data.displayname) { + $div.imageplaceholder(user, result.data.displayname); } else { - $div.hide(); + $div.imageplaceholder(user); } } else { - $div.show(); - if (ie8fix === true) { - $div.html('<img src="'+url+'#'+Math.floor(Math.random()*1000)+'">'); - } else { - $div.html('<img src="'+url+'">'); - } + $div.hide(); } - if(typeof callback === 'function') { - callback(); + } else { + $div.show(); + if (ie8fix === true) { + $div.html('<img src="'+url+'#'+Math.floor(Math.random()*1000)+'">'); + } else { + $div.html('<img src="'+url+'">'); } - }); + } + if(typeof callback === 'function') { + callback(); + } }); }; }(jQuery)); diff --git a/core/js/js.js b/core/js/js.js index 21ccee0f1d5..80d83dc07f9 100644 --- a/core/js/js.js +++ b/core/js/js.js @@ -194,6 +194,27 @@ var OC={ linkToRemoteBase:function(service) { return OC.webroot + '/remote.php/' + service; }, + + generateUrl: function(url, params) { + var _build = function (text, vars) { + return text.replace(/{([^{}]*)}/g, + function (a, b) { + var r = vars[b]; + return typeof r === 'string' || typeof r === 'number' ? r : a; + } + ); + }; + if (url.charAt(0) !== '/') { + url = '/' + url; + + } + return OC.webroot + '/index.php' + _build(url, params); + }, + + linkToRoute:function(route) { + return OC.webroot + '/index.php/' + route; + }, + /** * @brief Creates an absolute url for remote use * @param string $service id @@ -791,12 +812,10 @@ function initCore() { if (interval < 60) { interval = 60; } - OC.Router.registerLoadedCallback(function(){ - var url = OC.Router.generate('heartbeat'); - setInterval(function(){ - $.post(url); - }, interval * 1000); - }); + var url = OC.linkToRoute('heartbeat'); + setInterval(function(){ + $.post(url); + }, interval * 1000); } // session heartbeat (defaults to enabled) diff --git a/core/js/router.js b/core/js/router.js deleted file mode 100644 index e6ef54a1864..00000000000 --- a/core/js/router.js +++ /dev/null @@ -1,81 +0,0 @@ -OC.router_base_url = OC.webroot + '/index.php'; -OC.Router = { - // register your ajax requests to load after the loading of the routes - // has finished. otherwise you face problems with race conditions - registerLoadedCallback: function(callback){ - if (!this.routes_request){ - return; - } - this.routes_request.done(callback); - }, - routes_request: !window.TESTING && $.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') { - console.warn('To avoid race conditions, please register a callback');// 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/js/tags.js b/core/js/tags.js index bc6d7b4e071..bc2b42bf5ff 100644 --- a/core/js/tags.js +++ b/core/js/tags.js @@ -69,7 +69,7 @@ OC.Tags= { type = type ? type : this.type; var defer = $.Deferred(), self = this, - url = OC.Router.generate('core_tags_ids_for_tag', {type: type}); + url = OC.generateUrl('/tags/{type}/ids', {type: type}); $.getJSON(url, {tag: tag}, function(response) { if(response.status === 'success') { defer.resolve(response.ids); @@ -90,7 +90,7 @@ OC.Tags= { type = type ? type : this.type; var defer = $.Deferred(), self = this, - url = OC.Router.generate('core_tags_favorites', {type: type}); + url = OC.generateUrl('/tags/{type}/favorites', {type: type}); $.getJSON(url, function(response) { if(response.status === 'success') { defer.resolve(response.ids); @@ -111,7 +111,7 @@ OC.Tags= { type = type ? type : this.type; var defer = $.Deferred(), self = this, - url = OC.Router.generate('core_tags_tags', {type: type}); + url = OC.generateUrl('/tags/{type}', {type: type}); $.getJSON(url, function(response) { if(response.status === 'success') { defer.resolve(response.tags); @@ -133,7 +133,7 @@ OC.Tags= { type = type ? type : this.type; var defer = $.Deferred(), self = this, - url = OC.Router.generate('core_tags_tag', {type: type, id: id}); + url = OC.generateUrl('/tags/{type}/tag/{id}/', {type: type, id: id}); $.post(url, {tag: tag}, function(response) { if(response.status === 'success') { defer.resolve(response); @@ -157,7 +157,7 @@ OC.Tags= { type = type ? type : this.type; var defer = $.Deferred(), self = this, - url = OC.Router.generate('core_tags_untag', {type: type, id: id}); + url = OC.generateUrl('/tags/{type}/untag/{id}/', {type: type, id: id}); $.post(url, {tag: tag}, function(response) { if(response.status === 'success') { defer.resolve(response); @@ -181,7 +181,7 @@ OC.Tags= { type = type ? type : this.type; var defer = $.Deferred(), self = this, - url = OC.Router.generate('core_tags_favorite', {type: type, id: id}); + url = OC.generateUrl('/tags/{type}/favorite/{id}/', {type: type, id: id}); $.post(url, function(response) { if(response.status === 'success') { defer.resolve(response); @@ -205,7 +205,7 @@ OC.Tags= { type = type ? type : this.type; var defer = $.Deferred(), self = this, - url = OC.Router.generate('core_tags_unfavorite', {type: type, id: id}); + url = OC.generateUrl('/tags/{type}/unfavorite/{id}/', {type: type, id: id}); $.post(url, function(response) { if(response.status === 'success') { defer.resolve(); @@ -229,7 +229,7 @@ OC.Tags= { type = type ? type : this.type; var defer = $.Deferred(), self = this, - url = OC.Router.generate('core_tags_add', {type: type}); + url = OC.generateUrl('/tags/{type}/add', {type: type}); $.post(url,{tag:tag}, function(response) { if(typeof cb == 'function') { cb(response); @@ -256,7 +256,7 @@ OC.Tags= { type = type ? type : this.type; var defer = $.Deferred(), self = this, - url = OC.Router.generate('core_tags_delete', {type: type}); + url = OC.generateUrl('/tags/{type}/delete', {type: type}); if(!tags || !tags.length) { throw new Error(t('core', 'No tags selected for deletion.')); } diff --git a/core/routes.php b/core/routes.php index aea788bdc6b..76cf03c3673 100644 --- a/core/routes.php +++ b/core/routes.php @@ -65,8 +65,6 @@ $this->create('core_tags_delete', '/tags/{type}/delete') $this->create('js_config', '/core/js/oc.js') ->actionInclude('core/js/config.php'); // Routing -$this->create('core_ajax_routes', '/core/routes.json') - ->action('OC_Router', 'JSRoutes'); $this->create('core_ajax_preview', '/core/preview.png') ->actionInclude('core/ajax/preview.php'); $this->create('core_lostpassword_index', '/lostpassword/') |