From: Johannes Zellner Date: Tue, 1 Mar 2016 17:51:11 +0000 (+0100) Subject: Work off of the location hash to make browser history work X-Git-Url: https://git.immae.eu/?a=commitdiff_plain;h=04bc2989d966224126196280a529225d2bd115eb;p=perso%2FImmae%2FProjets%2FNodejs%2FSurfer.git Work off of the location hash to make browser history work --- diff --git a/app/js/app.js b/app/js/app.js index 8721486..228184d 100644 --- a/app/js/app.js +++ b/app/js/app.js @@ -21,7 +21,7 @@ function login(username, password) { localStorage.username = username; localStorage.password = password; - loadDirectory(app.path); + loadDirectory(window.location.hash.slice(1)); }); } @@ -43,6 +43,10 @@ function encode(filePath) { return filePath.split('/').map(encodeURIComponent).join('/'); } +function decode(filePath) { + return filePath.split('/').map(decodeURIComponent).join('/'); +} + var mimeTypes = { images: [ '.png', '.jpg', '.jpeg', '.tiff', '.gif' ], text: [ '.txt', '.md' ], @@ -81,13 +85,16 @@ function loadDirectory(filePath) { if (error) return console.error(error); if (result.statusCode === 401) return logout(); - result.body.entries.sort(function (a, b) { return a.isDirectory && b.isFile ? -1 : 1 }); + result.body.entries.sort(function (a, b) { return a.isDirectory && b.isFile ? -1 : 1; }); app.entries = result.body.entries.map(function (entry) { entry.previewUrl = getPreviewUrl(entry, filePath); return entry; }); app.path = filePath; - app.pathParts = filePath.split('/').filter(function (e) { return !!e; }); + app.pathParts = decode(filePath).split('/').filter(function (e) { return !!e; }); + + // update in case this was triggered from code + window.location.hash = app.path; Vue.nextTick(function () { $(function () { @@ -98,15 +105,18 @@ function loadDirectory(filePath) { } function open(entry) { - var path = sanitize(app.path + '/' + entry.filePath); + var path = encode(sanitize(app.path + '/' + entry.filePath)); - if (entry.isDirectory) return loadDirectory(path); + if (entry.isDirectory) { + window.location.hash = path; + return; + } window.open(path); } function up() { - loadDirectory(app.path.split('/').slice(0, -1).filter(function (p) { return !!p; }).join('/')); + window.location.hash = encode(sanitize(app.path.split('/').slice(0, -1).filter(function (p) { return !!p; }).join('/'))); } function upload() { @@ -219,4 +229,8 @@ var app = new Vue({ login(localStorage.username, localStorage.password); +$(window).on('hashchange', function () { + loadDirectory(window.location.hash.slice(1)); +}); + })();