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