redo gridftp navbar
[qcg-portal.git] / filex / static / filex / filex.js
index e8039ac..10dc6bc 100644 (file)
@@ -15,6 +15,10 @@ $(function(){
     });
 
     Filex.File = OfflineModel.extend({
+        defaults: {
+            checked: false
+        },
+
         isDir: function() {
             return false;
         },
@@ -25,6 +29,10 @@ $(function(){
 
         isHidden: function() {
             return this.get('name')[0] == '.';
+        },
+
+        toggle: function() {
+            this.set('checked', !this.get('checked'));
         }
     });
 
@@ -59,7 +67,7 @@ $(function(){
                 case 'file':
                     return new Filex.File(attrs, options);
                 default:
-                    console.log('Unknown model type:', attrs['type']);
+                    console.error('Unknown model type:', attrs['type']);
             }
         },
 
@@ -113,12 +121,13 @@ $(function(){
         tagName:  'tr',
 
         events: {
-            'click .link': 'selected'
+            'click': 'click'
         },
 
         initialize: function(options) {
             this.view = options.view;
 
+            this.listenTo(this.model, 'change', this.render);
             this.listenTo(this.model, 'remove', this.remove);
             this.listenTo(this.model, 'hidden', this.toggleHidden);
         },
@@ -136,6 +145,7 @@ $(function(){
                 path: this.view.path.full(),
                 name: this.model.get('name')
             });
+            data['cid'] = this.model.cid;
 
             this.$el.html(this.template()(data));
             this.toggleHidden();
@@ -147,11 +157,17 @@ $(function(){
             this.$el.toggleClass('hidden', this.model.isHidden() && !this.view.showHidden());
         },
 
-        selected: function(e) {
-            if (this.model.isDir()) {
-                e.preventDefault();
-                this.model.trigger('selected:dir', this.model);
+        click: function(e) {
+            if (e.target.className == 'link') {
+                if (this.model.isDir()) {
+                    e.preventDefault();
+                    this.model.trigger('selected:dir', this.model);
+                }
+
+                return;
             }
+
+            this.model.toggle();
         }
     });
 
@@ -193,14 +209,15 @@ $(function(){
 
         events: {
             'change #show-hidden': 'toggleHidden',
-            'click #host-controls .view': 'hostEdit'
+            'click #select-all': 'selectAll',
+            'click #btn-refresh': 'reloadFiles'
         },
 
         initialize: function(options) {
             this.$noItems = $('#no-items');
             this.$error = $('#error');
             this.$showHidden = $('#show-hidden');
-            this.$host = $('#host-controls');
+            this.$selectAll = $('#select-all');
 
             this.host = options.host;
             this.path = new Filex.Path();
@@ -212,6 +229,7 @@ $(function(){
             this.listenTo(this.path, 'selected', this.selectedPath);
             this.listenTo(this.files, 'reset', this.resetFiles);
             this.listenTo(this.files, 'selected:dir', this.selectedDir);
+            this.listenTo(this.files, 'change:checked', this.updateSelectAll);
 
             // used in selectize callbacks
             var view = this,
@@ -236,8 +254,6 @@ $(function(){
                     this.blur();
                 },
                 onBlur: function() {
-                    view.$host.toggleClass('editing');
-
                     var value = this.getValue();
                     if (!value) {
                         this.addItem(view.host, true);
@@ -259,8 +275,8 @@ $(function(){
         },
 
         render: function() {
-            this.$host.find('.view').text(this.host);
-            this.$noItems.toggle((this.showHidden() ? !Boolean(this.files.length) : !Boolean(this.files.visible().length)));
+            this.updateSelectAll();
+            this.$noItems.toggle(!Boolean(this.visibleFiles().length));
             this.$error.hide();
         },
 
@@ -291,6 +307,7 @@ $(function(){
 
                     var msg = (response.responseJSON || {}).msg || 'Błąd serwera';
 
+                    view.$noItems.hide();
                     view.$error.find('.msg').text(msg);
                     view.$error.show();
                     view.idle();
@@ -343,17 +360,42 @@ $(function(){
             return this.$showHidden[0].checked;
         },
 
-        hostEdit: function() {
-            this.$host.toggleClass('editing');
-            this.hostSelectize.focus();
-        },
-
         busy: function() {
             this.$el.addClass('busy');
         },
 
         idle: function() {
             this.$el.removeClass('busy');
+        },
+
+        visibleFiles: function() {
+            return this.showHidden() ? this.files.models : this.files.visible();
+        },
+
+        selectedFiles: function() {
+            return _.filter(this.visibleFiles(), function(item) {
+                return item.get('checked');
+            });
+        },
+
+        selectAll: function() {
+            var checked = this.$selectAll[0].checked;
+
+            _.each(this.visibleFiles(), function(item) {
+                item.set('checked', checked);
+            })
+        },
+
+        updateSelectAll: function() {
+            this.$selectAll.prop('checked', this.selectedFiles().length == this.visibleFiles().length);
+        },
+
+        clearSelection: function() {
+            _.each(this.visibleFiles(), function(item) {
+                item.set('checked', false);
+            });
+
+            this.updateSelectAll();
         }
     });
 });