]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/util/print.js
printing from modal windows... might should do modal and non-modal windows this way...
[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 params.modal != 'undefined' ? 'true' : 'false') + ') window.print(); }');
57                                         w = obj.win.open('data:text/html,<html><head><script src="' + window.escape(jsrc) + '"></script></head><body onload="my_init();">' + window.escape(msg) + '</body></html>','receipt_temp','chrome,resizable');
58                                 break;
59                                 default:
60                                         w = obj.win.open('data:' + content_type + ',' + window.escape(msg),'receipt_temp','chrome,resizable');
61                                 break;
62                         }
63
64                         w.minimize();
65
66                         setTimeout(
67                                 function() {
68                                         try {
69                                                 obj.NSPrint(w, silent, params);
70                                         } catch(E) {
71                                                 obj.error.sdump('D_ERROR','util.print.simple: ' + E);
72                                                 w.print();
73                                         }
74                                         w.minimize(); w.close();
75                                 }, 0
76                         );
77                 } catch(E) {
78                         this.error.standard_unexpected_error_alert('util.print.simple',E);
79                 }
80         },
81         
82         'tree_list' : function (params) { 
83                 try {
84                         dump('print.tree_list.params.list = \n' + this.error.pretty_print(js2JSON(params.list)) + '\n');
85                 } catch(E) {
86                         dump(E+'\n');
87                 }
88                 var cols;
89                 // FIXME -- This could be done better.. instead of finding the columns and handling a tree dump,
90                 // we could do a dump_with_keys instead
91                 switch(params.type) {
92                         case 'offline_checkout' :
93                                 JSAN.use('circ.util');
94                                 cols = util.functional.map_list(
95                                         circ.util.offline_checkout_columns( {} ),
96                                         function(o) {
97                                                 return '%' + o.id + '%';
98                                         }
99                                 );
100
101                         break;
102                         case 'offline_checkin' :
103                                 JSAN.use('circ.util');
104                                 cols = util.functional.map_list(
105                                         circ.util.offline_checkin_columns( {} ),
106                                         function(o) {
107                                                 return '%' + o.id + '%';
108                                         }
109                                 );
110
111                         break;
112                         case 'offline_renew' :
113                                 JSAN.use('circ.util');
114                                 cols = util.functional.map_list(
115                                         circ.util.offline_renew_columns( {} ),
116                                         function(o) {
117                                                 return '%' + o.id + '%';
118                                         }
119                                 );
120                         break;
121                         case 'offline_inhouse_use' :
122                                 JSAN.use('circ.util');
123                                 cols = util.functional.map_list(
124                                         circ.util.offline_inhouse_use_columns( {} ),
125                                         function(o) {
126                                                 return '%' + o.id + '%';
127                                         }
128                                 );
129                         break;
130                         case 'items':
131                                 JSAN.use('circ.util');
132                                 cols = util.functional.map_list(
133                                         circ.util.columns( {} ),
134                                         function(o) {
135                                                 return '%' + o.id + '%';
136                                         }
137                                 );
138                         break;
139                         case 'bills':
140                                 JSAN.use('patron.util');
141                                 cols = util.functional.map_list(
142                                         patron.util.mbts_columns( {} ),
143                                         function(o) {
144                                                 return '%' + o.id + '%';
145                                         }
146                                 );
147                         break;
148                         case 'payment':
149                                 cols = [ '%bill_id%','%payment%'];
150                         break;
151                         case 'holds':
152                                 JSAN.use('circ.util');
153                                 cols = util.functional.map_list(
154                                         circ.util.hold_columns( {} ),
155                                         function(o) {
156                                                 return '%' + o.id + '%';
157                                         }
158                                 );
159                         break;
160                         case 'patrons':
161                                 JSAN.use('patron.util');
162                                 cols = util.functional.map_list(
163                                         patron.util.columns( {} ),
164                                         function(o) {
165                                                 return '%' + o.id + '%';
166                                         }
167                                 );
168                         break;
169                 }
170
171                 var s = this.template_sub( params.header, cols, params );
172                 for (var i = 0; i < params.list.length; i++) {
173                         params.row = params.list[i];
174                         s += this.template_sub( params.line_item, cols, params );
175                 }
176                 s += this.template_sub( params.footer, cols, params );
177
178                 if (params.sample_frame) {
179                         var jsrc = 'data:text/javascript,' + window.escape('var params = { "data" : ' + js2JSON(params.data) + ', "list" : ' + js2JSON(params.list) + '};');
180                         params.sample_frame.setAttribute('src','data:text/html,<html><head><script src="' + window.escape(jsrc) + '"></script></head><body>' + window.escape(s) + '</body></html>');
181                 } else {
182                         this.simple(s,params);
183                 }
184         },
185
186         'template_sub' : function( msg, cols, params ) {
187                 if (!msg) { dump('template sub called with empty string\n'); return; }
188                 JSAN.use('util.date');
189                 var s = msg; var b;
190
191                 try{b = s; s = s.replace(/%patron_barcode%/,params.patron_barcode);}
192                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
193
194                 try{b = s; s = s.replace(/%LIBRARY%/,params.lib.name());}
195                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
196                 try{b = s; s = s.replace(/%PINES_CODE%/,params.lib.shortname());}
197                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
198                 try{b = s; s = s.replace(/%STAFF_FIRSTNAME%/,params.staff.first_given_name());}
199                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
200                 try{b = s; s = s.replace(/%STAFF_LASTNAME%/,params.staff.family_name());}
201                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
202                 try{b = s; s = s.replace(/%STAFF_BARCODE%/,params.staff.barcode); }
203                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
204                 try{b = s; s = s.replace(/%PATRON_FIRSTNAME%/,params.patron.first_given_name());}
205                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
206                 try{b = s; s = s.replace(/%PATRON_LASTNAME%/,params.patron.family_name());}
207                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
208                 try{b = s; s = s.replace(/%PATRON_BARCODE%/,params.patron.card().barcode());}
209                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
210
211                 try{b = s; s=s.replace(/%TODAY%/g,(new Date()));}
212                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
213                 try{b = s; s=s.replace(/%TODAY_m%/g,(util.date.formatted_date(new Date(),'%m')));}
214                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
215                 try{b = s; s=s.replace(/%TODAY_TRIM%/g,(util.date.formatted_date(new Date(),'')));}
216                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
217                 try{b = s; s=s.replace(/%TODAY_d%/g,(util.date.formatted_date(new Date(),'%d')));}
218                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
219                 try{b = s; s=s.replace(/%TODAY_Y%/g,(util.date.formatted_date(new Date(),'%Y')));}
220                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
221                 try{b = s; s=s.replace(/%TODAY_H%/g,(util.date.formatted_date(new Date(),'%H')));}
222                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
223                 try{b = s; s=s.replace(/%TODAY_I%/g,(util.date.formatted_date(new Date(),'%I')));}
224                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
225                 try{b = s; s=s.replace(/%TODAY_M%/g,(util.date.formatted_date(new Date(),'%M')));}
226                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
227                 try{b = s; s=s.replace(/%TODAY_D%/g,(util.date.formatted_date(new Date(),'%D')));}
228                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
229                 try{b = s; s=s.replace(/%TODAY_F%/g,(util.date.formatted_date(new Date(),'%F')));}
230                         catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
231
232                 try {
233                         if (typeof params.row != 'undefined') {
234                                 if (params.row.length >= 0) {
235                                         for (var i = 0; i < cols.length; i++) {
236                                                 var re = new RegExp(cols[i],"g");
237                                                 try{b = s; s=s.replace(re, params.row[i]);}
238                                                         catch(E){s = b; this.error.standard_unexpected_error_alert('string = <' + s + '> error = ' + js2JSON(E)+'\n',E);}
239                                         }
240                                 } else { 
241                                         /* for dump_with_keys */
242                                         for (var i in params.row) {
243                                                 var re = new RegExp('%'+i+'%',"g");
244                                                 try{b = s; s=s.replace(re, params.row[i]);}
245                                                         catch(E){s = b; this.error.standard_unexpected_error_alert('string = <' + s + '> error = ' + js2JSON(E)+'\n',E);}
246                                         }
247                                 }
248                         }
249
250                         if (typeof params.data != 'undefined') {
251                                 for (var i in params.data) {
252                                         var re = new RegExp('%'+i+'%',"g");
253                                         try{b = s; s=s.replace(re, params.data[i]);}
254                                                 catch(E){s = b; this.error.standard_unexpected_error_alert('string = <' + s + '> error = ' + js2JSON(E)+'\n',E);}
255                                 }
256                         }
257                 } catch(E) { dump(E+'\n'); }
258
259                 return s;
260         },
261
262
263         'NSPrint' : function(w,silent,params) {
264                 if (!w) w = window;
265                 var obj = this;
266                 try {
267                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
268                         var webBrowserPrint = w
269                                 .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
270                                 .getInterface(Components.interfaces.nsIWebBrowserPrint);
271                         this.error.sdump('D_PRINT','webBrowserPrint = ' + webBrowserPrint);
272                         if (webBrowserPrint) {
273                                 var gPrintSettings = obj.GetPrintSettings();
274                                 if (silent) gPrintSettings.printSilent = true;
275                                 else gPrintSettings.printSilent = false;
276                                 if (params) {
277                                         gPrintSettings.marginTop = 0;
278                                         gPrintSettings.marginLeft = 0;
279                                         gPrintSettings.marginBottom = 0;
280                                         gPrintSettings.marginRight = 0;
281                                         if (params.marginLeft) gPrintSettings.marginLeft = params.marginLeft;
282                                 }
283                                 gPrintSettings.headerStrLeft = '';
284                                 gPrintSettings.headerStrCenter = '';
285                                 gPrintSettings.headerStrRight = '';
286                                 gPrintSettings.footerStrLeft = '';
287                                 gPrintSettings.footerStrCenter = '';
288                                 gPrintSettings.footerStrRight = '';
289                                 //this.error.sdump('D_PRINT','gPrintSettings = ' + obj.error.pretty_print(js2JSON(gPrintSettings)));
290                                 //alert('gPrintSettings = ' + js2JSON(gPrintSettings));
291                                 webBrowserPrint.print(gPrintSettings, null);
292                                 /*
293                                 if (this.gPrintSettingsAreGlobal && this.gSavePrintSettings) {
294                                         var PSSVC = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
295                                                 .getService(Components.interfaces.nsIPrintSettingsService);
296                                         PSSVC.savePrintSettingsToPrefs( gPrintSettings, true, gPrintSettings.kInitSaveAll);
297                                         PSSVC.savePrintSettingsToPrefs( gPrintSettings, false, gPrintSettings.kInitSavePrinterName);
298                                 }
299                                 */
300                                 //this.error.sdump('D_PRINT','gPrintSettings 2 = ' + obj.error.pretty_print(js2JSON(gPrintSettings)));
301                                 //alert('Should be printing\n');
302                                 this.error.sdump('D_PRINT','Should be printing\n');
303                         } else {
304                                 //alert('Should not be printing\n');
305                                 this.error.sdump('D_PRINT','Should not be printing\n');
306                         }
307                 } catch (e) {
308                         //alert('Probably not printing: ' + e);
309                         // Pressing cancel is expressed as an NS_ERROR_ABORT return value,
310                         // causing an exception to be thrown which we catch here.
311                         // Unfortunately this will also consume helpful failures, so add a
312                         this.error.sdump('D_PRINT','PRINT EXCEPTION: ' + js2JSON(e) + '\n');
313                         // if you need to debug
314                 }
315
316         },
317
318         'GetPrintSettings' : function() {
319                 try {
320                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
321                         var pref = Components.classes["@mozilla.org/preferences-service;1"]
322                                 .getService(Components.interfaces.nsIPrefBranch);
323                         if (pref) {
324                                 this.gPrintSettingsAreGlobal = pref.getBoolPref("print.use_global_printsettings", false);
325                                 this.gSavePrintSettings = pref.getBoolPref("print.save_print_settings", false);
326                         }
327  
328                         var printService = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
329                                 .getService(Components.interfaces.nsIPrintSettingsService);
330                         if (this.gPrintSettingsAreGlobal) {
331                                 this.gPrintSettings = printService.globalPrintSettings;
332                                 this.setPrinterDefaultsForSelectedPrinter(printService);
333                         } else {
334                                 this.gPrintSettings = printService.newPrintSettings;
335                         }
336                 } catch (e) {
337                         this.error.sdump('D_PRINT',"GetPrintSettings() "+e+"\n");
338                         //alert("GetPrintSettings() "+e+"\n");
339                 }
340  
341                 return this.gPrintSettings;
342         },
343
344         'setPrinterDefaultsForSelectedPrinter' : function (aPrintService) {
345                 try {
346                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
347                         if (this.gPrintSettings.printerName == "") {
348                                 this.gPrintSettings.printerName = aPrintService.defaultPrinterName;
349                         }
350          
351                         // First get any defaults from the printer 
352                         aPrintService.initPrintSettingsFromPrinter(this.gPrintSettings.printerName, this.gPrintSettings);
353          
354                         // now augment them with any values from last time
355                         aPrintService.initPrintSettingsFromPrefs(this.gPrintSettings, true, this.gPrintSettings.kInitSaveAll);
356                 } catch(E) {
357                         this.error.sdump('D_PRINT',"setPrinterDefaultsForSelectedPrinter() "+E+"\n");
358                 }
359         }
360 }
361
362 dump('exiting util/print.js\n');