LP#1739286: Revert "lp1739286 default search box in Z39.50"
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / cat / services / z3950.js
index 4ef7e70..918de4b 100644 (file)
@@ -5,10 +5,12 @@ function($q,   egCore,   egAuth) {
     
     var service = {
         targets : [ ],
-        searchFields : { }
+        searchFields : { },
+        raw_search : ''
     };
     
     service.loadTargets = function() {
+        var default_targets = egCore.hatch.getLocalItem('eg.cat.z3950.default_targets') || [];
         egCore.net.request(
             'open-ils.search',
             'open-ils.search.z3950.retrieve_services',
@@ -20,13 +22,18 @@ function($q,   egCore,   egAuth) {
             var localTarget = res['native-evergreen-catalog'];
             delete res['native-evergreen-catalog'];
             angular.forEach(res, function(value, key) {
-                this.push({
+                var tgt = {
                     code:       key,
                     settings:   value,
-                    selected:   false,
+                    selected:   (key in default_targets),
                     username:   '',
                     password:   ''
-                });
+                };
+                if (tgt.code in default_targets && tgt.settings.auth == 't') {
+                    tgt['username'] = default_targets[tgt.code]['username'] || '';
+                    tgt['password'] = default_targets[tgt.code]['password'] || '';
+                }
+                this.push(tgt);
             }, service.targets);
             service.targets.sort(function (a, b) {
                 a = a.settings.label;
@@ -36,7 +43,7 @@ function($q,   egCore,   egAuth) {
             service.targets.unshift({
                 code:       'native-evergreen-catalog',
                 settings:   localTarget,
-                selected:   false,
+                selected:   ('native-evergreen-catalog' in default_targets),
                 username:   '',
                 password:   ''
             });
@@ -88,13 +95,49 @@ function($q,   egCore,   egAuth) {
                 query.password.push(target.password);
             }
         });
-        angular.forEach(service.searchFields, function(value, key) {
-            if (value.query && value.query.trim()) {
-                query.search[key] = value.query.trim();
+        if (service.raw_search) {
+            query.raw_search = service.raw_search;
+        } else {
+            angular.forEach(service.searchFields, function(value, key) {
+                if (value.query && value.query.trim()) {
+                    query.search[key] = value.query.trim();
+                }
+            });
+        }
+        return query;
+    }
+
+    // raw search can be done only if exactly one
+    // (real) Z39.50 target is selected
+    service.rawSearchImpossible = function() {
+        var z_selected = 0;
+        for (var i in service.targets) {
+            if (service.targets[i].code == 'native-evergreen-catalog') {
+                if (service.targets[i].selected) return true;
+            } else {
+                if (service.targets[i].selected) z_selected++;
             }
-        });
+        }
+        return !(z_selected == 1);
+    }
 
-        return query;
+    service.setRawSearch = function(raw_search) {
+        service.raw_search = raw_search;
+    }
+
+    // store selected targets
+    service.saveDefaultZ3950Targets = function() {
+        var saved_targets = {};
+        angular.forEach(service.targets, function(target, idx) {
+            if (target.selected) {
+                saved_targets[target.code] = {};
+                if (target.settings.auth == 't') {
+                    saved_targets[target.code]['username'] = target.username;
+                    saved_targets[target.code]['password'] = target.password;
+                }
+            }
+        }); 
+        egCore.hatch.setLocalItem('eg.cat.z3950.default_targets', saved_targets);
     }
 
     return service;