]> git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blame - app/js/app.js
Add upload button
[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; });
d3312ed1
JZ
58 });
59}
60
61function open(entry) {
62 var path = sanitize(app.path + '/' + entry.filePath);
63
64 if (entry.isDirectory) return loadDirectory(path);
65
ee9d1ada 66 window.open(path);
d3312ed1
JZ
67}
68
69function up() {
70 loadDirectory(app.path.split('/').slice(0, -1).filter(function (p) { return !!p; }).join('/'));
71}
72
6eb72d64
JZ
73var app = new Vue({
74 el: '#app',
75 data: {
76 busy: true,
d3312ed1
JZ
77 path: '/',
78 pathParts: [],
6eb72d64
JZ
79 session: {
80 valid: false
81 },
d3312ed1
JZ
82 loginData: {},
83 entries: []
6eb72d64
JZ
84 },
85 methods: {
86 login: login,
d3312ed1
JZ
87 logout: logout,
88 loadDirectory: loadDirectory,
89 open: open,
90 up: up
6eb72d64
JZ
91 }
92});
93
94window.app = app;
95
96login(localStorage.username, localStorage.password);
97
98})();