]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/z3950/app.js
webstaff: Z39.50 - various improvements
[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     var provider = egGridDataProvider.instance({});
37
38     provider.get = function(offset, count) {
39         var deferred = $q.defer();
40
41         var query = egZ3950TargetSvc.currentQuery();
42         if (Object.keys(query.search).length == 0) {
43             return $q.when();
44         }
45
46         query['limit'] = count;
47         query['offset'] = offset;
48
49         var resultIndex = offset;
50         egCore.net.request(
51             'open-ils.search',
52             'open-ils.search.z3950.search_class',
53             egCore.auth.token(),
54             query
55         ).then(
56             function() { deferred.resolve() },
57             null, // onerror
58             function(result) {
59                 for (var i in result.records) {
60                     result.records[i].mvr['service'] = result.service;
61                     result.records[i].mvr['index'] = resultIndex++;
62                     result.records[i].mvr['marcxml'] = result.records[i].marcxml;
63                     deferred.notify(result.records[i].mvr);
64                 }
65             }
66         );
67
68         return deferred.promise;
69     };
70
71     $scope.z3950SearchGridProvider = provider;
72     $scope.gridControls = {};
73
74     $scope.search = function() {
75         $scope.z3950SearchGridProvider.refresh();
76     };
77     $scope.clearForm = function() {
78         egZ3950TargetSvc.clearSearchFields();
79     };
80
81     var display_form = true;
82     $scope.show_search_form = function() {
83         return display_form;
84     }
85     $scope.toggle_search_form = function() {
86         display_form = !display_form;
87     }
88
89     $scope.showInCatalog = function() {
90         var items = $scope.gridControls.selectedItems();
91         // relying on cant_showInCatalog to protect us
92         var url = egCore.env.basePath +
93                   'cat/catalog/record/' + items[0].tcn();
94         $timeout(function() { $window.open(url, '_blank') });        
95     };
96     $scope.cant_showInCatalog = function() {
97         var items = $scope.gridControls.selectedItems();
98         if (items.length != 1) return true;
99         if (items[0]['service'] == 'native-evergreen-catalog') return false;
100         return true;
101     };
102
103     $scope.import = function() {
104         var deferred = $q.defer();
105         var items = $scope.gridControls.selectedItems();
106         egCore.net.request(
107             'open-ils.cat',
108             'open-ils.cat.biblio.record.xml.import',
109             egCore.auth.token(),
110             items[0]['marcxml']
111             // FIXME and more
112         ).then(
113             function() { deferred.resolve() },
114             null, // onerror
115             function(result) {
116                 console.debug('imported');
117             }
118         );
119
120         return deferred.promise;
121     };
122     $scope.cant_import = function() {
123         var items = $scope.gridControls.selectedItems();
124         if (items.length == 1) return false;
125         return true;
126     };
127
128     $scope.spawn_editor = function() {
129         var items = $scope.gridControls.selectedItems();
130         $modal.open({
131             templateUrl: './cat/z3950/t_marc_edit',
132             size: 'lg',
133             controller:
134                 ['$scope', '$modalInstance', function($scope, $modalInstance) {
135 console.debug('calling modal controller');
136                 $scope.focusMe = true;
137                 $scope.record_id = 0;
138                 $scope.dirty_flag = false;
139                 $scope.marc_xml = items[0]['marcxml'];
140                 $scope.ok = function(args) { $modalInstance.close(args) }
141                 $scope.cancel = function () { $modalInstance.dismiss() }
142             }]
143         }).result.then(function (args) {
144             if (!args || !args.name) return;
145         });
146     }
147 }])