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