]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/catalog/app.js
webstaff: Do not change the tab on iframe changes when using the summary bar 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.in_opac_call = false;
134     $scope.opac_call = function (opac_frame_function, force_opac_tab) {
135         if ($scope.opac_iframe) {
136             if (force_opac_tab) $scope.record_tab = 'catalog';
137             $scope.in_opac_call = true;
138             $scope.opac_iframe.contentWindow[opac_frame_function]();
139         }
140     }
141
142     $scope.stop_unload = false;
143     $scope.$watch('stop_unload',
144         function(newVal, oldVal) {
145             if (newVal && newVal != oldVal && $scope.opac_iframe) {
146                 $($scope.opac_iframe.contentWindow).on('beforeunload', function(){
147                     return 'There is unsaved data in this record.'
148                 });
149             } else {
150                 $($scope.opac_iframe.contentWindow).off('beforeunload');
151             }
152         }
153     );
154
155     // Set the "last bib" cookie, if we have that
156     if ($scope.record_id)
157         egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
158
159     // also set it when the iframe changes to a new record
160     $scope.handle_page = function(url) {
161
162         if (!url || url == 'about:blank') {
163             // nothing loaded.  If we already have a record ID, leave it.
164             return;
165         }
166
167         var match = url.match(/\/+opac\/+record\/+(\d+)/);
168         if (match) {
169             $scope.record_id = match[1];
170             egCore.hatch.setLocalItem("eg.cat.last_record_retrieved", $scope.record_id);
171         } else {
172             delete $scope.record_id;
173         }
174
175         // child scope is executing this function, so our digest doesn't fire ... thus,
176         $scope.$apply();
177
178         if (!$scope.in_opac_call) {
179             if ($scope.record_id) {
180                 var default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
181                 tab = $routeParams.record_tab || default_tab || 'catalog';
182             } else {
183                 tab = $routeParams.record_tab || 'catalog';
184             }
185             $scope.set_record_tab(tab);
186         } else {
187             $scope.in_opac_call = false;
188         }
189     }
190
191     // xulG catalog handlers
192     $scope.handlers = { }
193
194     // ------------------------------------------------------------------
195     // Holds 
196     var provider = egGridDataProvider.instance({});
197     $scope.hold_grid_data_provider = provider;
198     $scope.grid_actions = egHoldGridActions;
199     $scope.grid_actions.refresh = function () { provider.refresh() };
200     $scope.hold_grid_controls = {};
201
202     var hold_ids = []; // current list of holds
203     function fetchHolds(offset, count) {
204         var ids = hold_ids.slice(offset, offset + count);
205         return egHolds.fetch_holds(ids).then(null, null,
206             function(hold_data) { 
207                 return hold_data;
208             }
209         );
210     }
211
212     provider.get = function(offset, count) {
213         if ($scope.record_tab != 'holds') return $q.when();
214         var deferred = $q.defer();
215         hold_ids = []; // no caching ATM
216
217         // fetch the IDs
218         egCore.net.request(
219             'open-ils.circ',
220             'open-ils.circ.holds.retrieve_all_from_title',
221             egCore.auth.token(), $scope.record_id, 
222             {pickup_lib : egCore.org.descendants($scope.pickup_ou.id(), true)}
223         ).then(
224             function(hold_data) {
225                 angular.forEach(hold_data, function(list, type) {
226                     hold_ids = hold_ids.concat(list);
227                 });
228                 fetchHolds(offset, count).then(
229                     deferred.resolve, null, deferred.notify);
230             }
231         );
232
233         return deferred.promise;
234     }
235
236     $scope.detail_view = function(action, user_data, items) {
237         if (h = items[0]) {
238             $scope.detail_hold_id = h.hold.id();
239         }
240     }
241
242     $scope.list_view = function(items) {
243          $scope.detail_hold_id = null;
244     }
245
246     // refresh the list of record holds when the pickup lib is changed.
247     $scope.pickup_ou = egCore.org.get(egCore.auth.user().ws_ou());
248     $scope.pickup_ou_changed = function(org) {
249         $scope.pickup_ou = org;
250         provider.refresh();
251     }
252
253     $scope.print_holds = function() {
254         var holds = [];
255         angular.forEach($scope.hold_grid_controls.allItems(), function(item) {
256             holds.push({
257                 hold : egCore.idl.toHash(item.hold),
258                 patron_last : item.patron_last,
259                 patron_alias : item.patron_alias,
260                 patron_barcode : item.patron_barcode,
261                 copy : egCore.idl.toHash(item.copy),
262                 volume : egCore.idl.toHash(item.volume),
263                 title : item.mvr.title(),
264                 author : item.mvr.author()
265             });
266         });
267
268         egCore.print.print({
269             context : 'receipt', 
270             template : 'holds_for_bib', 
271             scope : {holds : holds}
272         });
273     }
274
275     $scope.mark_hold_transfer_dest = function() {
276         egCore.hatch.setLocalItem(
277             'eg.circ.hold.title_transfer_target', $scope.record_id);
278     }
279
280     // UI presents this option as "all holds"
281     $scope.transfer_holds_to_marked = function() {
282         var hold_ids = $scope.hold_grid_controls.allItems().map(
283             function(hold_data) {return hold_data.hold.id()});
284         egHolds.transfer_to_marked_title(hold_ids);
285     }
286
287     // ------------------------------------------------------------------
288     // Initialize the selected tab
289
290     function init_cat_url() {
291         // Set the initial catalog URL.  This only happens once.
292         // The URL is otherwise generated through user navigation.
293         if ($scope.catalog_url) return; 
294
295         var url = $location.absUrl().replace(/\/staff.*/, '/opac/advanced');
296
297         // A record ID in the path indicates a request for the record-
298         // specific page.
299         if ($routeParams.record_id) {
300             url = url.replace(/advanced/, '/record/' + $scope.record_id);
301         }
302
303         $scope.catalog_url = url;
304     }
305
306     $scope.set_record_tab = function(tab) {
307         $scope.record_tab = tab;
308
309         switch(tab) {
310
311             case 'catalog':
312                 init_cat_url();
313                 break;
314
315             case 'holds':
316                 $scope.detail_hold_record_id = $scope.record_id; 
317                 // refresh the holds grid
318                 provider.refresh();
319                 break;
320         }
321     }
322
323     $scope.set_default_record_tab = function() {
324         egCore.hatch.setLocalItem(
325             'eg.cat.default_record_tab', $scope.record_tab);
326     }
327
328     var tab;
329     if ($scope.record_id) {
330         var default_tab = egCore.hatch.getLocalItem( 'eg.cat.default_record_tab' );
331         tab = $routeParams.record_tab || default_tab || 'catalog';
332     } else {
333         tab = $routeParams.record_tab || 'catalog';
334     }
335     $scope.set_record_tab(tab);
336
337 }])
338