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