LP1806087 Org setting to enable experimental staff catalog
[Evergreen.git] / Open-ILS / web / js / ui / default / staff / services / navbar.js
1 angular.module('egCoreMod')
2
3 .directive('egNavbar', function() {
4     return {
5         restrict : 'AE',
6         transclude : true,
7         templateUrl : 'eg-navbar-template',
8         controller:['$scope','$window','$location','$timeout','hotkeys','$rootScope',
9                     'egCore','$uibModal','ngToast','egOpChange','$element','egLovefield',
10             function($scope , $window , $location , $timeout , hotkeys , $rootScope ,
11                      egCore , $uibModal , ngToast , egOpChange , $element , egLovefield) {
12
13                 $scope.rs = $rootScope;
14
15                 $scope.reprintLast = function (e) {
16                     egCore.print.reprintLast();
17                     return e.preventDefault();
18                 }
19
20                 function navTo(path) {
21                     path = path.replace(/^\.\//,'');
22                     $window.location.href = egCore.env.basePath + path;
23                 }       
24
25                 // adds a keyboard shortcut
26                 // http://chieffancypants.github.io/angular-hotkeys/
27                 $scope.addHotkey = function(key, path, desc, elm) {
28                     angular.forEach(key.split(' '), function (k) {
29                         hotkeys.add({
30                             combo: k,
31                             allowIn: ['INPUT','SELECT','TEXTAREA'],
32                             description: desc,
33                             callback: function(e) {
34                                 e.preventDefault();
35                                 if (path) return navTo(path);
36                                 return $timeout(function(){$(elm).trigger('click')});
37                             }
38                         });
39                     });
40                 };
41
42                 function find_accesskeys(elm) {
43                     elm = angular.element(elm);
44                     if (elm.attr('eg-accesskey')) {
45                         $scope.addHotkey(
46                             elm.attr('eg-accesskey'),
47                             elm.attr('href'),
48                             elm.attr('eg-accesskey-desc'),
49                             elm
50                         );
51                     }
52                     angular.forEach(elm.children(), find_accesskeys);
53                 }
54
55                 $scope.retrieveLastRecord = function() {
56                     var last_record = egCore.hatch.getLocalItem("eg.cat.last_record_retrieved");
57                     if (last_record) {
58                         $window.location.href =
59                             egCore.env.basePath + 'cat/catalog/record/' + last_record;
60                     }
61                 }
62
63                 $scope.applyLocale = function(locale) {
64                     // EGWeb.pm can change the locale for us w/ the right param
65                     // Note: avoid using $location.search() to derive a new
66                     // URL, since it creates an intermediate path change.
67                     // Instead, use the ham-fisted approach of killing any
68                     // search args and applying the args we want.
69                     $window.location.href = 
70                         $window.location.href.replace(
71                             /(\?|\&).*/,
72                             '?set_eg_locale=' + encodeURIComponent(locale)
73                         );
74                 }
75
76                 $scope.changeOperatorUndo = function() {
77                     egOpChange.changeOperatorUndo().then(function() {
78                         $scope.op_changed = false;
79                         $scope.username = egCore.auth.user().usrname();
80                     });
81                 }
82
83                 $scope.changeOperator = function() {
84                     egOpChange.changeOperator().then(function() {
85                         $scope.op_changed = egCore.auth.OCtoken() ? true : false;
86                         $scope.username = egCore.auth.user().usrname();
87                     });
88                 }
89
90                 $scope.currentToken = function () {
91                     return egCore.auth.token();
92                 }
93
94                 // Returns true if the browser is connected to Hatch
95                 $scope.hatchConnected = function() {
96                     return egCore.hatch.hatchAvailable;
97                 }
98
99                 // tied to logout link
100                 $scope.logout = function() {
101                     egCore.auth.logout();
102                     return true;
103                 };
104
105                 $scope.offlineDisabled = function() {
106                     return egLovefield.cannotConnect;
107                 }
108
109                 egCore.startup.go().then(
110                     function() {
111                         if (egCore.auth.user()) {
112                             $scope.op_changed = egCore.auth.OCtoken() ? true : false;
113                             $scope.username = egCore.auth.user().usrname();
114                             $scope.workstation = egCore.auth.workstation();
115
116                             egCore.org.settings([
117                                 'ui.staff.max_recent_patrons',
118                                 'ui.staff.angular_catalog.enabled'
119                             ]).then(function(s) {
120                                 var val = s['ui.staff.max_recent_patrons'];
121                                 $scope.showRecentPatron = val > 0;
122                                 $scope.showRecentPatrons = val > 1;
123
124                                 $scope.showAngularCatalog = 
125                                     s['ui.staff.angular_catalog.enabled'];
126                             });
127                         }
128                         // need to defer initialization of hotkeys to this point
129                         // as some of them are conditional on whether one is logged in
130                         // or is working in offline circulation mode
131                         $timeout(function(){find_accesskeys($element)});
132                     }
133                 );
134             }
135         ]
136     }
137 });
138