LP#1642378 Webstaff org selector misc. repairs
authorBill Erickson <berickxx@gmail.com>
Mon, 27 Feb 2017 21:43:09 +0000 (16:43 -0500)
committerKathy Lussier <klussier@masslnc.org>
Mon, 6 Mar 2017 22:09:51 +0000 (17:09 -0500)
1. Always apply default values after egStartup.go() has completed.
   Among other things, this prevents the occaisonal "cannot call ws_ou()
   on an undefined value" errors bubbling up from the org selector
   when an attempt to set default values occured before egAuth had
   retrieved the user (which occurs during startup).

2. For consistency, always run the $scope.onchange handler on initial
   page load when a selected value and change handler are defined.
   Similar to #1 above, the initial onchange always fires after
   egStartup has completed.

3. Run the change handler in a $timeout so that the $scope.selected
   value has a chance to propagate to the calling controller's $scope.
   Otherwise, the the calling controller has to manually track the
   selected value (which partially defeats the purpose of having this
   directive in the first place).

Signed-off-by: Bill Erickson <berickxx@gmail.com>
Signed-off-by: Terran McCanna <tmccanna@georgialibraries.org>
Signed-off-by: Kathy Lussier <klussier@masslnc.org>
Open-ILS/web/js/ui/default/staff/services/ui.js

index ad4c104..5d73487 100644 (file)
@@ -550,8 +550,8 @@ function($window , egStrings) {
            + '</ul>'
           + '</div>',
 
-        controller : ['$scope','$timeout','egOrg','egAuth','egCore','egStartup',
-              function($scope , $timeout , egOrg , egAuth , egCore , egStartup) {
+        controller : ['$scope','$timeout','egCore','egStartup',
+              function($scope , $timeout , egCore , egStartup) {
 
             if ($scope.alldisabled) {
                 $scope.disable_button = $scope.alldisabled == 'true' ? true : false;
@@ -559,19 +559,18 @@ function($window , egStrings) {
                 $scope.disable_button = false;
             }
 
-            $scope.egOrg = egOrg; // for use in the link function
-            $scope.egAuth = egAuth; // for use in the link function
-            $scope.hatch = egCore.hatch // for use in the link function
-
             // avoid linking the full fleshed tree to the scope by 
             // tossing in a flattened list.
             // --
             // Run-time code referencing post-start data should be run
             // from within a startup block, otherwise accessing this
             // module before startup completes will lead to failure.
+            //
+            // controller() runs before link().
+            // This post-startup code runs after link().
             egStartup.go().then(function() {
 
-                $scope.orgList = egOrg.list().map(function(org) {
+                $scope.orgList = egCore.org.list().map(function(org) {
                     return {
                         id : org.id(),
                         shortname : org.shortname(), 
@@ -579,10 +578,39 @@ function($window , egStrings) {
                     }
                 });
 
-                if (!$scope.selected && !$scope.nodefault)
-                    $scope.selected = egOrg.get(egAuth.user().ws_ou());
+                // Apply default values
+
+                if ($scope.stickySetting) {
+                    var orgId = egCore.hatch.getLocalItem($scope.stickySetting);
+                    if (orgId) {
+                        $scope.selected = egCore.org.get(orgId);
+                    }
+                }
+
+                if (!$scope.selected && !$scope.nodefault) {
+                    $scope.selected = 
+                        egCore.org.get(egCore.auth.user().ws_ou());
+                }
+
+                fire_orgsel_onchange(); // no-op if nothing is selected
             });
 
+            /**
+             * Fire onchange handler after a timeout, so the
+             * $scope.selected value has a chance to propagate to
+             * the page controllers before the onchange fires.  This
+             * way, the caller does not have to manually capture the
+             * $scope.selected value during onchange.
+             */
+            function fire_orgsel_onchange() {
+                if (!$scope.selected || !$scope.onchange) return;
+                $timeout(function() {
+                    console.debug(
+                        'egOrgSelector onchange('+$scope.selected.id()+')');
+                    $scope.onchange($scope.selected)
+                });
+            }
+
             $scope.getSelectedName = function() {
                 if ($scope.selected && $scope.selected.shortname)
                     return $scope.selected.shortname();
@@ -590,11 +618,11 @@ function($window , egStrings) {
             }
 
             $scope.orgChanged = function(org) {
-                $scope.selected = egOrg.get(org.id);
+                $scope.selected = egCore.org.get(org.id);
                 if ($scope.stickySetting) {
                     egCore.hatch.setLocalItem($scope.stickySetting, org.id);
                 }
-                if ($scope.onchange) $scope.onchange($scope.selected);
+                fire_orgsel_onchange();
             }
 
         }],
@@ -610,20 +638,7 @@ function($window , egStrings) {
                         scope[field] = false;
                 }
             );
-
-            if (scope.stickySetting) {
-                var orgId = scope.hatch.getLocalItem(scope.stickySetting);
-                if (orgId) {
-                    scope.selected = scope.egOrg.get(orgId);
-                    if (scope.onchange)
-                        scope.onchange(scope.selected);
-                }
-            }
-
-            if (!scope.selected && !scope.nodefault)
-                scope.selected = scope.egOrg.get(scope.egAuth.user().ws_ou());
         }
-
     }
 })