]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/cat/opac.js
augment the xulG that the OPAC wrapper gives its content
[working/Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / cat / opac.js
1 var docid; var marc_html; var top_pane; var bottom_pane; var opac_frame; var opac_url;
2
3 var marc_view_reset = true;
4 var marc_edit_reset = true;
5 var copy_browser_reset = true;
6 var hold_browser_reset = true;
7
8 function $(id) { return document.getElementById(id); }
9
10 function my_init() {
11     try {
12         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
13         if (typeof JSAN == 'undefined') { throw(document.getElementById('offlineStrings').getString('common.jsan.missing')); }
14         JSAN.errorLevel = "die"; // none, warn, or die
15         JSAN.addRepository('..');
16         JSAN.use('util.error'); g.error = new util.error();
17         g.error.sdump('D_TRACE','my_init() for cat/opac.xul');
18
19         JSAN.use('OpenILS.data'); g.data = new OpenILS.data(); g.data.init({'via':'stash'});
20         XML_HTTP_SERVER = g.data.server_unadorned;
21
22         JSAN.use('util.network'); g.network = new util.network();
23
24         g.cgi = new CGI();
25         try { authtime = g.cgi.param('authtime') || xulG.authtime; } catch(E) { g.error.sdump('D_ERROR',E); }
26         try { docid = g.cgi.param('docid') || xulG.docid; } catch(E) { g.error.sdump('D_ERROR',E); }
27         try { opac_url = g.cgi.param('opac_url') || xulG.opac_url; } catch(E) { g.error.sdump('D_ERROR',E); }
28         try { g.view_override = g.cgi.param('default_view') || xulG.default_view; } catch(E) { g.error.sdump('D_ERROR',E); }
29
30         JSAN.use('util.deck');
31         top_pane = new util.deck('top_pane');
32         bottom_pane = new util.deck('bottom_pane');
33
34         set_opac();
35
36     } catch(E) {
37         var err_msg = document.getElementById("offlineStrings").getFormattedString("common.exception", ["cat/opac.xul", E]);
38         try { g.error.sdump('D_ERROR',err_msg); } catch(E) { dump(err_msg); }
39         alert(err_msg);
40     }
41 }
42
43 function set_brief_view() {
44     var url = xulG.url_prefix( urls.XUL_BIB_BRIEF ) + '?docid=' + window.escape(docid); 
45     dump('spawning ' + url + '\n');
46     top_pane.set_iframe( 
47         url,
48         {}, 
49         { 
50             'set_tab_name' : function(n) { 
51                 if (typeof window.xulG == 'object' && typeof window.xulG.set_tab_name == 'function') {
52                     try { window.xulG.set_tab_name(document.getElementById('offlineStrings').getFormattedString("cat.bib_record", [n])); } catch(E) { alert(E); }
53                 } else {
54                     dump('no set_tab_name\n');
55                 }
56             }
57         }  
58     );
59 }
60
61 function set_marc_view() {
62     g.view = 'marc_view';
63     if (marc_view_reset) {
64         bottom_pane.reset_iframe( xulG.url_prefix( urls.XUL_MARC_VIEW ) + '?docid=' + window.escape(docid),{},xulG);
65         marc_view_reset = false;
66     } else {
67         bottom_pane.set_iframe( xulG.url_prefix( urls.XUL_MARC_VIEW ) + '?docid=' + window.escape(docid),{},xulG);
68     }
69 }
70
71 function set_marc_edit() {
72     g.view = 'marc_edit';
73     var a =    xulG.url_prefix( urls.XUL_MARC_EDIT );
74     var b =    {};
75     var c =    {
76             'record' : { 'url' : '/opac/extras/supercat/retrieve/marcxml/record/' + docid },
77             'fast_add_item' : function(doc_id,cn_label,cp_barcode) {
78                 try {
79                     var cat = { util: {} }; /* FIXME: kludge since we can't load remote JSAN libraries into chrome */
80                     cat.util.spawn_copy_editor = function(params) {
81                         try {
82                             if (!params.copy_ids && !params.copies) return;
83                             if (params.copy_ids && params.copy_ids.length == 0) return;
84                             if (params.copies && params.copies.length == 0) return;
85                             if (params.copy_ids) params.copy_ids = js2JSON(params.copy_ids); // legacy
86                             if (!params.caller_handles_update) params.handle_update = 1; // legacy
87
88                             var obj = {};
89                             JSAN.use('util.network'); obj.network = new util.network();
90                             JSAN.use('util.error'); obj.error = new util.error();
91                         
92                             var title = '';
93                             if (params.copy_ids && params.copy_ids.length > 1 && params.edit == 1)
94                                 title = $("offlineStrings").getString('staff.cat.util.copy_editor.batch_edit');
95                             else if(params.copies && params.copies.length > 1 && params.edit == 1)
96                                 title = $("offlineStrings").getString('staff.cat.util.copy_editor.batch_view');
97                             else if(params.copy_ids && params.copy_ids.length == 1)
98                                 title = $("offlineStrings").getString('staff.cat.util.copy_editor.edit');
99                             else
100                                 title = $("offlineStrings").getString('staff.cat.util.copy_editor.view');
101
102                             JSAN.use('util.window'); var win = new util.window();
103                             var my_xulG = win.open(
104                                 (urls.XUL_COPY_EDITOR),
105                                 title,
106                                 'chrome,modal,resizable',
107                                 params
108                             );
109                             if (!my_xulG.copies && params.edit) {
110                             } else {
111                                 return my_xulG.copies;
112                             }
113                             return [];
114                         } catch(E) {
115                             JSAN.use('util.error'); var error = new util.error();
116                             error.standard_unexpected_error_alert('Error in chrome/content/cat/opac.js, cat.util.spawn_copy_editor',E);
117                         }
118                     }
119                     cat.util.fast_item_add = function(doc_id,cn_label,cp_barcode) {
120                         var error;
121                         try {
122
123                             JSAN.use('util.error'); error = new util.error();
124                             JSAN.use('util.network'); var network = new util.network();
125
126                             var acn_id = network.simple_request(
127                                 'FM_ACN_FIND_OR_CREATE',
128                                 [ ses(), cn_label, doc_id, ses('ws_ou') ]
129                             );
130
131                             if (typeof acn_id.ilsevent != 'undefined') {
132                                 error.standard_unexpected_error_alert('Error in chrome/content/cat/opac.js, cat.util.fast_item_add', acn_id);
133                                 return;
134                             }
135
136                             var copy_obj = new acp();
137                             copy_obj.id( -1 );
138                             copy_obj.isnew('1');
139                             copy_obj.barcode( cp_barcode );
140                             copy_obj.call_number( acn_id );
141                             copy_obj.circ_lib( ses('ws_ou') );
142                             /* FIXME -- use constants */
143                             copy_obj.deposit(0);
144                             copy_obj.price(0);
145                             copy_obj.deposit_amount(0);
146                             copy_obj.fine_level(2);
147                             copy_obj.loan_duration(2);
148                             copy_obj.location(1);
149                             copy_obj.status(0);
150                             copy_obj.circulate(get_db_true());
151                             copy_obj.holdable(get_db_true());
152                             copy_obj.opac_visible(get_db_true());
153                             copy_obj.ref(get_db_false());
154
155                             JSAN.use('util.window'); var win = new util.window();
156                             return cat.util.spawn_copy_editor( { 'handle_update' : 1, 'edit' : 1, 'docid' : doc_id, 'copies' : [ copy_obj ] });
157
158                         } catch(E) {
159                             if (error) error.standard_unexpected_error_alert('Error in chrome/content/cat/opac.js, cat.util.fast_item_add #2',E); else alert('FIXME: ' + E);
160                         }
161                     }
162                     return cat.util.fast_item_add(doc_id,cn_label,cp_barcode);
163                 } catch(E) {
164                     alert('Error in chrome/content/cat/opac.js, set_marc_edit, fast_item_add: ' + E);
165                 }
166             },
167             'save' : {
168                 'label' : document.getElementById('offlineStrings').getString('cat.save_record'),
169                 'func' : function (new_marcxml) {
170                     try {
171                         var r = g.network.simple_request('MARC_XML_RECORD_UPDATE', [ ses(), docid, new_marcxml ]);
172                         marc_view_reset = true;
173                         copy_browser_reset = true;
174                         hold_browser_reset = true;
175                         if (typeof r.ilsevent != 'undefined') {
176                             throw(r);
177                         } else {
178                             return {
179                                 'id' : r.id(),
180                                 'oncomplete' : function() {}
181                             };
182                         }
183                     } catch(E) {
184                             g.error.standard_unexpected_error_alert(document.getElementById('offlineStrings').getString("cat.save.failure"), E);
185                     }
186                 }
187             }
188         };
189     if (marc_edit_reset) {
190         bottom_pane.reset_iframe( a,b,c );
191         marc_edit_reset = false;
192     } else {
193         bottom_pane.set_iframe( a,b,c );
194     }
195 }
196
197 function set_copy_browser() {
198     g.view = 'copy_browser';
199     if (copy_browser_reset) {
200         bottom_pane.reset_iframe( xulG.url_prefix( urls.XUL_COPY_VOLUME_BROWSE ) + '?docid=' + window.escape(docid),{},xulG);
201         copy_browser_reset =false;
202     } else {
203         bottom_pane.set_iframe( xulG.url_prefix( urls.XUL_COPY_VOLUME_BROWSE ) + '?docid=' + window.escape(docid),{},xulG);
204     }
205 }
206
207 function set_hold_browser() {
208     g.view = 'hold_browser';
209     if (hold_browser_reset) {
210         bottom_pane.reset_iframe( xulG.url_prefix( urls.XUL_HOLDS_BROWSER ) + '?docid=' + window.escape(docid),{},xulG);
211         hold_browser_reset = false;
212     } else {
213         bottom_pane.set_iframe( xulG.url_prefix( urls.XUL_HOLDS_BROWSER ) + '?docid=' + window.escape(docid),{},xulG);
214     }
215 }
216
217 function set_opac() {
218     g.view = 'opac';
219     try {
220         var content_params = { 
221             'show_nav_buttons' : true,
222             'show_print_button' : true,
223             'passthru_content_params' : { 
224                 'authtoken' : ses(), 
225                 'authtime' : ses('authtime'),
226                 'window_open' : function(a,b,c) {
227                     try {
228                         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserWrite');
229                         return window.open(a,b,c);
230                     } catch(E) {
231                         g.error.standard_unexpected_error_alert('window_open',E);
232                     }
233                 }
234             },
235             'on_url_load' : function(f) {
236                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
237                 var win;
238                 try {
239                     if (typeof f.contentWindow.wrappedJSObject.attachEvt != 'undefined') {
240                         win = f.contentWindow.wrappedJSObject;
241                     } else {
242                         win = f.contentWindow;
243                     }
244                 } catch(E) {
245                     win = f.contentWindow;
246                 }
247                 win.attachEvt("rdetail", "recordRetrieved",
248                     function(id){
249                         try {
250                             if (docid == id) return;
251                             docid = id;
252                             refresh_display(id);
253                         } catch(E) {
254                             g.error.standard_unexpected_error_alert('rdetail -> recordRetrieved',E);
255                         }
256                     }
257                 );
258                 
259                 g.f_record_start = null; g.f_record_prev = null; g.f_record_next = null; g.f_record_end = null;
260                 $('record_start').disabled = true; $('record_next').disabled = true;
261                 $('record_prev').disabled = true; $('record_end').disabled = true;
262                 $('record_pos').setAttribute('value','');
263
264                 win.attachEvt("rdetail", "nextPrevDrawn",
265                     function(rIndex,rCount){
266                         $('record_pos').setAttribute('value', document.getElementById('offlineStrings').getFormattedString('cat.record.counter', [(1+rIndex), rCount ? rCount : 1]));
267                         if (win.rdetailNext) {
268                             g.f_record_next = function() { 
269                                 g.view_override = g.view; 
270                                 win.rdetailNext(); 
271                             }
272                             $('record_next').disabled = false;
273                         }
274                         if (win.rdetailPrev) {
275                             g.f_record_prev = function() { 
276                                 g.view_override = g.view; 
277                                 win.rdetailPrev(); 
278                             }
279                             $('record_prev').disabled = false;
280                         }
281                         if (win.rdetailStart) {
282                             g.f_record_start = function() { 
283                                 g.view_override = g.view; 
284                                 win.rdetailStart(); 
285                             }
286                             $('record_start').disabled = false;
287                         }
288                         if (win.rdetailEnd) {
289                             g.f_record_end = function() { 
290                                 g.view_override = g.view; 
291                                 win.rdetailEnd(); 
292                             }
293                             $('record_end').disabled = false;
294                         }
295                     }
296                 );
297             },
298             'url_prefix' : xulG.url_prefix,
299         };
300         content_params.new_tab = xulG.new_tab;
301         content_params.set_tab = xulG.set_tab;
302         content_params.close_tab = xulG.close_tab;
303         content_params.new_patron_tab = xulG.new_patron_tab;
304         content_params.set_patron_tab = xulG.set_patron_tab;
305         content_params.volume_item_creator = xulG.volume_item_creator;
306         content_params.get_new_session = xulG.get_new_session;
307         content_params.holdings_maintenance_tab = xulG.holdings_maintenance_tab;
308         content_params.set_tab_name = xulG.set_tab_name;
309         content_params.open_chrome_window = xulG.open_chrome_window;
310         content_params.url_prefix = xulG.url_prefix;
311         content_params.network_meter = xulG.network_meter;
312         content_params.set_statusbar = xulG.set_statusbar;
313
314         if (opac_url) { content_params.url = opac_url; } else { content_params.url = xulG.url_prefix( urls.browser ); }
315         browser_frame = bottom_pane.set_iframe( xulG.url_prefix(urls.XUL_BROWSER) + '?name=Catalog', {}, content_params);
316         /* // Remember to use the REMOTE_BROWSER if we ever try to move this to remote xul again
317         browser_frame = bottom_pane.set_iframe( xulG.url_prefix(urls.XUL_REMOTE_BROWSER) + '?name=Catalog', {}, content_params);
318         */
319     } catch(E) {
320         g.error.sdump('D_ERROR','set_opac: ' + E);
321     }
322 }
323
324 function bib_in_new_tab() {
325     try {
326         var url = browser_frame.contentWindow.g.browser.controller.view.browser_browser.contentWindow.wrappedJSObject.location.href;
327         var content_params = { 'session' : ses(), 'authtime' : ses('authtime'), 'opac_url' : url };
328         xulG.new_tab(xulG.url_prefix(urls.XUL_OPAC_WRAPPER), {}, content_params);
329     } catch(E) {
330         g.error.sdump('D_ERROR',E);
331     }
332 }
333
334 function remove_me() {
335     var url = xulG.url_prefix( urls.XUL_BIB_BRIEF ) + '?docid=' + window.escape(docid);
336     dump('removing ' + url + '\n');
337     try { top_pane.remove_iframe( url ); } catch(E) { dump(E + '\n'); }
338     $('nav').setAttribute('hidden','true');
339 }
340
341 function add_to_bucket() {
342     JSAN.use('util.window'); var win = new util.window();
343     win.open(
344         xulG.url_prefix(urls.XUL_RECORD_BUCKETS_QUICK),
345         'sel_bucket_win' + win.window_name_increment(),
346         'chrome,resizable,modal,center',
347         {
348             record_ids: [ docid ]
349         }
350     );
351 }
352
353 function mark_for_overlay() {
354     g.data.marked_record = docid;
355     g.data.stash('marked_record');
356     var robj = g.network.simple_request('MODS_SLIM_RECORD_RETRIEVE.authoritative',[docid]);
357     if (typeof robj.ilsevent == 'undefined') {
358         g.data.marked_record_mvr = robj;
359     } else {
360         g.data.marked_record_mvr = null;
361         g.error.standard_unexpected_error_alert('in mark_for_overlay',robj);
362     }
363     g.data.stash('marked_record_mvr');
364     if (g.data.marked_record_mvr) {
365         alert(document.getElementById('offlineStrings').getFormattedString('cat.opac.record_marked_for_overlay.tcn.alert',[ g.data.marked_record_mvr.tcn() ]));
366         xulG.set_statusbar(1, $("offlineStrings").getFormattedString('staff.cat.z3950.marked_record_for_overlay_indicator.tcn.label',[g.data.marked_record_mvr.tcn()]) );
367     } else {
368         alert(document.getElementById('offlineStrings').getFormattedString('cat.opac.record_marked_for_overlay.record_id.alert',[ g.data.marked_record  ]));
369         xulG.set_statusbar(1, $("offlineStrings").getFormattedString('staff.cat.z3950.marked_record_for_overlay_indicator.record_id.label',[g.data.marked_record]) );
370     }
371 }
372
373 function mark_for_hold_transfer() {
374     g.data.marked_record_for_hold_transfer = docid;
375     g.data.stash('marked_record_for_hold_transfer');
376     var robj = g.network.simple_request('MODS_SLIM_RECORD_RETRIEVE.authoritative',[docid]);
377     if (typeof robj.ilsevent == 'undefined') {
378         g.data.marked_record_for_hold_transfer_mvr = robj;
379     } else {
380         g.data.marked_record_for_hold_transfer_mvr = null;
381         g.error.standard_unexpected_error_alert('in mark_for_hold_transfer',robj);
382     }
383     g.data.stash('marked_record_for_hold_transfer_mvr');
384     if (g.data.marked_record_mvr) {
385         var m = $("offlineStrings").getFormattedString('staff.cat.opac.marked_record_for_hold_transfer_indicator.tcn.label',[g.data.marked_record_for_hold_transfer_mvr.tcn()]);
386         alert(m); xulG.set_statusbar(1, m );
387     } else {
388         var m = $("offlineStrings").getFormattedString('staff.cat.opac.marked_record_for_hold_transfer_indicator.record_id.label',[g.data.marked_record_for_hold_transfer]);
389         alert(m); xulG.set_statusbar(1, m );
390     }
391 }
392
393 function transfer_title_holds() {
394     g.data.stash_retrieve();
395     var target = g.data.marked_record_for_hold_transfer;
396     if (!target) {
397         var m = $("offlineStrings").getString('staff.cat.opac.title_for_hold_transfer.destination_needed.label');
398         alert(m);
399         return;
400     }
401     var robj = g.network.simple_request('TRANSFER_TITLE_HOLDS',[ ses(), target, [ docid ] ]);
402     if (robj == 1) {
403         var m = $("offlineStrings").getString('staff.cat.opac.title_for_hold_transfer.success.label');
404         alert(m);
405     } else {
406         var m = $("offlineStrings").getString('staff.cat.opac.title_for_hold_transfer.failure.label');
407         alert(m);
408     }
409     hold_browser_reset = true;
410     if (g.view == 'hold_browser') { set_hold_browser(); };
411 }
412
413 function delete_record() {
414     if (g.error.yns_alert(
415         document.getElementById('offlineStrings').getFormattedString('cat.opac.delete_record.confirm', [docid]),
416         document.getElementById('offlineStrings').getString('cat.opac.delete_record'),
417         document.getElementById('offlineStrings').getString('cat.opac.delete'),
418         document.getElementById('offlineStrings').getString('cat.opac.cancel'),
419         null,
420         document.getElementById('offlineStrings').getString('cat.opac.record_deleted.confirm')) == 0) {
421         var robj = g.network.simple_request('FM_BRE_DELETE',[ses(),docid]);
422         if (typeof robj.ilsevent != 'undefined') {
423             alert(document.getElementById('offlineStrings').getFormattedString('cat.opac.record_deleted.error',  [docid, robj.textcode, robj.desc]) + '\n');
424         } else {
425             alert(document.getElementById('offlineStrings').getString('cat.opac.record_deleted'));
426             refresh_display(docid);
427         }
428     }
429 }
430
431 function undelete_record() {
432     if (g.error.yns_alert(
433         document.getElementById('offlineStrings').getFormattedString('cat.opac.undelete_record.confirm', [docid]),
434         document.getElementById('offlineStrings').getString('cat.opac.undelete_record'),
435         document.getElementById('offlineStrings').getString('cat.opac.undelete'),
436         document.getElementById('offlineStrings').getString('cat.opac.cancel'),
437         null,
438         document.getElementById('offlineStrings').getString('cat.opac.record_undeleted.confirm')) == 0) {
439
440         var robj = g.network.simple_request('FM_BRE_UNDELETE',[ses(),docid]);
441         if (typeof robj.ilsevent != 'undefined') {
442             alert(document.getElementById('offlineStrings').getFormattedString('cat.opac.record_undeleted.error',  [docid, robj.textcode, robj.desc]) + '\n');
443         } else {
444             alert(document.getElementById('offlineStrings').getString('cat.opac.record_undeleted'));
445             refresh_display(docid);
446         }
447     }
448 }
449
450 function refresh_display(id) {
451     try { 
452         marc_view_reset = true;
453         marc_edit_reset = true;
454         copy_browser_reset = true;
455         hold_browser_reset = true;
456         while(top_pane.node.lastChild) top_pane.node.removeChild( top_pane.node.lastChild );
457         var children = bottom_pane.node.childNodes;
458         for (var i = 0; i < children.length; i++) {
459             if (children[i] != browser_frame) bottom_pane.node.removeChild(children[i]);
460         }
461
462         set_brief_view();
463         $('nav').setAttribute('hidden','false');
464         var settings = g.network.simple_request(
465             'FM_AUS_RETRIEVE',
466             [ ses(), g.data.list.au[0].id() ]
467         );
468         var view = settings['staff_client.catalog.record_view.default'];
469         if (g.view_override) {
470             view = g.view_override;
471             g.view_override = null;
472         }
473         switch(view) {
474             case 'marc_view' : set_marc_view(); break;
475             case 'marc_edit' : set_marc_edit(); break;
476             case 'copy_browser' : set_copy_browser(); break;
477             case 'hold_browser' : set_hold_browser(); break;
478             case 'opac' :
479             default: set_opac(); break;
480         }
481     } catch(E) {
482         g.error.standard_unexpected_error_alert('in refresh_display',E);
483     }
484 }
485
486 function set_default() {
487     var robj = g.network.simple_request(
488         'FM_AUS_UPDATE',
489         [ ses(), g.data.list.au[0].id(), { 'staff_client.catalog.record_view.default' : g.view } ]
490     )
491     if (typeof robj.ilsevent != 'undefined') {
492         if (robj.ilsevent != 0) g.error.standard_unexpected_error_alert(document.getElementById('offlineStrings').getString('cat.preference.error'), robj);
493     }
494 }
495
496 function add_volumes() {
497     try {
498         var edit = 0;
499         try {
500             edit = g.network.request(
501                 api.PERM_MULTI_ORG_CHECK.app,
502                 api.PERM_MULTI_ORG_CHECK.method,
503                 [ 
504                     ses(), 
505                     ses('staff_id'), 
506                     [ ses('ws_ou') ],
507                     [ 'CREATE_VOLUME', 'CREATE_COPY' ]
508                 ]
509             ).length == 0 ? 1 : 0;
510         } catch(E) {
511             g.error.sdump('D_ERROR','batch permission check: ' + E);
512         }
513
514         if (edit==0) {
515             alert(document.getElementById('offlineStrings').getString('staff.circ.copy_status.add_volumes.perm_failure'));
516             return; // no read-only view for this interface
517         }
518
519         var title = document.getElementById('offlineStrings').getFormattedString('staff.circ.copy_status.add_volumes.title', [docid]);
520
521         JSAN.use('util.window'); var win = new util.window();
522         var w = win.open(
523             window.xulG.url_prefix(urls.XUL_VOLUME_COPY_CREATOR),
524             title,
525             'chrome,resizable',
526             { 'doc_id' : docid, 'ou_ids' : [ ses('ws_ou') ] }
527         );
528     } catch(E) {
529         alert('Error in chrome/content/cat/opac.js, add_volumes(): ' + E);
530     }
531 }