]> git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blobdiff - app/js/app.js
Add a shrinkwrap file
[perso/Immae/Projets/Nodejs/Surfer.git] / app / js / app.js
index 228184d313d3588ba1baa8f996e3f5b578670abc..9a2e532c5533e8eb5f9b40602923b3d8c37d9843 100644 (file)
@@ -77,13 +77,11 @@ function loadDirectory(filePath) {
 
     filePath = filePath ? sanitize(filePath) : '/';
 
-    console.log(filePath);
-
-    superagent.get('/api/files/' + filePath).query({ username: app.session.username, password: app.session.password }).end(function (error, result) {
+    superagent.get('/api/files/' + encode(filePath)).query({ username: app.session.username, password: app.session.password }).end(function (error, result) {
         app.busy = false;
 
+        if (result && result.statusCode === 401) return logout();
         if (error) return console.error(error);
-        if (result.statusCode === 401) return logout();
 
         result.body.entries.sort(function (a, b) { return a.isDirectory && b.isFile ? -1 : 1; });
         app.entries = result.body.entries.map(function (entry) {
@@ -91,7 +89,12 @@ function loadDirectory(filePath) {
             return entry;
         });
         app.path = filePath;
-        app.pathParts = decode(filePath).split('/').filter(function (e) { return !!e; });
+        app.pathParts = decode(filePath).split('/').filter(function (e) { return !!e; }).map(function (e, i, a) {
+            return {
+                name: e,
+                link: '#' + sanitize('/' + a.slice(0, i).join('/') + '/' + e)
+            };
+        });
 
         // update in case this was triggered from code
         window.location.hash = app.path;
@@ -105,18 +108,18 @@ function loadDirectory(filePath) {
 }
 
 function open(entry) {
-    var path = encode(sanitize(app.path + '/' + entry.filePath));
+    var path = sanitize(app.path + '/' + entry.filePath);
 
     if (entry.isDirectory) {
         window.location.hash = path;
         return;
     }
 
-    window.open(path);
+    window.open(encode(path));
 }
 
 function up() {
-    window.location.hash = encode(sanitize(app.path.split('/').slice(0, -1).filter(function (p) { return !!p; }).join('/')));
+    window.location.hash = sanitize(app.path.split('/').slice(0, -1).filter(function (p) { return !!p; }).join('/'));
 }
 
 function upload() {
@@ -126,22 +129,37 @@ function upload() {
         // detach event handler
         $(app.$els.upload).off('change');
 
-        var file = app.$els.upload.files[0];
-        var path = encode(sanitize(app.path + '/' + file.name));
+        var length = app.$els.upload.files.length;
+        var done = 0;
 
-        var formData = new FormData();
-        formData.append('file', file);
+        function uploadFile(file) {
+            var path = encode(sanitize(app.path + '/' + file.name));
 
-        superagent.put('/api/files' + path).query({ username: app.session.username, password: app.session.password }).send(formData).end(function (error, result) {
-            app.busy = false;
+            var formData = new FormData();
+            formData.append('file', file);
 
-            if (error) return console.error(error);
-            if (result.statusCode !== 201) return console.error('Error uploading file: ', result.statusCode);
+            superagent.put('/api/files' + path).query({ username: app.session.username, password: app.session.password }).send(formData).end(function (error, result) {
+                if (result && result.statusCode === 401) return logout();
+                if (result && result.statusCode !== 201) console.error('Error uploading file: ', result.statusCode);
+                if (error) console.error(error);
 
-            refresh();
-        });
+                ++done;
+
+                if (done >= length) {
+                    app.busy = false;
+                    refresh();
+                }
+            });
+        }
+
+        for(var i = 0; i < length; i++) {
+            uploadFile(app.$els.upload.files[i]);
+        }
     });
 
+    // reset the form first to make the change handler retrigger even on the same file selected
+    $('#fileUploadForm')[0].reset();
+
     app.$els.upload.click();
 }
 
@@ -158,8 +176,9 @@ function del(entry) {
     superagent.del('/api/files' + path).query({ username: app.session.username, password: app.session.password, recursive: true }).end(function (error, result) {
         app.busy = false;
 
+        if (result && result.statusCode === 401) return logout();
+        if (result && result.statusCode !== 200) return console.error('Error deleting file: ', result.statusCode);
         if (error) return console.error(error);
-        if (result.statusCode !== 200) return console.error('Error deleting file: ', result.statusCode);
 
         refresh();
 
@@ -170,18 +189,29 @@ function del(entry) {
 function createDirectoryAsk() {
     $('#modalcreateDirectory').modal('show');
     app.createDirectoryData = '';
+    app.createDirectoryError = null;
 }
 
 function createDirectory(name) {
     app.busy = true;
+    app.createDirectoryError = null;
 
     var path = encode(sanitize(app.path + '/' + name));
 
     superagent.put('/api/files' + path).query({ username: app.session.username, password: app.session.password, directory: true }).end(function (error, result) {
         app.busy = false;
 
+        if (result && result.statusCode === 401) return logout();
+        if (result && result.statusCode === 403) {
+            app.createDirectoryError = 'Name not allowed';
+            return;
+        }
+        if (result && result.statusCode === 409) {
+            app.createDirectoryError = 'Directory already exists';
+            return;
+        }
+        if (result && result.statusCode !== 201) return console.error('Error creating directory: ', result.statusCode);
         if (error) return console.error(error);
-        if (result.statusCode !== 201) return console.error('Error creating directory: ', result.statusCode);
 
         app.createDirectoryData = '';
         refresh();
@@ -211,6 +241,7 @@ var app = new Vue({
         loginData: {},
         deleteData: {},
         createDirectoryData: '',
+        createDirectoryError: null,
         entries: []
     },
     methods: {
@@ -227,10 +258,19 @@ var app = new Vue({
     }
 });
 
+window.app = app;
+
 login(localStorage.username, localStorage.password);
 
 $(window).on('hashchange', function () {
     loadDirectory(window.location.hash.slice(1));
 });
 
+// setup all the dialog focus handling
+['modalcreateDirectory'].forEach(function (id) {
+    $('#' + id).on('shown.bs.modal', function () {
+        $(this).find("[autofocus]:first").focus();
+    });
+});
+
 })();