]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/util/print.js
avoid another window.open error
[working/Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / util / print.js
1 dump('entering util/print.js\n');
2
3 if (typeof util == 'undefined') util = {};
4 util.print = function () {
5
6         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
7
8         JSAN.use('util.error'); this.error = new util.error();
9         JSAN.use('OpenILS.data'); this.data = new OpenILS.data(); this.data.init( { 'via':'stash' } );
10         JSAN.use('util.window'); this.win = new util.window();
11         JSAN.use('util.functional');
12         JSAN.use('util.file');
13
14         return this;
15 };
16
17 util.print.prototype = {
18
19         'reprint_last' : function() {
20                 try {
21                         var obj = this; obj.data.init({'via':'stash'});
22                         if (!obj.data.last_print) {
23                                 alert('Nothing to re-print');
24                                 return;
25                         }
26                         var msg = obj.data.last_print.msg;
27                         var params = obj.data.last_print.params; params.no_prompt = false;
28                         obj.simple( msg, params );
29                 } catch(E) {
30                         this.error.standard_unexpected_error_alert('util.print.reprint_last',E);
31                 }
32         },
33
34         'simple' : function(msg,params) {
35                 try {
36                         if (!params) params = {};
37
38                         var obj = this;
39
40                         obj.data.last_print = { 'msg' : msg, 'params' : params}; obj.data.stash('last_print');
41
42                         var silent = false;
43                         if ( params && params.no_prompt && (params.no_prompt == true || params.no_prompt == 'true') ) {
44                                 silent = true;
45                         }
46
47                         var content_type;
48                         if (params && params.content_type) {
49                                 content_type = params.content_type;
50                         } else {
51                                 content_type = 'text/html';
52                         }
53
54                         var w;
55                         switch(content_type) {
56                                 case 'text/html' :
57                                         var jsrc = 'data:text/javascript,' + window.escape('var params = { "data" : ' + js2JSON(params.data) + ', "list" : ' + js2JSON(params.list) + '}; function my_init() { if (typeof go_print == "function") { go_print(); } else { alert("Please inform the developers that the go_print bug occurred.  After this alert, we will try to print again."); window.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); }');
58                                         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');
59                                         w.minimize();
60                                         w.go_print = function() { 
61
62                                                 //setTimeout(
63                                                 //      function() {
64                                                                 try {
65                                                                         obj.NSPrint(w, silent, params);
66                                                                 } catch(E) {
67                                                                         obj.error.standard_unexpected_error_alert("Print Error in util.print.simple.  After this dialog we'll try a second print attempt. content_type = " + content_type,E);
68                                                                         w.print();
69                                                                 }
70                                                                 w.minimize(); w.close();
71                                                 //      }, 0
72                                                 //);
73
74                                         }
75                                 break;
76                                 default:
77                                         w = obj.win.open('data:' + content_type + ',' + window.escape(msg),'receipt_temp','chrome,resizable');
78                                         w.minimize();
79                                         setTimeout(
80                                                 function() {
81                                                         try {
82                                                                 obj.NSPrint(w, silent, params);
83                                                         } catch(E) {
84                                                                 obj.error.standard_unexpected_error_alert("Print Error in util.print.simple.  After this dialog we'll try a second print attempt. content_type = " + content_type,E);
85                                                                 w.print();
86                                                         }
87                                                         w.minimize(); w.close();
88                                                 }, 1000
89                                         );
90                                 break;
91                         }
92
93                 } catch(E) {
94                         this.error.standard_unexpected_error_alert('util.print.simple',E);
95                 }
96         },
97         
98         'tree_list' : function (params) { 
99                 try {
100                         dump('print.tree_list.params.list = \n' + this.error.pretty_print(js2JSON(params.list)) + '\n');
101                 } catch(E) {
102                         dump(E+'\n');
103                 }
104                 var cols = [];
105
106                 var s = '';
107                 if (params.header) s += this.template_sub( params.header, cols, params );
108                 if (params.list) {
109                         for (var i = 0; i < params.list.length; i++) {
110                                 params.row = params.list[i];
111                                 s += this.template_sub( params.line_item, cols, params );
112                         }
113                 }
114                 if (params.footer) s += this.template_sub( params.footer, cols, params );
115
116                 if (params.sample_frame) {
117                         var jsrc = 'data:text/javascript,' + window.escape('var params = { "data" : ' + js2JSON(params.data) + ', "list" : ' + js2JSON(params.list) + '};');
118                         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>');
119                 } else {
120                         this.simple(s,params);
121                 }
122         },
123
124         'template_sub' : function( msg, cols, params ) {
125                 if (!msg) { dump('template sub called with empty string\n'); return; }
126                 JSAN.use('util.date');
127                 var s = msg; var b;
128
129                 try{b = s; s = s.replace(/%patron_barcode%/,params.patron_barcode);}
130                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
131
132                 try{b = s; s = s.replace(/%LIBRARY%/,params.lib.name());}
133                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
134                 try{b = s; s = s.replace(/%PINES_CODE%/,params.lib.shortname());}
135                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
136                 try{b = s; s = s.replace(/%STAFF_FIRSTNAME%/,params.staff.first_given_name());}
137                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
138                 try{b = s; s = s.replace(/%STAFF_LASTNAME%/,params.staff.family_name());}
139                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
140                 try{b = s; s = s.replace(/%STAFF_BARCODE%/,params.staff.barcode); }
141                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
142                 try{b = s; s = s.replace(/%STAFF_PROFILE%/,obj.data.hash.pgt[ params.staff.profile() ].name() ); }
143                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
144                 try{b = s; s = s.replace(/%PATRON_FIRSTNAME%/,params.patron.first_given_name());}
145                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
146                 try{b = s; s = s.replace(/%PATRON_LASTNAME%/,params.patron.family_name());}
147                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
148                 try{b = s; s = s.replace(/%PATRON_BARCODE%/,typeof params.patron.card() == 'object' ? params.patron.card().barcode() : util.functional.find_id_object_in_list( params.patron.cards(), params.patron.card() ).barcode() ) ;}
149                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
150
151                 try{b = s; s=s.replace(/%TODAY%/g,(new Date()));}
152                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
153                 try{b = s; s=s.replace(/%TODAY_m%/g,(util.date.formatted_date(new Date(),'%m')));}
154                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
155                 try{b = s; s=s.replace(/%TODAY_TRIM%/g,(util.date.formatted_date(new Date(),'')));}
156                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
157                 try{b = s; s=s.replace(/%TODAY_d%/g,(util.date.formatted_date(new Date(),'%d')));}
158                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
159                 try{b = s; s=s.replace(/%TODAY_Y%/g,(util.date.formatted_date(new Date(),'%Y')));}
160                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
161                 try{b = s; s=s.replace(/%TODAY_H%/g,(util.date.formatted_date(new Date(),'%H')));}
162                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
163                 try{b = s; s=s.replace(/%TODAY_I%/g,(util.date.formatted_date(new Date(),'%I')));}
164                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
165                 try{b = s; s=s.replace(/%TODAY_M%/g,(util.date.formatted_date(new Date(),'%M')));}
166                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
167                 try{b = s; s=s.replace(/%TODAY_D%/g,(util.date.formatted_date(new Date(),'%D')));}
168                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
169                 try{b = s; s=s.replace(/%TODAY_F%/g,(util.date.formatted_date(new Date(),'%F')));}
170                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
171
172                 try {
173                         if (typeof params.row != 'undefined') {
174                                 if (params.row.length >= 0) {
175                                         alert('debug - please tell the developers that deprecated template code tried to execute');
176                                         for (var i = 0; i < cols.length; i++) {
177                                                 var re = new RegExp(cols[i],"g");
178                                                 try{b = s; s=s.replace(re, params.row[i]);}
179                                                         catch(E){s = b; this.error.standard_unexpected_error_alert('string = <' + s + '> error = ' + js2JSON(E)+'\n',E);}
180                                         }
181                                 } else { 
182                                         /* for dump_with_keys */
183                                         for (var i in params.row) {
184                                                 var re = new RegExp('%'+i+'%',"g");
185                                                 try{b = s; s=s.replace(re, params.row[i]);}
186                                                         catch(E){s = b; this.error.standard_unexpected_error_alert('string = <' + s + '> error = ' + js2JSON(E)+'\n',E);}
187                                         }
188                                 }
189                         }
190
191                         if (typeof params.data != 'undefined') {
192                                 for (var i in params.data) {
193                                         var re = new RegExp('%'+i+'%',"g");
194                                         try{b = s; s=s.replace(re, params.data[i]);}
195                                                 catch(E){s = b; this.error.standard_unexpected_error_alert('string = <' + s + '> error = ' + js2JSON(E)+'\n',E);}
196                                 }
197                         }
198                 } catch(E) { dump(E+'\n'); }
199
200                 return s;
201         },
202
203
204         'NSPrint' : function(w,silent,params) {
205                 if (!w) w = window;
206                 var obj = this;
207                 try {
208                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
209
210                         if (params.print_strategy || obj.data.print_strategy) {
211
212                                 switch(params.print_strategy || obj.data.print_strategy) {
213                                         case 'dos.print':
214                                                 obj._NSPrint_dos_print(w,silent,params);
215                                         break;  
216                                         case 'window.print':
217                                                 w.print();
218                                         break;  
219                                         case 'webBrowserPrint':
220                                                 obj._NSPrint_webBrowserPrint(w,silent,params);
221                                         break;  
222                                         default:
223                                                 //w.print();
224                                                 obj._NSPrint_webBrowserPrint(w,silent,params);
225                                         break;  
226                                 }
227
228                         } else {
229                                 //w.print();
230                                 obj._NSPrint_webBrowserPrint(w,silent,params);
231                         }
232
233                 } catch (e) {
234                         //alert('Probably not printing: ' + e);
235                         this.error.sdump('D_ERROR','PRINT EXCEPTION: ' + js2JSON(e) + '\n');
236                 }
237
238         },
239
240         '_NSPrint_dos_print' : function(w,silent,params) {
241                 var obj = this;
242                 try {
243                         /* This is a kludge/workaround.  webBrowserPrint doesn't always work.  So we're going to let
244                                 the html window handle our receipt template rendering, and then force a selection of all
245                                 the text nodes and dump that to a file, for printing through a dos utility */
246
247                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
248                         w.getSelection().selectAllChildren(w.document.firstChild);
249                         var text = w.getSelection().toString();
250
251                         var file = new util.file('receipt.txt');
252                         file.write_content('truncate',text); file.close();
253                         
254                         file = new util.file('receipt.bat');
255                         if (! file._file.exists()) { 
256                                 file.write_content('truncate','copy chrome\\open_ils_staff_client\\content\\conf\\receipt.txt lpt1 /b\n');
257                                 file.close();
258                                 file = new util.file('receipt.bat');
259                         }
260
261                         var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
262                         process.init(file._file);
263
264                         var args = [];
265
266                         process.run(true, args, args.length);
267
268                 } catch (e) {
269                         //alert('Probably not printing: ' + e);
270                         this.error.sdump('D_ERROR','_NSPrint_dos_print PRINT EXCEPTION: ' + js2JSON(e) + '\n');
271                 }
272         },
273
274         '_NSPrint_webBrowserPrint' : function(w,silent,params) {
275                 var obj = this;
276                 try {
277                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
278                         var webBrowserPrint = w
279                                 .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
280                                 .getInterface(Components.interfaces.nsIWebBrowserPrint);
281                         this.error.sdump('D_PRINT','webBrowserPrint = ' + webBrowserPrint);
282                         if (webBrowserPrint) {
283                                 var gPrintSettings = obj.GetPrintSettings();
284                                 if (silent) gPrintSettings.printSilent = true;
285                                 else gPrintSettings.printSilent = false;
286                                 if (params) {
287                                         if (params.marginLeft) gPrintSettings.marginLeft = params.marginLeft;
288                                 }
289                                 webBrowserPrint.print(gPrintSettings, null);
290                                 this.error.sdump('D_PRINT','Should be printing\n');
291                         } else {
292                                 this.error.sdump('D_ERROR','Should not be printing\n');
293                         }
294                 } catch (e) {
295                         //alert('Probably not printing: ' + e);
296                         // Pressing cancel is expressed as an NS_ERROR_ABORT return value,
297                         // causing an exception to be thrown which we catch here.
298                         // Unfortunately this will also consume helpful failures
299                         this.error.sdump('D_ERROR','_NSPrint_webBrowserPrint PRINT EXCEPTION: ' + js2JSON(e) + '\n');
300                 }
301         },
302
303         'GetPrintSettings' : function() {
304                 try {
305                         //alert('entering GetPrintSettings');
306                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
307                         var pref = Components.classes["@mozilla.org/preferences-service;1"]
308                                 .getService(Components.interfaces.nsIPrefBranch);
309                         //alert('pref = ' + pref);
310                         if (pref) {
311                                 this.gPrintSettingsAreGlobal = pref.getBoolPref("print.use_global_printsettings", false);
312                                 this.gSavePrintSettings = pref.getBoolPref("print.save_print_settings", false);
313                                 //alert('gPrintSettingsAreGlobal = ' + this.gPrintSettingsAreGlobal + '  gSavePrintSettings = ' + this.gSavePrintSettings);
314                         }
315  
316                         var printService = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
317                                 .getService(Components.interfaces.nsIPrintSettingsService);
318                         if (this.gPrintSettingsAreGlobal) {
319                                 this.gPrintSettings = printService.globalPrintSettings;
320                                 //alert('called setPrinterDefaultsForSelectedPrinter');
321                                 this.setPrinterDefaultsForSelectedPrinter(printService);
322                         } else {
323                                 this.gPrintSettings = printService.newPrintSettings;
324                                 //alert('used printService.newPrintSettings');
325                         }
326                 } catch (e) {
327                         this.error.sdump('D_ERROR',"GetPrintSettings() "+e+"\n");
328                         //alert("GetPrintSettings() "+e+"\n");
329                 }
330  
331                 return this.gPrintSettings;
332         },
333
334         'setPrinterDefaultsForSelectedPrinter' : function (aPrintService) {
335                 try {
336                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
337                         if (this.gPrintSettings.printerName == "") {
338                                 this.gPrintSettings.printerName = aPrintService.defaultPrinterName;
339                                 //alert('used .defaultPrinterName');
340                         }
341                         //alert('printerName = ' + this.gPrintSettings.printerName);
342          
343                         // First get any defaults from the printer 
344                         aPrintService.initPrintSettingsFromPrinter(this.gPrintSettings.printerName, this.gPrintSettings);
345          
346                         // now augment them with any values from last time
347                         aPrintService.initPrintSettingsFromPrefs(this.gPrintSettings, true, this.gPrintSettings.kInitSaveAll);
348
349                         // now augment from our own saved settings if they exist
350                         this.load_settings();
351
352                 } catch(E) {
353                         this.error.sdump('D_ERROR',"setPrinterDefaultsForSelectedPrinter() "+E+"\n");
354                 }
355         },
356
357         'page_settings' : function() {
358                 try {
359                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
360                         this.GetPrintSettings();
361                         var PO = Components.classes["@mozilla.org/gfx/printsettings-service;1"].getService(Components.interfaces.nsIPrintOptions);
362                         PO.ShowPrintSetupDialog(this.gPrintSettings);
363                 } catch(E) {
364                         this.error.standard_unexpected_error_alert("page_settings()",E);
365                 }
366         },
367
368         'load_settings' : function() {
369                 try {
370                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
371                         var file = new util.file('gPrintSettings');
372                         if (file._file.exists()) {
373                                 temp = file.get_object(); file.close();
374                                 for (var i in temp) {
375                                         this.gPrintSettings[i] = temp[i];
376                                 }
377                         } else {
378                                 this.gPrintSettings.marginTop = 0;
379                                 this.gPrintSettings.marginLeft = 0;
380                                 this.gPrintSettings.marginBottom = 0;
381                                 this.gPrintSettings.marginRight = 0;
382                                 this.gPrintSettings.headerStrLeft = '';
383                                 this.gPrintSettings.headerStrCenter = '';
384                                 this.gPrintSettings.headerStrRight = '';
385                                 this.gPrintSettings.footerStrLeft = '';
386                                 this.gPrintSettings.footerStrCenter = '';
387                                 this.gPrintSettings.footerStrRight = '';
388                         }
389                 } catch(E) {
390                         this.error.standard_unexpected_error_alert("load_settings()",E);
391                 }
392         },
393
394         'save_settings' : function() {
395                 try {
396                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
397                         var file = new util.file('gPrintSettings');
398                         file.set_object(this.gPrintSettings); file.close();
399                 } catch(E) {
400                         this.error.standard_unexpected_error_alert("save_settings()",E);
401                 }
402         },
403 }
404
405 dump('exiting util/print.js\n');