Quellcode durchsuchen

refactored print_table

version-14
Rushabh Mehta vor 12 Jahren
Ursprung
Commit
0611104a04
5 geänderte Dateien mit 416 neuen und 204 gelöschten Zeilen
  1. +1
    -0
      public/build.json
  2. +8
    -1
      public/js/legacy/widgets/form/form.js
  3. +200
    -202
      public/js/legacy/widgets/form/print_format.js
  4. +1
    -1
      public/js/wn/form/formatters.js
  5. +206
    -0
      public/js/wn/print/print_table.js

+ 1
- 0
public/build.json Datei anzeigen

@@ -145,6 +145,7 @@
"lib/public/js/legacy/wn/widgets/form/attachments.js",
"lib/public/js/legacy/wn/widgets/form/assign_to.js",
"lib/public/js/wn/form/linked_with.js",
"lib/public/js/wn/print/print_table.js",

'lib/public/js/lib/jquery/jquery.ui.interactions.min.js',


+ 8
- 1
public/js/legacy/widgets/form/form.js Datei anzeigen

@@ -132,6 +132,13 @@ _f.Frm.prototype.setup_print_layout = function() {
appframe.add_button("Print", function() {
me.print_doc();
}, 'icon-print');

this.$print_view_select = appframe.add_select("Select Preview", this.print_formats)
.css({"float":"right"})
.val(this.print_formats[0])
.change(function() {
me.refresh_print_layout();
})
appframe.add_ripped_paper_effect(this.print_wrapper);
@@ -421,7 +428,7 @@ _f.Frm.prototype.refresh_print_layout = function() {
}

// create print format here
_p.build(this.print_formats[0], print_callback, null, 1);
_p.build(this.$print_view_select.val(), print_callback, null, 1);
}




+ 200
- 202
public/js/legacy/widgets/form/print_format.js Datei anzeigen

