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