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