webstaff: Z39.50 search and import interface
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / cat / services / z3950.js
1 angular.module('egZ3950Mod', ['egCoreMod', 'ui.bootstrap'])
2 .factory('egZ3950TargetSvc',
3        ['$q', 'egCore', 'egAuth',
4 function($q,   egCore,   egAuth) {
5     
6     var service = {
7         targets : [ ],
8         searchFields : { }
9     };
10     
11     service.loadTargets = function() {
12         egCore.net.request(
13             'open-ils.search',
14             'open-ils.search.z3950.retrieve_services',
15             egAuth.token()
16         ).then(function(res) {
17             // keep the reference, just clear the list
18             service.targets.length = 0;
19             // native Evergreen search goes first
20             var localTarget = res['native-evergreen-catalog'];
21             delete res['native-evergreen-catalog'];
22             angular.forEach(res, function(value, key) {
23                 this.push({
24                     code:       key,
25                     settings:   value,
26                     selected:   false,
27                     username:   '',
28                     password:   ''
29                 });
30             }, service.targets);
31             service.targets.sort(function (a, b) {
32                 a = a.settings.label;
33                 b = b.settings.label;
34                 return a < b ? -1 : (a > b ? 1 : 0);
35             }); 
36             service.targets.unshift({
37                 code:       'native-evergreen-catalog',
38                 settings:   localTarget,
39                 selected:   false,
40                 username:   '',
41                 password:   ''
42             });
43         });
44     };
45
46     service.loadActiveSearchFields = function() {
47         // don't want to throw away the reference, otherwise
48         // directives bound to searchFields won't
49         // refresh
50         var curFormInput = {};
51         for (var field in service.searchFields) {
52             curFormInput[field] = service.searchFields[field].query;
53             delete service.searchFields[field];
54         }
55         angular.forEach(service.targets, function(target, idx) {
56             if (target.selected) {
57                 angular.forEach(target.settings.attrs, function(attr, key) {
58                     if (!(key in service.searchFields)) service.searchFields[key] = {
59                         label : attr.label,
60                         query : (key in curFormInput) ? curFormInput[key] : ''
61                     };
62                 });
63             }
64         });
65     };
66
67     service.clearSearchFields = function() {
68         for (var field in service.searchFields) {
69             service.searchFields[field].query = '';
70         }
71     }
72
73     // return the selected Z39.50 targets and search strings
74     // in a format suitable for passing directly to
75     // open-ils.search.z3950.search_class
76     service.currentQuery = function() {
77         var query = {
78             service  : [],
79             username : [],
80             password : [],
81             search   : {}
82         };
83
84         angular.forEach(service.targets, function(target, idx) {
85             if (target.selected) {
86                 query.service.push(target.code);
87                 query.username.push(target.username);
88                 query.password.push(target.password);
89             }
90         });
91         angular.forEach(service.searchFields, function(value, key) {
92             if (value.query && value.query.trim()) {
93                 query.search[key] = value.query.trim();
94             }
95         });
96
97         return query;
98     }
99
100     return service;
101 }])
102 .directive("egZ3950TargetList", function () {
103     return {
104         transclude: true,
105         restrict:   'AE',
106         scope: {
107             
108         },
109         templateUrl: './cat/z3950/t_target',
110         controller:
111                    ['$scope','egZ3950TargetSvc',
112             function($scope , egZ3950TargetSvc) {
113                 $scope.targets = egZ3950TargetSvc.targets;
114                 $scope.$watch('targets', function(oldVal, newVal) {
115                     egZ3950TargetSvc.loadActiveSearchFields();
116                 }, true);
117             }]
118     }
119 })
120 .directive("egZ3950SearchFieldList", ['egZ3950TargetSvc',
121     function(egZ3950TargetSvc) {
122         return {
123             restrict:   'AE',
124             scope: {
125             },
126             templateUrl: './cat/z3950/t_search_fields',
127             link: function(scope, elem, attr) {
128                 scope.fields = egZ3950TargetSvc.searchFields;
129             }
130         };
131     }
132 ]);