]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/catalog/app.js
webstaff: Toward dirty data flag and better prev/next/start/end buttons
[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', 'egMarcMod'])
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     $routeProvider.when('/cat/catalog/retrieve_by_id', {
26         templateUrl: './cat/catalog/t_retrieve_by_id',
27         controller: 'CatalogRecordRetrieve',
28         resolve : resolver
29     });
30
31     $routeProvider.when('/cat/catalog/retrieve_by_tcn', {
32         templateUrl: './cat/catalog/t_retrieve_by_tcn',
33         controller: 'CatalogRecordRetrieve',
34         resolve : resolver
35     });
36
37     // create some catalog page-specific mappings
38     $routeProvider.when('/cat/catalog/record/:record_id', {
39         templateUrl: './cat/catalog/t_catalog',
40         controller: 'CatalogCtrl',
41         resolve : resolver
42     });
43
44     // create some catalog page-specific mappings
45     $routeProvider.when('/cat/catalog/record/:record_id/:record_tab', {
46         templateUrl: './cat/catalog/t_catalog',
47         controller: 'CatalogCtrl',
48         resolve : resolver
49     });
50
51     $routeProvider.otherwise({redirectTo : '/cat/catalog/index'});
52 })
53
54
55 /**
56  * */
57 .controller('CatalogRecordRetrieve',
58        ['$scope','$routeParams','$location','$q','egCore',
59 function($scope , $routeParams , $location , $q , egCore ) {
60
61     $scope.focusMe = true;
62
63     // jump to the patron checkout UI
64     function loadRecord(record_id) {
65         $location
66         .path('/cat/catalog/record/' + record_id);
67     }
68
69     $scope.submitId = function(args) {
70         $scope.recordNotFound = null;
71         if (!args.record_id) return;
72
73         // blur so next time it's set to true it will re-apply select()
74         $scope.selectMe = false;
75
76         return loadRecord(args.record_id);
77     }
78
79     $scope.submitTCN = function(args) {
80         $scope.recordNotFound = null;
81         $scope.moreRecordsFound = null;
82         if (!args.record_tcn) return;
83
84         // blur so next time it's set to true it will re-apply select()
85         $scope.selectMe = false;
86
87         // lookup TCN
88         egCore.net.request(
89             'open-ils.search',
90             'open-ils.search.biblio.tcn',
91             args.record_tcn)
92
93         .then(function(resp) { // get_barcodes
94
95             if (evt = egCore.evt.parse(resp)) {
96                 alert(evt); // FIXME
97                 return;
98             }
99
100             if (!resp.count) {
101                 $scope.recordNotFound = args.record_tcn;
102                 $scope.selectMe = true;
103                 return;
104             }
105
106             if (resp.count > 1) {
107                 $scope.moreRecordsFound = args.record_tcn;
108                 $scope.selectMe = true;
109                 return;
110             }
111
112             var record_id = resp.ids[0];
113             return loadRecord(record_id);
114         });
115     }
116
117 }])
118
119 .controller('CatalogCtrl',
120        ['$scope','$routeParams','$location','$q','egCore','egHolds',
121         'egGridDataProvider','egHoldGridActions',
122 function($scope , $routeParams , $location , $q , egCore , egHolds, 
123          egGridDataProvider , egHoldGridActions) {
124
125     // set record ID on page load if available...
126     $scope.record_id = $routeParams.record_id;
127
128     if ($routeParams.record_id) $scope.from_route = true;
129     else $scope.from_route = false;
130
131     // will hold a ref to the opac iframe
132     $scope.opac_iframe = null;
133     $scope.opac_call = function (opac_frame_function, force_opac_tab) {
134         if ($scope.opac_iframe) {
135             if (force_opac_tab) $scope.record_tab = 'catalog';
136             $scope.opac_iframe.contentWindow[opac_frame_function]();
137         }
138     }
139
140     $scope.stop_unload = false;
141     $scope.$watch('stop_unload',
142         function(newVal, oldVal) {
143             if (newVal && newVal != oldVal && $scope.opac_iframe) {
144                 $($scope.opac_iframe.contentWindow).on('beforeunload', function(){
145                     return 'There is unsaved data in this record.'
146                 });
147             } else {
148                 $($scope.opac_iframe.contentWindow).off('beforeunload');
149             }
150         }
151     );
152
153     // Set the "last bib" cookie, if we have that
154     if ($scope.record_id)
155         egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
156
157     // also set it when the iframe changes to a new record
158     $scope.handle_page = function(url) {
159
160         if (!url || url == 'about:blank') {
161             // nothing loaded.  If we already have a record ID, leave it.
162             return;
163         }
164
165         var match = url.match(/\/+opac\/+record\/+(\d+)/);
166         if (match) {
167             $scope.record_id = match[1];
168             egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
169
170             // force the record_id to show up in the page.  
171             // not sure why a $digest isn't occuring here.
172             try { $scope.$apply() } catch(E) {}
173         } else {
174             delete $scope.record_id;
175         }
176
177         if ($scope.record_id) {
178             var default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
179             tab = $routeParams.record_tab || default_tab || 'catalog';
180         } else {
181             tab = $routeParams.record_tab || 'catalog';
182         }
183         $scope.set_record_tab(tab);
184     }
185
186     // xulG catalog handlers
187     $scope.handlers = { }
188
189     // ------------------------------------------------------------------
190     // Holds 
191     var provider = egGridDataProvider.instance({});
192     $scope.hold_grid_data_provider = provider;
193     $scope.grid_actions = egHoldGridActions;
194     $scope.grid_actions.refresh = function () { provider.refresh() };
195     $scope.hold_grid_controls = {};
196
197     var hold_ids = []; // current list of holds
198     function fetchHolds(offset, count) {
199         var ids = hold_ids.slice(offset, offset + count);
200         return egHolds.fetch_holds(ids).then(null, null,
201             function(hold_data) { 
202                 return hold_data;
203             }
204         );
205     }
206
207     provider.get = function(offset, count) {
208         if ($scope.record_tab != 'holds') return $q.when();
209         var deferred = $q.defer();
210         hold_ids = []; // no caching ATM
211
212         // fetch the IDs
213         egCore.net.request(
214             'open-ils.circ',
215             'open-ils.circ.holds.retrieve_all_from_title',
216             egCore.auth.token(), $scope.record_id, 
217             {pickup_lib : egCore.org.descendants($scope.pickup_ou.id(), true)}
218         ).then(
219             function(hold_data) {
220                 angular.forEach(hold_data, function(list, type) {
221                     hold_ids = hold_ids.concat(list);
222                 });
223                 fetchHolds(offset, count).then(
224                     deferred.resolve, null, deferred.notify);
225             }
226         );
227
228         return deferred.promise;
229     }
230
231     $scope.detail_view = function(action, user_data, items) {
232         if (h = items[0]) {
233             $scope.detail_hold_id = h.hold.id();
234         }
235     }
236
237     $scope.list_view = function(items) {
238          $scope.detail_hold_id = null;
239     }
240
241     // refresh the list of record holds when the pickup lib is changed.
242     $scope.pickup_ou = egCore.org.get(egCore.auth.user().ws_ou());
243     $scope.pickup_ou_changed = function(org) {
244         $scope.pickup_ou = org;
245         provider.refresh();
246     }
247
248     $scope.print_holds = function() {
249         var holds = [];
250         angular.forEach($scope.hold_grid_controls.allItems(), function(item) {
251             holds.push({
252                 hold : egCore.idl.toHash(item.hold),
253                 patron_last : item.patron_last,
254                 patron_alias : item.patron_alias,
255                 patron_barcode : item.patron_barcode,
256                 copy : egCore.idl.toHash(item.copy),
257                 volume : egCore.idl.toHash(item.volume),
258                 title : item.mvr.title(),
259                 author : item.mvr.author()
260             });
261         });
262
263         egCore.print.print({
264             context : 'receipt', 
265             template : 'holds_for_bib', 
266             scope : {holds : holds}
267         });
268     }
269
270     $scope.mark_hold_transfer_dest = function() {
271         egCore.hatch.setLocalItem(
272             'eg.circ.hold.title_transfer_target', $scope.record_id);
273     }
274
275     // UI presents this option as "all holds"
276     $scope.transfer_holds_to_marked = function() {
277         var hold_ids = $scope.hold_grid_controls.allItems().map(
278             function(hold_data) {return hold_data.hold.id()});
279         egHolds.transfer_to_marked_title(hold_ids);
280     }
281
282     // ------------------------------------------------------------------
283     // Initialize the selected tab
284
285     function init_cat_url() {
286         // Set the initial catalog URL.  This only happens once.
287         // The URL is otherwise generated through user navigation.
288         if ($scope.catalog_url) return; 
289
290         var url = $location.absUrl().replace(/\/staff.*/, '/opac/advanced');
291
292         // A record ID in the path indicates a request for the record-
293         // specific page.
294         if ($routeParams.record_id) {
295             url = url.replace(/advanced/, '/record/' + $scope.record_id);
296         }
297
298         $scope.catalog_url = url;
299     }
300
301     $scope.set_record_tab = function(tab) {
302         $scope.record_tab = tab;
303
304         switch(tab) {
305
306             case 'catalog':
307                 init_cat_url();
308                 break;
309
310             case 'holds':
311                 $scope.detail_hold_record_id = $scope.record_id; 
312                 // refresh the holds grid
313                 provider.refresh();
314                 break;
315         }
316     }
317
318     $scope.set_default_record_tab = function() {
319         egCore.hatch.setLocalItem(
320             'eg.cat.default_record_tab', $scope.record_tab);
321     }
322
323     var tab;
324     if ($scope.record_id) {
325         var default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
326         tab = $routeParams.record_tab || default_tab || 'catalog';
327     } else {
328         tab = $routeParams.record_tab || 'catalog';
329     }
330     $scope.set_record_tab(tab);
331
332 }])
333