This fixes one class of caching problems with the chrome OPAC wrapper (all the differ...
[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                         'save' : {
77                                 'label' : document.getElementById('offlineStrings').getString('cat.save_record'),
78                                 'func' : function (new_marcxml) {
79                                         try {
80                                                 var r = g.network.simple_request('MARC_XML_RECORD_UPDATE', [ ses(), docid, new_marcxml ]);
81                                                 if (typeof r.ilsevent != 'undefined') {
82                                                         throw(r);
83                                                 } else {
84                                                         alert(document.getElementById('offlineStrings').getString("cat.save.success"));
85                                                 }
86                                         } catch(E) {
87                                                         g.error.standard_unexpected_error_alert(document.getElementById('offlineStrings').getString("cat.save.failure"), E);
88                                         }
89                                 }
90                         }
91                 };
92         if (marc_edit_reset) {
93                 bottom_pane.reset_iframe( a,b,c );
94         marc_edit_reset = false;
95         } else {
96                 bottom_pane.set_iframe( a,b,c );
97         }
98 }
99
100 function set_copy_browser() {
101         g.view = 'copy_browser';
102         if (copy_browser_reset) {
103                 bottom_pane.reset_iframe( xulG.url_prefix( urls.XUL_COPY_VOLUME_BROWSE ) + '?docid=' + window.escape(docid),{},xulG);
104         copy_browser_reset =false;
105         } else {
106                 bottom_pane.set_iframe( xulG.url_prefix( urls.XUL_COPY_VOLUME_BROWSE ) + '?docid=' + window.escape(docid),{},xulG);
107         }
108 }
109
110 function set_hold_browser() {
111         g.view = 'hold_browser';
112         if (hold_browser_reset) {
113                 bottom_pane.reset_iframe( xulG.url_prefix( urls.XUL_HOLDS_BROWSER ) + '?docid=' + window.escape(docid),{},xulG);
114         hold_browser_reset = false;
115         } else {
116                 bottom_pane.set_iframe( xulG.url_prefix( urls.XUL_HOLDS_BROWSER ) + '?docid=' + window.escape(docid),{},xulG);
117         }
118 }
119
120 function set_opac() {
121         g.view = 'opac';
122         try {
123                 var content_params = { 
124                         'show_nav_buttons' : true,
125                         'show_print_button' : true,
126                         'passthru_content_params' : { 
127                                 'authtoken' : ses(), 
128                                 'authtime' : ses('authtime'),
129                                 'window_open' : function(a,b,c) {
130                                         try {
131                                                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserWrite');
132                                                 return window.open(a,b,c);
133                                         } catch(E) {
134                                                 g.error.standard_unexpected_error_alert('window_open',E);
135                                         }
136                                 }
137                         },
138                         'on_url_load' : function(f) {
139                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
140                                 var win;
141                                 try {
142                                         if (typeof f.contentWindow.wrappedJSObject.attachEvt != 'undefined') {
143                                                 win = f.contentWindow.wrappedJSObject;
144                                         } else {
145                                                 win = f.contentWindow;
146                                         }
147                                 } catch(E) {
148                                         win = f.contentWindow;
149                                 }
150                                 win.attachEvt("rdetail", "recordRetrieved",
151                                         function(id){
152                                                 try {
153                                                         if (docid == id) return;
154                                                         docid = id;
155                                                         refresh_display(id);
156                                                 } catch(E) {
157                                                         g.error.standard_unexpected_error_alert('rdetail -> recordRetrieved',E);
158                                                 }
159                                         }
160                                 );
161                                 
162                                 g.f_record_start = null; g.f_record_prev = null; g.f_record_next = null; g.f_record_end = null;
163                                 $('record_start').disabled = true; $('record_next').disabled = true;
164                                 $('record_prev').disabled = true; $('record_end').disabled = true;
165                                 $('record_pos').setAttribute('value','');
166
167                                 win.attachEvt("rdetail", "nextPrevDrawn",
168                                         function(rIndex,rCount){
169                                                 $('record_pos').setAttribute('value', document.getElementById('offlineStrings').getFormattedString('cat.record.counter', [(1+rIndex), rCount]));
170                                                 if (win.rdetailNext) {
171                                                         g.f_record_next = function() { 
172                                                                 g.view_override = g.view; 
173                                                                 win.rdetailNext(); 
174                                                         }
175                                                         $('record_next').disabled = false;
176                                                 }
177                                                 if (win.rdetailPrev) {
178                                                         g.f_record_prev = function() { 
179                                                                 g.view_override = g.view; 
180                                                                 win.rdetailPrev(); 
181                                                         }
182                                                         $('record_prev').disabled = false;
183                                                 }
184                                                 if (win.rdetailStart) {
185                                                         g.f_record_start = function() { 
186                                                                 g.view_override = g.view; 
187                                                                 win.rdetailStart(); 
188                                                         }
189                                                         $('record_start').disabled = false;
190                                                 }
191                                                 if (win.rdetailEnd) {
192                                                         g.f_record_end = function() { 
193                                                                 g.view_override = g.view; 
194                                                                 win.rdetailEnd(); 
195                                                         }
196                                                         $('record_end').disabled = false;
197                                                 }
198                                         }
199                                 );
200                         },
201                         'url_prefix' : xulG.url_prefix,
202                 };
203                 if (opac_url) { content_params.url = opac_url; } else { content_params.url = xulG.url_prefix( urls.browser ); }
204                 browser_frame = bottom_pane.set_iframe( xulG.url_prefix(urls.XUL_BROWSER) + '?name=Catalog', {}, content_params);
205         } catch(E) {
206                 g.error.sdump('D_ERROR','set_opac: ' + E);
207         }
208 }
209
210 function bib_in_new_tab() {
211         try {
212                 var url = browser_frame.contentWindow.g.browser.controller.view.browser_browser.contentWindow.wrappedJSObject.location.href;
213                 var content_params = { 'session' : ses(), 'authtime' : ses('authtime'), 'opac_url' : url };
214                 xulG.new_tab(xulG.url_prefix(urls.XUL_OPAC_WRAPPER), {}, content_params);
215         } catch(E) {
216                 g.error.sdump('D_ERROR',E);
217         }
218 }
219
220 function remove_me() {
221         var url = xulG.url_prefix( urls.XUL_BIB_BRIEF ) + '?docid=' + window.escape(docid);
222         dump('removing ' + url + '\n');
223         try { top_pane.remove_iframe( url ); } catch(E) { dump(E + '\n'); }
224         $('nav').setAttribute('hidden','true');
225 }
226
227 function add_to_bucket() {
228         JSAN.use('util.window'); var win = new util.window();
229         win.open(
230                 xulG.url_prefix(urls.XUL_RECORD_BUCKETS_QUICK)
231                 + '?record_ids=' + js2JSON( [ docid ] ),
232                 'sel_bucket_win' + win.window_name_increment(),
233                 'chrome,resizable,modal,center'
234         );
235 }
236
237 function mark_for_overlay() {
238         g.data.marked_record = docid;
239         g.data.stash('marked_record');
240 }
241
242 function delete_record() {
243         if (g.error.yns_alert(
244                 document.getElementById('offlineStrings').getFormattedString('cat.opac.delete_record.confirm', [docid]),
245                 document.getElementById('offlineStrings').getString('cat.opac.delete_record'),
246                 document.getElementById('offlineStrings').getString('cat.opac.delete'),
247                 document.getElementById('offlineStrings').getString('cat.opac.cancel'),
248                 null,
249                 document.getElementById('offlineStrings').getString('cat.opac.record_deleted.confirm')) == 0) {
250                 var robj = g.network.simple_request('FM_BRE_DELETE',[ses(),docid]);
251                 if (typeof robj.ilsevent != 'undefined') {
252                         alert(document.getElementById('offlineStrings').getFormattedString('cat.opac.record_deleted.error',  [docid, robj.textcode, robj.desc]) + '\n');
253                 } else {
254                         alert(document.getElementById('offlineStrings').getString('cat.opac.record_deleted'));
255                         refresh_display(docid);
256                 }
257         }
258 }
259
260 function undelete_record() {
261     if (g.error.yns_alert(
262                 document.getElementById('offlineStrings').getFormattedString('cat.opac.undelete_record.confirm', [docid]),
263                 document.getElementById('offlineStrings').getString('cat.opac.undelete_record'),
264                 document.getElementById('offlineStrings').getString('cat.opac.undelete'),
265                 document.getElementById('offlineStrings').getString('cat.opac.cancel'),
266                 null,
267                 document.getElementById('offlineStrings').getString('cat.opac.record_undeleted.confirm')) == 0) {
268
269         var robj = g.network.simple_request('FM_BRE_UNDELETE',[ses(),docid]);
270         if (typeof robj.ilsevent != 'undefined') {
271                         alert(document.getElementById('offlineStrings').getFormattedString('cat.opac.record_undeleted.error',  [docid, robj.textcode, robj.desc]) + '\n');
272         } else {
273                         alert(document.getElementById('offlineStrings').getString('cat.opac.record_undeleted'));
274                         refresh_display(docid);
275         }
276     }
277 }
278
279 function refresh_display(id) {
280         try { 
281         marc_view_reset = true;
282         marc_edit_reset = true;
283         copy_browser_reset = true;
284         hold_browser_reset = true;
285                 while(top_pane.node.lastChild) top_pane.node.removeChild( top_pane.node.lastChild );
286                 var children = bottom_pane.node.childNodes;
287                 for (var i = 0; i < children.length; i++) {
288                         if (children[i] != browser_frame) bottom_pane.node.removeChild(children[i]);
289                 }
290
291                 set_brief_view();
292                 $('nav').setAttribute('hidden','false');
293                 var settings = g.network.simple_request(
294                         'FM_AUS_RETRIEVE',
295                         [ ses(), g.data.list.au[0].id() ]
296                 );
297                 var view = settings['staff_client.catalog.record_view.default'];
298                 if (g.view_override) {
299                         view = g.view_override;
300                         g.view_override = null;
301                 }
302                 switch(view) {
303                         case 'marc_view' : set_marc_view(); break;
304                         case 'marc_edit' : set_marc_edit(); break;
305                         case 'copy_browser' : set_copy_browser(); break;
306                         case 'hold_browser' : set_hold_browser(); break;
307                         case 'opac' :
308                         default: set_opac(); break;
309                 }
310         } catch(E) {
311                 g.error.standard_unexpected_error_alert('in refresh_display',E);
312         }
313 }
314
315 function set_default() {
316         var robj = g.network.simple_request(
317                 'FM_AUS_UPDATE',
318                 [ ses(), g.data.list.au[0].id(), { 'staff_client.catalog.record_view.default' : g.view } ]
319         )
320         if (typeof robj.ilsevent != 'undefined') {
321                 if (robj.ilsevent != 0) g.error.standard_unexpected_error_alert(document.getElementById('offlineStrings').getString('cat.preference.error'), robj);
322         }
323 }
324
325