@@ -289,21 +289,19 @@ $.extend(_p, {

// This is used to calculate and substitude values in the HTML
run_embedded_js: function(container, doc) {
var jslist = container.getElementsByTagName('script');
while(jslist && jslist.length > 0) {
for(i in jslist) {
if(jslist[i] && jslist[i].innerHTML) {
var code = jslist[i].innerHTML;
var parent = jslist[i].parentNode;
var span = $a(parent, 'span');
parent.replaceChild(span, jslist[i]);
var val = code ? eval(code) : '';
if(!val || typeof(val)=='object') { val = ''; }
span.innerHTML = val;
}
}
jslist = container.getElementsByTagName('script');
}
$(container).find("script").each(function(element) {
var code = this.innerHTML;
var new_html = code ? (eval(code) || "") : "";
if(typeof new_html=="string")
$(this).replaceWith(new_html);

// var parent = jslist[i].parentNode;
// var span = $a(parent, 'span');
// parent.replaceChild(span, jslist[i]);
// var val = code ? eval(code) : '';
// if(!val || typeof(val)=='object') { val = ''; }
// span.innerHTML = val;
})
},
@@ -645,190 +643,190 @@ $.extend(_p, {
});


print_table = function(dt, dn, fieldname, tabletype, cols, head_labels, widths, condition, cssClass, modifier, hide_empty) {
var me = this;
$.extend(this, {
flist: (function() {
var f_list = [];
var fl = wn.meta.docfield_list[tabletype];
if(fl) {
for(var i=0; i<fl.length; i++) {
f_list.push(copy_dict(fl[i]));
}
}
return f_list;
})(),
data: function() {
var children = getchildren(
tabletype, // child_dt
dn, // parent
fieldname, // parentfield
dt // parenttype
);
var data = []
for(var i=0; i<children.length; i++) {
data.push(copy_dict(children[i]));
}
return data;
}(),
cell_style: {
border: '1px solid #999',
padding: '3px',
verticalAlign: 'top'
},
head_cell_style: {
border: '1px solid #999',
padding: '3px',
verticalAlign: 'top',
backgroundColor: '#ddd',
fontWeight: 'bold'
},
table_style: {
width: '100%',
borderCollapse: 'collapse',
marginBottom: '10px',
marginTop: '10px'
},
remove_empty_cols: function(flist) {
var non_empty_cols = []
for(var i=0; i<me.data.length; i++) {
for(var c=0; c<flist.length; c++) {
if(flist[c].print_hide || !inList(['', null], me.data[i][flist[c].fieldname])) {
if(!inList(non_empty_cols, flist[c])) {
non_empty_cols.push(flist[c]);
}
}
}
}
for(var c=0; c<flist.length; c++) {
if(!inList(non_empty_cols, flist[c])) {
flist.splice(c, 1);
c = c - 1;
}
}
},
/*
This function prepares a list of columns to be displayed and calls make_print_table to create a table with these columns
*/
prepare_col_heads: function(flist) {
var new_flist = [];
if(!cols || (cols && cols.length && hide_empty)) {
me.remove_empty_cols(flist);
}
// Make a list of column headings
if(cols && cols.length) {
// If cols to be displayed are passed in print_table
if(cols[0] == 'SR') { new_flist.push('SR') }
for(var i = 0; i < cols.length; i++) {
for(var j = 0; j < flist.length; j++) {
if(flist[j].fieldname == cols[i]) {
new_flist.push(flist[j]);
break;
}
}
}
} else {
// Default action: remove hidden cols
new_flist.push('SR');
for(var i = 0; i < flist.length; i++) {
if(!flist[i].print_hide) {
new_flist.push(flist[i]);
}
}
}
// Changing me.flist so that it could be used to hide data
me.flist = new_flist;
},
// This function makes a new table with its heading rows
make_print_table: function(flist) {
// Make a table
var wrapper = document.createElement('div');
var table = $a(wrapper, 'table', '', me.table_style);
table.wrapper = wrapper;
// Make Head Row
table.insertRow(0);
var col_start = 0;
// If 'SR' exists in flist, then create its heading column cell
if(flist[0]=='SR') {
var cell = table.rows[0].insertCell(0);
cell.innerHTML = head_labels?head_labels[0]:'<b>SR</b>';
$y(cell, { width: '30px' });
$y(cell, me.head_cell_style);
col_start++;
}
for(var c = col_start; c < flist.length; c++) {
var cell = table.rows[0].insertCell(c);
$y(cell, me.head_cell_style);
cell.innerHTML = head_labels?head_labels[c]:flist[c].label;
if(flist[c].width) { $y(cell, {width: flist[c].width}); }
if(widths) { $y(cell, {width: widths[c]}); }
if(in_list(['Currency', 'Float'], flist[c].fieldtype)) {
$y(cell, { textAlign: 'right' });
}
}
return table;
},
// Populate table with data
populate_table: function(table, data) {
for(var r = 0; r < data.length; r++) {
if((!condition) || (condition(data[r]))) {
// Check for page break
if(data[r].page_break) {
table = me.make_print_table(me.flist);
me.table_list.push(table.wrapper);
}
var row = table.insertRow(table.rows.length);
// Add serial number if required
if(me.flist[0] == 'SR') {
var cell = row.insertCell(0);
cell.innerHTML = r + 1;
$y(cell, me.cell_style);
}
for(var c=me.flist.indexOf('SR')+1; c<me.flist.length; c++){
var cell = row.insertCell(c);
$y(cell, me.cell_style);
if(modifier && me.flist[c].fieldname in modifier) {
data[r][me.flist[c].fieldname] = modifier[me.flist[c].fieldname](data[r]);
}
$s(cell, data[r][me.flist[c].fieldname],
me.flist[c].fieldtype);
if(in_list(['Currency', 'Float'], me.flist[c].fieldtype)) {
cell.style.textAlign = 'right';
}
}
}
}
}
});
// If no data, do not create table
if(!this.data.length) { return document.createElement('div'); }
this.prepare_col_heads(this.flist);
var table = me.make_print_table(this.flist);
this.table_list = [table.wrapper];
this.populate_table(table, this.data);
// If multiple tables exists, send whole list, else send only one table
return (me.table_list.length > 1) ? me.table_list : me.table_list[0];
}
// print_table = function(dt, dn, fieldname, tabletype, cols, head_labels, widths, condition, cssClass, modifier, hide_empty) {
// var me = this;
// $.extend(this, {
// flist: (function() {
// var f_list = [];
// var fl = wn.meta.docfield_list[tabletype];
// if(fl) {
// for(var i=0; i<fl.length; i++) {
// f_list.push(copy_dict(fl[i]));
// }
// }
// return f_list;
// })(),
//
// data: function() {
// var children = getchildren(
// tabletype, // child_dt
// dn, // parent
// fieldname, // parentfield
// dt // parenttype
// );
// var data = []
// for(var i=0; i<children.length; i++) {
// data.push(copy_dict(children[i]));
// }
// return data;
// }(),
//
// cell_style: {
// border: '1px solid #999',
// padding: '3px',
// verticalAlign: 'top'
// },
//
// head_cell_style: {
// border: '1px solid #999',
// padding: '3px',
// verticalAlign: 'top',
// backgroundColor: '#ddd',
// fontWeight: 'bold'
// },
//
// table_style: {
// width: '100%',
// borderCollapse: 'collapse',
// marginBottom: '10px',
// marginTop: '10px'
// },
//
// remove_empty_cols: function(flist) {
// var non_empty_cols = []
// for(var i=0; i<me.data.length; i++) {
// for(var c=0; c<flist.length; c++) {
// if(flist[c].print_hide || !inList(['', null], me.data[i][flist[c].fieldname])) {
// if(!inList(non_empty_cols, flist[c])) {
// non_empty_cols.push(flist[c]);
// }
// }
// }
// }
// for(var c=0; c<flist.length; c++) {
// if(!inList(non_empty_cols, flist[c])) {
// flist.splice(c, 1);
// c = c - 1;
// }
// }
// },
//
// /*
// This function prepares a list of columns to be displayed and calls make_print_table to create a table with these columns
// */
// prepare_col_heads: function(flist) {
// var new_flist = [];
//
// if(!cols || (cols && cols.length && hide_empty)) {
// me.remove_empty_cols(flist);
// }
//
// // Make a list of column headings
// if(cols && cols.length) {
// // If cols to be displayed are passed in print_table
// if(cols[0] == 'SR') { new_flist.push('SR') }
// for(var i = 0; i < cols.length; i++) {
// for(var j = 0; j < flist.length; j++) {
// if(flist[j].fieldname == cols[i]) {
// new_flist.push(flist[j]);
// break;
// }
// }
// }
// } else {
// // Default action: remove hidden cols
// new_flist.push('SR');
// for(var i = 0; i < flist.length; i++) {
// if(!flist[i].print_hide) {
// new_flist.push(flist[i]);
// }
// }
// }
//
// // Changing me.flist so that it could be used to hide data
// me.flist = new_flist;
// },
//
// // This function makes a new table with its heading rows
// make_print_table: function(flist) {
// // Make a table
// var wrapper = document.createElement('div');
// var table = $a(wrapper, 'table', '', me.table_style);
// table.wrapper = wrapper;
//
// // Make Head Row
// table.insertRow(0);
// var col_start = 0;
//
// // If 'SR' exists in flist, then create its heading column cell
// if(flist[0]=='SR') {
// var cell = table.rows[0].insertCell(0);
// cell.innerHTML = head_labels?head_labels[0]:'<b>SR</b>';
// $y(cell, { width: '30px' });
// $y(cell, me.head_cell_style);
// col_start++;
// }
//
// for(var c = col_start; c < flist.length; c++) {
// var cell = table.rows[0].insertCell(c);
// $y(cell, me.head_cell_style);
// cell.innerHTML = head_labels?head_labels[c]:flist[c].label;
// if(flist[c].width) { $y(cell, {width: flist[c].width}); }
// if(widths) { $y(cell, {width: widths[c]}); }
// if(in_list(['Currency', 'Float'], flist[c].fieldtype)) {
// $y(cell, { textAlign: 'right' });
// }
// }
// return table;
// },
//
// // Populate table with data
// populate_table: function(table, data) {
// for(var r = 0; r < data.length; r++) {
// if((!condition) || (condition(data[r]))) {
// // Check for page break
// if(data[r].page_break) {
// table = me.make_print_table(me.flist);
// me.table_list.push(table.wrapper);
// }
//
// var row = table.insertRow(table.rows.length);
//
// // Add serial number if required
// if(me.flist[0] == 'SR') {
// var cell = row.insertCell(0);
// cell.innerHTML = r + 1;
// $y(cell, me.cell_style);
// }
//
// for(var c=me.flist.indexOf('SR')+1; c<me.flist.length; c++){
// var cell = row.insertCell(c);
// $y(cell, me.cell_style);
// if(modifier && me.flist[c].fieldname in modifier) {
// data[r][me.flist[c].fieldname] = modifier[me.flist[c].fieldname](data[r]);
// }
// $s(cell, data[r][me.flist[c].fieldname],
// me.flist[c].fieldtype);
// if(in_list(['Currency', 'Float'], me.flist[c].fieldtype)) {
// cell.style.textAlign = 'right';
// }
// }
// }
// }
// }
// });
//
// // If no data, do not create table
// if(!this.data.length) { return document.createElement('div'); }
//
// this.prepare_col_heads(this.flist);
//
// var table = me.make_print_table(this.flist);
//
// this.table_list = [table.wrapper];
//
// this.populate_table(table, this.data);
//
// // If multiple tables exists, send whole list, else send only one table
// return (me.table_list.length > 1) ? me.table_list : me.table_list[0];
// }

+ 1
- 1
public/js/wn/form/formatters.js Datei anzeigen

@@ -19,7 +19,7 @@ wn.form.formatters = {
},
Link: function(value, docfield) {
if(!value) return "";
if(docfield.options) {
if(docfield && docfield.options) {
return repl('<a href="#Form/%(doctype)s/%(name)s">\
<i class="icon icon-share" title="Open %(name)s" \
style="margin-top:-1px"></i></a> %(name)s', {


+ 206
- 0
public/js/wn/print/print_table.js Datei anzeigen

@@ -0,0 +1,206 @@
// Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
//
// MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

wn.provide("wn.print");

// opts:
// doctype (parent)
// docname
// tabletype
// fieldname
// show_all = false;

wn.print.Table = Class.extend({
init: function(opts) {
$.extend(this, opts);
if(!this.columns)
this.columns = this.get_columns();
this.data = this.get_data();
if(!this.show_all)
this.remove_empty_cols();
this.set_widths();
this.make();
},
get_columns: function() {
return ['Sr'].concat($.map(wn.meta.docfield_list[this.tabletype], function(df) {
return df.print_hide ? null : df.fieldname;
}));
},
get_data: function() {
var children = wn.model.get(this.tabletype, {
parent:this.docname, parenttype:this.doctype, parentfield: this.fieldname})
var data = []
for(var i=0; i<children.length; i++) {
data.push(copy_dict(children[i]));
}
return data;
},
remove_empty_cols: function(flist) {
var cols_with_value = []
var me = this;
$.each(this.data, function(i, row) {
$.each(me.columns, function(ci, fieldname) {
var value = row[fieldname];
if((value!==null && value!=="") || ci==0) {
if(!in_list(cols_with_value, fieldname)) {
cols_with_value.push(fieldname);
}
}
})
});
this.columns = cols_with_value;
},
make: function() {
var me = this;
this.tables = [];
var table_data = [];
$.each(this.data, function(i, d) {
table_data.push(d);
if(d.page_break) {
this.add_table(table_data);
table_data = [];
}
});
if(table_data)
this.add_table(table_data);
},
add_table: function(data) {
var me = this;
var wrapper = $("<div>")
var table = $("<table>").css(this.table_style).appendTo(wrapper);

var headrow = $("<tr>").appendTo(table);
$.each(me.columns, function(ci, fieldname) {
if(this.head_labels) {
var label = this.head_labels[i];
} else {
var df = wn.meta.docfield_map[me.tabletype][fieldname];
var label = df ? df.label : fieldname;
}
$("<th>").html(label)
.appendTo(headrow)
.css(me.head_cell_style)
.css({"width": me.widths[ci] + "%"});
})
$.each(data, function(ri, row) {
var allow = true;
if(this.condition) {
allow = this.condition(row);
}
if(allow) {
var tr = $("<tr>").appendTo(table);
$.each(me.columns, function(ci, fieldname) {
if(ci==0)
var value = ri + 1;
else
var value = row[fieldname];

if(this.modifier && this.modifier[fieldname])
value = this.modifier[fieldname](row);
var df = wn.meta.docfield_map[me.tabletype][fieldname];
value = wn.form.get_formatter(
df && df.fieldtype || "Data")(value);

$("<td>").html(value)
.css(me.cell_style)
.appendTo(tr);
});
}
});
this.tables.push(wrapper)
},
set_widths: function() {
var me = this;
if(!this.widths) {
this.widths = $.map(this.columns, function(fieldname, ci) {
df = wn.meta.docfield_map[me.tabletype][fieldname];
return df && df.width ||
(fieldname=="Sr" ? 20 : 80);
})
}
var sum = 0;
$.each(this.widths, function(i, w) {
sum += cint(w);
});
this.widths = $.map(this.widths, function(w) {
return (flt(w) / sum * 100).toFixed(0);
});
},
get_tables: function() {
if(this.tables.length > 1) {
return $.map(this.tables, function(t) {
t.get(0);
});
} else {
return this.tables[0].get(0);
}
},
cell_style: {
border: '1px solid #999',
padding: '3px',
'vertical-align': 'top'
},
head_cell_style: {
border: '1px solid #999',
padding: '3px',
'vertical-align': 'top',
'background-color': '#ddd',
'font-weight': 'bold'
},
table_style: {
width: '100%',
'border-collapse': 'collapse',
'margin-bottom': '10px',
'margin-top': '10px',
'table-layout': 'fixed'
},
})

function print_table(dt, dn, fieldname, tabletype, cols, head_labels, widths, condition, cssClass, modifier, hide_empty) {
return new wn.print.Table({
doctype: dt,
docname: dn,
fieldname: fieldname,
tabletype: tabletype,
columns: cols,
head_labels: head_labels,
widths: widths,
condition: condition,
cssClass: cssClass,
modifier: modifier
}).get_tables();
}

Laden…
Abbrechen
Speichern