]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/z3950.js
more standardization of universal list actions
[working/Evergreen.git] / Open-ILS / xul / staff_client / server / cat / z3950.js
1 dump('entering cat.z3950.js\n');
2
3 function $(id) { return document.getElementById(id); }
4
5 if (typeof cat == 'undefined') cat = {};
6 cat.z3950 = function (params) {
7         try {
8                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
9                 JSAN.use('util.error'); this.error = new util.error();
10                 JSAN.use('util.network'); this.network = new util.network();
11         } catch(E) {
12                 dump('cat.z3950: ' + E + '\n');
13         }
14 }
15
16 cat.z3950.prototype = {
17
18         'creds_version' : 2,
19
20     'number_of_result_sets' : 0,
21
22     'result_set' : [],
23
24     'limit' : 10,
25
26         'init' : function( params ) {
27
28                 try {
29                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
30                         JSAN.use('util.widgets');
31
32                         var obj = this;
33
34             JSAN.use('OpenILS.data'); obj.data = new OpenILS.data(); obj.data.init({'via':'stash'});
35
36                         obj.load_creds();
37
38                         JSAN.use('circ.util');
39                         var columns = circ.util.columns(
40                                 {
41                                         'tcn' : { 'hidden' : false },
42                                         'isbn' : { 'hidden' : false },
43                                         'title' : { 'hidden' : false, 'flex' : '1' },
44                                         'author' : { 'hidden' : false },
45                                         'edition' : { 'hidden' : false },
46                                         'pubdate' : { 'hidden' : false },
47                                         'publisher' : { 'hidden' : false },
48                                         'service' : { 'hidden' : false }
49                                 }
50             );
51
52                         JSAN.use('util.list'); obj.list = new util.list('results');
53                         obj.list.init(
54                                 {
55                                         'columns' : columns,
56                                         'map_row_to_columns' : circ.util.std_map_row_to_columns(),
57                                         'on_select' : function(ev) {
58                                                 try {
59                                                         JSAN.use('util.functional');
60                                                         var sel = obj.list.retrieve_selection();
61                                                         document.getElementById('sel_clip').setAttribute('disabled', sel.length < 1);
62                                                         var list = util.functional.map_list(
63                                                                 sel,
64                                                                 function(o) { return o.getAttribute('retrieve_id'); }
65                                                         );
66                                                         obj.error.sdump('D_TRACE','cat/z3950: selection list = ' + js2JSON(list) );
67                                                         obj.controller.view.marc_import.disabled = false;
68                                                         obj.controller.view.marc_import.setAttribute('retrieve_id',list[0]);
69                                                         obj.controller.view.marc_import_overlay.disabled = false;
70                                                         obj.controller.view.marc_import_overlay.setAttribute('retrieve_id',list[0]);
71                                                         obj.controller.view.marc_view_btn.disabled = false;
72                                                         obj.controller.view.marc_view_btn.setAttribute('retrieve_id',list[0]);
73                                                 } catch(E) {
74                                                         obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.obj_list_init.list_construction_error'),E);
75                                                 }
76                                         },
77                                 }
78                         );
79
80                         JSAN.use('util.controller'); obj.controller = new util.controller();
81                         obj.controller.init(
82                                 {
83                                         control_map : {
84                                                 'save_columns' : [ [ 'command' ], function() { obj.list.save_columns(); } ],
85                                                 'sel_clip' : [ ['command'], function() { obj.list.clipboard(); } ],
86                                                 'cmd_z3950_csv_to_clipboard' : [ ['command'], function() { obj.list.dump_csv_to_clipboard(); } ],
87                         'cmd_z3950_csv_to_printer' : [ ['command'], function() { obj.list.dump_csv_to_printer(); } ], 
88                         'cmd_z3950_csv_to_file' : [ ['command'], function() { obj.list.dump_csv_to_file( { 'defaultFileName' : 'z3950_results.txt' } ); } ],
89                                                 'cmd_broken' : [
90                                                         ['command'],
91                                                         function() { alert('Not Yet Implemented'); }
92                                                 ],
93                                                 'result_message' : [['render'],function(e){return function(){};}],
94                                                 'clear' : [
95                                                         ['command'],
96                                                         function() {
97                                                                 obj.clear();
98                                                         }
99                                                 ],
100                                                 'save_creds' : [
101                                                         ['command'],
102                                                         function() {
103                                                                 obj.save_creds();
104                                 setTimeout( function() { obj.focus(); }, 0 );
105                                                         }
106                                                 ],
107                         'marc_view_btn' : [
108                             ['render'],
109                             function(e) {
110                                 e.setAttribute('label', $("catStrings").getString('staff.cat.z3950.marc_view.label'));
111                                 e.setAttribute('accesskey', $("catStrings").getString('staff.cat.z3950.marc_view.accesskey'));
112                             }
113                         ],
114                                                 'marc_view' : [
115                                                         ['command'],
116                                                         function(ev) {
117                                                                 try {
118                                                                         var n = obj.controller.view.marc_view_btn;
119                                                                         if (n.getAttribute('toggle') == '1') {
120                                                                                 document.getElementById('deck').selectedIndex = 0;
121                                                                                 n.setAttribute('toggle','0');
122                                                                                 n.setAttribute('label', $("catStrings").getString('staff.cat.z3950.marc_view.label'));
123                                                                                 n.setAttribute('accesskey', $("catStrings").getString('staff.cat.z3950.marc_view.accesskey'));
124                                                                                 document.getElementById('results').focus();
125                                                                         } else {
126                                                                                 document.getElementById('deck').selectedIndex = 1;
127                                                                                 n.setAttribute('toggle','1');
128                                                                                 n.setAttribute('label', $("catStrings").getString('staff.cat.z3950.results_view.label'));
129                                                                                 n.setAttribute('accesskey', $("catStrings").getString('staff.cat.z3950.results_view.accesskey'));
130                                                                                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
131                                                                                 var f = get_contentWindow(document.getElementById('marc_frame'));
132                                         var retrieve_id = n.getAttribute('retrieve_id');
133                                         var result_idx = retrieve_id.split('-')[0];
134                                         var record_idx = retrieve_id.split('-')[1];
135                                                                                 f.xulG = { 'marcxml' : obj.result_set[result_idx].records[ record_idx ].marcxml };
136                                                                                 f.my_init();
137                                                                                 f.document.body.firstChild.focus();
138                                                                         }
139                                                                 } catch(E) {
140                                                 obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.obj_controller_init.marc_view_error'),E);
141                                                                 }
142                                                         },
143                                                 ],
144                                                 'marc_import' : [
145                                                         ['command'],
146                                                         function() {
147                                 try {
148                                     var retrieve_id = obj.controller.view.marc_import.getAttribute('retrieve_id');
149                                     var result_idx = retrieve_id.split('-')[0];
150                                     var record_idx = retrieve_id.split('-')[1];
151                                     obj.spawn_marc_editor( 
152                                         obj.result_set[ result_idx ].records[ record_idx ].marcxml,
153                                         obj.result_set[ result_idx ].records[ record_idx ].service /* FIXME: we want biblio_source here */
154                                     );
155                                 } catch(E) {
156                                                 obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.obj_controller_init.marc_import_error'),E);
157                                 }
158                                                         },
159                                                 ],
160                                                 'marc_import_overlay' : [ 
161                                                         ['command'],
162                                                         function() {
163                                                                 try {
164                                     var retrieve_id = obj.controller.view.marc_import_overlay.getAttribute('retrieve_id');
165                                     var result_idx = retrieve_id.split('-')[0];
166                                     var record_idx = retrieve_id.split('-')[1];
167                                     obj.spawn_marc_editor_for_overlay( 
168                                         obj.result_set[ result_idx ].records[ record_idx ].marcxml,
169                                         obj.result_set[ result_idx ].records[ record_idx ].service /* FIXME: we want biblio_source here */
170                                     );
171                                                                 } catch(E) {
172                                                 obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.obj_controller_init.marc_import_overlay_error'),E);
173                                                                 }
174                                                         },
175                                                 ],
176                                                 'search' : [
177                                                         ['command'],
178                                                         function() {
179                                                                 obj.initial_search();
180                                                         },
181                                                 ],
182                                                 'page_next' : [
183                                                         ['command'],
184                                                         function() {
185                                                                 obj.page_next();
186                                                         },
187                                                 ],
188                         'toggle_form_btn' : [
189                             ['render'],
190                             function(e) {
191                                 e.setAttribute('image',"/xul/server/skin/media/images/up_arrow.gif");
192                                 e.setAttribute('label',$("catStrings").getString('staff.cat.z3950.hide_top_pane.label'));
193                                 e.setAttribute('accesskey',$("catStrings").getString('staff.cat.z3950.hide_top_pane.accesskey'));
194                             }
195                         ],
196                         'toggle_form' : [
197                             ['command'],
198                             function() {
199                                 var x = document.getElementById('top_pane');
200                                 x.hidden = ! x.hidden;
201                                                                 var n = obj.controller.view.toggle_form_btn;
202                                 if (x.hidden) {
203                                     n.setAttribute('image',"/xul/server/skin/media/images/down_arrow.gif");
204                                     n.setAttribute('label',$("catStrings").getString('staff.cat.z3950.unhide_top_pane.label'));
205                                     n.setAttribute('accesskey',$("catStrings").getString('staff.cat.z3950.unhide_top_pane.accesskey'));
206                                 } else {
207                                     n.setAttribute('image',"/xul/server/skin/media/images/up_arrow.gif");
208                                     n.setAttribute('label',$("catStrings").getString('staff.cat.z3950.hide_top_pane.label'));
209                                     n.setAttribute('accesskey',$("catStrings").getString('staff.cat.z3950.hide_top_pane.accesskey'));
210                                 }
211                             },
212                         ],
213                                                 'service_rows' : [
214                                                         ['render'],
215                                                         function(e) {
216                                                                 return function() {
217                                                                         try {
218
219                                                                                 function handle_switch(node) {
220                                             try {
221                                                 obj.active_services = [];
222                                                 var snl = document.getElementsByAttribute('mytype','service_class');
223                                                 for (var i = 0; i < snl.length; i++) {
224                                                     var n = snl[i];
225                                                     if (n.nodeName == 'checkbox') {
226                                                         if (n.checked) obj.active_services.push( n.getAttribute('service') );
227                                                     }
228                                                 }
229                                                 var nl = document.getElementsByAttribute('mytype','search_class');
230                                                 for (var i = 0; i < nl.length; i++) { nl[i].disabled = true; }
231                                                 var attrs = {};
232                                                 for (var j = 0; j < obj.active_services.length; j++) {
233                                                     if (obj.services[obj.active_services[j]]) for (var i in obj.services[obj.active_services[j]].attrs) {
234                                                         var attr = obj.services[obj.active_services[j]].attrs[i];
235                                                         if (! attrs[i]) {
236                                                             attrs[i] = { 'labels' : {} };
237                                                         }
238                                                         if (attr.label) {
239                                                             attrs[i].labels[ attr.label ] = true;
240                                                         } else if (document.getElementById('commonStrings').testString('staff.z39_50.search_class.' + i)) {
241                                                             attrs[i].labels[ document.getElementById('commonStrings').getString('staff.z39_50.search_class.' + i) ] = true;
242                                                         } else if (attr.name) {
243                                                             attrs[i].labels[ attr.name ] = true;
244                                                         } else {
245                                                             attrs[i].labels[ i ] = true;
246                                                         }
247
248                                                     }
249                                                     
250                                                 }
251
252                                                 function set_label(x,attr) {
253                                                     var labels = [];
254                                                     for (var j in attrs[attr].labels) {
255                                                         labels.push(j);
256                                                     }
257                                                     if (labels.length > 0) {
258                                                         x.setAttribute('value',labels[0]);
259                                                         x.setAttribute('tooltiptext',labels.join(','));
260                                                         if (labels.length > 1) x.setAttribute('class','multiple_labels');
261                                                     }
262                                                 }
263
264                                                 for (var i in attrs) {
265                                                     var x = document.getElementById(i + '_input');
266                                                     if (x) {
267                                                         x.disabled = false;
268                                                         var y = document.getElementById(i + '_label',i);
269                                                         if (y) set_label(y,i);
270                                                     } else {
271                                                         var rows = document.getElementById('query_inputs');
272                                                         var row = document.createElement('row'); rows.appendChild(row);
273                                                         var label = document.createElement('label');
274                                                         label.setAttribute('id',i+'_label');
275                                                         label.setAttribute('control',i+'_input');
276                                                         label.setAttribute('search_class',i);
277                                                         label.setAttribute('style','-moz-user-focus: ignore');
278                                                         row.appendChild(label);
279                                                         set_label(label,i);
280                                                         label.addEventListener('click',function(ev){
281                                                                 var a = ev.target.getAttribute('search_class');
282                                                                 if (a) obj.default_attr = a;
283                                                             },false
284                                                         );
285                                                         var tb = document.createElement('textbox');
286                                                         tb.setAttribute('id',i+'_input');
287                                                         tb.setAttribute('mytype','search_class');
288                                                         tb.setAttribute('search_class',i);
289                                                         row.appendChild(tb);
290                                                         tb.addEventListener('keypress',function(ev) { return obj.handle_enter(ev); },false);
291                                                     }
292                                                 }
293                                             } catch(E) {
294                                                                                         obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.obj_controller_init.search_fields_error'),E);
295                                             }
296                                                                                 }
297
298                                         document.getElementById('native-evergreen-catalog_service').addEventListener('command',handle_switch,false);
299
300                                                                                 var robj = obj.network.simple_request(
301                                                                                         'RETRIEVE_Z3950_SERVICES',
302                                                                                         [ ses() ]
303                                                                                 );
304                                                                                 if (typeof robj.ilsevent != 'undefined') throw(robj);
305                                                                                 obj.services = robj;
306                                         var x = document.getElementById('service_rows');
307                                                                                 for (var i in obj.services) {
308                                             try {
309                                                 if (i == 'native-evergreen-catalog') continue;
310                                                 var r = document.createElement('row'); x.appendChild(r);
311                                                 var cb = document.createElement('checkbox'); 
312                                                     if (obj.services[i].label) {
313                                                         cb.setAttribute('label',obj.services[i].label);
314                                                     } else if (obj.services[i].name) {
315                                                         cb.setAttribute('label',obj.services[i].name);
316                                                     } else {
317                                                         cb.setAttribute('label',i);
318                                                     }
319                                                     cb.setAttribute('tooltiptext',i + ' : ' + obj.services[i].db + '@' + obj.services[i].host + ':' + obj.services[i].port); 
320                                                     cb.setAttribute('mytype','service_class'); cb.setAttribute('service',i);
321                                                     cb.setAttribute('id',i+'_service'); r.appendChild(cb);
322                                                     cb.addEventListener('command',handle_switch,false);
323                                                 var username = document.createElement('textbox'); username.setAttribute('id',i+'_username'); 
324                                                 if (obj.creds.hosts[ obj.data.server_unadorned ] && obj.creds.hosts[ obj.data.server_unadorned ].services[i]) username.setAttribute('value',obj.creds.hosts[ obj.data.server_unadorned ].services[i].username);
325                                                 r.appendChild(username);
326                                                 if (typeof obj.services[i].auth != 'undefined') username.hidden = ! get_bool( obj.services[i].auth );
327                                                 var password = document.createElement('textbox'); password.setAttribute('id',i+'_password'); 
328                                                 if (obj.creds.hosts[ obj.data.server_unadorned ] && obj.creds.hosts[ obj.data.server_unadorned ].services[i]) password.setAttribute('value',obj.creds.hosts[ obj.data.server_unadorned ].services[i].password);
329                                                 password.setAttribute('type','password'); r.appendChild(password);
330                                                 if (typeof obj.services[i].auth != 'undefined') password.hidden = ! get_bool( obj.services[i].auth );
331                                             } catch(E) {
332                                                 alert(E);
333                                             }
334                                         }
335                                         //obj.services[ 'native-evergreen-catalog' ] = { 'attrs' : { 'author' : {}, 'title' : {} } };
336                                         setTimeout(
337                                                                                         function() { 
338                                                 if (obj.creds.hosts[ obj.data.server_unadorned ]) {
339                                                     for (var i = 0; i < obj.creds.hosts[ obj.data.server_unadorned ].default_services.length; i++) {
340                                                         var x = document.getElementById(obj.creds.hosts[ obj.data.server_unadorned ].default_services[i]+'_service');
341                                                         if (x) x.checked = true;
342                                                     }
343                                                 } else if (obj.creds.default_service) {
344                                                     var x = document.getElementById(obj.creds.default_service+'_service');
345                                                     if (x) x.checked = true;
346                                                 }
347                                                 handle_switch();
348                                                                                         },0
349                                                                                 );
350                                                                         } catch(E) {
351                                                                                 obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.obj_controller_init.z39_service_error'),E);
352                                                                         }
353                                                                 }
354                                                         }
355                                                 ],
356                                         }
357                                 }
358                         );
359
360                         obj.controller.render();
361
362             setTimeout( function() { obj.focus(); }, 0 );
363
364                 } catch(E) {
365                         this.error.sdump('D_ERROR','cat.z3950.init: ' + E + '\n');
366                 }
367         },
368
369         'focus' : function() {
370                 var obj = this;
371         var focus_me; var or_focus_me;
372         for (var i = 0; i < obj.active_services.length; i++) {
373             if (obj.creds.hosts[ obj.data.server_unadorned ] && obj.creds.hosts[ obj.data.server_unadorned ].services[ obj.active_services[i] ]) {
374                         var x = obj.creds.hosts[ obj.data.server_unadorned ].services[ obj.active_services[i] ].default_attr;
375                 if (x) { focus_me = x; break; }
376             }
377             if (ob.services[ obj.active_services[i] ]) for (var i in obj.services[ obj.active_services[i] ].attr) { or_focus_me = i; }
378         }
379         if (! focus_me) focus_me = or_focus_me;
380                 var xx = document.getElementById(focus_me+'_input'); if (xx) xx.focus();
381         },
382
383         'clear' : function() {
384                 var obj = this;
385                 var nl = document.getElementsByAttribute('mytype','search_class');
386                 for (var i = 0; i < nl.length; i++) { nl[i].value = ''; nl[i].setAttribute('value',''); }
387                 //obj.focus(obj.controller.view.service_menu.value);
388         },
389
390         'search_params' : {},
391
392         'initial_search' : function() {
393                 try {
394                         var obj = this;
395             obj.result_set = []; obj.number_of_result_sets = 0;
396                         JSAN.use('util.widgets');
397                         util.widgets.remove_children( obj.controller.view.result_message );
398                         var x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
399             if (obj.active_services.length < 1) {
400                             x.appendChild( document.createTextNode($("catStrings").getString('staff.cat.z3950.initial_search.no_search_selection')));
401                 return;
402             }
403                         x.appendChild( document.createTextNode($("catStrings").getString('staff.cat.z3950.initial_search.searching')));
404                         obj.search_params = {}; obj.list.clear();
405                         obj.controller.view.page_next.disabled = true;
406                         obj.controller.view.cmd_z3950_csv_to_file.setAttribute('disabled','true');
407                         obj.controller.view.cmd_z3950_csv_to_clipboard.setAttribute('disabled','true');
408                         obj.controller.view.cmd_z3950_csv_to_printer.setAttribute('disabled','true');
409
410                         obj.search_params.service = []; 
411                         obj.search_params.username = [];
412                         obj.search_params.password = [];
413             for (var i = 0; i < obj.active_services.length; i++) {
414                 obj.search_params.service.push( obj.active_services[i] );
415                 obj.search_params.username.push( document.getElementById( obj.active_services[i]+'_username' ).value );
416                 obj.search_params.password.push( document.getElementById( obj.active_services[i]+'_password' ).value );
417             }
418                         obj.search_params.limit = Math.ceil( obj.limit / obj.active_services.length );
419                         obj.search_params.offset = 0;
420
421                         obj.search_params.search = {};
422                         var nl = document.getElementsByAttribute('mytype','search_class');
423                         var count = 0;
424                         for (var i = 0; i < nl.length; i++) {
425                                 if (nl[i].disabled) continue;
426                                 if (nl[i].value == '') continue;
427                                 count++;
428                                 obj.search_params.search[ nl[i].getAttribute('search_class') ] = nl[i].value;
429                         }
430                         if (count>0) {
431                                 obj.search();
432                         } else {
433                                 util.widgets.remove_children( obj.controller.view.result_message );
434                         }
435                 } catch(E) {
436                         this.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.initial_search.failed_search'),E);
437                 }
438         },
439
440         'page_next' : function() {
441                 try {
442                         var obj = this;
443                         JSAN.use('util.widgets');
444                         util.widgets.remove_children( obj.controller.view.result_message );
445                         var x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
446                         x.appendChild( document.createTextNode($("catStrings").getString('staff.cat.z3950.page_next.more_results')));
447                         obj.search_params.offset += obj.search_params.limit;
448                         obj.search();
449                 } catch(E) {
450                         this.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.page_next.subsequent_search_error'),E);
451                 }
452         },
453
454         'search' : function() {
455                 try {
456                         var obj = this;
457                         var method;
458                         if (typeof obj.search_params.query == 'undefined') {
459                                 method = 'FM_BLOB_RETRIEVE_VIA_Z3950_SEARCH';
460                         } else {
461                                 method = 'FM_BLOB_RETRIEVE_VIA_Z3950_RAW_SEARCH';
462                         }
463                         obj.network.simple_request(
464                                 method,
465                                 [ ses(), obj.search_params ],
466                                 function(req) {
467                                         obj.handle_results(req.getResultObject())
468                                 }
469                         );
470                         document.getElementById('deck').selectedIndex = 0;
471                 } catch(E) {
472                         this.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.search.search_error'),E);
473                 }
474         },
475
476         'handle_results' : function(results) {
477                 var obj = this;
478                 try {
479                         JSAN.use('util.widgets');
480                         util.widgets.remove_children( obj.controller.view.result_message ); var x;
481                         if (results == null) {
482                                 x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
483                                 x.appendChild( document.createTextNode($("catStrings").getString('staff.cat.z3950.handle_results.null_server_error')));
484                                 return;
485                         }
486                         if (typeof results.ilsevent != 'undefined') {
487                                 x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
488                                 x.appendChild( document.createTextNode($("catStrings").getFormattedString('staff.cat.z3950.handle_results.server_error', [results.textcode, results.desc])));
489                                 return;
490                         }
491             obj.controller.view.cmd_z3950_csv_to_file.setAttribute('disabled','false');
492             obj.controller.view.cmd_z3950_csv_to_clipboard.setAttribute('disabled','false');
493             obj.controller.view.cmd_z3950_csv_to_printer.setAttribute('disabled','false');
494             if (typeof results.length == 'undefined') results = [ results ];
495             for (var i = 0; i < results.length; i++) {
496                 if (results[i].query) {
497                     x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
498                     x.appendChild( document.createTextNode($("catStrings").getFormattedString('staff.cat.z3950.handle_results.raw_query', [results[i].query])));
499                 }
500                 if (results[i].count) {
501                     if (results[i].records) {
502                         x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
503                         var showing = obj.search_params.offset + results[i].records.length; 
504                         x.appendChild(
505                             document.createTextNode($("catStrings").getFormattedString('staff.cat.z3950.handle_results.showing_results',
506                                                                 [(showing > results[i].count ? results[i].count : showing), results[i].count, results[i].service]))
507                         );
508                     }
509                     if (obj.search_params.offset + obj.search_params.limit <= results[i].count) {
510                         obj.controller.view.page_next.disabled = false;
511                     }
512                 } else {
513                         x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
514                         x.appendChild(
515                             document.createTextNode($("catStrings").getFormattedString('staff.cat.z3950.handle_results.num_of_results', [(results[i].count ? results[i].count : 0)])));
516                 }
517                 if (results[i].records) {
518                     obj.result_set[ ++obj.number_of_result_sets ] = results[i];
519                     obj.controller.view.marc_import.disabled = true;
520                     obj.controller.view.marc_import_overlay.disabled = true;
521                     var x = obj.controller.view.marc_view_btn;
522                     if (x.getAttribute('toggle') == '0') x.disabled = true;
523                     for (var j = 0; j < obj.result_set[ obj.number_of_result_sets ].records.length; j++) {
524                         var f;
525                         var n = obj.list.append(
526                             {
527                                 'retrieve_id' : String( obj.number_of_result_sets ) + '-' + String( j ),
528                                 'row' : {
529                                     'my' : {
530                                         'mvr' : function(a){return a;}(obj.result_set[ obj.number_of_result_sets ].records[j].mvr),
531                                         'service' : results[i].service
532                                     }
533                                 }
534                             }
535                         );
536                         if (!f) { n.my_node.parentNode.focus(); f = n; } 
537                     }
538                 } else {
539                     x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
540                     x.appendChild(
541                         document.createTextNode($("catStrings").getString('staff.cat.z3950.handle_results.result_error'))
542                     );
543                 }
544             }
545                 } catch(E) {
546                         this.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.handle_results.search_result_error'),E);
547                 }
548         },
549
550         'replace_tab_with_opac' : function(doc_id) {
551                 var opac_url = xulG.url_prefix( urls.opac_rdetail ) + '?r=' + doc_id;
552                 var content_params = { 
553                         'session' : ses(),
554                         'authtime' : ses('authtime'),
555                         'opac_url' : opac_url,
556                 };
557                 xulG.set_tab(
558                         xulG.url_prefix(urls.XUL_OPAC_WRAPPER), 
559                         {'tab_name': $("catStrings").getString('staff.cat.z3950.replace_tab_with_opac.tab_name')}, 
560                         content_params
561                 );
562         },
563
564         'spawn_marc_editor' : function(my_marcxml,biblio_source) {
565                 var obj = this;
566                 xulG.new_tab(
567                         xulG.url_prefix(urls.XUL_MARC_EDIT), 
568                         { 'tab_name' : 'MARC Editor' }, 
569                         { 
570                                 'record' : { 'marc' : my_marcxml },
571                                 'save' : {
572                                         'label' : 'Import Record',
573                                         'func' : function (new_marcxml) {
574                                                 try {
575                                                         var r = obj.network.simple_request('MARC_XML_RECORD_IMPORT', [ ses(), new_marcxml, biblio_source ]);
576                                                         if (typeof r.ilsevent != 'undefined') {
577                                                                 switch(Number(r.ilsevent)) {
578                                                                         case 1704 /* TCN_EXISTS */ :
579                                                                                 var msg = $("catStrings").getFormattedString('staff.cat.z3950.spawn_marc_editor.same_tcn', [r.payload.tcn]);
580                                                                                 var title = $("catStrings").getString('staff.cat.z3950.spawn_marc_editor.title');
581                                                                                 var btn1 = $("catStrings").getString('staff.cat.z3950.spawn_marc_editor.btn1_overlay');
582                                                                                 var btn2 = typeof r.payload.new_tcn == 'undefined' ? null : $("catStrings").getFormattedString('staff.cat.z3950.spawn_marc_editor.btn2_import', [r.payload.new_tcn]);
583                                                                                 if (btn2) {
584                                                                                         obj.data.init({'via':'stash'});
585                                                                                         var robj = obj.network.simple_request(
586                                                                                                 'PERM_CHECK',[
587                                                                                                         ses(),
588                                                                                                         obj.data.list.au[0].id(),
589                                                                                                         obj.data.list.au[0].ws_ou(),
590                                                                                                         [ 'ALLOW_ALT_TCN' ]
591                                                                                                 ]
592                                                                                         );
593                                                                                         if (typeof robj.ilsevent != 'undefined') {
594                                                                                                 obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor.permission_error'),E);
595                                                                                         }
596                                                                                         if (robj.length != 0) btn2 = null;
597                                                                                 }
598                                                                                 var btn3 = $("catStrings").getString('staff.cat.z3950.spawn_marc_editor.btn3_cancel_import');
599                                                                                 var p = obj.error.yns_alert(msg,title,btn1,btn2,btn3,$("catStrings").getString('staff.cat.z3950.spawn_marc_editor.confirm_action'));
600                                                                                 obj.error.sdump('D_ERROR','option ' + p + 'chosen');
601                                                                                 switch(p) {
602                                                                                         case 0:
603                                                                                                 var r3 = obj.network.simple_request('MARC_XML_RECORD_UPDATE', [ ses(), r.payload.dup_record, new_marcxml, biblio_source ]);
604                                                                                                 if (typeof r3.ilsevent != 'undefined') {
605                                                                                                         throw(r3);
606                                                                                                 } else {
607                                                                                                         alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor.successful_overlay'));
608                                                                                                         obj.replace_tab_with_opac(r3.id());
609                                                                                                 }
610                                                                                         break;
611                                                                                         case 1:
612                                                                                                 var r2 = obj.network.request(
613                                                                                                         api.MARC_XML_RECORD_IMPORT.app,
614                                                                                                         api.MARC_XML_RECORD_IMPORT.method + '.override',
615                                                                                                         [ ses(), new_marcxml, biblio_source ]
616                                                                                                 );
617                                                                                                 if (typeof r2.ilsevent != 'undefined') {
618                                                                                                         throw(r2);
619                                                                                                 } else {
620                                                                                                         alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor.successful_import_with_new_tcn'));
621                                                                                                         obj.replace_tab_with_opac(r2.id());
622                                                                                                 }
623                                                                                         break;
624                                                                                         case 2:
625                                                                                         default:
626                                                                                                 alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor.import_cancelled'));
627                                                                                         break;
628                                                                                 }
629                                                                         break;
630                                                                         default:
631                                                                                 throw(r);
632                                                                         break;
633                                                                 }
634                                                         } else {
635                                                                 alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor.successful_import'));
636                                                                 obj.replace_tab_with_opac(r.id());
637                                                         }
638                                                 } catch(E) {
639                                                         obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor.import_error'),E);
640                                                 }
641                                         }
642                                 }
643                         } 
644                 );
645         },
646
647         'confirm_overlay' : function(record_ids) {
648                 var obj = this; // JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
649                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserWrite');
650                 var top_xml = '<vbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" flex="1" >';
651                 top_xml += '<description>'+$("catStrings").getString('staff.cat.z3950.confirm_overlay.description')+'</description>';
652                 top_xml += '<hbox><button id="lead" disabled="false" label="'+$("catStrings").getString('staff.cat.z3950.confirm_overlay.lead.label')+'" name="fancy_submit"';
653                 top_xml += ' accesskey="'+$("catStrings").getString('staff.cat.z3950.confirm_overlay.lead.accesskey')+'"/>';
654                 top_xml += ' <button label="'+$("catStrings").getString('staff.cat.z3950.confirm_overlay.cancel.label')+'" accesskey="'+
655                                                 $("catStrings").getString('staff.cat.z3950.confirm_overlay.cancel.accesskey')+'" name="fancy_cancel"/></hbox></vbox>';
656
657                 var xml = '<form xmlns="http://www.w3.org/1999/xhtml">';
658                 xml += '<table width="100%"><tr valign="top">';
659                 for (var i = 0; i < record_ids.length; i++) {
660                         xml += '<td nowrap="nowrap"><iframe src="' + urls.XUL_BIB_BRIEF; 
661                         xml += '?docid=' + record_ids[i] + '"/></td>';
662                 }
663                 xml += '</tr><tr valign="top">';
664                 for (var i = 0; i < record_ids.length; i++) {
665                         html = obj.network.simple_request('MARC_HTML_RETRIEVE',[ record_ids[i] ]);
666                         xml += '<td nowrap="nowrap"><iframe style="min-height: 1000px; min-width: 300px;" flex="1" src="data:text/html,' + window.escape(html) + '"/></td>';
667                 }
668                 xml += '</tr></table></form>';
669                 // data.temp_merge_top = top_xml; data.stash('temp_merge_top');
670                 // data.temp_merge_mid = xml; data.stash('temp_merge_mid');
671                 JSAN.use('util.window'); var win = new util.window();
672                 var fancy_prompt_data = win.open(
673                         urls.XUL_FANCY_PROMPT,
674                         // + '?xml_in_stash=temp_merge_mid'
675                         // + '&top_xml_in_stash=temp_merge_top'
676                         // + '&title=' + window.escape('Record Overlay'),
677                         'fancy_prompt', 'chrome,resizable,modal,width=700,height=500',
678                         { 'top_xml' : top_xml, 'xml' : xml, 'title' : $("catStrings").getString('staff.cat.z3950.confirm_overlay.title') }
679                 );
680                 //data.stash_retrieve();
681                 if (fancy_prompt_data.fancy_status == 'incomplete') { alert($("catStrings").getString('staff.cat.z3950.confirm_overlay.aborted')); return false; }
682                 return true;
683         },
684
685         'spawn_marc_editor_for_overlay' : function(my_marcxml,biblio_source) {
686                 var obj = this;
687                 obj.data.init({'via':'stash'});
688                 if (!obj.data.marked_record) {
689                         alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor_for_overlay.try_again'));
690                         return;
691                 }
692
693                 xulG.new_tab(
694                         xulG.url_prefix(urls.XUL_MARC_EDIT), 
695                         { 'tab_name' : $("catStrings").getString('staff.cat.z3950.spawn_marc_editor_for_overlay.tab_name') },
696                         { 
697                                 'record' : { 'marc' : my_marcxml },
698                                 'save' : {
699                                         'label' : $("catStrings").getString('staff.cat.z3950.spawn_marc_editor_for_overlay.overlay_record_label'),
700                                         'func' : function (new_marcxml) {
701                                                 try {
702                                                         if (! obj.confirm_overlay( [ obj.data.marked_record ] ) ) { return; }
703                                                         var r = obj.network.simple_request('MARC_XML_RECORD_REPLACE', [ ses(), obj.data.marked_record, new_marcxml, biblio_source ]);
704                                                         if (typeof r.ilsevent != 'undefined') {
705                                                                 switch(Number(r.ilsevent)) {
706                                                                         case 1704 /* TCN_EXISTS */ :
707                                                                                 var msg = $("catStrings").getFormattedString('staff.cat.z3950.spawn_marc_editor_for_overlay.same_tcn', [r.payload.tcn]);
708                                                                                 var title = $("catStrings").getString('staff.cat.z3950.spawn_marc_editor_for_overlay.import_collision');
709                                                                                 var btn1 = typeof r.payload.new_tcn == 'undefined' ? null : $("catStrings").getFormattedString('staff.cat.z3950.spawn_marc_editor_for_overlay.btn1_overlay', [r.payload.new_tcn]);
710                                                                                 if (btn1) {
711                                                                                         var robj = obj.network.simple_request(
712                                                                                                 'PERM_CHECK',[
713                                                                                                         ses(),
714                                                                                                         obj.data.list.au[0].id(),
715                                                                                                         obj.data.list.au[0].ws_ou(),
716                                                                                                         [ 'ALLOW_ALT_TCN' ]
717                                                                                                 ]
718                                                                                         );
719                                                                                         if (typeof robj.ilsevent != 'undefined') {
720                                                                                                 obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor_for_overlay.permission_error'),E);
721                                                                                         }
722                                                                                         if (robj.length != 0) btn1 = null;
723                                                                                 }
724                                                                                 var btn2 = $("catStrings").getString('staff.cat.z3950.spawn_marc_editor_for_overlay.btn2_cancel');
725                                                                                 var p = obj.error.yns_alert(msg,title,btn1,btn2,null, $("catStrings").getString('staff.cat.z3950.spawn_marc_editor_for_overlay.confirm_action'));
726                                                                                 obj.error.sdump('D_ERROR','option ' + p + 'chosen');
727                                                                                 switch(p) {
728                                                                                         case 0:
729                                                                                                 var r2 = obj.network.request(
730                                                                                                         api.MARC_XML_RECORD_REPLACE.app,
731                                                                                                         api.MARC_XML_RECORD_REPLACE.method + '.override',
732                                                                                                         [ ses(), obj.data.marked_record, new_marcxml, biblio_source ]
733                                                                                                 );
734                                                                                                 if (typeof r2.ilsevent != 'undefined') {
735                                                                                                         throw(r2);
736                                                                                                 } else {
737                                                                                                         alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor_for_overlay.successful_overlay_with_new_TCN'));
738                                                                                                         obj.replace_tab_with_opac(r2.id());
739                                                                                                 }
740                                                                                         break;
741                                                                                         case 1:
742                                                                                         default:
743                                                                                                 alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor_for_overlay.cancelled_overlay'));
744                                                                                         break;
745                                                                                 }
746                                                                         break;
747                                                                         default:
748                                                                                 throw(r);
749                                                                         break;
750                                                                 }
751                                                         } else {
752                                                                 alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor_for_overlay.success_overlay'));
753                                                                 obj.replace_tab_with_opac(r.id());
754                                                         }
755                                                 } catch(E) {
756                                                         obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.spawn_marc_editor_for_overlay.overlay_error'),E);
757                                                 }
758                                         }
759                                 }
760                         } 
761                 );
762         },
763
764
765         'load_creds' : function() {
766                 var obj = this;
767                 try {
768                         obj.creds = { 'version' : g.save_version, 'services' : {}, 'hosts' : {} };
769                         /*
770                                 {
771                                         'version' : xx,
772                                         'default_service' : xx,
773                                         'services' : {
774
775                                                 'xx' : {
776                                                         'username' : xx,
777                                                         'password' : xx,
778                                                         'default_attr' : xx,
779                                                 },
780
781                                                 'xx' : {
782                                                         'username' : xx,
783                                                         'password' : xx,
784                                                         'default_attr' : xx,
785                                                 },
786                                         },
787                     // new in version 2
788                     'hosts' : {
789                         'xxxx' : {
790                             'default_services' : [ xx, ... ],
791                             'services' : {
792
793                                 'xx' : {
794                                     'username' : xx,
795                                     'password' : xx,
796                                     'default_attr' : xx,
797                                 },
798
799                                 'xx' : {
800                                     'username' : xx,
801                                     'password' : xx,
802                                     'default_attr' : xx,
803                                 },
804                             },
805                         }
806                     }
807                                 }
808                         */
809                         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
810                         JSAN.use('util.file'); var file = new util.file('z3950_store');
811                         if (file._file.exists()) {
812                                 var creds = file.get_object(); file.close();
813                                 if (typeof creds.version != 'undefined') {
814                                         if (creds.version >= obj.creds_version) {  /* so apparently, this guy is assuming that future versions will be backwards compatible */
815                         if (typeof creds.hosts == 'undefined') creds.hosts = {};
816                                                 obj.creds = creds;
817                                         }
818                                 }
819                         }
820                 } catch(E) {
821                         obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.load_creds.z3950_cred_error'),E);
822                 }
823         },
824
825         'save_creds' : function () {
826                 try {
827                         var obj = this;
828             if (typeof obj.creds.hosts == 'undefined') obj.creds.hosts = {};
829             if (typeof obj.creds.hosts[ obj.data.server_unadorned ] == 'undefined') obj.creds.hosts[ obj.data.server_unadorned ] = { 'services' : {} };
830             obj.creds.hosts[ obj.data.server_unadorned ].default_services = obj.active_services;
831             for (var i = 0; i < obj.creds.hosts[ obj.data.server_unadorned ].default_services.length; i++) {
832                             var service = obj.creds.hosts[ obj.data.server_unadorned ].default_services[i];
833                         if (typeof obj.creds.hosts[ obj.data.server_unadorned ].services[ service ] == 'undefined') {
834                     obj.creds.hosts[ obj.data.server_unadorned ].services[ service ] = {}
835                         }
836                         obj.creds.hosts[ obj.data.server_unadorned ].services[service].username = document.getElementById(service + '_username').value;
837                         obj.creds.hosts[ obj.data.server_unadorned ].services[service].password = document.getElementById(service + '_password').value;
838                         if (obj.default_attr) {
839                                 obj.creds.hosts[ obj.data.server_unadorned ].services[service].default_attr = obj.default_attr;
840                         }
841             }
842                         obj.creds.version = obj.creds_version;
843                         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
844                         JSAN.use('util.file'); var file = new util.file('z3950_store');
845                         file.set_object(obj.creds);
846                         file.close();
847                 } catch(E) {
848                         obj.error.standard_unexpected_error_alert($("catStrings").getString('staff.cat.z3950.save_creds.z3950_cred_error'),E);
849                 }
850         },
851
852         'handle_enter' : function(ev) {
853                 var obj = this;
854                 if (ev.target.tagName != 'textbox') return;
855                 if (ev.keyCode == 13 /* enter */ || ev.keyCode == 77 /* enter on a mac */) setTimeout( function() { obj.initial_search(); }, 0);
856         },
857 }
858
859 dump('exiting cat.z3950.js\n');