From 3c4b1911fdd99e58fa585f905f7786cdabf96c91 Mon Sep 17 00:00:00 2001 From: Nils Asmussen Date: Sun, 21 Aug 2016 15:12:32 +0200 Subject: [PATCH] Move URL building into one function. --- src/scripts/album.js | 14 ++++---------- src/scripts/lychee.js | 12 ++++++++++++ src/scripts/photo.js | 27 ++++++++++----------------- 3 files changed, 26 insertions(+), 27 deletions(-) diff --git a/src/scripts/album.js b/src/scripts/album.js index 5828c26..c12110f 100644 --- a/src/scripts/album.js +++ b/src/scripts/album.js @@ -627,18 +627,12 @@ album.share = function(service) { album.getArchive = function(albumIDs) { - let link = '' - let url = `${ api.path }?function=Album::getArchive&albumIDs=${ albumIDs.join(',') }` + let path = `${ api.path }?function=Album::getArchive&albumIDs=${ albumIDs.join(',') }` + let url = lychee.getURL(path) - var pos = location.href.indexOf('#') - link = pos!=-1 ? location.href.substring(0, pos) : location.href + if (lychee.publicMode===true) url += `&password=${ encodeURIComponent(password.value) }` - if (location.href.indexOf('index.html')>0) link = link.replace('index.html', url) - else link += url - - if (lychee.publicMode===true) link += `&password=${ encodeURIComponent(password.value) }` - - location.href = link + location.href = url } diff --git a/src/scripts/lychee.js b/src/scripts/lychee.js index 81be85b..1a47c85 100644 --- a/src/scripts/lychee.js +++ b/src/scripts/lychee.js @@ -417,6 +417,18 @@ lychee.html = function(literalSections, ...substs) { } +lychee.getURL = function(path) { + + let pos = location.href.indexOf('#') + let url = pos!=-1 ? location.href.substring(0, pos) : location.href + + if (location.href.indexOf('index.html')>0) url = url.replace('index.html', path) + else url += path + + return url + +} + lychee.error = function(errorThrown, params, data) { console.error({ diff --git a/src/scripts/photo.js b/src/scripts/photo.js index 0e606c7..75d9cad 100644 --- a/src/scripts/photo.js +++ b/src/scripts/photo.js @@ -635,29 +635,23 @@ photo.share = function(photoID, service) { photo.getPhoto = function(photoID) { - let link - let url = `${ api.path }?function=Photo::getPhoto&photoID=${ photoID }` + let path = `${ api.path }?function=Photo::getPhoto&photoID=${ photoID }` + let url = lychee.getURL(path) - if (location.href.indexOf('index.html')>0) link = location.href.replace(location.hash, '').replace('index.html', url) - else link = location.href.replace(location.hash, '') + url + if (lychee.publicMode===true) url += `&password=${ encodeURIComponent(password.value) }` - if (lychee.publicMode===true) link += `&password=${ encodeURIComponent(password.value) }` - - location.href = link + location.href = url } photo.getArchive = function(photoIDs) { - let link - let url = `${ api.path }?function=Photo::getArchive&photoIDs=${ photoIDs.join(',') }` + let path = `${ api.path }?function=Photo::getArchive&photoIDs=${ photoIDs.join(',') }` + let url = lychee.getURL(path) - if (location.href.indexOf('index.html')>0) link = location.href.replace(location.hash, '').replace('index.html', url) - else link = location.href.replace(location.hash, '') + url + if (lychee.publicMode===true) url += `&password=${ encodeURIComponent(password.value) }` - if (lychee.publicMode===true) link += `&password=${ encodeURIComponent(password.value) }` - - location.href = link + location.href = url } @@ -673,9 +667,8 @@ photo.getDirectLink = function() { photo.getViewLink = function(photoID) { - let url = 'view.php?p=' + photoID + let path = 'view.php?p=' + photoID - if (location.href.indexOf('index.html')>0) return location.href.replace('index.html' + location.hash, url) - else return location.href.replace(location.hash, url) + return lychee.getURL(path) } \ No newline at end of file