diff options
Diffstat (limited to 'frontend/js')
-rw-r--r-- | frontend/js/app.js | 57 |
1 files changed, 34 insertions, 23 deletions
diff --git a/frontend/js/app.js b/frontend/js/app.js index d659b18..1500457 100644 --- a/frontend/js/app.js +++ b/frontend/js/app.js | |||
@@ -1,6 +1,23 @@ | |||
1 | (function () { | 1 | (function () { |
2 | 'use strict'; | 2 | 'use strict'; |
3 | 3 | ||
4 | // poor man's async | ||
5 | function asyncForEach(items, handler, callback) { | ||
6 | var cur = 0; | ||
7 | |||
8 | if (items.length === 0) return callback(); | ||
9 | |||
10 | (function iterator() { | ||
11 | handler(items[cur], function (error) { | ||
12 | if (error) return callback(error); | ||
13 | if (cur >= items.length-1) return callback(); | ||
14 | ++cur; | ||
15 | |||
16 | iterator(); | ||
17 | }); | ||
18 | })(); | ||
19 | } | ||
20 | |||
4 | function getProfile(accessToken, callback) { | 21 | function getProfile(accessToken, callback) { |
5 | callback = callback || function (error) { if (error) console.error(error); }; | 22 | callback = callback || function (error) { if (error) console.error(error); }; |
6 | 23 | ||
@@ -114,14 +131,12 @@ function up() { | |||
114 | function uploadFiles(files) { | 131 | function uploadFiles(files) { |
115 | if (!files || !files.length) return; | 132 | if (!files || !files.length) return; |
116 | 133 | ||
117 | app.uploadStatus = { | 134 | app.uploadStatus.busy = true; |
118 | busy: true, | 135 | app.uploadStatus.count = files.length; |
119 | count: files.length, | 136 | app.uploadStatus.done = 0; |
120 | done: 0, | 137 | app.uploadStatus.percentDone = 0; |
121 | percentDone: 0 | ||
122 | }; | ||
123 | 138 | ||
124 | function uploadFile(file) { | 139 | asyncForEach(files, function (file, callback) { |
125 | var path = encode(sanitize(app.path + '/' + file.name)); | 140 | var path = encode(sanitize(app.path + '/' + file.name)); |
126 | 141 | ||
127 | var formData = new FormData(); | 142 | var formData = new FormData(); |
@@ -129,28 +144,24 @@ function uploadFiles(files) { | |||
129 | 144 | ||
130 | superagent.post('/api/files' + path).query({ access_token: localStorage.accessToken }).send(formData).end(function (error, result) { | 145 | superagent.post('/api/files' + path).query({ access_token: localStorage.accessToken }).send(formData).end(function (error, result) { |
131 | if (result && result.statusCode === 401) return logout(); | 146 | if (result && result.statusCode === 401) return logout(); |
132 | if (result && result.statusCode !== 201) console.error('Error uploading file: ', result.statusCode); | 147 | if (result && result.statusCode !== 201) return callback('Error uploading file: ', result.statusCode); |
133 | if (error) console.error(error); | 148 | if (error) return callback(error); |
134 | 149 | ||
135 | app.uploadStatus.done += 1; | 150 | app.uploadStatus.done += 1; |
136 | app.uploadStatus.percentDone = Math.round(app.uploadStatus.done / app.uploadStatus.count * 100); | 151 | app.uploadStatus.percentDone = Math.round(app.uploadStatus.done / app.uploadStatus.count * 100); |
137 | 152 | ||
138 | if (app.uploadStatus.done >= app.uploadStatus.count) { | 153 | callback(); |
139 | app.uploadStatus = { | ||
140 | busy: false, | ||
141 | count: 0, | ||
142 | done: 0, | ||
143 | percentDone: 100 | ||
144 | }; | ||
145 | |||
146 | refresh(); | ||
147 | } | ||
148 | }); | 154 | }); |
149 | } | 155 | }, function (error) { |
156 | if (error) console.error(error); | ||
150 | 157 | ||
151 | for(var i = 0; i < app.uploadStatus.count; ++i) { | 158 | app.uploadStatus.busy = false; |
152 | uploadFile(files[i]); | 159 | app.uploadStatus.count = 0; |
153 | } | 160 | app.uploadStatus.done = 0; |
161 | app.uploadStatus.percentDone = 100; | ||
162 | |||
163 | refresh(); | ||
164 | }); | ||
154 | } | 165 | } |
155 | 166 | ||
156 | function dragOver(event) { | 167 | function dragOver(event) { |