corrade-nucleus-nucleons – Blame information for rev 20

Subversion Repositories:
Rev:
Rev Author Line No. Line
20 office 1 (function ($) {
2 'use strict';
3  
4 var sprintf = $.fn.bootstrapTable.utils.sprintf;
5  
6 function printPageBuilderDefault(table) {
7 return '<html><head>' +
8 '<style type="text/css" media="print">' +
9 ' @page { size: auto; margin: 25px 0 25px 0; }' +
10 '</style>' +
11 '<style type="text/css" media="all">' +
12 'table{border-collapse: collapse; font-size: 12px; }\n' +
13 'table, th, td {border: 1px solid grey}\n' +
14 'th, td {text-align: center; vertical-align: middle;}\n' +
15 'p {font-weight: bold; margin-left:20px }\n' +
16 'table { width:94%; margin-left:3%; margin-right:3%}\n' +
17 'div.bs-table-print { text-align:center;}\n' +
18 '</style></head><title>Print Table</title><body>' +
19 '<p>Printed on: ' + new Date + ' </p>' +
20 '<div class="bs-table-print">' + table + "</div></body></html>";
21 }
22 $.extend($.fn.bootstrapTable.defaults, {
23 showPrint: false,
24 printAsFilteredAndSortedOnUI: true, //boolean, when true - print table as sorted and filtered on UI.
25 //Please note that if true is set, along with explicit predefined print options for filtering and sorting (printFilter, printSortOrder, printSortColumn)- then they will be applied on data already filtered and sorted by UI controls.
26 //For printing data as filtered and sorted on UI - do not set these 3 options:printFilter, printSortOrder, printSortColumn
27 printSortColumn: undefined , //String, set column field name to be sorted by
28 printSortOrder: 'asc', //String: 'asc' , 'desc' - relevant only if printSortColumn is set
29 printPageBuilder: function(table){return printPageBuilderDefault(table)} // function, receive html <table> element as string, returns html string for printing. by default delegates to function printPageBuilderDefault(table). used for styling and adding header or footer
30 });
31 $.extend($.fn.bootstrapTable.COLUMN_DEFAULTS, {
32 printFilter: undefined, //set value to filter by in print page
33 printIgnore: false, //boolean, set true to ignore this column in the print page
34 printFormatter:undefined //function(value, row, index), formats the cell value for this column in the printed table. Function behaviour is similar to the 'formatter' column option
35 });
36 $.extend($.fn.bootstrapTable.defaults.icons, {
37 print: 'glyphicon-print icon-share'
38 });
39  
40 var BootstrapTable = $.fn.bootstrapTable.Constructor,
41 _initToolbar = BootstrapTable.prototype.initToolbar;
42  
43 BootstrapTable.prototype.initToolbar = function () {
44 this.showToolbar = this.options.showPrint;
45  
46 _initToolbar.apply(this, Array.prototype.slice.apply(arguments));
47  
48 if (this.options.showPrint) {
49 var that = this,
50 $btnGroup = this.$toolbar.find('>.btn-group'),
51 $print = $btnGroup.find('button.bs-print');
52  
53 if (!$print.length) {
54 $print = $([
55 '<button class="bs-print btn btn-default' + sprintf(' btn-%s"', this.options.iconSize) + ' name="print" title="print" type="button">',
56 sprintf('<i class="%s %s"></i> ', this.options.iconsPrefix, this.options.icons.print),
57 '</button>'].join('')).appendTo($btnGroup);
58  
59 $print.click(function () {
60 function formatValue(row, i, column ) {
61 var value = row[column.field];
62 if (typeof column.printFormatter === 'function') {
63 return column.printFormatter.apply(column, [value, row, i]);
64 }
65 else {
66 return value || "-";
67 }
68 }
69 function buildTable(data,columns) {
70 var out = "<table><thead><tr>";
71 for(var h = 0; h < columns.length; h++) {
72 if(!columns[h].printIgnore) {
73 out += ("<th>"+columns[h].title+"</th>");
74 }
75 }
76 out += "</tr></thead><tbody>";
77 for(var i = 0; i < data.length; i++) {
78 out += "<tr>";
79 for(var j = 0; j < columns.length; j++) {
80 if(!columns[j].printIgnore) {
81 out += ("<td>"+ formatValue(data[i], i, columns[j])+"</td>");
82 }
83 }
84 out += "</tr>";
85 }
86 out += "</tbody></table>";
87 return out;
88 }
89 function sortRows(data,colName,sortOrder) {
90 if(!colName){
91 return data;
92 }
93 var reverse = sortOrder != 'asc';
94 reverse = -((+reverse) || -1);
95 return data.sort(function (a, b) {
96 return reverse * (a[colName].localeCompare(b[colName]));
97 });
98 }
99 function filterRow(row,filters) {
100 for (var index = 0; index < filters.length; ++index) {
101 if(row[filters[index].colName]!=filters[index].value) {
102 return false;
103 }
104 }
105 return true;
106 }
107 function filterRows(data,filters) {
108 return data.filter(function (row) {
109 return filterRow(row,filters)
110 });
111 }
112 function getColumnFilters(columns) {
113 return !columns || !columns[0] ? [] : columns[0].filter(function (col) {
114 return col.printFilter;
115 }).map(function (col) {
116 return {colName:col.field, value:col.printFilter};
117 });
118 }
119 var doPrint = function (data) {
120 data=filterRows(data,getColumnFilters(that.options.columns));
121 data=sortRows(data,that.options.printSortColumn,that.options.printSortOrder);
122 var table=buildTable(data,that.options.columns[0]);
123 var newWin = window.open("");
124 newWin.document.write(that.options.printPageBuilder.call(this, table));
125 newWin.print();
126 newWin.close();
127 };
128 doPrint(that.options.printAsFilteredAndSortedOnUI? that.getData() : that.options.data.slice(0));
129 });
130 }
131 }
132 };
133 })(jQuery);