]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/services/navbar.js
c7bb4edfa91d5b56678cbdc077486783fb38d384
[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         link : function(scope, element, attrs) {
9
10             // Find all eg-accesskey entries within the menu and attach
11             // hotkey handlers for each.  
12             // jqlite doesn't support selectors, so we have to 
13             // manually navigate to the elements we're interested in.
14             function inspect(elm) {
15                 elm = angular.element(elm);
16                 if (elm.attr('eg-accesskey')) {
17                     scope.addHotkey(
18                         elm.attr('eg-accesskey'),
19                         elm.attr('href'),
20                         elm.attr('eg-accesskey-desc'),
21                         elm
22                     );
23                 }
24                 angular.forEach(elm.children(), inspect);
25             }
26             inspect(element);
27         },
28
29         controller:['$scope','$window','$location','$timeout','hotkeys',
30                     'egCore','$uibModal','ngToast','egOpChange',
31             function($scope , $window , $location , $timeout , hotkeys ,
32                      egCore , $uibModal , ngToast, egOpChange) {
33
34                 $scope.reprintLast = function (e) {
35                     egCore.print.reprintLast();
36                     return e.preventDefault();
37                 }
38
39                 function navTo(path) {                                           
40                     // Strip the leading "./" if any.
41                     path = path.replace(/^\.\//,'');
42                     var reg = new RegExp($location.path());
43                     $window.location.href = egCore.env.basePath + path;
44                 }       
45
46                 // adds a keyboard shortcut
47                 // http://chieffancypants.github.io/angular-hotkeys/
48                 $scope.addHotkey = function(key, path, desc, elm) {                 
49                     angular.forEach(key.split(' '), function (k) {
50                         hotkeys.add({
51                             combo: k,
52                             allowIn: ['INPUT','SELECT','TEXTAREA'],
53                             description: desc,
54                             callback: function(e) {
55                                 e.preventDefault();
56                                 if (path) return navTo(path);
57                                 return $timeout(function(){$(elm).trigger('click')});
58                             }
59                         });
60                     });
61                 };
62
63                 $scope.retrieveLastRecord = function() {
64                     var last_record = egCore.hatch.getLocalItem("eg.cat.last_record_retrieved");
65                     if (last_record) {
66                         $window.location.href =
67                             egCore.env.basePath + 'cat/catalog/record/' + last_record;
68                     }
69                 }
70
71                 $scope.applyLocale = function(locale) {
72                     // EGWeb.pm can change the locale for us w/ the right param
73                     // Note: avoid using $location.search() to derive a new
74                     // URL, since it creates an intermediate path change.
75                     // Instead, use the ham-fisted approach of killing any
76                     // search args and applying the args we want.
77                     $window.location.href = 
78                         $window.location.href.replace(
79                             /(\?|\&).*/,
80                             '?set_eg_locale=' + encodeURIComponent(locale)
81                         );
82                 }
83
84                 $scope.changeOperatorUndo = function() {
85                     egOpChange.changeOperatorUndo().then(function() {
86                         $scope.op_changed = false;
87                         $scope.username = egCore.auth.user().usrname();
88                     });
89                 }
90
91                 $scope.changeOperator = function() {
92                     egOpChange.changeOperator().then(function() {
93                         $scope.op_changed = egCore.auth.OCtoken() ? true : false;
94                         $scope.username = egCore.auth.user().usrname();
95                     });
96                 }
97
98                 $scope.currentToken = function () {
99                     return egCore.auth.token();
100                 }
101
102                 // tied to logout link
103                 $scope.logout = function() {
104                     egCore.auth.logout();
105                     return true;
106                 };
107
108                 egCore.startup.go().then(
109                     function() {
110                         if (egCore.auth.user()) {
111                             $scope.op_changed = egCore.auth.OCtoken() ? true : false;
112                             $scope.username = egCore.auth.user().usrname();
113                             $scope.workstation = egCore.auth.workstation();
114                         }
115                     }
116                 );
117             }
118         ]
119     }
120 });
121