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