initial grid cleanup collab/berick/web-staff-proto-grid-cleanup
authorBill Erickson <berick@esilibrary.com>
Tue, 10 Jun 2014 17:13:51 +0000 (13:13 -0400)
committerBill Erickson <berick@esilibrary.com>
Tue, 10 Jun 2014 17:13:51 +0000 (13:13 -0400)
Signed-off-by: Bill Erickson <berick@esilibrary.com>
Open-ILS/src/templates/staff/circ/patron/t_group.tt2
Open-ILS/web/js/ui/default/staff/circ/patron/app.js
Open-ILS/web/js/ui/default/staff/services/grid.js

index 8d28161..1917f4d 100644 (file)
@@ -11,9 +11,7 @@
 <div class="pad-vert"></div>
 <eg-grid
   idl-class="au"
-  query="gridQuery"
   sort="gridSort"
-  revision="gridRevision"
   grid-controls="gridControls"
   menu-label="[% l('Group Actions') %]">
 
index 71bdb98..46080a9 100644 (file)
@@ -381,12 +381,13 @@ function($q , $timeout , $location , egCore,  egUser , $locale) {
        ['$scope','$q','$location','$filter','egCore','egUser','patronSvc',
 function($scope,  $q,  $location , $filter,  egCore,  egUser,  patronSvc) {
 
+    // returns true if a redirect occurs
     function redirectToAlertPanel() {
 
         $scope.alert_penalties = 
             function() {return patronSvc.alert_penalties}
 
-        if (patronSvc.alertsShown) return;
+        if (patronSvc.alertsShown) return false;
         patronSvc.alertsShown = true;
 
         // if the patron has any unshown alerts, show them now
@@ -396,7 +397,7 @@ function($scope,  $q,  $location , $filter,  egCore,  egUser,  patronSvc) {
             $location
                 .path('/circ/patron/' + patronSvc.current.id() + '/alerts')
                 .search('card', null);
-            return;
+            return true;
         }
 
         // no alert required.  If the patron has fines and the show-bills
@@ -408,7 +409,11 @@ function($scope,  $q,  $location , $filter,  egCore,  egUser,  patronSvc) {
             $location
                 .path('/circ/patron/' + patronSvc.current.id() + '/bills')
                 .search('card', null);
+
+            return true;
         }
+
+        return false;
     }
 
     // called after each route-specified controller is instantiated.
@@ -1112,13 +1117,14 @@ function($scope,  $routeParams , $q , $location , egCore ,
     var usr_id = $routeParams.id;
 
     var statsCache = {};
-    var gridQuery = {};
     $scope.totals = {owed : 0, total_out : 0, overdue : 0}
 
-    $scope.gridQuery = function() {return gridQuery}
-    $scope.gridRevision = 0;
     $scope.gridSort = ['create_date'];
-    $scope.gridControls = {
+    var initQuery;
+    var grid = $scope.gridControls = {
+        activateItem : function(item) {
+            $location.path('/circ/patron/' + item.id + '/checkout');
+        },
         itemRetrieved : function(item) {
 
             if (statsCache[item.id]) {
@@ -1131,15 +1137,26 @@ function($scope,  $routeParams , $q , $location , egCore ,
                     $scope.totals.overdue += stats.checkouts.overdue; 
                 });
             }
+        },
+        /*
+        setQuery : function(q) {
+            if (q) initQuery = q;
+            return initQuery;
         }
+        */
     }
 
-    $scope.initTab('other', $routeParams.id).then(function() {
+    $scope.initTab('other', $routeParams.id)
+    .then(function(redirect) {
+        // if we are redirecting to the alerts page, avoid updating the
+        // grid query.
+        if (redirect) return;
         // let initTab() fetch the user first so we can know the usrgroup
-        gridQuery = {
+
+        grid.setQuery({
             usrgroup : patronSvc.current.usrgroup(),
             deleted : 'f'
-        }
+        });
         $scope.totals.owed = patronSvc.patron_stats.fines.group_balance_owed;
     });
 
@@ -1157,7 +1174,7 @@ function($scope,  $routeParams , $q , $location , egCore ,
             );
         });
 
-        $q.all(promises).then(function() {$scope.gridRevision++});
+        $q.all(promises).then(function() {grid.refresh()});
     }
 
     function addUserToGroup(user) {
@@ -1168,9 +1185,7 @@ function($scope,  $routeParams , $q , $location , egCore ,
             'open-ils.actor.patron.update',
             egCore.auth.token(), user
 
-        ).then(function() {
-            $scope.gridRevision++;
-        });
+        ).then(function() {grid.refresh()});
     }
 
     // fetch each user ("selected" has flattened users)
@@ -1194,9 +1209,7 @@ function($scope,  $routeParams , $q , $location , egCore ,
             );
         });
 
