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