1c9f97ccc1fddd5acb5cb6442522f1b581aae2a3
[working/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','egCore','$uibModal','ngToast',
30                     'egOpChange',
31             function($scope , $window , $location , $timeout , hotkeys , egCore , $uibModal , ngToast,
32                     egOpChange) {
33
34                 function navTo(path) {                                           
35                     // Strip the leading "./" if any.
36                     path = path.replace(/^\.\//,'');
37                     var reg = new RegExp($location.path());
38                     $window.location.href = egCore.env.basePath + path;
39                 }       
40
41                 // adds a keyboard shortcut
42                 // http://chieffancypants.github.io/angular-hotkeys/
43                 $scope.addHotkey = function(key, path, desc, elm) {                 
44                     hotkeys.add({
45                         combo: key,
46                         allowIn: ['INPUT','SELECT','TEXTAREA'],
47                         description: desc,
48                         callback: function(e) {
49                             e.preventDefault();
50                             if (path) return navTo(path);
51                             return $timeout(function(){$(elm).trigger('click')});
52                         }
53                     });
54                 };
55
56                 $scope.retrieveLastRecord = function() {
57                     var last_record = egCore.hatch.getLocalItem("eg.cat.last_record_retrieved");
58                     if (last_record) {
59                         $window.location.href =
60                             egCore.env.basePath + 'cat/catalog/record/' + last_record;
61                     }
62                 }
63
64                 $scope.applyLocale = function(locale) {
65                     // EGWeb.pm can change the locale for us w/ the right param
66                     // Note: avoid using $location.search() to derive a new
67                     // URL, since it creates an intermediate path change.
68                     // Instead, use the ham-fisted approach of killing any
69                     // search args and applying the args we want.
70                     $window.location.href = 
71                         $window.location.href.replace(
72                             /(\?|\&).*/,
73                             '?set_eg_locale=' + encodeURIComponent(locale)
74                         );
75                 }
76
77                 $scope.changeOperatorUndo = function() {
78                     $scope.op_changed = egOpChange.changeOperatorUndo();
79                 }
80
81                 $scope.changeOperator = function() {
82                     $scope.op_changed = egOpChange.changeOperator(true);
83                 }
84
85                 $scope.currentToken = function () {
86                     return egCore.auth.token();
87                 }
88
89                 // tied to logout link
90                 $scope.logout = function() {
91                     egCore.auth.logout();
92                     return true;
93                 };
94
95                 egCore.startup.go().then(
96                     function() {
97                         if (egCore.auth.user()) {
98                             $scope.op_changed = egCore.auth.OCtoken() ? true : false;
99                             $scope.username = egCore.auth.user().usrname();
100                             $scope.workstation = egCore.auth.workstation();
101                         }
102                     }
103                 );
104             }
105         ]
106     }
107 });
108