lp1724019 offline working locations unsorted
authorMike Risher <mrisher@catalyte.io>
Sun, 8 Mar 2020 19:31:43 +0000 (19:31 +0000)
committerMike Risher <mrisher@catalyte.io>
Tue, 10 Mar 2020 17:50:26 +0000 (17:50 +0000)
The offline working locations are not sorted alphabetically. This
fix involves putting all orgs in alphabetical order and preserving
org hierarchy. This change is only being applied to the offline
working locations dropdown of orgs.

Signed-off-by: Mike Risher <mrisher@catalyte.io>
 Changes to be committed:
modified:   Open-ILS/src/templates/staff/offline-interface.tt2
modified:   Open-ILS/web/js/ui/default/staff/services/ui.js

Open-ILS/src/templates/staff/offline-interface.tt2
Open-ILS/web/js/ui/default/staff/services/ui.js

index dc7b56b..eb8ca80 100644 (file)
@@ -18,7 +18,8 @@
       </div>
       <div class="input-group-addon"><b>[% l('Working location') %]</b></div>
       <div class="input-group-addon">
-        <eg-org-selector sticky-setting="eg.org.offline_location" selected="org"></eg-org-selector>
+        <eg-org-selector sticky-setting="eg.org.offline_location" selected="org"
+          alphabetized-orgs="true"></eg-org-selector>
       </div>
       <div class="input-group-addon">
         <button
index f8f7cd1..d105ba5 100644 (file)
@@ -1081,7 +1081,10 @@ function($uibModal , $interpolate , egCore) {
 
             // optional name of settings key for persisting
             // the last selected org unit
-            stickySetting : '@'
+            stickySetting : '@',
+
+            // if set to true alphabetize org list
+            alphabetizedOrgs : '@'
         },
 
         templateUrl : './share/t_org_select',
@@ -1096,6 +1099,54 @@ function($uibModal , $interpolate , egCore) {
                 return " ".repeat(org.ou_type().depth()) + org.shortname();
             }
 
+            // creates list of $scope.shortNames from orgArray provided
+            function createShortNames(orgArray) {
+                $scope.shortNames = orgArray
+                .filter(function(org) {
+                    return !(
+                        $scope.hiddenTest && 
+                        $scope.hiddenTest(org.id())
+                    );
+                }).map(function(org) {
+                    return formatName(org);
+                });
+            }
+
+            // finds the deepest nested org and saves that value as $scope.maxDepth
+            function findMaxDepth() {
+                if ($scope.maxDepth === undefined) {
+                    $scope.maxDepth = 0;
+                    $scope.allOrgs = egCore.org.list();
+                    $scope.allOrgs.forEach(function (org) {
+                        if (org.ou_type().depth() > $scope.maxDepth) {
+                            $scope.maxDepth = org.ou_type().depth();
+                        }
+                    })
+                }
+            }
+            
+            // alphabetizes and adds all children to parents of a given depth
+            function addChildren(orgs, targetDepth) {
+                orgs.forEach(function (org, index) {
+                    if (org.ou_type().depth() === targetDepth) {
+                        currentOrgChildren = org.children();
+                        alphabetizeArray(currentOrgChildren);
+                        $scope.allOrgsSorted.splice.apply($scope.allOrgsSorted, 
+                            [index+1, 0].concat(currentOrgChildren));
+                    }
+                })
+            }
+
+            // alphabetizes a given array by the shortname() of each org
+            function alphabetizeArray (array) {
+                array.sort(function(a, b){
+                    if(a.shortname() < b.shortname()) { return -1; }
+                    if(a.shortname() > b.shortname()) { return 1; }
+                    return 0;
+                });
+                return array;
+            }
+
             // avoid linking the full fleshed tree to the scope by 
             // tossing in a flattened list.
             // --
@@ -1112,21 +1163,26 @@ function($uibModal , $interpolate , egCore) {
                 }
             ).then(
                 function() {
-
-                    $scope.selecteName = '';
-
-                    $scope.shortNames = egCore.org.list()
-                    .filter(function(org) {
-                        return !(
-                            $scope.hiddenTest && 
-                            $scope.hiddenTest(org.id())
-                        );
-                    }).map(function(org) {
-                        return formatName(org);
-                    });
+                    $scope.selectedName = '';
+                    if ($scope.alphabetizedOrgs === "true") {
+                        findMaxDepth();
+                        // start with only one top level org
+                        $scope.allOrgsSorted = egCore.org.list()
+                        .filter(function (org) {
+                            return (org.ou_type().depth() === 0)
+                        });
+                        if ($scope.allOrgsSorted) {
+                            // add children, then grandchildren, etc
+                            for (var depth=0; depth < $scope.maxDepth; depth++) {
+                                addChildren($scope.allOrgsSorted, depth);
+                            }
+                            createShortNames($scope.allOrgsSorted);
+                        }
+                    } else {
+                        createShortNames(egCore.org.list());
+                    }
     
                     // Apply default values
-    
                     if ($scope.stickySetting) {
                         var orgId = egCore.hatch.getLocalItem($scope.stickySetting);
                         if (orgId) {