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