Auto sorting from column picker in AutoGrid
authorBill Erickson <berick@esilibrary.com>
Wed, 28 Mar 2012 15:25:27 +0000 (11:25 -0400)
committerBill Erickson <berick@esilibrary.com>
Wed, 28 Mar 2012 15:25:27 +0000 (11:25 -0400)
Signed-off-by: Bill Erickson <berick@esilibrary.com>
Open-ILS/web/js/dojo/openils/widget/AutoGrid.js

index 4b8e748..c1021e7 100644 (file)
@@ -49,6 +49,7 @@ if(!dojo._hasResource['openils.widget.AutoGrid']) {
             },
 
             startup : function() {
+                var _this = this;
                 this.selectionMode = 'single';
                 this.sequence = openils.widget.AutoGrid.sequence++;
                 openils.widget.AutoGrid.gridCache[this.sequence] = this;
@@ -58,28 +59,6 @@ if(!dojo._hasResource['openils.widget.AutoGrid']) {
                 this.setStore(this.buildAutoStore());
                 this.cachedQueryOpts = {};
                 this._showing_create_pane = false;
-
-                if(this.showColumnPicker) {
-                    if(!this.columnPersistKey) {
-                        console.error("No columnPersistKey defined");
-                    } else {
-                        var picker = new openils.widget.GridColumnPicker(
-                            openils.User.authtoken, this.columnPersistKey, this);
-
-                        var _this = this;
-                        picker.onSortChange = function(fields) {
-                            if (_this.onSortChange)
-                                _this.onSortChange(fields)
-                        };
-
-                        if(openils.User.authtoken) {
-                            picker.load();
-                        } else {
-                            openils.Util.addOnLoad(function() { picker.load() });
-                        }
-                    }
-                }
-
                 this.overrideEditWidgets = {};
                 this.overrideEditWidgetClass = {};
                 this.overrideWidgetArgs = {};
@@ -138,10 +117,10 @@ if(!dojo._hasResource['openils.widget.AutoGrid']) {
                                 href : 'javascript:void(0);', 
                                 onclick : function() { 
                                     if (!self.filterDialog) {
-                                        self.filterDialog = new openils.widget.PCrudFilterDialog({fmClass:self.fmClass, suppressFilterFields:self.suppressFilterFields})
+                                        self.filterDialog = new openils.widget.PCrudFilterDialog(
+                                            {fmClass:self.fmClass, suppressFilterFields:self.suppressFilterFields})
                                         self.filterDialog.onApply = function(filter) {
-                                            self.resetStore();
-                                            self.loadAll(self.cachedQueryOpts, filter);
+                                            self.refresh(self.cachedQueryOpts, filter);
                                         };
                                         self.filterDialog.startup();
                                     }
@@ -597,19 +576,66 @@ if(!dojo._hasResource['openils.widget.AutoGrid']) {
                 this.setStore(this.buildAutoStore());
             },
 
-            refresh : function() {
+            refresh : function(opts, search) {
+                opts = opts || this.cachedQueryOpts;
+                search = search || this.cachedQuerySearch;
                 this.resetStore();
                 if (this.dataLoader)
                     this.dataLoader()
                 else
-                    this.loadAll(this.cachedQueryOpts, this.cachedQuerySearch);
+                    this._loadAll(opts, search);
+            },
+
+            // called after a sort change occurs within the column picker
+            cpSortHandler : function(fields) {
+                console.log("AutoGrod cpSortHandler(): " + fields);
+                // user-defined sort handler
+                if (this.onSortChange) { 
+                    this.onSortChange(fields)
+
+                // default sort handler
+                } else { 
+                    if (!this.cachedQueryOpts) 
+                        this.cachedQueryOpts = {};
+                    this.cachedQueryOpts.order_by = {};
+                    this.cachedQueryOpts.order_by[this.fmClass] = fields.join(',');
+                    this.refresh();
+                }
             },
 
             loadAll : function(opts, search) {
+                var _this = this;
+
+                // first we have to load the column picker to determine the sort fields.
+               
+                if(this.showColumnPicker && !this.columnPicker) {
+                    if(!this.columnPersistKey) {
+                        console.error("No columnPersistKey defined");
+                        this.columnPicker = {};
+                    } else {
+                        this.columnPicker = new openils.widget.GridColumnPicker(
+                            openils.User.authtoken, this.columnPersistKey, this);
+                        this.columnPicker.onSortChange = function(fields) {_this.cpSortHandler(fields)};
+                        this.columnPicker.onLoad = function(cpOpts) {
+                            _this.cachedQueryOpts = opts;
+                            _this.cachedQuerySearch = search;
+                            _this.cpSortHandler(cpOpts.sortFields); // calls refresh() -> _loadAll()
+                        };
+                        this.columnPicker.load();
+                        return;
+                    }
+                }
+
+                // column picker not wanted or already loaded
+                this._loadAll(opts, search);
+            },
+
+            _loadAll : function(opts, search) {
+                var self = this;
+
                 dojo.require('openils.PermaCrud');
                 if(this.loadProgressIndicator)
                     dojo.style(this.loadProgressIndicator, 'visibility', 'visible');
-                var self = this;
                 opts = dojo.mixin(
                     {limit : this.displayLimit, offset : this.displayOffset}, 
                     opts || {}