Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorMorris Jobke <morris.jobke@gmail.com>2014-03-06 18:07:28 +0400
committerMorris Jobke <morris.jobke@gmail.com>2014-03-06 18:07:28 +0400
commit5f0a22586f7b912694df268776e97e3cff5d659d (patch)
tree1da30832d01e211e518b3d798765c8ea6fb90d2f /core
parent6e4ea09d52b23c216109d020dcceea5370817a73 (diff)
parentb46517f0129345e2199e160c73f418a0db1d509f (diff)
Merge pull request #7579 from owncloud/introduce-generateUrl-master
Introduce OC.generateUrl() in master
Diffstat (limited to 'core')
-rw-r--r--core/js/js.js24
-rw-r--r--core/js/tests/specs/coreSpec.js9
2 files changed, 33 insertions, 0 deletions
diff --git a/core/js/js.js b/core/js/js.js
index 77aadd23e03..279a5390a88 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
diff --git a/core/js/tests/specs/coreSpec.js b/core/js/tests/specs/coreSpec.js
index bb43810ef16..069546387c7 100644
--- a/core/js/tests/specs/coreSpec.js
+++ b/core/js/tests/specs/coreSpec.js
@@ -270,5 +270,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');
+ });
+ });
});