Эх сурвалжийг харах

HUE-3622 [home] Enable sorting

Johan Ahlen 9 жил өмнө
parent
commit
1b525cbc2b

+ 47 - 10
desktop/core/src/desktop/static/desktop/js/fileBrowser/hueFileEntry.js

@@ -25,6 +25,33 @@
   }
   }
 }(this, function (ko, HueDocument) {
 }(this, function (ko, HueDocument) {
 
 
+  var sorts = {
+    nameAsc: function (a, b) {
+      return a.definition().name.localeCompare(b.definition().name);
+    },
+    nameDesc: function (a, b) {
+      return sorts.nameAsc(b, a);
+    },
+    typeAsc: function (a, b) {
+      return a.definition().type.localeCompare(b.definition().type);
+    },
+    typeDesc: function (a, b) {
+      return sorts.typeAsc(b, a);
+    },
+    ownerAsc: function (a, b) {
+      return a.definition().owner.localeCompare(b.definition().owner);
+    },
+    ownerDesc: function (a, b) {
+      return sorts.ownerAsc(b, a);
+    },
+    lastModifiedAsc: function (a, b) {
+      return a.definition().last_modified_ts - b.definition().last_modified_ts;
+    },
+    lastModifiedDesc: function (a, b) {
+      return sorts.lastModifiedAsc(b, a);
+    }
+  };
+
   /**
   /**
    *
    *
    * @param {Object} options
    * @param {Object} options
@@ -116,6 +143,8 @@
 
 
     self.entries = ko.observableArray([]);
     self.entries = ko.observableArray([]);
 
 
+    self.activeSort = options.activeSort;
+
     self.selectedEntries = ko.pureComputed(function () {
     self.selectedEntries = ko.pureComputed(function () {
       return $.grep(self.entries(), function (entry) {
       return $.grep(self.entries(), function (entry) {
         return entry.selected();
         return entry.selected();
@@ -161,7 +190,23 @@
     if (! self.isTrash() && ! self.isTrashed()) {
     if (! self.isTrash() && ! self.isTrashed()) {
       $('#createDirectoryModal').modal('show');
       $('#createDirectoryModal').modal('show');
     }
     }
-  }
+  };
+
+  HueFileEntry.prototype.setSort = function (name) {
+    var self = this;
+    if (self.activeSort().indexOf(name) === -1) {
+      if (name === 'lastModified') {
+        self.activeSort('lastModifiedDesc');
+      } else {
+        self.activeSort(name + 'Asc')
+      }
+    } else if (self.activeSort().indexOf('Asc') !== -1) {
+      self.activeSort(name + 'Desc');
+    } else {
+      self.activeSort(name + 'Asc');
+    }
+    self.entries.sort(sorts[self.activeSort()]);
+  };
 
 
   HueFileEntry.prototype.showSharingModal = function (entry) {
   HueFileEntry.prototype.showSharingModal = function (entry) {
     var self = this;
     var self = this;
@@ -337,15 +382,7 @@
             }
             }
           });
           });
 
 
-          newEntries.sort(function (a, b) {
-            if (a.isDirectory() && ! b.isDirectory()) {
-              return -1;
-            }
-            if (b.isDirectory() && ! a.isDirectory()) {
-              return 1;
-            }
-            return a.definition().name.localeCompare(b.definition().name);
-          });
+          newEntries.sort(sorts[self.activeSort()]);
           self.entries(newEntries);
           self.entries(newEntries);
           if (! self.parent && data.parent) {
           if (! self.parent && data.parent) {
             self.parent = self.createNewEntry({
             self.parent = self.createNewEntry({

+ 1 - 0
desktop/core/src/desktop/static/desktop/js/home2.vm.js

@@ -53,6 +53,7 @@
       app: 'documents',
       app: 'documents',
       user: self.user,
       user: self.user,
       superuser: self.superuser,
       superuser: self.superuser,
+      activeSort: ko.observable('lastModifiedDesc'),
       definition: {
       definition: {
         name: '/'
         name: '/'
       }
       }

+ 1 - 0
desktop/core/src/desktop/templates/assist.mako

@@ -1066,6 +1066,7 @@ from desktop.views import _ko
           assistHelper: options.assistHelper,
           assistHelper: options.assistHelper,
           app: 'documents',
           app: 'documents',
           user: options.user,
           user: options.user,
+          activeSort: ko.observable('name'),
           definition: {
           definition: {
             name: '/',
             name: '/',
             type: 'directory'
             type: 'directory'

+ 12 - 5
desktop/core/src/desktop/templates/file_browser.mako

@@ -171,7 +171,6 @@ from desktop.views import _ko
 
 
     .fb-primary-col {
     .fb-primary-col {
       flex: 1;
       flex: 1;
-      height: 42px;
       vertical-align: middle;
       vertical-align: middle;
       padding-left: 8px;
       padding-left: 8px;
       white-space: nowrap;
       white-space: nowrap;
@@ -179,6 +178,14 @@ from desktop.views import _ko
       overflow: hidden;
       overflow: hidden;
     }
     }
 
 
+    .fb-header .fb-primary-col {
+      height: 30px;
+    }
+
+    .fb-row .fb-primary-col {
+      height: 42px;
+    }
+
     .fb-primary-col .fa {
     .fb-primary-col .fa {
       vertical-align: middle;
       vertical-align: middle;
     }
     }
@@ -529,12 +536,12 @@ from desktop.views import _ko
       <!-- ko with: activeEntry -->
       <!-- ko with: activeEntry -->
       <!-- ko if: entries().length > 0 -->
       <!-- ko if: entries().length > 0 -->
       <div class="fb-header">
       <div class="fb-header">
-        <div class="fb-primary-col">${ _('Name') }</div>
+        <div class="fb-primary-col" data-bind="click: function () { setSort('name') }, css: { 'sorting_asc' : activeSort() === 'nameAsc', 'sorting_desc' : activeSort() === 'nameDesc', 'sorting' : activeSort().indexOf('name') !== 0 }">${ _('Name') }</div>
         <div class="fb-attr-group">
         <div class="fb-attr-group">
           <div class="pull-right">
           <div class="pull-right">
-            <div class="fb-attr-col fb-type">${ _('Type') }</div>
-            <div class="fb-attr-col fb-owner">${ _('Owner') }</div>
-            <div class="fb-attr-col fb-modified">${ _('Last Modified') }</div>
+            <div class="fb-attr-col fb-type" data-bind="click: function () { setSort('type') }, css: { 'sorting_asc' : activeSort() === 'typeAsc', 'sorting_desc' : activeSort() === 'typeDesc', 'sorting' : activeSort().indexOf('type') !== 0 }">${ _('Type') }</div>
+            <div class="fb-attr-col fb-owner" data-bind="click: function () { setSort('owner') }, css: { 'sorting_asc' : activeSort() === 'ownerAsc', 'sorting_desc' : activeSort() === 'ownerDesc', 'sorting' : activeSort().indexOf('owner') !== 0 }">${ _('Owner') }</div>
+            <div class="fb-attr-col fb-modified" data-bind="click: function () { setSort('lastModified') }, css: { 'sorting_asc' : activeSort() === 'lastModifiedAsc', 'sorting_desc' : activeSort() === 'lastModifiedDesc', 'sorting' : activeSort().indexOf('lastModified') !== 0 }">${ _('Last Modified') }</div>
           </div>
           </div>
         </div>
         </div>
       </div>
       </div>