job template model
[qcg-portal.git] / filex / static / filex / filex.js
index 29589c3..ac71ea5 100644 (file)
@@ -107,7 +107,7 @@ $(function(){
         },
 
         full: function() {
-            return this.pluck('path').join('/') || '/';
+            return this.pluck('name').join('/').replace(/^\/+/, '/');
         }
     });
 
@@ -126,7 +126,7 @@ $(function(){
         initialize: function(options) {
             this.view = options.view;
 
-            this.listenTo(this.model, 'change', this.render);
+            this.listenTo(this.model, 'change:checked', this.toggleChecked);
             this.listenTo(this.model, 'remove', this.remove);
             this.listenTo(this.model, 'hidden', this.toggleHidden);
         },
@@ -141,8 +141,7 @@ $(function(){
             var data = this.model.toJSON();
             data['url_params'] = $.param({
                 host: this.view.host,
-                path: this.view.path.full(),
-                name: this.model.get('name')
+                path: this.view.path.full() + '/' + this.model.get('name')
             });
             data['cid'] = this.model.cid;
 
@@ -153,7 +152,16 @@ $(function(){
         },
 
         toggleHidden: function() {
-            this.$el.toggleClass('hidden', this.model.isHidden() && !this.view.showHidden());
+            var isHidden = this.model.isHidden() && !this.view.showHidden();
+            this.$el.toggleClass('hidden', isHidden);
+
+            if (isHidden && this.model.get('checked'))
+                this.model.toggle();
+        },
+
+        toggleChecked: function(obj, value) {
+            this.$el.toggleClass('active', value);
+            this.$el.find('input[type="checkbox"]').prop('checked', value);
         },
 
         click: function(e) {
@@ -184,12 +192,12 @@ $(function(){
 
         render: function() {
             if (this.model.get('active')) {
-                this.$el.text(this.model.get('text'));
+                this.$el.text(this.model.get('name'));
             }
             else {
                 this.$el.html($('<a/>', {
                     href: '#',
-                    text: this.model.get('text')
+                    text: this.model.get('name')
                 }));
             }
             this.$el.toggleClass('active', this.model.get('active'));
@@ -253,20 +261,24 @@ $(function(){
                 lockOptgroupOrder: true,
                 create: function(input, callback) {
                     var $form = $('#favorite-form'),
-                        parts = input.split('/', 1),
+                        parts = input.split('/'),
                         callback_called = false;
 
-                    $form.find('#id_host').val(parts[0]);
+                    $form[0].reset();
+                    $form.find('.alert-danger').remove();
+                    $form.find('#id_host').val(parts.shift());
 
-                    if (parts.length > 1)
-                        $form.find('#id_path').val(parts[1]);
+                    if (parts.length)
+                        $form.find('#id_path').val(parts.join('/'));
 
+                    $form.off();
                     $form.on('submit', function(e) {
                         var $this = $(this),
                             $btn = $this.find('[type="submit"]');
 
                         e.preventDefault();
                         $btn.button('loading');
+                        $this.find('.alert-danger').remove();
 
                         $.post($this.attr('action'), $this.serialize(), function(data) {
                             callback(data);
@@ -274,9 +286,27 @@ $(function(){
 
                             $this.modal('hide');
                             $btn.button('reset');
-                        }, 'json').fail(function() {
+                        }, 'json').fail(function(xhr) {
                             console.error(arguments);
-                            $btn.button('error');
+                            var error = (xhr.responseJSON || {}).error || undefined;
+
+                            if (error.__all__) {
+                                error = 'Podana lokalizacja jest już zapisana'
+                            }
+                            else if (xhr.status == 400) {
+                                error = 'Niepoprawna nazwa hosta lub ścieżka'
+                            }
+
+                            if (typeof error === 'string') {
+                                $('<div>', {
+                                    'class': 'alert alert-danger',
+                                    html: '<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span> ' + error
+                                }).prependTo($this.find('.modal-body'));
+                                $btn.button('reset');
+                            }
+                            else {
+                                $btn.button('error');
+                            }
                         });
                     });
 
@@ -296,17 +326,11 @@ $(function(){
                         return '<div class="create">Dodaj <em>' + escape(data.input) + '</em>&hellip;</div>';
                     }
                 },
-                onItemRemove: function(value) {
-                    this.oldValue = value;
-                },
                 onItemAdd: function(value) {
                     view.load(value);
                     this.blur();
                 },
                 onBlur: function() {
-                    if (!this.getValue() && this.oldValue)
-                        this.addItem(this.oldValue);
-
                     $('#host').removeClass('edit');
                     this.clear();
                 }
@@ -324,13 +348,15 @@ $(function(){
         },
 
         load: function(location) {
-            var path = location.replace(/(^\/+|\/+$)/g, '').split('/'),
-                host = path.shift(),
-                pathBits = [new Filex.PathBit({'text': '/', 'path': ''})].concat(_.map(path, function(name) {
-                    return new Filex.PathBit({'text': name, 'path': name});
-                }));
-
-            this.host = host;
+            var hostRootPath = location.split(/\/(\/|~)(.*)/),
+                pathBits = [new Filex.PathBit({'name': hostRootPath[1]})].concat(
+                    _.chain(hostRootPath[2].split('/'))
+                        .filter(_.identity)
+                        .map(function(name) { return new Filex.PathBit({'name': name}) })
+                        .value()
+                );
+
+            this.host = hostRootPath[0];
             this.path.reset(pathBits);
 
             this.$host.text(this.host);
@@ -351,7 +377,7 @@ $(function(){
                 error: function(collection, response) {
                     view.files.reset();
 
-                    var msg = (response.responseJSON || {}).msg || 'Błąd serwera';
+                    var msg = (response.responseJSON || {}).error || 'Błąd serwera';
 
                     view.$noItems.hide();
                     view.$error.find('.msg').text(msg);
@@ -391,7 +417,7 @@ $(function(){
         },
 
         selectedDir: function(dir) {
-            this.path.add({'text': dir.get('name'), 'path': dir.get('name')});
+            this.path.add({'name': dir.get('name')});
         },
 
         selectedPath: function(bit) {
@@ -457,20 +483,13 @@ $(function(){
 
             $btn.button('loading');
 
-            $.post(url, data, 'json').done(function () {
+            $.post(url, data, 'json').done(function (response) {
                 $btn.button('reset');
 
-                if (is_active) {
-                    locations.removeOption(data.host + data.path);
-                }
-                else {
-                    locations.addOption({
-                        group: 'usr',
-                        host: data.host,
-                        path: data.path,
-                        value: data.host + data.path
-                    });
-                }
+                if (is_active)
+                    locations.removeOption(data.host + '/' + data.path);
+                else
+                    locations.addOption(response);
             }).fail(function() {
                 $btn.button('reset');
                 $btn.button('toggle');
@@ -493,7 +512,7 @@ $(function(){
         },
 
         updateFavorites: function() {
-            var loc = this.host + this.path.full(),
+            var loc = this.host + '/' + this.path.full(),
                 favorites = this.hostSelectize.options;
 
             if (favorites.hasOwnProperty(loc)) {