]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/util/print.js
print tweaks.. checkout is bad because it can seems to break the print xpcom if the...
[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                 // FIXME -- This could be done better.. instead of finding the columns and handling a tree dump,
105                 // we could do a dump_with_keys instead
106                 /*
107                 switch(params.type) {
108                         case 'offline_checkout' :
109                                 JSAN.use('circ.util');
110                                 cols = util.functional.map_list(
111                                         circ.util.offline_checkout_columns( {} ),
112                                         function(o) {
113                                                 return '%' + o.id + '%';
114                                         }
115                                 );
116
117                         break;
118                         case 'offline_checkin' :
119                                 JSAN.use('circ.util');
120                                 cols = util.functional.map_list(
121                                         circ.util.offline_checkin_columns( {} ),
122                                         function(o) {
123                                                 return '%' + o.id + '%';
124                                         }
125                                 );
126
127                         break;
128                         case 'offline_renew' :
129                                 JSAN.use('circ.util');
130                                 cols = util.functional.map_list(
131                                         circ.util.offline_renew_columns( {} ),
132                                         function(o) {
133                                                 return '%' + o.id + '%';
134                                         }
135                                 );
136                         break;
137                         case 'offline_inhouse_use' :
138                                 JSAN.use('circ.util');
139                                 cols = util.functional.map_list(
140                                         circ.util.offline_inhouse_use_columns( {} ),
141                                         function(o) {
142                                                 return '%' + o.id + '%';
143                                         }
144                                 );
145                         break;
146                         case 'items':
147                                 JSAN.use('circ.util');
148                                 cols = util.functional.map_list(
149                                         circ.util.columns( {} ),
150                                         function(o) {
151                                                 return '%' + o.id + '%';
152                                         }
153                                 );
154                         break;
155                         case 'bills':
156                                 JSAN.use('patron.util');
157                                 cols = util.functional.map_list(
158                                         patron.util.mbts_columns( {} ),
159                                         function(o) {
160                                                 return '%' + o.id + '%';
161                                         }
162                                 );
163                         break;
164                         case 'payment':
165                                 //cols = [ '%bill_id%','%payment%'];
166                                 cols = [];
167                         break;
168                         case 'transits':
169                                 cols = [];
170                         break;
171                         case 'holds':
172                                 JSAN.use('circ.util');
173                                 cols = util.functional.map_list(
174                                         circ.util.hold_columns( {} ),
175                                         function(o) {
176                                                 return '%' + o.id + '%';
177                                         }
178                                 );
179                         break;
180                         case 'patrons':
181                                 JSAN.use('patron.util');
182                                 cols = util.functional.map_list(
183                                         patron.util.columns( {} ),
184                                         function(o) {
185                                                 return '%' + o.id + '%';
186                                         }
187                                 );
188                         break;
189                 }
190                 */
191
192                 var s = this.template_sub( params.header, cols, params );
193                 for (var i = 0; i < params.list.length; i++) {
194                         params.row = params.list[i];
195                         s += this.template_sub( params.line_item, cols, params );
196                 }
197                 s += this.template_sub( params.footer, cols, params );
198
199                 if (params.sample_frame) {
200                         var jsrc = 'data:text/javascript,' + window.escape('var params = { "data" : ' + js2JSON(params.data) + ', "list" : ' + js2JSON(params.list) + '};');
201                         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>');
202                 } else {
203                         this.simple(s,params);
204                 }
205         },
206
207         'template_sub' : function( msg, cols, params ) {
208                 if (!msg) { dump('template sub called with empty string\n'); return; }
209                 JSAN.use('util.date');
210                 var s = msg; var b;
211
212                 try{b = s; s = s.replace(/%patron_barcode%/,params.patron_barcode);}
213                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
214
215                 try{b = s; s = s.replace(/%LIBRARY%/,params.lib.name());}
216                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
217                 try{b = s; s = s.replace(/%PINES_CODE%/,params.lib.shortname());}
218                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
219                 try{b = s; s = s.replace(/%STAFF_FIRSTNAME%/,params.staff.first_given_name());}
220                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
221                 try{b = s; s = s.replace(/%STAFF_LASTNAME%/,params.staff.family_name());}
222                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
223                 try{b = s; s = s.replace(/%STAFF_BARCODE%/,params.staff.barcode); }
224                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
225                 try{b = s; s = s.replace(/%PATRON_FIRSTNAME%/,params.patron.first_given_name());}
226                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
227                 try{b = s; s = s.replace(/%PATRON_LASTNAME%/,params.patron.family_name());}
228                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
229                 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() ) ;}
230                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
231
232                 try{b = s; s=s.replace(/%TODAY%/g,(new Date()));}
233                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
234                 try{b = s; s=s.replace(/%TODAY_m%/g,(util.date.formatted_date(new Date(),'%m')));}
235                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
236                 try{b = s; s=s.replace(/%TODAY_TRIM%/g,(util.date.formatted_date(new Date(),'')));}
237                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
238                 try{b = s; s=s.replace(/%TODAY_d%/g,(util.date.formatted_date(new Date(),'%d')));}
239                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
240                 try{b = s; s=s.replace(/%TODAY_Y%/g,(util.date.formatted_date(new Date(),'%Y')));}
241                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
242                 try{b = s; s=s.replace(/%TODAY_H%/g,(util.date.formatted_date(new Date(),'%H')));}
243                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
244                 try{b = s; s=s.replace(/%TODAY_I%/g,(util.date.formatted_date(new Date(),'%I')));}
245                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
246                 try{b = s; s=s.replace(/%TODAY_M%/g,(util.date.formatted_date(new Date(),'%M')));}
247                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
248                 try{b = s; s=s.replace(/%TODAY_D%/g,(util.date.formatted_date(new Date(),'%D')));}
249                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
250                 try{b = s; s=s.replace(/%TODAY_F%/g,(util.date.formatted_date(new Date(),'%F')));}
251                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
252
253                 try {
254                         if (typeof params.row != 'undefined') {
255                                 if (params.row.length >= 0) {
256                                         alert('debug pause');
257                                         for (var i = 0; i < cols.length; i++) {
258                                                 var re = new RegExp(cols[i],"g");
259                                                 try{b = s; s=s.replace(re, params.row[i]);}
260                                                         catch(E){s = b; this.error.standard_unexpected_error_alert('string = <' + s + '> error = ' + js2JSON(E)+'\n',E);}
261                                         }
262                                 } else { 
263                                         /* for dump_with_keys */
264                                         for (var i in params.row) {
265                                                 var re = new RegExp('%'+i+'%',"g");
266                                                 try{b = s; s=s.replace(re, params.row[i]);}
267                                                         catch(E){s = b; this.error.standard_unexpected_error_alert('string = <' + s + '> error = ' + js2JSON(E)+'\n',E);}
268                                         }
269                                 }
270                         }
271
272                         if (typeof params.data != 'undefined') {
273                                 for (var i in params.data) {
274                                         var re = new RegExp('%'+i+'%',"g");
275                                         try{b = s; s=s.replace(re, params.data[i]);}
276                                                 catch(E){s = b; this.error.standard_unexpected_error_alert('string = <' + s + '> error = ' + js2JSON(E)+'\n',E);}
277                                 }
278                         }
279                 } catch(E) { dump(E+'\n'); }
280
281                 return s;
282         },
283
284
285         'NSPrint' : function(w,silent,params) {
286                 if (!w) w = window;
287                 var obj = this;
288                 try {
289                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
290
291                         if (obj.data.print_strategy) {
292
293                                 switch(obj.data.print_strategy) {
294                                         case 'dos.print':
295                                                 obj._NSPrint_dos_print(w,silent,params);
296                                         break;  
297                                         case 'window.print':
298                                                 w.print();
299                                         break;  
300                                         case 'webBrowserPrint':
301                                                 obj._NSPrint_webBrowserPrint(w,silent,params);
302                                         break;  
303                                         default:
304                                                 //w.print();
305                                                 obj._NSPrint_webBrowserPrint(w,silent,params);
306                                         break;  
307                                 }
308
309                         } else {
310                                 //w.print();
311                                 obj._NSPrint_webBrowserPrint(w,silent,params);
312                         }
313
314                 } catch (e) {
315                         //alert('Probably not printing: ' + e);
316                         this.error.sdump('D_ERROR','PRINT EXCEPTION: ' + js2JSON(e) + '\n');
317                 }
318
319         },
320
321         '_NSPrint_dos_print' : function(w,silent,params) {
322                 var obj = this;
323                 try {
324                         /* This is a kludge/workaround.  webBrowserPrint doesn't always work.  So we're going to let
325                                 the html window handle our receipt template rendering, and then force a selection of all
326                                 the text nodes and dump that to a file, for printing through a dos utility */
327
328                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
329                         w.getSelection().selectAllChildren(w.document.firstChild);
330                         var text = w.getSelection().toString();
331
332                         JSAN.use('util.file'); var file = new util.file('receipt.txt');
333                         file.write_content('truncate',text); file.close();
334                         
335                         file = new util.file('receipt.bat');
336                         if (! file._file.exists()) { 
337                                 file.write_content('truncate','copy chrome\\open_ils_staff_client\\content\\conf\\receipt.txt lpt1 /b\n');
338                                 file.close();
339                                 file = new util.file('receipt.bat');
340                         }
341
342                         var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
343                         process.init(file._file);
344
345                         var args = [];
346
347                         process.run(true, args, args.length);
348
349                 } catch (e) {
350                         //alert('Probably not printing: ' + e);
351                         this.error.sdump('D_ERROR','PRINT EXCEPTION: ' + js2JSON(e) + '\n');
352                 }
353         },
354
355         '_NSPrint_webBrowserPrint' : function(w,silent,params) {
356                 var obj = this;
357                 try {
358                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
359                         var webBrowserPrint = w
360                                 .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
361                                 .getInterface(Components.interfaces.nsIWebBrowserPrint);
362                         this.error.sdump('D_PRINT','webBrowserPrint = ' + webBrowserPrint);
363                         if (webBrowserPrint) {
364                                 var gPrintSettings = obj.GetPrintSettings();
365                                 if (silent) gPrintSettings.printSilent = true;
366                                 else gPrintSettings.printSilent = false;
367                                 if (params) {
368                                         if (params.marginLeft) gPrintSettings.marginLeft = params.marginLeft;
369                                 }
370                                 webBrowserPrint.print(gPrintSettings, null);
371                                 this.error.sdump('D_PRINT','Should be printing\n');
372                         } else {
373                                 this.error.sdump('D_ERROR','Should not be printing\n');
374                         }
375                 } catch (e) {
376                         //alert('Probably not printing: ' + e);
377                         // Pressing cancel is expressed as an NS_ERROR_ABORT return value,
378                         // causing an exception to be thrown which we catch here.
379                         // Unfortunately this will also consume helpful failures
380                         this.error.sdump('D_ERROR','PRINT EXCEPTION: ' + js2JSON(e) + '\n');
381                 }
382         },
383
384         'GetPrintSettings' : function() {
385                 try {
386                         //alert('entering GetPrintSettings');
387                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
388                         var pref = Components.classes["@mozilla.org/preferences-service;1"]
389                                 .getService(Components.interfaces.nsIPrefBranch);
390                         //alert('pref = ' + pref);
391                         if (pref) {
392                                 this.gPrintSettingsAreGlobal = pref.getBoolPref("print.use_global_printsettings", false);
393                                 this.gSavePrintSettings = pref.getBoolPref("print.save_print_settings", false);
394                                 //alert('gPrintSettingsAreGlobal = ' + this.gPrintSettingsAreGlobal + '  gSavePrintSettings = ' + this.gSavePrintSettings);
395                         }
396  
397                         var printService = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
398                                 .getService(Components.interfaces.nsIPrintSettingsService);
399                         if (this.gPrintSettingsAreGlobal) {
400                                 this.gPrintSettings = printService.globalPrintSettings;
401                                 //alert('called setPrinterDefaultsForSelectedPrinter');
402                                 this.setPrinterDefaultsForSelectedPrinter(printService);
403                         } else {
404                                 this.gPrintSettings = printService.newPrintSettings;
405                                 //alert('used printService.newPrintSettings');
406                         }
407                 } catch (e) {
408                         this.error.sdump('D_ERROR',"GetPrintSettings() "+e+"\n");
409                         //alert("GetPrintSettings() "+e+"\n");
410                 }
411  
412                 return this.gPrintSettings;
413         },
414
415         'setPrinterDefaultsForSelectedPrinter' : function (aPrintService) {
416                 try {
417                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
418                         if (this.gPrintSettings.printerName == "") {
419                                 this.gPrintSettings.printerName = aPrintService.defaultPrinterName;
420                                 //alert('used .defaultPrinterName');
421                         }
422                         //alert('printerName = ' + this.gPrintSettings.printerName);
423          
424                         // First get any defaults from the printer 
425                         aPrintService.initPrintSettingsFromPrinter(this.gPrintSettings.printerName, this.gPrintSettings);
426          
427                         // now augment them with any values from last time
428                         aPrintService.initPrintSettingsFromPrefs(this.gPrintSettings, true, this.gPrintSettings.kInitSaveAll);
429
430                         // now augment from our own saved settings if they exist
431                         this.load_settings();
432
433                 } catch(E) {
434                         this.error.sdump('D_ERROR',"setPrinterDefaultsForSelectedPrinter() "+E+"\n");
435                 }
436         },
437
438         'page_settings' : function() {
439                 try {
440                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
441                         this.GetPrintSettings();
442                         var PO = Components.classes["@mozilla.org/gfx/printsettings-service;1"].getService(Components.interfaces.nsIPrintOptions);
443                         PO.ShowPrintSetupDialog(this.gPrintSettings);
444                 } catch(E) {
445                         this.error.standard_unexpected_error_alert("page_settings()",E);
446                 }
447         },
448
449         'load_settings' : function() {
450                 try {
451                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
452                         JSAN.use('util.file'); var file = new util.file('gPrintSettings');
453                         if (file._file.exists()) {
454                                 temp = file.get_object(); file.close();
455                                 for (var i in temp) {
456                                         this.gPrintSettings[i] = temp[i];
457                                 }
458                         } else {
459                                 this.gPrintSettings.marginTop = 0;
460                                 this.gPrintSettings.marginLeft = 0;
461                                 this.gPrintSettings.marginBottom = 0;
462                                 this.gPrintSettings.marginRight = 0;
463                                 this.gPrintSettings.headerStrLeft = '';
464                                 this.gPrintSettings.headerStrCenter = '';
465                                 this.gPrintSettings.headerStrRight = '';
466                                 this.gPrintSettings.footerStrLeft = '';
467                                 this.gPrintSettings.footerStrCenter = '';
468                                 this.gPrintSettings.footerStrRight = '';
469                         }
470                 } catch(E) {
471                         this.error.standard_unexpected_error_alert("load_settings()",E);
472                 }
473         },
474
475         'save_settings' : function() {
476                 try {
477                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
478                         JSAN.use('util.file'); var file = new util.file('gPrintSettings');
479                         file.set_object(this.gPrintSettings); file.close();
480                 } catch(E) {
481                         this.error.standard_unexpected_error_alert("save_settings()",E);
482                 }
483         },
484 }
485
486 dump('exiting util/print.js\n');