webstaff: Z39.50 - more improvements
[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         raw_search : ''
10     };
11     
12     service.loadTargets = function() {
13         egCore.net.request(
14             'open-ils.search',
15             'open-ils.search.z3950.retrieve_services',
16             egAuth.token()
17         ).then(function(res) {
18             // keep the reference, just clear the list
19             service.targets.length = 0;
20             // native Evergreen search goes first
21             var localTarget = res['native-evergreen-catalog'];
22             delete res['native-evergreen-catalog'];
23             angular.forEach(res, function(value, key) {
24                 this.push({
25                     code:       key,
26                     settings:   value,
27                     selected:   false,
28                     username:   '',
29                     password:   ''
30                 });
31             }, service.targets);
32             service.targets.sort(function (a, b) {
33                 a = a.settings.label;
34                 b = b.settings.label;
35                 return a < b ? -1 : (a > b ? 1 : 0);
36             }); 
37             service.targets.unshift({
38                 code:       'native-evergreen-catalog',
39                 settings:   localTarget,
40                 selected:   false,
41                 username:   '',
42                 password:   ''
43             });
44         });
45     };
46
47     service.loadActiveSearchFields = function() {
48         // don't want to throw away the reference, otherwise
49         // directives bound to searchFields won't
50         // refresh
51         var curFormInput = {};
52         for (var field in service.searchFields) {
53             curFormInput[field] = service.searchFields[field].query;
54             delete service.searchFields[field];
55         }
56         angular.forEach(service.targets, function(target, idx) {
57             if (target.selected) {
58                 angular.forEach(target.settings.attrs, function(attr, key) {
59                     if (!(key in service.searchFields)) service.searchFields[key] = {
60                         label : attr.label,
61                         query : (key in curFormInput) ? curFormInput[key] : ''
62                     };
63                 });
64             }
65         });
66     };
67
68     service.clearSearchFields = function() {
69         for (var field in service.searchFields) {
70             service.searchFields[field].query = '';
71         }
72     }
73
74     // return the selected Z39.50 targets and search strings
75     // in a format suitable for passing directly to
76     // open-ils.search.z3950.search_class
77     service.currentQuery = function() {
78         var query = {
79             service  : [],
80             username : [],
81             password : [],
82             search   : {}
83         };
84
85         angular.forEach(service.targets, function(target, idx) {
86             if (target.selected) {
87                 query.service.push(target.code);
88                 query.username.push(target.username);
89                 query.password.push(target.password);
90             }
91         });
92         if (service.raw_search) {
93             query.raw_search = service.raw_search;
94         } else {
95             angular.forEach(service.searchFields, function(value, key) {
96                 if (value.query && value.query.trim()) {
97                     query.search[key] = value.query.trim();
98                 }
99             });
100         }
101         return query;
102     }
103
104     // raw search can be done only if exactly one
105     // (real) Z39.50 target is selected
106     service.rawSearchImpossible = function() {
107         var z_selected = 0;
108         for (var i in service.targets) {
109             if (service.targets[i].code == 'native-evergreen-catalog') {
110                 if (service.targets[i].selected) return true;
111             } else {
112                 if (service.targets[i].selected) z_selected++;
113             }
114         }
115         return !(z_selected == 1);
116     }
117
118     service.setRawSearch = function(raw_search) {
119         service.raw_search = raw_search;
120     }
121
122     return service;
123 }])
124 .directive("egZ3950TargetList", function () {
125     return {
126         transclude: true,
127         restrict:   'AE',
128         scope: {
129             
130         },
131         templateUrl: './cat/z3950/t_target',
132         controller:
133                    ['$scope','egZ3950TargetSvc',
134             function($scope , egZ3950TargetSvc) {
135                 $scope.targets = egZ3950TargetSvc.targets;
136                 $scope.$watch('targets', function(oldVal, newVal) {
137                     egZ3950TargetSvc.loadActiveSearchFields();
138                 }, true);
139             }]
140     }
141 })
142 .directive("egZ3950SearchFieldList", ['egZ3950TargetSvc',
143     function(egZ3950TargetSvc) {
144         return {
145             restrict:   'AE',
146             scope: {
147             },
148             templateUrl: './cat/z3950/t_search_fields',
149             link: function(scope, elem, attr) {
150                 scope.fields = egZ3950TargetSvc.searchFields;
151             }
152         };
153     }
154 ]);