new <eg-org-selector> directive
authorBill Erickson <berick@esilibrary.com>
Wed, 30 Apr 2014 16:42:32 +0000 (12:42 -0400)
committerBill Erickson <berick@esilibrary.com>
Wed, 30 Apr 2014 16:42:32 +0000 (12:42 -0400)
Signed-off-by: Bill Erickson <berick@esilibrary.com>
Open-ILS/web/js/ui/default/staff/services/ui.js

index 4eebe7d..44a466e 100644 (file)
@@ -1,7 +1,7 @@
 /**
   * UI tools and directives.
   */
-angular.module('egUiMod', ['ui.bootstrap'])
+angular.module('egUiMod', ['egCoreMod', 'ui.bootstrap'])
 
 
 /**
@@ -133,3 +133,65 @@ function($timeout, $parse) {
 
     return service;
 })
+
+/**
+ * Nested org unit selector modeled as a Bootstrap dropdown button.
+ */
+.directive('egOrgSelector', function() {
+    return {
+        restrict : 'AE',
+        transclude : true,
+        scope : {
+            selected : '=', // defaults to workstation or root org
+            orgChanged : '=' // called when selected org changes
+        },
+
+        // any reason to move this into a TT2 template?
+        template : 
+            '<button type="button" class="btn btn-default dropdown-toggle"'
+            + 'data-toggle="dropdown">'
+            + '<span style="padding-right: 5px;">{{getSelectedName()}}</span>'
+            + '<span class="caret"></span>'
+          + '</button>'
+          + '<ul class="dropdown-menu">'
+            + '<li ng-repeat="org in orgList">'
+              + '<a href="" ng-click="orgChanged(org)"'
+                + 'style="padding-left: {{org.depth * 10 + 5}}px">'
+                + '{{org.shortname}}'
+              + '</a>'
+            + '</li>'
+          + '</ul>',
+
+        controller : ['$scope','$timeout','egOrg','egAuth',
+              function($scope , $timeout , egOrg , egAuth) {
+
+            // avoid linking the full fleshed tree to the scope by 
+            // tossing in a flattened list.
+            $scope.orgList = egOrg.list().map(function(org) {
+                return {
+                    id : org.id(),
+                    shortname : org.shortname(), 
+                    depth : org.ou_type().depth()
+                }
+            });
+
+            $scope.getSelectedName = function() {
+                if ($scope.selected)
+                    return $scope.selected.shortname();
+            }
+
+            $scope.orgChanged = function(org) {
+                $scope.selected = egOrg.get(org.id);
+            }
+
+            if (!$scope.selected) {
+                // apply a default value if none provided
+                $scope.selected = 
+                    egAuth.workstation() ?
+                    egOrg.get(egAuth.workstation().owning_lib()) :
+                    egOrg.tree();
+            }
+        }]
+    }
+})
+