]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/z3950/app.js
webstaff: use localStorage for the overlay-record selection
[Evergreen.git] / Open-ILS / web / js / ui / default / staff / cat / z3950 / app.js
1 /*
2  * Z39.50 search and import
3  */
4
5 angular.module('egCatZ3950Search',
6     ['ngRoute', 'ui.bootstrap', 'egCoreMod', 'egUiMod', 'egGridMod', 'egZ3950Mod', 'egMarcMod'])
7
8 .config(function($routeProvider, $locationProvider, $compileProvider) {
9     $locationProvider.html5Mode(true);
10     $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|blob):/); // grid export
11
12     var resolver = {delay : function(egStartup) {return egStartup.go()}};
13
14     // search page shows the list view by default
15     $routeProvider.when('/cat/z3950/search', {
16         templateUrl: './cat/z3950/t_list',
17         controller: 'Z3950SearchCtrl',
18         resolve : resolver
19     });
20
21     // default page / bucket view
22     $routeProvider.otherwise({redirectTo : '/cat/z3950/search'});
23 })
24
25 /**
26  * List view - grid stuff
27  */
28 .controller('Z3950SearchCtrl',
29        ['$scope','$q','$location','$timeout','$window','egCore','egGridDataProvider','egZ3950TargetSvc','$modal',
30 function($scope , $q , $location , $timeout , $window,  egCore , egGridDataProvider,  egZ3950TargetSvc,  $modal) {
31
32     // get list of targets
33     egZ3950TargetSvc.loadTargets();
34     egZ3950TargetSvc.loadActiveSearchFields();
35
36     $scope.field_strip_groups = [];
37     egCore.startup.go().then(function() {
38         // and list of field strip groups; need to ensure
39         // that enough of the startup has happened so that
40         // we have the user WS
41         egCore.pcrud.search('vibtg',
42             {
43                 always_apply : 'f',
44                 owner : {
45                     'in' : {
46                         select : {
47                             aou : [{
48                                 column : 'id',
49                                 transform : 'actor.org_unit_ancestors',
50                                 result_field : 'id'
51                             }]
52                         },
53                         from : 'aou',
54                         where : {
55                             id : egCore.auth.user().ws_ou()
56                         }
57                     }
58                 }
59             },
60             { order_by : { vibtq : ['label'] } }
61         ).then(null, null, function(strip_group) {
62             strip_group.selected = false;
63             $scope.field_strip_groups.push(strip_group);
64         });
65     });
66
67     $scope.total_hits = 0;
68
69     var provider = egGridDataProvider.instance({});
70
71     provider.get = function(offset, count) {
72         var deferred = $q.defer();
73
74         var query = egZ3950TargetSvc.currentQuery();
75         if (!query.raw_search && Object.keys(query.search).length == 0) {
76             return $q.when();
77         }
78
79         var method = query.raw_search ?
80                        'open-ils.search.z3950.search_service' :
81                        'open-ils.search.z3950.search_class';
82
83         if (query.raw_search) {
84             query.query = query.raw_search;
85             delete query['search'];
86             delete query['raw_search'];
87             query.service = query.service[0];
88             query.username = query.username[0];
89             query.password = query.password[0];
90         }
91
92         query['limit'] = count;
93         query['offset'] = offset;
94
95         var resultIndex = offset;
96         $scope.total_hits = 0;
97         $scope.searchInProgress = true;
98         egCore.net.request(
99             'open-ils.search',
100             method,
101             egCore.auth.token(),
102             query
103         ).then(
104             function() { $scope.searchInProgress = false; deferred.resolve() },
105             null, // onerror
106             function(result) {
107                 // FIXME when the search offset is > 0, the
108                 // total hits count can be wrong if one of the
109                 // Z39.50 targets has fewer than $offset hits; in that
110                 // case, result.count is not supplied.
111                 $scope.total_hits += (result.count || 0);
112                 for (var i in result.records) {
113                     result.records[i].mvr['service'] = result.service;
114                     result.records[i].mvr['index'] = resultIndex++;
115                     result.records[i].mvr['marcxml'] = result.records[i].marcxml;
116                     deferred.notify(result.records[i].mvr);
117                 }
118             }
119         );
120
121         return deferred.promise;
122     };
123
124     $scope.z3950SearchGridProvider = provider;
125     $scope.gridControls = {};
126
127     $scope.search = function() {
128         $scope.z3950SearchGridProvider.refresh();
129     };
130     $scope.clearForm = function() {
131         egZ3950TargetSvc.clearSearchFields();
132     };
133
134     var display_form = true;
135     $scope.show_search_form = function() {
136         return display_form;
137     }
138     $scope.toggle_search_form = function() {
139         display_form = !display_form;
140     }
141
142     $scope.raw_search_impossible = function() {
143         return egZ3950TargetSvc.rawSearchImpossible();
144     }
145     $scope.showRawSearchForm = function() {
146         $modal.open({
147             templateUrl: './cat/z3950/t_raw_search',
148             size: 'md',
149             controller:
150                 ['$scope', '$modalInstance', function($scope, $modalInstance) {
151                 egZ3950TargetSvc.setRawSearch('');
152                 $scope.focusMe = true;
153                 $scope.ok = function(args) { $modalInstance.close(args) }
154                 $scope.cancel = function () { $modalInstance.dismiss() }
155             }]
156         }).result.then(function (args) {
157             if (!args || !args.raw_search) return;
158             $scope.clearForm();
159             egZ3950TargetSvc.setRawSearch(args.raw_search);
160             $scope.z3950SearchGridProvider.refresh();
161         });
162     }
163
164     $scope.showInCatalog = function() {
165         var items = $scope.gridControls.selectedItems();
166         // relying on cant_showInCatalog to protect us
167         var url = egCore.env.basePath +
168                   'cat/catalog/record/' + items[0].tcn();
169         $timeout(function() { $window.open(url, '_blank') });        
170     };
171     $scope.cant_showInCatalog = function() {
172         var items = $scope.gridControls.selectedItems();
173         if (items.length != 1) return true;
174         if (items[0]['service'] == 'native-evergreen-catalog') return false;
175         return true;
176     };
177
178     $scope.local_overlay_target = egCore.hatch.getLocalItem('eg.cat.marked_overlay_record') || 0;
179     $scope.mark_as_overlay_target = function() {
180         var items = $scope.gridControls.selectedItems();
181         if ($scope.local_overlay_target == items[0].tcn()) {
182             $scope.local_overlay_target = 0;
183         } else {
184             $scope.local_overlay_target = items[0].tcn();
185         }
186         egCore.hatch.setLocalItem('eg.cat.marked_overlay_record',$scope.local_overlay_target);
187     }
188     $scope.cant_overlay = function() {
189         if (!$scope.local_overlay_target) return true;
190         var items = $scope.gridControls.selectedItems();
191         if (items.length != 1) return true;
192         if (
193                 items[0]['service'] == 'native-evergreen-catalog' &&
194                 items[0].tcn() == $scope.local_overlay_target
195            ) return true;
196         return false;
197     }
198
199     $scope.selectFieldStripGroups = function() {
200         var groups = [];
201         angular.forEach($scope.field_strip_groups, function(grp, idx) {
202             if (grp.selected) {
203                 groups.push(grp.id());
204             }
205         });
206         return groups;
207     };
208     $scope.import = function() {
209         var deferred = $q.defer();
210         var items = $scope.gridControls.selectedItems();
211         egCore.net.request(
212             'open-ils.cat',
213             'open-ils.cat.biblio.record.xml.import',
214             egCore.auth.token(),
215             items[0]['marcxml'],
216             null, // FIXME bib source
217             null,
218             null,
219             $scope.selectFieldStripGroups()
220         ).then(
221             function() { deferred.resolve() },
222             null, // onerror
223             function(result) {
224                 console.debug('imported');
225             }
226         );
227
228         return deferred.promise;
229     };
230     $scope.need_one_selected = function() {
231         var items = $scope.gridControls.selectedItems();
232         if (items.length == 1) return false;
233         return true;
234     };
235
236     $scope.spawn_editor = function() {
237         var items = $scope.gridControls.selectedItems();
238         $modal.open({
239             templateUrl: './cat/z3950/t_marc_edit',
240             size: 'lg',
241             controller:
242                 ['$scope', '$modalInstance', function($scope, $modalInstance) {
243                 $scope.focusMe = true;
244                 $scope.record_id = 0;
245                 $scope.dirty_flag = false;
246                 $scope.marc_xml = items[0]['marcxml'];
247                 $scope.ok = function(args) { $modalInstance.close(args) }
248                 $scope.cancel = function () { $modalInstance.dismiss() }
249             }]
250         }).result.then(function (args) {
251             if (!args || !args.name) return;
252         });
253     }
254
255     $scope.view_marc = function() {
256         var items = $scope.gridControls.selectedItems();
257         $modal.open({
258             templateUrl: './cat/z3950/t_marc_html',
259             size: 'lg',
260             controller:
261                 ['$scope', '$modalInstance', function($scope, $modalInstance) {
262                 $scope.focusMe = true;
263                 $scope.marc_xml = items[0]['marcxml'];
264                 $scope.isbn = (items[0].isbn() || '').replace(/ .*/, '');
265                 $scope.ok = function(args) { $modalInstance.close(args) }
266                 $scope.cancel = function () { $modalInstance.dismiss() }
267             }]
268         }).result.then(function (args) {
269             if (!args || !args.name) return;
270         });
271     }
272
273     $scope.overlay_record = function() {
274         var items = $scope.gridControls.selectedItems();
275         var overlay_target = $scope.local_overlay_target;
276         var args = {
277             'marc_xml' : items[0]['marcxml']
278         };
279         $modal.open({
280             templateUrl: './cat/z3950/t_overlay',
281             size: 'lg',
282             controller:
283                 ['$scope', '$modalInstance', function($scope, $modalInstance) {
284                 $scope.focusMe = true;
285                 $scope.overlay_target = overlay_target;
286                 $scope.args = args;
287                 $scope.ok = function(args) { $modalInstance.close(args) };
288                 $scope.cancel = function () { $modalInstance.dismiss() };
289                 $scope.editOverlayRecord = function() {
290                     $modal.open({
291                         templateUrl: './cat/z3950/t_edit_overlay_record',
292                         size: 'lg',
293                         controller:
294                             ['$scope', '$modalInstance', function($scope, $modalInstance) {
295                             $scope.focusMe = true;
296                             $scope.record_id = 0;
297                             $scope.dirty_flag = false;
298                             $scope.args = args;
299                             $scope.ok = function(args) { $modalInstance.close(args) }
300                             $scope.cancel = function () { $modalInstance.dismiss() }
301                         }]
302                     }).result.then(function (args) {
303                         if (!args || !args.name) return;
304                     });
305                 };
306             }]
307         }).result.then(function (args) {
308             egCore.net.request(
309                 'open-ils.cat',
310                 'open-ils.cat.biblio.record.marc.replace',
311                 egCore.auth.token(),
312                 overlay_target,
313                 args.marc_xml,
314                 null, // FIXME bib source
315                 null,
316                 $scope.selectFieldStripGroups()
317             ).then(
318                 function(result) {
319                     console.debug('overlay complete');
320                 }
321             );            
322         });
323     }
324 }])