From b827b4b5d31074c2ec2cfb6635b541b243add728 Mon Sep 17 00:00:00 2001 From: Thomas Berezansky Date: Fri, 19 Aug 2011 15:15:48 -0400 Subject: [PATCH] Receipt Macros: %SORT()% Added to line item templates. Takes one or more comma separated parameters: field AS type ASC/DESC AS type is optional ASC/DESC is optional Field is the field name without % signs. Type is DATE, INT, FLOAT, NUMBER, LOWER, or UPPER DATE will sort based on wrapping both sides in "new Date" INT will sort based on wrapping both sides in parseInt FLOAT and NUMBER will sort based on wrapping both sides in parseFloat LOWER will apply .toLowerCase to both sides UPPER will apply .toUpperCase to both sides ASC (default) will sort in ascending order. DESC will sort in descending order. Can be specified more than once: %SORT(a,b)% %SORT(c)% Would sort by a, then b, then c. Signed-off-by: Thomas Berezansky Signed-off-by: Jason Etheridge --- .../xul/staff_client/chrome/content/util/print.js | 56 +++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) 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 5c24b03e85..45f531f92f 100644 --- a/Open-ILS/xul/staff_client/chrome/content/util/print.js +++ b/Open-ILS/xul/staff_client/chrome/content/util/print.js @@ -203,6 +203,60 @@ util.print.prototype = { var s = ''; if (params.header) s += this.template_sub( params.header, cols, params ); if (params.list) { + // Pre-templating sort + // %SORT(field[ AS type][ ASC|DESC][,...])% + var sort_blocks = params.line_item.match(/%SORT\([^)]+\)%/g); + if(sort_blocks) { + for(var i = 0; i < sort_blocks.length; i++) { + sort_blocks[i] = sort_blocks[i].substring(6,sort_blocks[i].length-2); + } + sort_blocks = sort_blocks.join(',').split(/\s*,\s*/); // Supports %SORT(a,b)% and %SORT(a)% %SORT(b)% methods + for(var i = 0; i < sort_blocks.length; i++) { + sort_blocks[i] = sort_blocks[i].match(/([^ ]+)(?:\s+AS\s+([^ ]+))?(?:\s+(ASC|DESC))?/); + sort_blocks[i].shift(); // Removes the "full match" entry + } + + function sorter(a, b) { + var return_val = 0; + for(var i = 0; i < sort_blocks.length && return_val == 0; i++) { + var sort = sort_blocks[i]; + var a_test = a[sort[0]]; + var b_test = b[sort[0]]; + sort[1] = sort[1] || ''; + sort[2] = sort[2] || 'ASC'; + switch(sort[1].toUpperCase()) { + case 'DATE': + a_test = new Date(a_test); + b_test = new Date(b_test); + break; + case 'INT': + a_test = parseInt(a_test); + b_test = parseInt(b_test); + break; + case 'FLOAT': + case 'NUMBER': + a_test = parseFloat(a_test); + b_test = parseFloat(b_test); + break; + case 'LOWER': + a_test = a_test.toLowerCase(); + b_test = b_test.toLowerCase(); + break; + case 'UPPER': + a_test = a_test.toUpperCase(); + b_test = b_test.toUpperCase(); + break; + } + if(a_test > b_test) return_val = 1; + if(a_test < b_test) return_val = -1; + if(sort[2] == 'DESC') return_val *= -1; + } + return return_val; + } + params.list.sort(sorter); + params.line_item = params.line_item.replace(/%SORT\([^)]*\)%/g,''); + } + for (var i = 0; i < params.list.length; i++) { params.row = params.list[i]; params.row_idx = i; @@ -291,7 +345,7 @@ util.print.prototype = { /* for dump_with_keys */ for (var i in params.row) { var re = new RegExp('%'+i+'%',"g"); - try{b = s; s=s.replace(re, this.escape_html(params.row[i]));} + try{b = s; s=s.replace(re, this.escape_html(params.row[i].toString()));} catch(E){s = b; this.error.standard_unexpected_error_alert('print.js, template_sub(): 2 string = <' + s + '>',E);} } } -- 2.11.0