From 865c23330a9e891024e2df3696dfe5a827ed545c Mon Sep 17 00:00:00 2001 From: Thomas Berezansky Date: Wed, 24 Aug 2011 11:31:59 -0400 Subject: [PATCH] Remove in-template javascript, add script files Adds support for a print_custom.js file and an org unit specified file. If either exists and contains a print_custom function it will be called before printing to allow for DOM manipulation. If neither exists the print_win.js file will call all functions it has defined. In addition, if any code sets do_print to false the window will close instead of attempting to print. The print_custom function will receive the type of template used, if any. This only applies to "Receipt Template" based print jobs, and the type in question is the "Type" that shows in the template editor. Functions defined in print_win.js: print_init Main function, checks for print_custom and does final printing print_do_sums Does summing of values. print_check_alt Does swapping out of template blocks with display:none styling. print_check_noprint Disables printing under certain circumstances. For more information about usage of the print_do_sums, print_check_alt, and print_check_noprint functions check the comments in print_win.js. Signed-off-by: Thomas Berezansky Signed-off-by: Jason Etheridge --- Open-ILS/src/sql/Pg/950.data.seed-values.sql | 11 ++ .../staff_client/chrome/content/util/print.js | 29 ++- .../chrome/content/util/print_win.js | 165 ++++++++++++++++++ 3 files changed, 201 insertions(+), 4 deletions(-) create mode 100644 Open-ILS/xul/staff_client/chrome/content/util/print_win.js diff --git a/Open-ILS/src/sql/Pg/950.data.seed-values.sql b/Open-ILS/src/sql/Pg/950.data.seed-values.sql index e605528866..1c84e9767b 100644 --- a/Open-ILS/src/sql/Pg/950.data.seed-values.sql +++ b/Open-ILS/src/sql/Pg/950.data.seed-values.sql @@ -3734,6 +3734,17 @@ INSERT into config.org_unit_setting_type 'coust', 'description'), 'bool', null) +,( 'print.custom_js_file', 'circ', + oils_i18n_gettext('print.custom_js_file', + 'Printing: Custom Javascript File', + 'coust', 'label'), + oils_i18n_gettext('print.custom_js_file', + 'Full URL path to a Javascript File to be loaded when printing. Should' + || ' implement a print_custom function for DOM manipulation. Can change' + || ' the value of the do_print variable to false to cancel printing.', + 'coust', 'description'), + 'string', null) + ,( 'serial.prev_issuance_copy_location', 'serial', oils_i18n_gettext('serial.prev_issuance_copy_location', 'Previous Issuance Copy Location', diff --git a/Open-ILS/xul/staff_client/chrome/content/util/print.js b/Open-ILS/xul/staff_client/chrome/content/util/print.js index a4222fa4a4..320152c9d4 100644 --- a/Open-ILS/xul/staff_client/chrome/content/util/print.js +++ b/Open-ILS/xul/staff_client/chrome/content/util/print.js @@ -155,10 +155,25 @@ util.print.prototype = { switch(content_type) { case 'text/html' : - var jsrc = 'data:text/javascript,' + window.escape('var params = window.arguments[0]; window.go_print = window.arguments[1];'); - var print_url = 'data:text/html,' - + '' - + '' + window.escape(msg) + ''; + if(!params.type) { + params.type = ''; + } + var my_prefix = '/xul/server/'; + if(window.location.protocol == "chrome:") { + // Likely in offline interface + my_prefix = 'chrome://open_ils_staff_client/content/'; + } else { + if(xulG && xulG.url_prefix) { + my_prefix = xulG.url_prefix(my_prefix); + } + } + var print_url = 'data:text/html,' + + '' + + ''; + if(this.data.hash.aous['print.custom_js_file']) { + print_url += ''; + } + print_url += '' + window.escape(msg.replace(/]*>.*?<\/script>/gi,'')) + ''; w = obj.win.openDialog(print_url,'receipt_temp','chrome,resizable,minimizable', null, { "data" : params.data, "list" : params.list}, function() { try { obj.NSPrint(w, silent, params); @@ -265,6 +280,12 @@ util.print.prototype = { } if (params.footer) s += this.template_sub( params.footer, cols, params ); + // Sanity check, no javascript in templates + // Note: [\s\S] is a workaround for . not including newlines. + s=s.replace(/]*>[\s\S]*?<\/script[^>]*>/gi,'') + s=s.replace(/onload\s*=\s*"[^"]*"/gi,''); + s=s.replace(/onload\s*=\s*'[^']*'/gi,''); + if (params.sample_frame) { var jsrc = 'data:text/javascript,' + window.escape('var params = { "data" : ' + js2JSON(params.data) + ', "list" : ' + js2JSON(params.list) + '};'); params.sample_frame.setAttribute('src','data:text/html,' + window.escape(s) + ''); diff --git a/Open-ILS/xul/staff_client/chrome/content/util/print_win.js b/Open-ILS/xul/staff_client/chrome/content/util/print_win.js new file mode 100644 index 0000000000..136b3bff2f --- /dev/null +++ b/Open-ILS/xul/staff_client/chrome/content/util/print_win.js @@ -0,0 +1,165 @@ +// Print Window Functions +// Loaded when print.js creates a window for printing + +var params = window.arguments[0]; +window.go_print = window.arguments[1]; +var do_print = true; + +function print_init(type) { + if (typeof print_custom == "function") { + print_custom(type); + } else { + print_do_sums(); + print_check_alt(); + print_check_noprint(); + } + if (do_print) { + go_print(); + } else { + window.close(); + } +} + +/* Example "swap slip" code + * Use example: + *
+ * %some_replace% + * + *
+ *
+ * + *
+ *
+ * + *
+ *
+ * Code1 + * Code2 + *
+ */ +function print_check_alt() { + var spans = document.getElementsByTagName('span'); + if(!spans) return; + var groups_check = {}; + var foundgroups = false; + for (var i = 0; i < spans.length; i++) { + var group = spans[i].getAttribute('altcheck'); + if(group) { + groups_check[group] = spans[i].textContent; + foundgroups = true; + } + } + if(!foundgroups) return; + foundgroups = false; + var groups_show = {}; + for (var i = 0; i < spans.length; i++) { + var group = spans[i].getAttribute('alt'); + if(group && groups_check[group] && spans[i].textContent == groups_check[group]) { + groups_show[group] = spans[i].getAttribute('altshow'); + foundgroups = true; + } + } + if(!foundgroups) return; + for (var i = 0; i < spans.length; i++) { + var group = spans[i].getAttribute('altgroup'); + if(group && groups_check[group]) { + spans[i].style.display = (groups_show[group] == spans[i].getAttribute('altid') ? '' : 'none'); + } + } + var divs = document.getElementsByTagName('div'); + if (!divs) return; + for (var i = 0; i < divs.length; i++) { + var group = divs[i].getAttribute('altgroup'); + if(group && groups_check[group]) { + divs[i].style.display = (groups_show[group] == divs[i].getAttribute('altid') ? '' : 'none'); + } + } +} + +/* Example "don't print" code + * Use example: + * + * %some_replace% + * %some_other_replace% + * + *
+ * Code1 + * Code2 + *
+ */ +function print_check_noprint() { + var spans = document.getElementsByTagName('span'); + if(!spans) return; + var noprints = {}; + var foundnoprints = false; + for (var i = 0; i < spans.length; i++) { + var noprint = spans[i].getAttribute('noprintcheck'); + if(noprint) { + noprints[noprint] = spans[i].textContent; + foundnoprints = true; + } + } + if(!foundnoprints) return; + for (var i = 0; i < spans.length; i++) { + var noprint = spans[i].getAttribute('noprint'); + if(noprint) { + if(noprints[noprint] == spans[i].textContent) { + do_print = false; + } + } + } +} + +/* Example "sum up" code + * Use example: + * + * + * $5.00 + * $15.00 + * $25.00 + * + * $ + */ +function print_do_sums() { + var spans = document.getElementsByTagName('span'); + if(!spans) return; + var sums = {}; + var foundsums = false; + for (var i = 0; i < spans.length; i++) { + var sumset = spans[i].getAttribute("sum"); + if(sumset) { + if(typeof sums[sumset] == 'undefined') { + sums[sumset] = 0.0; + foundsums = true; + } + var newVal = spans[i].textContent; + // strip off a single non-digit character + // Don't want to assume dollar sign + // But don't strip a - + newVal = newVal.replace(/^[^-0-9]/,''); + newVal = parseFloat(newVal); + if(!isNaN(newVal)) { + sums[sumset] += newVal; + } + } + } + if(!foundsums) return; + for (var i = 0; i < spans.length; i++) { + var sumset = spans[i].getAttribute("sumout"); + if(sumset) { + if(typeof sums[sumset] == 'undefined') { + sums[sumset] = 0; + } + var fixed = spans[i].getAttribute("fixed"); + if(fixed) { + fixed = parseInt(fixed); + if(isNaN(fixed)) { + fixed = 0; + } + spans[i].textContent=sums[sumset].toFixed(fixed); + } else { + spans[i].textContent = sums[sumset]; + } + } + } +} -- 2.43.2