]> git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blame - app/js/app.js
Make directory listing navigatable
[perso/Immae/Projets/Nodejs/Surfer.git] / app / js / app.js
CommitLineData
6eb72d64
JZ
1(function () {
2'use strict';
3
4function 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;
d3312ed1
JZ
23
24 loadDirectory(app.path);
6eb72d64
JZ
25 });
26}
27
28function logout() {
29 app.session.valid = false;
30 app.session.username = username;
31 app.session.password = password;
32
33 delete localStorage.username;
34 delete localStorage.password;
35}
36
d3312ed1
JZ
37function sanitize(filePath) {
38 filePath = '/' + filePath;
39 return filePath.replace(/\/+/g, '/');
40}
41
42function loadDirectory(filePath) {
43 app.busy = true;
44
45 filePath = filePath ? sanitize(filePath) : '/';
46
47 console.log(filePath);
48
49 superagent.get('/api/files/' + filePath).query({ username: app.session.username, password: app.session.password }).end(function (error, result) {
50 app.busy = false;
51
52 if (error) return console.error(error);
53 if (result.statusCode === 401) return logout();
54
55 app.entries = result.body.entries;
56 app.path = filePath;
57 app.pathParts = filePath.split('/').filter(function (e) { return !!e; });
58 console.log(app.pathParts)
59 });
60}
61
62function open(entry) {
63 var path = sanitize(app.path + '/' + entry.filePath);
64
65 if (entry.isDirectory) return loadDirectory(path);
66
67 window.location.href = window.location.origin + path;
68}
69
70function up() {
71 loadDirectory(app.path.split('/').slice(0, -1).filter(function (p) { return !!p; }).join('/'));
72}
73
6eb72d64
JZ
74var app = new Vue({
75 el: '#app',
76 data: {
77 busy: true,
d3312ed1
JZ
78 path: '/',
79 pathParts: [],
6eb72d64
JZ
80 session: {
81 valid: false
82 },
d3312ed1
JZ
83 loginData: {},
84 entries: []
6eb72d64
JZ
85 },
86 methods: {
87 login: login,
d3312ed1
JZ
88 logout: logout,
89 loadDirectory: loadDirectory,
90 open: open,
91 up: up
6eb72d64
JZ
92 }
93});
94
95window.app = app;
96
97login(localStorage.username, localStorage.password);
98
99})();