]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/services/navbar.js
LP#1402797 Implement "Retrieve Last Bib Record" using localStorage record cache
[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                     );
22                 }
23                 angular.forEach(elm.children(), inspect);
24             }
25             inspect(element);
26         },
27
28         controller:['$scope','$window','$location','hotkeys','egCore',
29             function($scope , $window , $location , hotkeys , egCore) {
30
31                 function navTo(path) {                                           
32                     // $location.path() does not want a leading ".",
33                     // which <a>'s will have.  
34                     // Note: avoid using $location.path() to derive the new
35                     // URL, since it creates an intermediate path change.
36                     path = path.replace(/^\./,'');
37                     var reg = new RegExp($location.path());
38                     $window.location.href = 
39                         $window.location.href.replace(reg, path);
40                 }       
41
42                 // adds a keyboard shortcut
43                 // http://chieffancypants.github.io/angular-hotkeys/
44                 $scope.addHotkey = function(key, path, desc) {                 
45                     hotkeys.add(key, desc, function() { navTo(path) });
46                 };
47
48                 $scope.retrieveLastRecord = function() {
49                     var last_record = egCore.hatch.getLocalItem("eg.cat.last_record_retrieved");
50                     if (last_record) {
51                         var reg = new RegExp($location.path());
52                         $window.location.href =
53                             $window.location.href + 'cat/catalog/record/' + last_record;
54                     }
55                 }
56
57                 $scope.applyLocale = function(locale) {
58                     // EGWeb.pm can change the locale for us w/ the right param
59                     // Note: avoid using $location.search() to derive a new
60                     // URL, since it creates an intermediate path change.
61                     // Instead, use the ham-fisted approach of killing any
62                     // search args and applying the args we want.
63                     $window.location.href = 
64                         $window.location.href.replace(
65                             /(\?|\&).*/,
66                             '?set_eg_locale=' + encodeURIComponent(locale)
67                         );
68                 }
69
70                 // tied to logout link
71                 $scope.logout = function() {
72                     egCore.auth.logout();
73                     return true;
74                 };
75
76                 egCore.startup.go().then(
77                     function() {
78                         if (egCore.auth.user()) {
79                             $scope.username = egCore.auth.user().usrname();
80                             $scope.workstation = egCore.auth.workstation();
81                         }
82                     }
83                 );
84             }
85         ]
86     }
87 });
88