// 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',
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.
// --
}
).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) {