fix error message
[qcg-portal.git] / qcg / templates / qcg / gridftp.html
index c457396..e75e621 100644 (file)
@@ -3,6 +3,22 @@
 
 {% block extra_css %}
     <link href="{% static 'qcg/selectize/selectize.bootstrap3.css' %}" rel="stylesheet">
+
+    <style>
+        html {
+            height: 100%;
+        }
+
+        body {
+            height: 100%;
+            margin-bottom: 0;
+        }
+
+        body > .container {
+            height: calc(100% - 122px);
+            overflow-y: hidden;
+        }
+    </style>
 {% endblock %}
 
 {% block extra_js %}
     {% include 'filex/source.js.html' %}
 
     <script>
+        var filex = filex || {},
+            hostArchiveSupport = ['moss.man.poznan.pl', 'ui.plgrid.wcss.wroc.pl'];
+
         $(function () {
+            'use strict';
+
+            filex.initialLoad();
+
             var statusTimeout;
 
+            String.prototype.endsWith = function(suffix) {
+                return this.indexOf(suffix, this.length - suffix.length) !== -1;
+            };
+
             function status(msg) {
                 clearTimeout(statusTimeout);
                 statusTimeout = setTimeout(function() {
                 $('#status').text(msg);
             }
 
+            function failModal(msg) {
+                return function(xhr) {
+                    var $errorModal = $('#error-modal');
+
+                    $errorModal.find('#error-modal-label').text('Błąd serwera');
+                    $errorModal.find('.modal-body').html($('<h4>', {text: msg}));
+
+                    var error = (xhr.responseJSON || {}).error || undefined;
+
+                    if (typeof error === 'string')
+                        $errorModal.find('.modal-body').append($('<pre>', {text: error}));
+
+                    filex.idle();
+                    $errorModal.modal();
+
+                    console.error(arguments);
+                };
+            }
+
+            function conflictingName(name, modal) {
+                if (filex.files.some(function(item) { return item.get('name') == name })) {
+                    $('<div>', {
+                        'class': 'alert alert-danger',
+                        html: '<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span> Plik o podanej nazwie już istnieje!'
+                    }).prependTo(modal.find('.modal-body'));
+
+                    return true;
+                }
+
+                return false;
+            }
+
             filex.files.on('change:checked reset', function() {
-                var selected = filex.selectedFiles().length;
+                var selected = filex.selectedFiles().length,
+                    supportsArchive = hostArchiveSupport.indexOf(filex.host) != -1;
 
                 $('#btn-rename').toggleClass('disabled', selected != 1);
                 $('#btn-delete').toggleClass('disabled', selected == 0);
+                $('#btn-compress').toggleClass('disabled', !supportsArchive || selected == 0);
+
+                if (supportsArchive && selected == 1) {
+                    var filename = filex.selectedFiles()[0].get('name'),
+                        is_archive = _.some(['.zip', '.tar.gz', '.tgz', '.tar.bz2', 'tbz'], function(ext) {
+                            return filename.endsWith(ext);
+                        });
+
+                    $('#btn-extract').toggleClass('disabled', !is_archive);
+                }
+                else {
+                    $('#btn-extract').addClass('disabled');
+                }
+            });
+
+            $('form').on('show.bs.modal', function() {
+                $(this).find('.alert').remove();
+                this.reset();
+            }).on('shown.bs.modal', function() {
+                $(this).find('input[type="text"]')[0].focus();
             });
 
             $('#btn-upload').on('click', function() {
-                var url = '{% url 'gridftp_upload' %}?' + $.param({host: filex.host, path: filex.path.full() + '/'});
+                var url = '{% url 'gridftp_upload' %}?' + $.param({host: filex.host, path: filex.path.full()});
 
                 var win = window.open(url, url, 'height=500,width=800');
                 win.focus();
 
             $('#btn-delete').on('click', function() {
                 var selected = _.groupBy(filex.selectedFiles(), function(item) { return item.get('type') }),
-                    dirs = _.map(selected.directory || [], function (item) { return item.get('name') }),
-                    files = _.map(selected.file || [], function (item) { return item.get('name') }),
+                    path = filex.path.full() + '/',
+                    dirs = _.map(selected.directory || [], function (item) { return path + item.get('name') }),
+                    files = _.map(selected.file || [], function (item) { return path + item.get('name') }),
                     data = {
                         host: filex.host,
-                        path: filex.path.full() + '/',
                         dirs: dirs,
                         files: files
                     },
 
                         if (keys.length) {
                             var $errorModal = $('#error-modal'),
-                                $errorList = $errorModal.find('dl').html('');
+                                $errorBody = $errorModal.find('.modal-body')
+                                                        .html($('<h4>', {text: 'Wystąpiły problemy podczas usuwania:'})),
+                                $errorList = $('<dl>', {'class': 'dl-horizontal'}).appendTo($errorBody);
+
+                            $errorModal.find('#error-modal-label').text('Błąd');
 
                             for (var i in keys) {
-                                $('<dt>', {text: keys[i]}).appendTo($errorList);
-                                $('<dd>', {text: response.fail[keys[i]]}).appendTo($errorList);
+                                if(keys.hasOwnProperty(i)) {
+                                    $('<dt>', {text: keys[i].replace(path, '')}).appendTo($errorList);
+                                    $('<dd>', {text: response.fail[keys[i]]}).appendTo($errorList);
+                                }
                             }
 
                             $errorModal.modal();
                         if (response.done.length)
                             filex.reloadFiles();
 
-                    }, 'json');
+                    }, 'json').fail(failModal('Nie udało się usunąć plików'));
 
                     $confirmModal.modal('hide');
                 });
                 var $this = $(this);
 
                 e.preventDefault();
+
+                if (conflictingName($this.find('#id_name').val(), $this))
+                    return;
+
                 filex.busy();
                 $this.modal('hide');
 
                 $this.find('#id_host').val(filex.host);
                 $this.find('#id_path').val(filex.path.full());
 
-                $.post($this.attr('action'), $this.serialize(), function(response) {
+                $.post($this.attr('action'), $this.serialize(), function() {
                     status('Katalog utworzono pomyślnie');
                     filex.reloadFiles();
-                }, 'json').fail(function(xhr) {
-                    status('Wystąpił błąd: ' + xhr.responseJSON.msg);
-                    filex.idle();
-                });
+                }, 'json').fail(failModal('Nie udało się utworzyć katalogu'));
             });
 
             $('#rename-form').on('show.bs.modal', function() {
-                var $this = $(this);
+                $(this).find('#id_dst').val(filex.selectedFiles()[0].get('name'));
+            }).on('submit', function(e) {
+                e.preventDefault();
 
-                var file = filex.selectedFiles()[0];
+                var $this = $(this),
+                    path = filex.path.full() + '/',
+                    newName = $this.find('#id_dst').val(),
+                    data = {
+                        host: filex.host,
+                        src: path + filex.selectedFiles()[0].get('name'),
+                        dst: path + newName
+                    };
 
-                $this.find('#id_host').val(filex.host);
-                $this.find('#id_path').val(filex.path.full());
-                $this.find('#id_src').val(file.get('name'));
-                $this.find('#id_dst').val(file.get('name'));
-            }).on('submit', function(e) {
-                var $this = $(this);
+                if (conflictingName(newName, $this))
+                    return;
 
-                e.preventDefault();
                 filex.busy();
                 $this.modal('hide');
 
-                $.post($this.attr('action'), $this.serialize(), function(response) {
+                $.post($this.attr('action'), data, function() {
                     status('Nazwę zmieniono pomyślnie');
                     filex.reloadFiles();
-                }, 'json').fail(function(xhr) {
-                    status('Wystąpił błąd: ' + xhr.responseJSON.msg);
-                    filex.idle();
-                });
+                }, 'json').fail(failModal('Nie udało się zmienić nazwy'));
+            });
+
+            $('#compress-form').on('submit', function(e) {
+                e.preventDefault();
+
+                var $this = $(this),
+                    name = $this.find('#id_archive').val(),
+                    type = $this.find('#id_type').val(),
+                    path = filex.path.full(),
+                    archive = name + (name.endsWith(type) ? '' : type),
+                    data = {
+                        host: filex.host,
+                        path: path,
+                        files: _.map(filex.selectedFiles(), function (item) { return item.get('name') }),
+                        archive: path  + '/' + archive
+                    };
+
+                if (conflictingName(archive, $this))
+                    return;
+
+                filex.busy();
+                $this.modal('hide');
+
+                $.post($this.attr('action'), data, function() {
+                    status('Archiwum utworzono pomyślnie');
+                    filex.reloadFiles();
+                }, 'json').fail(failModal('Nie udało się utworzyć archiwum'));
+            });
+
+            $('#btn-extract').on('click', function() {
+                filex.busy();
+
+                var data = {
+                    host: filex.host,
+                    path: filex.path.full() + '/' + filex.selectedFiles()[0].get('name'),
+                    dst: filex.path.full()
+                };
+
+                $.post('{% url 'filex:extract' %}', data, function() {
+                    status('Archiwum rozpakowano pomyślnie');
+                    filex.reloadFiles();
+                }, 'json').fail(failModal('Nie udało się rozpakować archiwum'));
             });
         })
     </script>
                     <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                         <span aria-hidden="true">&times;</span>
                     </button>
