]> git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blob - app/js/app.js
Bump version
[perso/Immae/Projets/Nodejs/Surfer.git] / app / js / app.js
1 (function () {
2 'use strict';
3
4 function login(username, password) {
5 username = username || app.loginData.username;
6 password = password || app.loginData.password;
7
8 app.busy = true;
9
10 superagent.get('/api/files/').query({ username: username, password: password }).end(function (error, result) {
11 app.busy = false;
12
13 if (error) return console.error(error);
14 if (result.statusCode === 401) return console.error('Invalid credentials');
15
16 app.session.valid = true;
17 app.session.username = username;
18 app.session.password = password;
19
20 // clearly not the best option
21 localStorage.username = username;
22 localStorage.password = password;
23
24 loadDirectory(window.location.hash.slice(1));
25 });
26 }
27
28 function logout() {
29 app.session.valid = false;
30 app.session.username = null;
31 app.session.password = null;
32
33 delete localStorage.username;
34 delete localStorage.password;
35 }
36
37 function sanitize(filePath) {
38 filePath = '/' + filePath;
39 return filePath.replace(/\/+/g, '/');
40 }
41
42 function encode(filePath) {
43 return filePath.split('/').map(encodeURIComponent).join('/');
44 }
45
46 function decode(filePath) {
47 return filePath.split('/').map(decodeURIComponent).join('/');
48 }
49
50 var mimeTypes = {
51 images: [ '.png', '.jpg', '.jpeg', '.tiff', '.gif' ],
52 text: [ '.txt', '.md' ],
53 pdf: [ '.pdf' ],
54 html: [ '.html', '.htm', '.php' ],
55 video: [ '.mp4', '.mpg', '.mpeg', '.ogg', '.mkv' ]
56 };
57
58 function getPreviewUrl(entry, basePath) {
59 var path = '/_admin/img/';
60
61 if (entry.isDirectory) return path + 'directory.png';
62 if (mimeTypes.images.some(function (e) { return entry.filePath.endsWith(e); })) return sanitize(basePath + '/' + entry.filePath);
63 if (mimeTypes.text.some(function (e) { return entry.filePath.endsWith(e); })) return path +'text.png';
64 if (mimeTypes.pdf.some(function (e) { return entry.filePath.endsWith(e); })) return path + 'pdf.png';
65 if (mimeTypes.html.some(function (e) { return entry.filePath.endsWith(e); })) return path + 'html.png';
66 if (mimeTypes.video.some(function (e) { return entry.filePath.endsWith(e); })) return path + 'video.png';
67
68 return path + 'unknown.png';
69 }
70
71 function refresh() {
72 loadDirectory(app.path);
73 }
74
75 function loadDirectory(filePath) {
76 app.busy = true;
77
78 filePath = filePath ? sanitize(filePath) : '/';
79
80 superagent.get('/api/files/' + encode(filePath)).query({ username: app.session.username, password: app.session.password }).end(function (error, result) {
81 app.busy = false;
82
83 if (result && result.statusCode === 401) return logout();
84 if (error) return console.error(error);
85
86 result.body.entries.sort(function (a, b) { return a.isDirectory && b.isFile ? -1 : 1; });
87 app.entries = result.body.entries.map(function (entry) {
88 entry.previewUrl = getPreviewUrl(entry, filePath);
89 return entry;
90 });
91 app.path = filePath;
92 app.pathParts = decode(filePath).split('/').filter(function (e) { return !!e; }).map(function (e, i, a) {
93 return {
94 name: e,
95 link: '#' + sanitize('/' + a.slice(0, i).join('/') + '/' + e)
96 };
97 });
98
99 // update in case this was triggered from code
100 window.location.hash = app.path;
101
102 Vue.nextTick(function () {
103 $(function () {
104 $('[data-toggle="tooltip"]').tooltip();
105 });
106 });
107 });
108 }
109
110 function open(entry) {
111 var path = sanitize(app.path + '/' + entry.filePath);
112
113 if (entry.isDirectory) {
114 window.location.hash = path;
115 return;
116 }
117
118 window.open(encode(path));
119 }
120
121 function up() {
122 window.location.hash = sanitize(app.path.split('/').slice(0, -1).filter(function (p) { return !!p; }).join('/'));
123 }
124
125 function upload() {
126 $(app.$els.upload).on('change', function () {
127 app.busy = true;
128
129 // detach event handler
130 $(app.$els.upload).off('change');
131
132 var file = app.$els.upload.files[0];
133 var path = encode(sanitize(app.path + '/' + file.name));
134
135 var formData = new FormData();
136 formData.append('file', file);
137
138 superagent.put('/api/files' + path).query({ username: app.session.username, password: app.session.password }).send(formData).end(function (error, result) {
139 app.busy = false;
140
141 if (result && result.statusCode === 401) return logout();
142 if (result && result.statusCode !== 201) return console.error('Error uploading file: ', result.statusCode);
143 if (error) return console.error(error);
144
145 refresh();
146 });
147 });
148
149 // reset the form first to make the change handler retrigger even on the same file selected
150 $('#fileUploadForm')[0].reset();
151
152 app.$els.upload.click();
153 }
154
155 function delAsk(entry) {
156 $('#modalDelete').modal('show');
157 app.deleteData = entry;
158 }
159
160 function del(entry) {
161 app.busy = true;
162
163 var path = encode(sanitize(app.path + '/' + entry.filePath));
164
165 superagent.del('/api/files' + path).query({ username: app.session.username, password: app.session.password, recursive: true }).end(function (error, result) {
166 app.busy = false;
167
168 if (result && result.statusCode === 401) return logout();
169 if (result && result.statusCode !== 200) return console.error('Error deleting file: ', result.statusCode);
170 if (error) return console.error(error);
171
172 refresh();
173
174 $('#modalDelete').modal('hide');
175 });
176 }
177
178 function createDirectoryAsk() {
179 $('#modalcreateDirectory').modal('show');
180 app.createDirectoryData = '';
181 app.createDirectoryError = null;
182 }
183
184 function createDirectory(name) {
185 app.busy = true;
186 app.createDirectoryError = null;
187
188 var path = encode(sanitize(app.path + '/' + name));
189
190 superagent.put('/api/files' + path).query({ username: app.session.username, password: app.session.password, directory: true }).end(function (error, result) {
191 app.busy = false;
192
193 if (result && result.statusCode === 401) return logout();
194 if (result && result.statusCode === 403) {
195 app.createDirectoryError = 'Name not allowed';
196 return;
197 }
198 if (result && result.statusCode === 409) {
199 app.createDirectoryError = 'Directory already exists';
200 return;
201 }
202 if (result && result.statusCode !== 201) return console.error('Error creating directory: ', result.statusCode);
203 if (error) return console.error(error);
204
205 app.createDirectoryData = '';
206 refresh();
207
208 $('#modalcreateDirectory').modal('hide');
209 });
210 }
211
212 Vue.filter('prettyDate', function (value) {
213 var d = new Date(value);
214 return d.toDateString();
215 });
216
217 Vue.filter('prettyFileSize', function (value) {
218 return filesize(value);
219 });
220
221 var app = new Vue({
222 el: '#app',
223 data: {
224 busy: true,
225 path: '/',
226 pathParts: [],
227 session: {
228 valid: false
229 },
230 loginData: {},
231 deleteData: {},
232 createDirectoryData: '',
233 createDirectoryError: null,
234 entries: []
235 },
236 methods: {
237 login: login,
238 logout: logout,
239 loadDirectory: loadDirectory,
240 open: open,
241 up: up,
242 upload: upload,
243 delAsk: delAsk,
244 del: del,
245 createDirectoryAsk: createDirectoryAsk,
246 createDirectory: createDirectory
247 }
248 });
249
250 window.app = app;
251
252 login(localStorage.username, localStorage.password);
253
254 $(window).on('hashchange', function () {
255 loadDirectory(window.location.hash.slice(1));
256 });
257
258 // setup all the dialog focus handling
259 ['modalcreateDirectory'].forEach(function (id) {
260 $('#' + id).on('shown.bs.modal', function () {
261 $(this).find("[autofocus]:first").focus();
262 });
263 });
264
265 })();