diff options
author | Johannes Zellner <johannes@nebulon.de> | 2016-03-01 18:51:11 +0100 |
---|---|---|
committer | Johannes Zellner <johannes@nebulon.de> | 2016-03-01 18:51:11 +0100 |
commit | 04bc2989d966224126196280a529225d2bd115eb (patch) | |
tree | a6abd3cec1d35439d06fa968537fd337dbcbc33d /app/js | |
parent | 95c627bf3b81d0b9443d03a08c4dc5d7a366e0a2 (diff) | |
download | Surfer-04bc2989d966224126196280a529225d2bd115eb.tar.gz Surfer-04bc2989d966224126196280a529225d2bd115eb.tar.zst Surfer-04bc2989d966224126196280a529225d2bd115eb.zip |
Work off of the location hash to make browser history work
Diffstat (limited to 'app/js')
-rw-r--r-- | app/js/app.js | 26 |
1 files changed, 20 insertions, 6 deletions
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) { | |||
21 | localStorage.username = username; | 21 | localStorage.username = username; |
22 | localStorage.password = password; | 22 | localStorage.password = password; |
23 | 23 | ||
24 | loadDirectory(app.path); | 24 | loadDirectory(window.location.hash.slice(1)); |
25 | }); | 25 | }); |
26 | } | 26 | } |
27 | 27 | ||
@@ -43,6 +43,10 @@ function encode(filePath) { | |||
43 | return filePath.split('/').map(encodeURIComponent).join('/'); | 43 | return filePath.split('/').map(encodeURIComponent).join('/'); |
44 | } | 44 | } |
45 | 45 | ||
46 | function decode(filePath) { | ||
47 | return filePath.split('/').map(decodeURIComponent).join('/'); | ||
48 | } | ||
49 | |||
46 | var mimeTypes = { | 50 | var mimeTypes = { |
47 | images: [ '.png', '.jpg', '.jpeg', '.tiff', '.gif' ], | 51 | images: [ '.png', '.jpg', '.jpeg', '.tiff', '.gif' ], |
48 | text: [ '.txt', '.md' ], | 52 | text: [ '.txt', '.md' ], |
@@ -81,13 +85,16 @@ function loadDirectory(filePath) { | |||
81 | if (error) return console.error(error); | 85 | if (error) return console.error(error); |
82 | if (result.statusCode === 401) return logout(); | 86 | if (result.statusCode === 401) return logout(); |
83 | 87 | ||
84 | result.body.entries.sort(function (a, b) { return a.isDirectory && b.isFile ? -1 : 1 }); | 88 | result.body.entries.sort(function (a, b) { return a.isDirectory && b.isFile ? -1 : 1; }); |
85 | app.entries = result.body.entries.map(function (entry) { | 89 | app.entries = result.body.entries.map(function (entry) { |
86 | entry.previewUrl = getPreviewUrl(entry, filePath); | 90 | entry.previewUrl = getPreviewUrl(entry, filePath); |
87 | return entry; | 91 | return entry; |
88 | }); | 92 | }); |
89 | app.path = filePath; | 93 | app.path = filePath; |
90 | app.pathParts = filePath.split('/').filter(function (e) { return !!e; }); | 94 | app.pathParts = decode(filePath).split('/').filter(function (e) { return !!e; }); |
95 | |||
96 | // update in case this was triggered from code | ||
97 | window.location.hash = app.path; | ||
91 | 98 | ||
92 | Vue.nextTick(function () { | 99 | Vue.nextTick(function () { |
93 | $(function () { | 100 | $(function () { |
@@ -98,15 +105,18 @@ function loadDirectory(filePath) { | |||
98 | } | 105 | } |
99 | 106 | ||
100 | function open(entry) { | 107 | function open(entry) { |
101 | var path = sanitize(app.path + '/' + entry.filePath); | 108 | var path = encode(sanitize(app.path + '/' + entry.filePath)); |
102 | 109 | ||
103 | if (entry.isDirectory) return loadDirectory(path); | 110 | if (entry.isDirectory) { |
111 | window.location.hash = path; | ||
112 | return; | ||
113 | } | ||
104 | 114 | ||
105 | window.open(path); | 115 | window.open(path); |
106 | } | 116 | } |
107 | 117 | ||
108 | function up() { | 118 | function up() { |
109 | loadDirectory(app.path.split('/').slice(0, -1).filter(function (p) { return !!p; }).join('/')); | 119 | window.location.hash = encode(sanitize(app.path.split('/').slice(0, -1).filter(function (p) { return !!p; }).join('/'))); |
110 | } | 120 | } |
111 | 121 | ||
112 | function upload() { | 122 | function upload() { |
@@ -219,4 +229,8 @@ var app = new Vue({ | |||
219 | 229 | ||
220 | login(localStorage.username, localStorage.password); | 230 | login(localStorage.username, localStorage.password); |
221 | 231 | ||
232 | $(window).on('hashchange', function () { | ||
233 | loadDirectory(window.location.hash.slice(1)); | ||
234 | }); | ||
235 | |||
222 | })(); | 236 | })(); |