-                    <h4 class="modal-title" id="error-modal-label">Błąd</h4>
+                    <h4 class="modal-title" id="error-modal-label"></h4>
                 </div>
                 <div class="modal-body">
-                    <h4>Wystąpiły problemy podczas usuwania:</h4>
-                    <dl class="dl-horizontal"></dl>
                 </div>
                 <div class="modal-footer">
                     <button type="button" class="btn btn-primary" data-dismiss="modal">OK</button>
             </div>
         </div>
     </form>
+
+    <form id="compress-form" action="{% url 'filex:compress' %}" class="modal fade form-horizontal" tabindex="-1" role="dialog" aria-labelledby="compress-modal-label" aria-hidden="true">
+        <div class="modal-dialog">
+            <div class="modal-content">
+                <div class="modal-header">
+                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
+                        <span aria-hidden="true">&times;</span>
+                    </button>
+                    <h4 class="modal-title" id="compress-modal-label">Podaj nazwę archiwum</h4>
+                </div>
+                <div class="modal-body">
+                    {% csrf_token %}
+                    {% bootstrap_form archive_form layout='horizontal' %}
+                </div>
+                <div class="modal-footer">
+                    <button type="button" class="btn btn-default" data-dismiss="modal">Anuluj</button>
+                    <button type="submit" class="btn btn-primary">OK</button>
+                </div>
+            </div>
+        </div>
+    </form>
 {% endblock container %}
 
 {% block footer %}
-    <footer class="navbar navbar-default navbar-fixed-bottom" style="position:fixed">
+    <footer class="navbar navbar-default navbar-fixed-bottom">
         <div class="container">
             <div class="btn-toolbar" role="toolbar" style="float: left">
                 <div class="btn-group" role="group">
                     <button class="btn btn-default navbar-btn" data-toggle="modal" data-target="#mkdir-form">Utwórz katalog</button>
                 </div>
                 <div class="btn-group" role="group">
-                    <button id="btn-rename" class="btn btn-default navbar-btn disabled"  data-toggle="modal" data-target="#rename-form">Zmień nazwę</button>
+                    <button id="btn-rename" class="btn btn-default navbar-btn disabled" data-toggle="modal" data-target="#rename-form">Zmień nazwę</button>
                     <button id="btn-delete" class="btn btn-default navbar-btn disabled">Usuń</button>
                 </div>
+                <div class="btn-group" role="group">
+                    <button id="btn-compress" class="btn btn-default navbar-btn disabled" data-toggle="modal" data-target="#compress-form">Spakuj</button>
+                    <button id="btn-extract" class="btn btn-default navbar-btn disabled">Rozpakuj</button>
+                </div>
             </div>
             <p id="status" class="navbar-text"></p>
         </div>