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