-        $q.all(promises).then(function() {
-            $scope.gridRevision++;
-        });
+        $q.all(promises).then(function() {grid.refresh()});
     }
 
     function showMoveToGroupConfirm(barcode, selected) {
@@ -1250,6 +1263,5 @@ function($scope,  $routeParams , $q , $location , egCore ,
 
     $scope.moveToGroup = function() { moveToGroup() };
     $scope.moveToAnotherGroup = function(selected) { moveToGroup(selected) };
-
 }])
 
index e47181a..dd27cc4 100644 (file)
@@ -10,14 +10,6 @@ angular.module('egGridMod',
             // IDL class hint (e.g. "aou")
             idlClass : '@',
 
-            // reference to a function returning a query for the default
-            // egGridFlatDataProvider handler.  Any time the return value
-            // changes, the grid will be refreshed.
-            query : '=',
-
-            // flattener-style sort array
-            sort : '=',
-
             // default page size
             pageSize : '@',
 
@@ -47,7 +39,7 @@ angular.module('egGridMod',
             //
             // controller => $scope.myProvider === undefined
             //
-            // Storing the provider in the local scope just hog
+            // Storing the provider in the local scope just use
             // resources, anyway.
             //
             itemsProvider : '=',
@@ -85,11 +77,9 @@ angular.module('egGridMod',
             //  allItems      : function(allItems) {}
             //  selectedItems : function(selected) {}
             //  selectItems   : function(ids) {}
+            //  setQuery      : function(queryStruct) {} // causes reload
+            //  setSort       : function(sortSturct) {} // causes reload
             gridControls : '=',
-
-            // if set, we watch this scope variable for changes.  If it
-            // changes, we refresh the grid.
-            revision : '='
         },
 
         // TODO: avoid hard-coded url
@@ -116,14 +106,15 @@ angular.module('egGridMod',
                 $scope.items = [];
                 $scope.showGridConf = false;
                 grid.totalCount = -1;
+                $scope.selected = {};
 
+                // actions for selected items
                 $scope.actions = [];
                 grid.addAction = function(act) {
                     $scope.actions.push(act);
                 }
 
-                $scope.selected = {};
-
+                // global actions
                 $scope.menuItems = [];
                 grid.addMenuItem = function(item) {
                     $scope.menuItems.push(item);
@@ -137,16 +128,11 @@ angular.module('egGridMod',
                     }
                 }
 
-                grid.gridControls = $scope.gridControls || {};
+                grid.idlClass = $scope.idlClass;
+                delete $scope.idlClass;
 
-                // items needed only by the grid; remove from scope
-                angular.forEach( 
-                    ['idlClass', 'persistKey'], 
-                    function(field) {
-                        grid[field] = $scope[field];
-                        delete $scope[field];
-                    }
-                );
+                grid.persistKey = $scope.persistKey;
+                delete $scope.persistKey;
 
                 grid.indexField = $scope.idField;
                 delete $scope.idField;
@@ -194,14 +180,6 @@ angular.module('egGridMod',
                     grid.columnsProvider.compileAutoColumns();
                     delete $scope.autoFields;
                 }
-
-                /*
-                if (grid.onSelectionChange) {
-                    $scope.$watch('selected', function() {
-                        grid.onSelectionChange(grid.getSelectedItems())
-                    }, true);
-                }
-                */
    
                 if (!grid.dataProvider) {
 
@@ -212,57 +190,41 @@ angular.module('egGridMod',
                         columnsProvider : grid.columnsProvider,
                         query : $scope.query
                     });
-
-                    if (!grid.dataProvider.query) {
-                        console.error(
-                            "Grid query is required when no data provider is supplied");
-                        return;
-                    }
-
-                    $scope.$watch(
-                        function() { return grid.dataProvider.query() },
-                        function(newVal, oldVal) { 
-                            if (newVal !== oldVal)
-                                grid.dataProvider.load(true);
-                        },
-                        true // object comparison
-                    );
-                    delete $scope.query;
                 }
 
-
                 grid.dataProvider.sort = $scope.sort || [];
                 delete $scope.sort;
 
-                if (angular.isDefined($scope.revision)) {
-                    $scope.$watch('revision', function(newVal, oldVal) {
-                        if (newVal != oldVal) 
-                            grid.dataProvider.load(true);
-                    });
-                }
+                $scope.itemFieldValue = grid.dataProvider.itemFieldValue;
+                $scope.indexValue = function(item) {
+                    return grid.indexValue(item)
+                };
 
-                grid.dataProvider.load = function(reset) { 
-                    if (reset) grid.offset = 0;
-                    grid.collect();
-                }
-                
-                grid.dataProvider.getSelectedItems = function() {
-                    return grid.getSelectedItems();
-                }
+                grid.applyControlFunctions();
 
-                grid.dataProvider.selectOneItem = function(index) {
-                    return grid.selectOneItem(index);
-                }
+                grid.loadConfig().then(function() { 
+                    // link columns to scope after loadConfig(), since it
+                    // replaces the columns array.
+                    $scope.columns = grid.columnsProvider.columns;
+                });
+            }
+
+            // link our control functions into the gridControls scope
+            // object so the caller can access them.
+            grid.applyControlFunctions = function() {
 
-                // only link the caller mediation functions into 
-                // the scope if the caller defines handlers. 
-                grid.gridControls.selectedItems = function() {
+                var controls = $scope.gridControls;
+
+                // link in the control functions
+                controls.selectedItems = function() {
                     return grid.getSelectedItems()
                 }
-                grid.gridControls.allItems = function() {
+
+                controls.allItems = function() {
                     return $scope.items;
                 }
-                grid.gridControls.selectItems = function(ids) {
+
+                controls.selectItems = function(ids) {
                     if (!ids) return;
                     $scope.selected = {};
                     angular.forEach(ids, function(i) {
@@ -270,29 +232,35 @@ angular.module('egGridMod',
                     });
                 }
 
-                $scope.itemFieldValue = grid.dataProvider.itemFieldValue;
-                $scope.indexValue = function(item) {
-                    return grid.indexValue(item)
-                };
+                // if the caller provided a functional setQuery,
+                // extract the value before replacing it
+                if (controls.setQuery) {
+                    grid.dataProvider.query = 
+                        controls.setQuery();
+                }
 
-                // this allows the caller to pass in initializtion 
-                // values, like offset, for cases when the caller may
-                // be caching grid data between route loads.
-                if (angular.isFunction(grid.dataProvider.initialize)) {
-                    var conf = grid.dataProvider.initialize();
-                    if (conf) {
-                        angular.forEach(conf, function(val, key) {
-                            if (val !== null) grid[key] = val;
-                        });
-                    }
+                controls.setQuery = function(query) {
+                    grid.dataProvider.query = query;
+                    controls.refresh(true);
                 }
-    
-                grid.loadConfig().then(function() { 
-                    // link columns to scope after loadConfig(), since it
-                    // replaces the columns array.
-                    $scope.columns = grid.columnsProvider.columns;
-                    //if (grid.selfManagedData) grid.collect();
-                });
+
+                // if the caller provided a functional setSort
+                // extract the value before replacing it
+                if (controls.setSort) {
+                    grid.dataProvider.sort = 
+                        controls.setSort();
+                }
+
+                controls.setSort = function(sort) {
+                    controls.refresh(true);
+                }
+
+                controls.refresh = function(reset) {
+                    if (reset) grid.offset = 0;
+                    grid.collect();
+                }
+
+                grid.dataProvider.refresh = controls.refresh;
             }
 
             // remove the stored column configuration preferenc, then recover 
@@ -789,6 +757,10 @@ angular.module('egGridMod',
 
             // asks the dataProvider for a page of data
             grid.collect = function() {
+
+                // avoid firing the collect if there is nothing to collect.
+                if (grid.selfManagedData && !grid.dataProvider.query) return;
+
                 if (grid.collecting) return; // avoid parallel collects()
 
                 // ensure all of our dropdowns are closed
@@ -801,15 +773,15 @@ angular.module('egGridMod',
                 grid.collecting = true;
                 grid.dataProvider.get(grid.offset, grid.limit).then(
                 function() {
-                    if (grid.gridControls.allItemsRetrieved)
-                        grid.gridControls.allItemsRetrieved();
+                    if ($scope.gridControls.allItemsRetrieved)
+                        $scope.gridControls.allItemsRetrieved();
                 },
                 null, 
                 function(item) {
                     if (item) {
                         $scope.items.push(item)
-                        if (grid.gridControls.itemRetrieved)
-                            grid.gridControls.itemRetrieved(item);
+                        if ($scope.gridControls.itemRetrieved)
+                            $scope.gridControls.itemRetrieved(item);
                     }
                 })['finally'](function() { grid.collecting = false })
             }
@@ -1197,18 +1169,9 @@ angular.module('egGridMod',
                 return def.promise;
             }
 
-            gridData.initialize = function() {
-                return {};
-            }
-
-            // incrementing the provider informs the grid it's time to
-            // redraw itself.  If reset is true, it resets the grid paging
-            // to page 0.
-            gridData.increment = function(reset) {
-                // if called before the grid is instantiated, 
-                // load() may not yet be defined.
-                if (gridData.load) gridData.load(reset);
-            }
+            // Calls the grid refresh function.  Once instantiated, the
+            // grid will replace this function with it's own refresh()
+            gridData.refresh = function(reset) { }
 
             // returns a promise whose notify() delivers items
             gridData.get = function(index, count) {
@@ -1273,11 +1236,6 @@ angular.module('egGridMod',
                 if (obj === null || obj === undefined || obj === '') return '';
                 if (!idlField) return obj;
                 return obj;
-
-                // egGridValueFilter is applied in the template.
-                // not sure why this was here.. may be getting
-                // called from another context
-                //return $filter('egGridValueFilter')(obj, idlField);
             }
         }
 
@@ -1301,11 +1259,10 @@ angular.module('egGridMod',
                 var provider = egGridDataProvider.instance(args);
 
                 provider.get = function(offset, count) {
-                    if (!angular.isFunction(provider.query)) 
-                        return $q.when();
 
-                    var query = provider.query();
-                    if (!query || angular.equals(query, {})) 
+                    // no query means no call
+                    if (!provider.query || 
+                            angular.equals(provider.query, {})) 
                         return $q.when();
 
                     // find all of the currently visible columns
@@ -1322,7 +1279,7 @@ angular.module('egGridMod',
                         'open-ils.fielder',
                         'open-ils.fielder.flattened_search',
                         egCore.auth.token(), provider.idlClass, 
-                        queryFields, query,
+                        queryFields, provider.query,
                         {   sort : provider.sort,
                             limit : count,
                             offset : offset