LP#1084758 store staff client tpac (etc) zoom pref
[working/Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / util / browser.js
1 dump('entering util.browser.js\n');
2
3 if (typeof util == 'undefined') util = {};
4 util.browser = function (params) {
5     try {
6         JSAN.use('util.error'); this.error = new util.error();
7     } catch(E) {
8         dump('util.browser: ' + E + '\n');
9     }
10 }
11
12 util.browser.prototype = {
13
14     'lock_reload' : false, // as opposed to lock 'n load :)
15
16     'back_button_clicked' : false,
17     'from_back' : false,
18
19     'init' : function( params ) {
20
21         try {
22             var obj = this;
23
24             obj.url = params['url'];
25             obj.push_xulG = params['push_xulG'];
26             obj.html_source = params['html_source'];
27             obj.browser_id = params['browser_id'];
28             obj.debug_label = params['debug_label'];
29             obj.passthru_content_params = params['passthru_content_params'];
30             obj.on_url_load = params['on_url_load'];
31             obj.printer_context = params['printer_context'] || 'default';
32
33             JSAN.use('util.controller'); obj.controller = new util.controller();
34             obj.controller.init(
35                 {
36                     control_map : {
37                         'cmd_broken' : [
38                             ['command'],
39                             function() { alert('Not Yet Implemented'); }
40                         ],
41                         'cmd_debug' : [
42                             ['command'],
43                             function() {
44                                 var curr_url = obj.get_content().location.href;
45                                 var url = window.prompt('Original URL: ' + obj.url + '\nCurrent URL: ' + curr_url + '\nEnter new URL:',curr_url);
46                                 if (url) { obj.get_content().location.href = url; }
47                             }
48                         ],
49                         'cmd_view_source' : [
50                             ['command'],
51                             function() {
52                                 var curr_url = obj.get_content().location.href;
53                                 //obj.get_content().location.href = 'view-source:' + curr_url; // This works too, but the openDialog below is more feature-rich
54                                 window.openDialog("chrome://global/content/viewSource.xul", "", "all,dialog=no", curr_url);
55                             }
56                         ],
57                         'cmd_print' : [
58                             ['command'],
59                             function() {
60                                 try {
61                                     var content = obj.get_content();
62                                     JSAN.use('util.print'); var p = new util.print(obj.printer_context);
63                                     var print_params = {};
64                                     if (obj.html_source) {
65                                         print_params.msg = obj.html_source;
66                                         print_params.content_type = 'text/html';
67                                     }
68                                     if (typeof content.printable_output == 'function') {
69                                         print_params.msg = content.printable_output();
70                                         print_params.content_type = 'text/plain';
71                                         content = print_params.msg;
72                                     }
73                                     JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
74                                     // Override the print strategy temporarily if it's not set or is equal to webBrowserPrint (which is buggy here)
75                                     if (data.print_strategy) {
76                                         if (data.print_strategy[obj.printer_context] || data.print_strategy['default']) {
77                                             if (data.print_strategy[obj.printer_context]) {
78                                                 if (data.print_strategy[obj.printer_context] == 'webBrowserPrint') {
79                                                     print_params.print_strategy = 'window.print';
80                                                 }
81                                             } else {
82                                                 if (data.print_strategy['default'] == 'webBrowserPrint') {
83                                                     print_params.print_strategy = 'window.print';
84                                                 }
85                                             }
86                                         } else {
87                                             print_params.print_strategy = 'window.print';
88                                         }
89                                     } else {
90                                         print_params.print_strategy = 'window.print';
91                                     }
92                                     p.NSPrint(content,false,print_params);
93                                 } catch(E) {
94                                     alert('browser.js, cmd_print exception: ' + E);
95                                 }
96                             }
97                         ],
98                         'cmd_forward' : [
99                             ['command'],
100                             function() {
101                                 try {
102                                     var n = obj.getWebNavigation();
103                                     if (n.canGoForward) n.goForward();
104                                 } catch(E) {
105                                     var err = 'cmd_forward: ' + E;
106                                     obj.error.sdump('D_ERROR',err);
107                                 }
108                             }
109                         ],
110                         'cmd_back' : [
111                             ['command'],
112                             function() {
113                                 try {
114                                     var n = obj.getWebNavigation();
115                                     if (n.canGoBack) {
116                                         obj.back_button_clicked = true;
117                                         n.goBack();
118                                     }
119                                 } catch(E) {
120                                     var err = 'cmd_back: ' + E;
121                                     obj.error.sdump('D_ERROR',err);
122                                 }
123                             }
124                         ],
125                         'cmd_reload' : [
126                             ['command'],
127                             function() {
128                                 try {
129                                     obj.reload();
130                                 } catch(E) {
131                                     var err = 'cmd_reload: ' + E;
132                                     obj.error.sdump('D_ERROR',err);
133                                 }
134                             }
135                         ],
136                         'cmd_find' : [
137                             ['command'],
138                             function() {
139                                 var text = window.prompt('Enter text to find:');
140                                 obj.find(text);
141                             }
142                         ]
143                     }
144                 }
145             );
146             obj.controller.render();
147
148             var browser_id = 'browser_browser'; if (obj.browser_id) browser_id = obj.browser_id;
149             obj.controller.view.browser_browser = document.getElementById(browser_id);
150
151             obj.buildProgressListener();
152             /*
153             dump('obj.controller.view.browser_browser.addProgressListener = ' 
154                 + obj.controller.view.browser_browser.addProgressListener + '\n');
155             */
156             obj.controller.view.browser_browser.addProgressListener(obj.progressListener,
157                             Components.interfaces.nsIWebProgress.NOTIFY_ALL );
158
159             obj.controller.view.browser_browser.setAttribute('src',obj.url);
160             //dump('browser url = ' + obj.url + '\n');
161
162             window.help_context_set_locally = true;
163
164         } catch(E) {
165             this.error.sdump('D_ERROR','util.browser.init: ' + E + '\n');
166         }
167     },
168
169     /*
170      * If the browser page is given a name (e.g. the catalog is "Catalog") 
171      * and the user applies a zoom value (ctrl+ / ctrl-), store the value
172      * in the oils.util.browser.zoom.<name> pref.  On page load, if a value
173      * is set for the given page, load and apply the value to the page.
174      */
175     'apply_zoom' : function(val, delta) {
176         var name = xul_param('name');
177         if (!name) return;
178         var key = 'oils.util.browser.zoom.' + name; // pref key
179         var viewer = this.controller.view.browser_browser.markupDocumentViewer;
180
181         var prefs = Components.classes[
182             '@mozilla.org/preferences-service;1'].getService(
183                 Components.interfaces['nsIPrefBranch']);
184
185         if (!val) {
186             if (delta) {
187                 val = viewer.fullZoom;
188                 val += delta
189             } else {
190                 if (prefs.prefHasUserValue(key))
191                     val = prefs.getCharPref(key);
192             }
193         } 
194
195         val = Number(val);
196         if (!val || isNaN(val) || val < 0) return;
197
198         viewer.fullZoom = val;
199         prefs.setCharPref(key, ''+val);
200     },
201
202     'reload' : function() {
203         var obj = this;
204         if (obj.lock_reload) {
205             if (window.confirm( $('offlineStrings').getString('browser.reload.unsaved_data_warning') )) {
206                 obj.lock_reload = false;
207                 window.xulG.unlock_tab();
208             } else {
209                 return;
210             }
211         }
212         var n = obj.getWebNavigation();
213         n.reload( Components.interfaces.nsIWebNavigation.LOAD_FLAGS_NONE );
214     },
215
216     'find' : function(text) {
217         var obj = this;
218         try {
219             function getBrowser() {
220                 return obj.controller.view.browser_browser;
221             }
222
223             function getFocusedSelCtrl() {
224                 var ds = getBrowser().docShell;
225                 var dsEnum = ds.getDocShellEnumerator(Components.interfaces.nsIDocShellTreeItem.typeContent,
226                                                     Components.interfaces.nsIDocShell.ENUMERATE_FORWARDS);
227                 while (dsEnum.hasMoreElements()) {
228                     ds = dsEnum.getNext().QueryInterface(Components.interfaces.nsIDocShell);
229                     if (ds.hasFocus) {
230                         var display = ds.QueryInterface(Components.interfaces.nsIInterfaceRequestor).getInterface(Components.interfaces.nsISelectionDisplay);
231                         if (!display) return null;
232                         return display.QueryInterface(Components.interfaces.nsISelectionController);
233                     }
234                 }
235
236                 // One last try
237                 return getBrowser().docShell
238                     .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
239                     .getInterface(Components.interfaces.nsISelectionDisplay)
240                     .QueryInterface(Components.interfaces.nsISelectionController);
241             }
242
243             function setSelection(range) {
244                 try {
245                     var selctrlcomp = Components.interfaces.nsISelectionController;
246                     var selctrl = getFocusedSelCtrl();
247                     var sel = selctrl.getSelection(selctrlcomp.SELECTION_NORMAL);
248                     sel.removeAllRanges();
249                     sel.addRange(range.cloneRange());
250
251                     selctrl.scrollSelectionIntoView(selctrlcomp.SELECTION_NORMAL,
252                         selctrlcomp.SELECTION_FOCUS_REGION,
253                         true);
254                 } catch(e) {alert("setSelection: " + e);}
255             }
256
257             var doc = obj.get_content().document;
258             var body = doc.body;
259             var count = body.childNodes.length;
260             var finder = Components.classes["@mozilla.org/embedcomp/rangefind;1"].createInstance()
261                 .QueryInterface(Components.interfaces.nsIFind);
262             var searchRange = doc.createRange();
263             var startPt = doc.createRange();
264             var endPt = doc.createRange();
265                 searchRange.setStart(body,0);
266                 searchRange.setEnd(body, count);
267                 startPt.setStart(body, 0);
268                 startPt.setEnd(body, 0);
269                 endPt.setStart(body, count);
270                 endPt.setEnd(body, count);
271             var retRange = finder.Find(text, searchRange, startPt, endPt);
272             dump('retRange = ' + retRange + '\n');
273             setSelection(retRange);
274         } catch(E) {
275             alert('Error in browser.js, find(): ' + E);
276         }
277     },
278
279     'get_content' : function() {
280         try {
281             if (this.controller.view.browser_browser.contentWindow.wrappedJSObject) {
282                 return this.controller.view.browser_browser.contentWindow.wrappedJSObject;
283             } else {
284                 return this.controller.view.browser_browser.contentWindow;
285             }
286         } catch(E) {
287             this.error.sdump('D_ERROR','util.browser.get_content(): ' + E);
288         }
289     },
290
291     'push_variables' : function() {
292         try {
293             var obj = this;
294             var s = '';
295             try { s += obj.url + '\n' + obj.get_content().location.href + '\n'; } catch(E) { s+=E + '\n'; }
296             if (!obj.push_xulG) return;
297             var cw = this.get_content();
298             cw.IAMXUL = true;
299             cw.XUL_BUILD_ID = '/xul/server/'.split(/\//)[2];
300             cw.xulG = obj.passthru_content_params || {};
301             cw.xulG.fromBack = obj.from_back;
302             if (!cw.xulG.set_tab) { cw.xulG.set_tab = function(a,b,c) { return window.xulG.set_tab(a,b,c); }; }
303             if (!cw.xulG.new_tab) { cw.xulG.new_tab = function(a,b,c) { return window.xulG.new_tab(a,b,c); }; }
304             if (!cw.xulG.close_tab) { cw.xulG.close_tab = function(a) { return window.xulG.close_tab(a); }; }
305             if (!cw.xulG.lock_tab) {
306                 cw.xulG.lock_tab = function() {
307                     obj.lock_reload = true;
308                     return window.xulG.lock_tab();
309                 };
310             }
311             if (!cw.xulG.unlock_tab) {
312                 cw.xulG.unlock_tab = function() {
313                     obj.lock_reload = false;
314                     return window.xulG.unlock_tab();
315                 };
316             }
317             if (!cw.xulG.inspect_tab) { cw.xulG.inspect_tab = function() { return window.xulG.inspect_tab(); }; }
318             if (!cw.xulG.is_tab_locked) { cw.xulG.is_tab_locked = function() { return window.xulG.is_tab_locked(); }; }
319             if (!cw.xulG.new_patron_tab) { cw.xulG.new_patron_tab = function(a,b) { return window.xulG.new_patron_tab(a,b); }; }
320             if (!cw.xulG.set_patron_tab) { cw.xulG.set_patron_tab = function(a,b) { return window.xulG.set_patron_tab(a,b); }; }
321             if (!cw.xulG.volume_item_creator) { cw.xulG.volume_item_creator = function(a) { return window.xulG.volume_item_creator(a); }; }
322             if (!cw.xulG.get_new_session) { cw.xulG.get_new_session = function(a) { return window.xulG.get_new_session(a); }; }
323             if (!cw.xulG.holdings_maintenance_tab) { cw.xulG.holdings_maintenance_tab = function(a,b,c) { return window.xulG.holdings_maintenance_tab(a,b,c); }; }
324             if (!cw.xulG.url_prefix) { cw.xulG.url_prefix = function(url,secure) { return window.xulG.url_prefix(url,secure); }; }
325             if (!cw.xulG.urls) { cw.xulG.urls = window.urls; }
326             try { s += ('******** cw = ' + cw + ' cw.xulG = ' + (cw.xulG) + '\n'); } catch(E) { s+=E + '\n'; }
327             obj.error.sdump('D_BROWSER',s);
328         } catch(E) {
329             this.error.sdump('D_ERROR','util.browser.push_variables: ' + E + '\n');
330         }
331     },
332
333     'getWebNavigation' : function() {
334         try {
335             var wn = this.controller.view.browser_browser.webNavigation;
336             var s = this.url + '\n' + this.get_content().location.href + '\n';
337             s += ('getWebNavigation() = ' + wn + '\n');
338             //this.error.sdump('D_BROWSER',s);
339             return wn;
340         } catch(E) {
341             this.error.sdump('D_ERROR','util.browser.getWebNavigation(): ' + E );
342         }
343     },
344
345     'updateNavButtons' : function() {
346         var obj = this; 
347         var s = obj.url + '\n' + obj.get_content().location.href + '\n';
348         try {
349             var n = obj.getWebNavigation();
350             s += ('webNavigation = ' + n + '\n');
351             s += ('webNavigation.canGoForward = ' + n.canGoForward + '\n');
352             if (n.canGoForward) {
353                 if (typeof obj.controller.view.cmd_forward != 'undefined') {
354                     obj.controller.view.cmd_forward.disabled = false;
355                     obj.controller.view.cmd_forward.setAttribute('disabled','false');
356                 }
357             } else {
358                 if (typeof obj.controller.view.cmd_forward != 'undefined') {
359                     obj.controller.view.cmd_forward.disabled = true;
360                     obj.controller.view.cmd_forward.setAttribute('disabled','true');
361                 }
362             }
363         } catch(E) {
364             s += E + '\n';
365         }
366         try {
367             var n = obj.getWebNavigation();
368             s += ('webNavigation = ' + n + '\n');
369             s += ('webNavigation.canGoBack = ' + n.canGoBack + '\n');
370             if (n.canGoBack) {
371                 if (typeof obj.controller.view.cmd_back != 'undefined') {
372                     obj.controller.view.cmd_back.disabled = false;
373                     obj.controller.view.cmd_back.setAttribute('disabled','false');
374                 }
375             } else {
376                 if (typeof obj.controller.view.cmd_back != 'undefined') {
377                     obj.controller.view.cmd_back.disabled = true;
378                     obj.controller.view.cmd_back.setAttribute('disabled','true');
379                 }
380             }
381         } catch(E) {
382             s += E + '\n';
383         }
384         //this.error.sdump('D_BROWSER',s);
385
386         // Let's also update @protocol, @hostname, @port, and @pathname on the <help> widget
387         if (xulG.set_help_context) {
388             try {
389                 var help_params = {
390                     'protocol' : obj.get_content().location.protocol,
391                     'hostname' : obj.get_content().location.hostname,
392                     'port' : obj.get_content().location.port,
393                     'pathname' : obj.get_content().location.pathname,
394                     'src' : ''
395                 };
396                 xulG.set_help_context(help_params);
397             } catch(E) {
398                 dump('Error in browser.js, setting location on help widget: ' + E);
399             }
400         } else {
401             dump(location.href + ': browser.js, updateNavButtons, xulG = ' + xulG + ' xulG.set_help_context = ' + xulG.set_help_context + '\n');
402         }
403     },
404
405     'buildProgressListener' : function() {
406
407         try {
408             var obj = this;
409             obj.progressListener = {
410                 onProgressChange    : function(webProgress,request,curSelfProgress,maxSelfProgress,curTotalProgress,maxTotalProgress){
411                     try {
412                         /*dump('browser.js onProgressChange('
413                             +webProgress
414                             +','+request
415                             +','+curSelfProgress
416                             +','+maxSelfProgress
417                             +','+curTotalProgress
418                             +','+maxTotalProgress+')\n');*/
419                     } catch(E) {
420                         dump('error in util.browser.progresslistener.onProgressChange: ' + E + '\n');
421                     }
422                 },
423                 onLocationChange    : function(webProgress,request,uri){
424                     try {
425                         /*dump('browser.js onLocationChange('
426                             +webProgress
427                             +','+request
428                             +','+uri+')\n');*/
429                     } catch(E) {
430                         dump('error in util.browser.progresslistener.onLocationChange: ' + E + '\n');
431                     }
432                 },
433                 onStatusChange        : function(webProgress,request,status,message){
434                     try {
435                         /*dump('browser.js onStatusChange('
436                             +webProgress+','
437                             +request
438                             +','+status
439                             +','+message+')\n');*/
440                     } catch(E) {
441                         dump('error in util.browser.progresslistener.onStatusChange: ' + E + '\n');
442                     }
443                 },
444                 onSecurityChange    : function(webProgress,request,state){
445                     try {
446                         /*dump('browser.js onSecurityChange('
447                             +webProgress
448                             +','+request
449                             +','+state+')\n');*/
450                     } catch(E) {
451                         dump('error in util.browser.progresslistener.onSecurityChange: ' + E + '\n');
452                     }
453                 },
454                 onStateChange         : function ( webProgress, request, stateFlags, status) {
455                     try {
456                         /*dump('browser.js onStateChange('
457                             +webProgress
458                             +','+request
459                             +','+stateFlags
460                             +','+status+')\n');*/
461                         var s = obj.url + '\n' + obj.get_content().location.href + '\n';
462                         const nsIWebProgressListener = Components.interfaces.nsIWebProgressListener;
463                         const nsIChannel = Components.interfaces.nsIChannel;
464                         ////// handle the throbber
465                         var throbber = xulG.page_meter;
466                         if (throbber) {
467                             var busy = false;
468                             if (!(stateFlags & nsIWebProgressListener.STATE_RESTORING)) {
469                                 busy = true;
470                                 throbber.on();
471                             }
472                             if (stateFlags & nsIWebProgressListener.STATE_STOP) {
473                                 busy = false;
474                                 setTimeout(
475                                     function() {
476                                         if (!busy) { throbber.off(); }
477                                     }, 2000
478                                 );
479                             }
480                         }
481                         //////
482                         if ( (stateFlags & nsIWebProgressListener.STATE_IS_REQUEST
483                                 && stateFlags & nsIWebProgressListener.STATE_TRANSFERRING)
484                             || (stateFlags & nsIWebProgressListener.STATE_IS_REQUEST
485                                 && stateFlags & nsIWebProgressListener.STATE_START)
486                             || (stateFlags & nsIWebProgressListener.STATE_IS_REQUEST
487                                 && stateFlags & nsIWebProgressListener.STATE_STOP)
488                             ) {
489                             return;
490                         }
491                         s += ('onStateChange: stateFlags = ' + stateFlags + ' status = ' + status + '\n');
492                         if (stateFlags & nsIWebProgressListener.STATE_IS_REQUEST) {
493                             s += ('\tSTATE_IS_REQUEST\n');
494                         }
495                         if (stateFlags & nsIWebProgressListener.STATE_IS_DOCUMENT) {
496                             s += ('\tSTATE_IS_DOCUMENT\n');
497                             if( stateFlags & nsIWebProgressListener.STATE_STOP ) {
498                                 var alert_string = 'document has stopped: ' + new Date() + '\n'; dump(alert_string);
499                                 try {
500                                     obj.push_variables(); obj.updateNavButtons();
501                                     obj.apply_zoom();
502                                 } catch(E) {
503                                     var err_msg = 'browser.js STATE_IS_DOCUMENT STATE_STOP error with push_variables or updateNavButtons: ' + E + '\n';
504                                     dump(err_msg);
505                                     obj.error.sdump('D_ERROR',err_msg);
506                                 }
507                                 if (typeof obj.on_url_load == 'function') {
508                                     try {
509                                         obj.error.sdump('D_TRACE','calling on_url_load');
510                                         var helpers = {
511                                             'doc_write' : function(html) {
512                                                 obj.get_content().document.write(html);
513                                             },
514                                             'doc_close' : function() {
515                                                 obj.get_content().document.close();
516                                             }
517                                         }
518                                         obj.on_url_load( obj.controller.view.browser_browser, obj, helpers );
519                                     } catch(E) {
520                                         obj.error.sdump('D_ERROR','on_url_load: ' + E );
521                                     }
522                                 }
523                                 if (obj.debug_label) {
524                                     try {
525                                         document.getElementById(obj.debug_label).setAttribute('tooltiptext','url: ' + obj.get_content().location.href);
526                                     } catch(E) {
527                                         obj.error.sdump('D_ERROR','on_url_load, debug_label: ' + E );
528                                     }
529                                 }
530                             }
531                         }
532                         if (stateFlags & nsIWebProgressListener.STATE_IS_NETWORK) {
533                             s += ('\tSTATE_IS_NETWORK\n');
534                         }
535                         if (stateFlags & nsIWebProgressListener.STATE_IS_WINDOW) {
536                             s += ('\tSTATE_IS_WINDOW\n');
537                         }
538                         if (stateFlags & nsIWebProgressListener.STATE_START) {
539                             s += ('\tSTATE_START\n');
540                             obj.from_back = obj.back_button_clicked;
541                             obj.back_button_clicked = false;
542                         }
543                         if (stateFlags & nsIWebProgressListener.STATE_REDIRECTING) {
544                             s += ('\tSTATE_REDIRECTING\n');
545                         }
546                         if (stateFlags & nsIWebProgressListener.STATE_TRANSFERING) {
547                             s += ('\tSTATE_TRANSFERING\n');
548                         }
549                         if (stateFlags & nsIWebProgressListener.STATE_NEGOTIATING) {
550                             s += ('\tSTATE_NEGOTIATING\n');
551                         }
552                         if (stateFlags & nsIWebProgressListener.STATE_STOP) {
553                             s += ('\tSTATE_STOP\n');
554                         }
555                         //obj.error.sdump('D_BROWSER',s);    
556                         if (throbber) { throbber.tooltip(s); }
557                     } catch(E) {
558                         dump('error in util.browser.progresslistener.onStateChange: ' + E + '\n');
559                         obj.error.sdump('D_ERROR','util.browser.progresslistener.onstatechange: ' + (E));
560                     }
561                 }
562             }
563             obj.progressListener.QueryInterface = function(){return this;};
564         } catch(E) {
565             this.error.sdump('D_ERROR','util.browser.buildProgressListener: ' + E + '\n');
566         }
567     }
568 }
569
570 dump('exiting util.browser.js\n');