From 8e667d1934d6bd9b4344ac624fdd2846c89abb0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Thu, 6 Mar 2014 00:17:48 +0100 Subject: [PATCH 1/2] adding new javascript function OC.generateUrl(url, params) --- core/js/js.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/core/js/js.js b/core/js/js.js index 21ccee0f1d..89797ed2a3 100644 --- a/core/js/js.js +++ b/core/js/js.js @@ -175,6 +175,30 @@ var OC={ appswebroots:(typeof oc_appswebroots !== 'undefined') ? oc_appswebroots:false, currentUser:(typeof oc_current_user!=='undefined')?oc_current_user:false, coreApps:['', 'admin','log','search','settings','core','3rdparty'], + + /** + * Generates the absolute url for the given relative url, which can contain parameters. + * + * @returns {string} + * @param {string} url + * @param params + */ + 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); + }, + /** * get an absolute url to a file in an appen * @param app the id of the app the file belongs to From b46517f0129345e2199e160c73f418a0db1d509f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Thu, 6 Mar 2014 00:26:57 +0100 Subject: [PATCH 2/2] adding js unit tests for OC.generateUrl() --- core/js/tests/specs/coreSpec.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/core/js/tests/specs/coreSpec.js b/core/js/tests/specs/coreSpec.js index 478505e928..d69e1fc7e2 100644 --- a/core/js/tests/specs/coreSpec.js +++ b/core/js/tests/specs/coreSpec.js @@ -276,5 +276,14 @@ describe('Core base tests', function() { }); }); + describe('Generate Url', function() { + it('returns absolute urls', function() { + expect(OC.generateUrl('heartbeat')).toEqual(OC.webroot + '/index.php/heartbeat'); + expect(OC.generateUrl('/heartbeat')).toEqual(OC.webroot + '/index.php/heartbeat'); + }); + it('substitutes parameters', function() { + expect(OC.generateUrl('apps/files/download{file}', {file: '/Welcome.txt'})).toEqual(OC.webroot + '/index.php/apps/files/download/Welcome.txt'); + }); + }); });