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