]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/catalog/app.js
LP#1350042 Browser client templates/scripts (phase 1)
[Evergreen.git] / Open-ILS / web / js / ui / default / staff / cat / catalog / app.js
1 /**
2  * TPAC Frame App
3  *
4  * currently, this app doesn't use routes for each sub-ui, because 
5  * reloading the catalog each time is sloooow.  better so far to 
6  * swap out divs w/ ng-if / ng-show / ng-hide as needed.
7  *
8  */
9
10 angular.module('egCatalogApp', ['ui.bootstrap','ngRoute','egCoreMod','egGridMod'])
11
12 .config(function($routeProvider, $locationProvider, $compileProvider) {
13     $locationProvider.html5Mode(true);
14     $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|blob):/); // grid export
15
16     var resolver = {delay : 
17         ['egStartup', function(egStartup) {return egStartup.go()}]}
18
19     $routeProvider.when('/cat/catalog/index', {
20         templateUrl: './cat/catalog/t_catalog',
21         controller: 'CatalogCtrl',
22         resolve : resolver
23     });
24
25     // create some catalog page-specific mappings
26     $routeProvider.when('/cat/catalog/record/:record_id', {
27         templateUrl: './cat/catalog/t_catalog',
28         controller: 'CatalogCtrl',
29         resolve : resolver
30     });
31
32     // create some catalog page-specific mappings
33     $routeProvider.when('/cat/catalog/record/:record_id/:record_tab', {
34         templateUrl: './cat/catalog/t_catalog',
35         controller: 'CatalogCtrl',
36         resolve : resolver
37     });
38
39     $routeProvider.otherwise({redirectTo : '/cat/catalog/index'});
40 })
41
42
43 /**
44  * */
45 .controller('CatalogCtrl',
46        ['$scope','$routeParams','$location','$q','egCore','egHolds',
47         'egGridDataProvider','egHoldGridActions',
48 function($scope , $routeParams , $location , $q , egCore , egHolds, 
49          egGridDataProvider , egHoldGridActions) {
50
51     // set record ID on page load if available...
52     $scope.record_id = $routeParams.record_id;
53
54     // also set it when the iframe changes to a new record
55     $scope.handle_page = function(url) {
56
57         if (!url || url == 'about:blank') {
58             // nothing loaded.  If we already have a record ID, leave it.
59             return;
60         }
61
62         var match = url.match(/\/+opac\/+record\/+(\d+)/);
63         if (match) {
64             $scope.record_id = match[1];
65
66             // force the record_id to show up in the page.  
67             // not sure why a $digest isn't occuring here.
68             try { $scope.$apply() } catch(E) {}
69         } else {
70             delete $scope.record_id;
71         }
72     }
73
74     // xulG catalog handlers
75     $scope.handlers = { }
76
77     // ------------------------------------------------------------------
78     // Holds 
79     var provider = egGridDataProvider.instance({});
80     $scope.hold_grid_data_provider = provider;
81     $scope.grid_actions = egHoldGridActions;
82     $scope.hold_grid_controls = {};
83
84     var hold_ids = []; // current list of holds
85     function fetchHolds(offset, count) {
86         var ids = hold_ids.slice(offset, offset + count);
87         return egHolds.fetch_holds(ids).then(null, null,
88             function(hold_data) { 
89                 return hold_data;
90             }
91         );
92     }
93
94     provider.get = function(offset, count) {
95         if ($scope.record_tab != 'holds') return $q.when();
96         var deferred = $q.defer();
97         hold_ids = []; // no caching ATM
98
99         // fetch the IDs
100         egCore.net.request(
101             'open-ils.circ',
102             'open-ils.circ.holds.retrieve_all_from_title',
103             egCore.auth.token(), $scope.record_id, 
104             {pickup_lib : $scope.pickup_ou.id()}
105         ).then(
106             function(hold_data) {
107                 angular.forEach(hold_data, function(list, type) {
108                     hold_ids = hold_ids.concat(list);
109                 });
110                 fetchHolds(offset, count).then(
111                     deferred.resolve, null, deferred.notify);
112             }
113         );
114
115         return deferred.promise;
116     }
117
118     $scope.detail_view = function(action, user_data, items) {
119         if (h = items[0]) {
120             $scope.detail_hold_id = h.hold.id();
121         }
122     }
123
124     $scope.list_view = function(items) {
125          $scope.detail_hold_id = null;
126     }
127
128     // refresh the list of record holds when the pickup lib is changed.
129     $scope.pickup_ou = egCore.org.get(egCore.auth.user().ws_ou());
130     $scope.pickup_ou_changed = function(org) {
131         $scope.pickup_ou = org;
132         provider.refresh();
133     }
134
135     $scope.print_holds = function() {
136         var holds = [];
137         angular.forEach($scope.hold_grid_controls.allItems(), function(item) {
138             holds.push({
139                 hold : egCore.idl.toHash(item.hold),
140                 patron_last : item.patron_last,
141                 patron_alias : item.patron_alias,
142                 patron_barcode : item.patron_barcode,
143                 copy : egCore.idl.toHash(item.copy),
144                 volume : egCore.idl.toHash(item.volume),
145                 title : item.mvr.title(),
146                 author : item.mvr.author()
147             });
148         });
149
150         egCore.print.print({
151             context : 'receipt', 
152             template : 'holds_for_bib', 
153             scope : {holds : holds}
154         });
155     }
156
157     $scope.mark_hold_transfer_dest = function() {
158         egCore.hatch.setLocalItem(
159             'eg.circ.hold.title_transfer_target', $scope.record_id);
160     }
161
162     // UI presents this option as "all holds"
163     $scope.transfer_holds_to_marked = function() {
164         var hold_ids = $scope.hold_grid_controls.allItems().map(
165             function(hold_data) {return hold_data.hold.id()});
166         egHolds.transfer_to_marked_title(hold_ids);
167     }
168
169     // ------------------------------------------------------------------
170     // Initialize the selected tab
171
172     function init_cat_url() {
173         // Set the initial catalog URL.  This only happens once.
174         // The URL is otherwise generated through user navigation.
175         if ($scope.catalog_url) return; 
176
177         var url = $location.absUrl().replace(/\/staff.*/, '/opac/advanced');
178
179         // A record ID in the path indicates a request for the record-
180         // specific page.
181         if ($routeParams.record_id) {
182             url = url.replace(/advanced/, '/record/' + $scope.record_id);
183         }
184
185         $scope.catalog_url = url;
186     }
187
188     $scope.set_record_tab = function(tab) {
189         $scope.record_tab = tab;
190
191         switch(tab) {
192
193             case 'catalog':
194                 init_cat_url();
195                 break;
196
197             case 'holds':
198                 $scope.detail_hold_record_id = $scope.record_id; 
199                 // refresh the holds grid
200                 provider.refresh();
201                 break;
202         }
203     }
204
205     var tab = $routeParams.record_tab || 'catalog';
206     $scope.set_record_tab(tab);
207
208 }])
209