checkboxes for selecting multiple files
[qcg-portal.git] / filex / static / filex / filex.js
index e1dc50f..81b401c 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'));
         }
     });
 
@@ -113,12 +121,14 @@ $(function(){
         tagName:  'tr',
 
         events: {
-            'click .link': 'selected'
+            'click .link': 'selected',
+            'click input[type=checkbox]': 'toggle'
         },
 
         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 +146,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();
@@ -149,7 +160,17 @@ $(function(){
 
         selected: function(e) {
             e.preventDefault();
-            this.model.trigger(this.model.isDir() ? 'selected:dir' : 'selected:file', this.model);
+            if (this.model.isDir()) {
+                this.model.trigger('selected:dir', this.model);
+            }
+            else {
+                this.model.trigger('selected:file', this.model);
+                this.toggle();
+            }
+        },
+
+        toggle: function() {
+            this.model.toggle();
         }
     });
 
@@ -191,7 +212,8 @@ $(function(){
 
         events: {
             'change #show-hidden': 'toggleHidden',
-            'click #host-controls .view': 'hostEdit'
+            'click #host-controls .view': 'hostEdit',
+            'click #select-all': 'selectAll'
         },
 
         initialize: function(options) {
@@ -199,6 +221,7 @@ $(function(){
             this.$error = $('#error');
             this.$showHidden = $('#show-hidden');
             this.$host = $('#host-controls');
+            this.$selectAll = $('#select-all');
 
             this.host = options.host;
             this.path = new Filex.Path();
@@ -211,6 +234,7 @@ $(function(){
             this.listenTo(this.files, 'reset', this.resetFiles);
             this.listenTo(this.files, 'selected:dir', this.selectedDir);
             this.listenTo(this.files, 'selected:file', this.selectedFile);
+            this.listenTo(this.files, 'change:checked', this.updateSelectAll);
 
             // used in selectize callbacks
             var view = this,
@@ -258,8 +282,9 @@ $(function(){
         },
 
         render: function() {
+            this.updateSelectAll();
             this.$host.find('.view').text(this.host);
-            this.$noItems.toggle((this.showHidden() ? !Boolean(this.files.length) : !Boolean(this.files.visible().length)));
+            this.$noItems.toggle(!Boolean(this.visibleFiles().length));
             this.$error.hide();
         },
 
@@ -358,6 +383,28 @@ $(function(){
 
         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);
         }
     });
 });