AutoGrid column picker sort handler
authorBill Erickson <berick@esilibrary.com>
Wed, 28 Mar 2012 15:53:31 +0000 (11:53 -0400)
committerBill Erickson <berick@esilibrary.com>
Wed, 28 Mar 2012 15:53:31 +0000 (11:53 -0400)
Adds a default sort handler for column picker onSortChange events.  The
handler simply reloads the grid with the new columns and assumes all
columns are fields on the base class.

Also supports a user custom onSortChange handler for more complicated
situations.

Initial grid load now happens after the column picker loads when the
column picker is enabled to sure the grid only loads once on the initial
load.

Signed-off-by: Bill Erickson <berick@esilibrary.com>
Open-ILS/web/js/dojo/openils/widget/AutoGrid.js

index d765276..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,21 +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);
-                        if(openils.User.authtoken) {
-                            picker.load();
-                        } else {
-                            openils.Util.addOnLoad(function() { picker.load() });
-                        }
-                    }
-                }
-
                 this.overrideEditWidgets = {};
                 this.overrideEditWidgetClass = {};
                 this.overrideWidgetArgs = {};
@@ -131,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();
                                     }
@@ -590,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 || {}