there was a race condition between the print window rendering and the print call...
authorpines <pines@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Wed, 1 Nov 2006 19:39:25 +0000 (19:39 +0000)
committerpines <pines@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Wed, 1 Nov 2006 19:39:25 +0000 (19:39 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@6538 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/xul/staff_client/chrome/content/util/print.js
Open-ILS/xul/staff_client/server/admin/printer_settings.html
Open-ILS/xul/staff_client/server/admin/printer_settings.js
Open-ILS/xul/staff_client/server/circ/util.js

index b8231f1..8b4cf5b 100644 (file)
@@ -53,27 +53,42 @@ util.print.prototype = {
                        var w;
                        switch(content_type) {
                                case 'text/html' :
-                                       var jsrc = 'data:text/javascript,' + window.escape('var params = { "data" : ' + js2JSON(params.data) + ', "list" : ' + js2JSON(params.list) + '}; function my_init() { return; /* FIXME - mozilla bug#301560 - xpcom kills it too */ if (' + (typeof params.modal != 'undefined' ? 'true' : 'false') + ') setTimeout(function(){ try { window.print(); window.close(); } catch(E) { alert(E); } },0); }');
-                                       w = obj.win.open('data:text/html,<html><head><script src="/xul/server/main/JSAN.js"></script><script src="' + window.escape(jsrc) + '"></script></head><body onload="try{my_init();}catch(E){alert(E);}">' + window.escape(msg) + '</body></html>','receipt_temp','chrome,resizable');
+                                       var jsrc = 'data:text/javascript,' + window.escape('var params = { "data" : ' + js2JSON(params.data) + ', "list" : ' + js2JSON(params.list) + '}; function my_init() { return go_print(); /* FIXME - mozilla bug#301560 - xpcom kills it too */ if (' + (typeof params.modal != 'undefined' ? 'true' : 'false') + ') setTimeout(function(){ try { window.print(); window.close(); } catch(E) { alert(E); } },0); }');
+                                       w = obj.win.open('data:text/html,<html id="top"><head><script src="/xul/server/main/JSAN.js"></script><script src="' + window.escape(jsrc) + '"></script></head><body onload="try{my_init();}catch(E){alert(E);}">' + window.escape(msg) + '</body></html>','receipt_temp','chrome,resizable');
+                                       w.minimize();
+                                       w.go_print = function() { 
+
+                                               setTimeout(
+                                                       function() {
+                                                               try {
+                                                                       obj.NSPrint(w, silent, params);
+                                                               } catch(E) {
+                                                                       obj.error.sdump('D_ERROR','util.print.simple: ' + E);
+                                                                       w.print();
+                                                               }
+                                                               w.minimize(); w.close();
+                                                       }, 0
+                                               );
+
+                                       }
                                break;
                                default:
                                        w = obj.win.open('data:' + content_type + ',' + window.escape(msg),'receipt_temp','chrome,resizable');
+                                       w.minimize();
+                                       setTimeout(
+                                               function() {
+                                                       try {
+                                                               obj.NSPrint(w, silent, params);
+                                                       } catch(E) {
+                                                               obj.error.sdump('D_ERROR','util.print.simple: ' + E);
+                                                               w.print();
+                                                       }
+                                                       w.minimize(); w.close();
+                                               }, 1000
+                                       );
                                break;
                        }
 
-                       w.minimize();
-
-                       setTimeout(
-                               function() {
-                                       try {
-                                               obj.NSPrint(w, silent, params);
-                                       } catch(E) {
-                                               obj.error.sdump('D_ERROR','util.print.simple: ' + E);
-                                               w.print();
-                                       }
-                                       w.minimize(); w.close();
-                               }, 0
-                       );
                } catch(E) {
                        this.error.standard_unexpected_error_alert('util.print.simple',E);
                }
@@ -181,7 +196,7 @@ util.print.prototype = {
 
                if (params.sample_frame) {
                        var jsrc = 'data:text/javascript,' + window.escape('var params = { "data" : ' + js2JSON(params.data) + ', "list" : ' + js2JSON(params.list) + '};');
-                       params.sample_frame.setAttribute('src','data:text/html,<html><head><script src="' + window.escape(jsrc) + '"></script></head><body>' + window.escape(s) + '</body></html>');
+                       params.sample_frame.setAttribute('src','data:text/html,<html id="top"><head><script src="' + window.escape(jsrc) + '"></script></head><body>' + window.escape(s) + '</body></html>');
                } else {
                        this.simple(s,params);
                }
@@ -269,56 +284,89 @@ util.print.prototype = {
                var obj = this;
                try {
                        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
+
+                       if (obj.data.print_strategy) {
+
+                               switch(obj.data.print_strategy) {
+                                       case 'dos.print':
+                                               obj._NSPrint_dos_print(w,silent,params);
+                                       break;  
+                                       case 'window.print':
+                                               w.print();
+                                       break;  
+                                       case 'webBrowserPrint':
+                                               obj._NSPrint_webBrowserPrint(w,silent,params);
+                                       break;  
+                                       default:
+                                               //w.print();
+                                               obj._NSPrint_webBrowserPrint(w,silent,params);
+                                       break;  
+                               }
+
+                       } else {
+                               //w.print();
+                               obj._NSPrint_webBrowserPrint(w,silent,params);
+                       }
+
+               } catch (e) {
+                       //alert('Probably not printing: ' + e);
+                       this.error.sdump('D_ERROR','PRINT EXCEPTION: ' + js2JSON(e) + '\n');
+               }
+
+       },
+
+       '_NSPrint_dos_print' : function(w,silent,params) {
+               var obj = this;
+               try {
+                       /* This is a kludge/workaround.  webBrowserPrint doesn't always work.  So we're going to let
+                               the html window handle our receipt template rendering, and then force a selection of all
+                               the text nodes and dump that to a file, for printing through a dos utility */
+
+                       netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
+                       w.getSelection().selectAllChildren(w.document.firstChild);
+                       var text = w.getSelection().toString();
+
+                       JSAN.use('util.file'); var file = new util.file('receipt.txt');
+                       file.write_content('truncate',text); file.close();
+                       
+                       file = new util.file('receipt.bat');
+                       if (! file._file.exists()) { 
+                               file.write_content('truncate','copy chrome\\open_ils_staff_client\\content\\conf\\receipt.txt lpt1 /b\n');
+                               file.close();
+                               file = new util.file('receipt.bat');
+                       }
+
+                       var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
+                       process.init(file._file);
+
+                       var args = [];
+
+                       process.run(true, args, args.length);
+
+               } catch (e) {
+                       //alert('Probably not printing: ' + e);
+                       this.error.sdump('D_ERROR','PRINT EXCEPTION: ' + js2JSON(e) + '\n');
+               }
+       },
+
+       '_NSPrint_webBrowserPrint' : function(w,silent,params) {
+               var obj = this;
+               try {
+                       netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
                        var webBrowserPrint = w
                                .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                                .getInterface(Components.interfaces.nsIWebBrowserPrint);
                        this.error.sdump('D_PRINT','webBrowserPrint = ' + webBrowserPrint);
                        if (webBrowserPrint) {
                                var gPrintSettings = obj.GetPrintSettings();
-                               //obj.error.standard_unexpected_error_alert('debugging printer settings #1',gPrintSettings);
-                               //var s = '1: '; for (var i in gPrintSettings) if (i.match(/^paper/)) s += i + ': ' + gPrintSettings[i] + '\n'; alert(s);
                                if (silent) gPrintSettings.printSilent = true;
                                else gPrintSettings.printSilent = false;
-                               //alert('silent = ' + silent + ' printSilent = ' + gPrintSettings.printSilent);
                                if (params) {
-                                       /* They can set these now in Local Admin */
-                                       /*
-                                       gPrintSettings.marginTop = 0;
-                                       gPrintSettings.marginLeft = 0;
-                                       gPrintSettings.marginBottom = 0;
-                                       gPrintSettings.marginRight = 0;
-                                       */
                                        if (params.marginLeft) gPrintSettings.marginLeft = params.marginLeft;
                                }
-                               /*
-                               gPrintSettings.headerStrLeft = '';
-                               gPrintSettings.headerStrCenter = '';
-                               gPrintSettings.headerStrRight = '';
-                               gPrintSettings.footerStrLeft = '';
-                               gPrintSettings.footerStrCenter = '';
-                               gPrintSettings.footerStrRight = '';
-                               */
-                               //this.error.sdump('D_PRINT','gPrintSettings = ' + obj.error.pretty_print(js2JSON(gPrintSettings)));
-                               //alert('gPrintSettings = ' + js2JSON(gPrintSettings));
                                webBrowserPrint.print(gPrintSettings, null);
-                               //var s = '2: '; for (var i in gPrintSettings) if (i.match(/^paper/)) s += i + ': ' + gPrintSettings[i] + '\n'; alert(s);
-
-                               /* This isn't working for kInitSavePageData, so we're going to save gPrintSettings ourselves from the local admin screen */
-                               /*
-                               if (this.gPrintSettingsAreGlobal && this.gSavePrintSettings) {
-                                       var PSSVC = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
-                                               .getService(Components.interfaces.nsIPrintSettingsService);
-                                       PSSVC.savePrintSettingsToPrefs( gPrintSettings, true, gPrintSettings.kInitSaveAll);
-                                       PSSVC.savePrintSettingsToPrefs( gPrintSettings, false, gPrintSettings.kInitSavePrinterName);
-                               }
-                               */
-                               //var s = '3: '; for (var i in gPrintSettings) if (i.match(/^paper/)) s += i + ': ' + gPrintSettings[i] + '\n'; alert(s);
-                               //obj.error.standard_unexpected_error_alert('debugging printer settings #3',gPrintSettings);
-                               //this.error.sdump('D_PRINT','gPrintSettings 2 = ' + obj.error.pretty_print(js2JSON(gPrintSettings)));
-                               //alert('Should be printing\n');
                                this.error.sdump('D_PRINT','Should be printing\n');
                        } else {
-                               //alert('Should not be printing\n');
                                this.error.sdump('D_ERROR','Should not be printing\n');
                        }
                } catch (e) {
@@ -328,7 +376,6 @@ util.print.prototype = {
                        // Unfortunately this will also consume helpful failures
                        this.error.sdump('D_ERROR','PRINT EXCEPTION: ' + js2JSON(e) + '\n');
                }
-
        },
 
        'GetPrintSettings' : function() {
index 49cd7e2..fdb05b3 100644 (file)
@@ -1,12 +1,29 @@
 <html><head>
-       <script type="text/javascript" src="/opac/common/js/JSON.js" />
+
+       <script type="text/javascript" src="/opac/common/js/utils.js"></script>
+       <script type="text/javascript" src="/opac/common/js/CGI.js"></script>
+       <script type="text/javascript" src="/opac/common/js/md5.js"></script>
+       <script type="text/javascript" src="/opac/common/js/JSON.js"></script>
+       <script type="text/javascript" src="/opac/common/js/fmall.js"></script>
+       <script type="text/javascript" src="/opac/common/js/fmgen.js"></script>
+       <script type="text/javascript" src="/opac/common/js/RemoteRequest.js"></script>
+       <script type="text/javascript" src="/opac/common/js/OrgTree.js"></script>
+       <script type="text/javascript" src="/opac/common/js/org_utils.js"></script>
+
        <script type="text/javascript" src="/xul/server/OpenILS/global_utils.js"></script>
        <script type="text/javascript" src="/xul/server/main/constants.js"></script>
        <script type="text/javascript" src="/xul/server/main/JSAN.js"></script>
        <script type="text/javascript" src="printer_settings.js"></script>
 </head><body onload="try { my_init(); } catch(E) { alert(E); }" style="background: white;">
+       <h1>Normal Settings</h1>
        <iframe id="sample" src="printer_settings.txt"></iframe><br />
        <button onclick="try { g.printer_settings(); } catch(E) { alert(E); }">Set Default Printer and Print Test Page</button>
        <button onclick="try { g.page_settings(); } catch(E) { alert(E); }">Page Settings</button><br />
+       <h1>Advanced Settings</h1>
+       <button onclick="try { g.set_print_strategy('webBrowserPrint'); } catch(E) { alert(E); }">Use default print strategy (Mozilla Print)</button>
+       <button onclick="try { g.set_print_strategy('dos.print'); } catch(E) { alert(E); }">Use alternate print strategy (DOS LPT1 Print)</button>
+       <p>
+               <b>Warning:</b> The alternate (DOS LPT1) print strategy will ignore the printer settings made in the "Normal Settings" section.  In Windows, you must map your printer to the LPT1 port, under Start Menu -&gt; Printers and Faxes -&gt; your printer -&gt; right-click, Properties -&gt; Ports.  Also, HTML styling such as different font weights and sizes will be lost when using the DOS LPT1 print.  Data is sent to the printer as simple text in this case.
+       </p>
 </body></html>
 
index e768b43..80c759a 100644 (file)
@@ -37,4 +37,14 @@ g.printer_settings = function() {
        g.print.save_settings();
 }
 
+g.set_print_strategy = function(which) {
+       netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
+       JSAN.use('util.file'); var file = new util.file('print_strategy');
+       file.write_content( 'truncate', String( which ) );
+       file.close();
+       JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
+       data.print_strategy = which; data.stash('print_strategy');
+       alert('Print strategy (' + which + ') saved to file system.');
+}
+
 g.save_settings = function() { g.print.save_settings(); }
index 983060b..ecd96f4 100644 (file)
@@ -1133,7 +1133,8 @@ circ.util.checkin_via_barcode2 = function(session,params,backdate,auto_print,che
                                        if (rv == 0) {
                                                try {
                                                        JSAN.use('util.print'); var print = new util.print();
-                                                       print.simple( msg, { 'no_prompt' : true, 'content_type' : 'text/plain' } );
+                                                       msg = msg.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n/g,'<br/>');
+                                                       print.simple( msg , { 'no_prompt' : true, 'content_type' : 'text/html' } );
                                                } catch(E) {
                                                        dump('Please inform your helpdesk/developers of this error:\nFIXME: ' + E + '\n');
                                                        alert('Please inform your helpdesk/developers of this error:\nFIXME: ' + E + '\n');
@@ -1218,7 +1219,9 @@ circ.util.checkin_via_barcode2 = function(session,params,backdate,auto_print,che
                        if (rv == 0) {
                                try {
                                        JSAN.use('util.print'); var print = new util.print();
-                                       print.simple( msg, { 'no_prompt' : true, 'content_type' : 'text/plain' } );
+                                       //print.simple( msg, { 'no_prompt' : true, 'content_type' : 'text/plain' } );
+                                       msg = msg.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n/g,'<br/>');
+                                       print.simple( msg , { 'no_prompt' : true, 'content_type' : 'text/html' } );
                                } catch(E) {
                                        dump('Please inform your helpdesk/developers of this error:\nFIXME: ' + E + '\n');
                                        alert('Please inform your helpdesk/developers of this error:\nFIXME: ' + E + '\n');