Debian Codegraph @

js/widgets/widget-print.js

raw symbols SCIP
1 /* Widget: print - updated 12/8/2016 (v2.28.1) *//*
2 * Requires tablesorter v2.8+ and jQuery 1.2.6+
3 */
4 /*jshint browser:true, jquery:true, unused:false */
5 /*global jQuery: false */
6 ;(function($) {
7 'use strict';
8
9 var ts = $.tablesorter,
10
11 printTable = ts.printTable = {
12
13 event : 'printTable',
14 basicStyle : 'table, tr, td, th { border : solid 1px black; border-collapse : collapse; } td, th { padding: 2px; }',
15 popupStyle : 'width=500,height=300,scrollbars=1,resizable=1',
16
17 init : function(c) {
18 c.$table
19 .unbind(printTable.event)
20 .bind(printTable.event, function() {
21 // explicitly use table.config.widgetOptions because we want
22 // the most up-to-date values; not the 'wo' from initialization
23 printTable.process(c, c.widgetOptions);
24 return false;
25 });
26 },
27
28 process : function(c, wo) {
29 var $this, data,
30 $table = $('<div/>').append(c.$table.clone()),
31 printStyle = printTable.basicStyle + 'table { width: 100%; }' +
32 // hide filter row
33 '.' + ( ts.css.filterRow || 'tablesorter-filter-row' ) +
34 // hide filtered rows
35 ', .' + ( wo.filter_filteredRow || 'filtered' ) + ' { display: none; }' +
36 // hide sort arrows
37 '.' + ( ts.css.header || 'tablesorter-header' ) + ' { background-image: none !important; }' +
38
39 '@media print { .print_widget_hidden { display: none; } }';
40
41 // replace content with data-attribute content
42 $table.find('[' + wo.print_dataAttrib + ']').each(function() {
43 $this = $(this);
44 $this.text( $this.attr(wo.print_dataAttrib) );
45 });
46
47 // Make sure all lazy loaded images are visible - see #1169
48 data = 'data-' + (wo.lazyload_data_attribute || 'original');
49 $table.find('img[' + data + ']').each(function() {
50 $this = $(this);
51 $this.attr('src', $this.attr(data));
52 });
53
54 // === rows ===
55 // Assume 'visible' means rows hidden by the pager (rows set to 'display:none')
56 // or hidden by a class name which is added to the wo.print_extraCSS definition
57 // look for jQuery filter selector in wo.print_rows & use if found
58 if ( /^f/i.test( wo.print_rows ) ) {
59 printStyle += 'tbody tr:not(.' + ( wo.filter_filteredRow || 'filtered' ) + ') { display: table-row !important; }';
60 } else if ( /^a/i.test( wo.print_rows ) ) {
61 // default force show of all rows
62 printStyle += 'tbody tr { display: table-row !important; }';
63 } else if ( /^[.#:\[]/.test( wo.print_rows ) ) {
64 // look for '.' (class selector), '#' (id selector),
65 // ':' (basic filters, e.g. ':not()') or '[' (attribute selector start)
66 printStyle += 'tbody tr' + wo.print_rows + ' { display: table-row !important; }';
67 }
68
69 // === columns ===
70 // columnSelector -> c.selector.$style
71 // Assume 'visible' means hidden columns have a 'display:none' style, or a class name
72 // add the definition to the wo.print_extraCSS option
73 if (/s/i.test(wo.print_columns) && c.selector && ts.hasWidget( c.table, 'columnSelector' )) {
74 // show selected (visible) columns; make a copy of the columnSelector widget css (not media queries)
75 printStyle += wo.columnSelector_mediaquery && c.selector.auto ? '' : c.selector.$style.text();
76 } else if (/a/i.test(wo.print_columns)) {
77 // force show all cells
78 printStyle += 'td, th { display: table-cell !important; }';
79 }
80
81 printStyle += wo.print_extraCSS;
82
83 // callback function
84 if ( $.isFunction(wo.print_callback) ) {
85 wo.print_callback( c, $table, printStyle );
86 } else {
87 printTable.printOutput(c, $table.html(), printStyle);
88 }
89
90 }, // end process
91
92 printOutput : function(c, data, style) {
93 var wo = c.widgetOptions,
94 lang = ts.language,
95 generator = window.open( '', wo.print_title, printTable.popupStyle ),
96 t = wo.print_title || c.$table.find('caption').text() || c.$table[0].id || document.title || 'table',
97 button = wo.print_now ? '' : '<div class="print_widget_hidden"><a href="javascript:window.print();">' +
98 '<button type="button">' + lang.button_print + '</button></a> <a href="javascript:window.close();">' +
99 '<button type="button">' + lang.button_close + '</button></a><hr></div>';
100 generator.document.write(
101 '<html><head><title>' + t + '</title>' +
102 ( wo.print_styleSheet ? '<link rel="stylesheet" href="' + wo.print_styleSheet + '">' : '' ) +
103 '<style>' + style + '</style>' +
104 '</head><body>' + button + data + '</body></html>'
105 );
106 generator.document.close();
107 // use timeout to allow browser to build DOM before printing
108 // Print preview in Chrome doesn't work without this code
109 if ( wo.print_now ) {
110 setTimeout( function() {
111 generator.print();
112 generator.close();
113 }, 10 );
114 }
115 return true;
116 },
117
118 remove : function(c) {
119 c.$table.off(printTable.event);
120 }
121
122 };
123
124 ts.language.button_close = 'Close';
125 ts.language.button_print = 'Print';
126
127 ts.addWidget({
128 id: 'print',
129 options: {
130 print_title : '', // this option > caption > table id > 'table'
131 print_dataAttrib : 'data-name', // header attrib containing modified header name
132 print_rows : 'filtered', // (a)ll, (v)isible, (f)iltered or custom css selector
133 print_columns : 'selected', // (a)ll, (v)isbible or (s)elected (if columnSelector widget is added)
134 print_extraCSS : '', // add any extra css definitions for the popup window here
135 print_styleSheet : '', // add the url of your print stylesheet
136 print_now : true, // Open the print dialog immediately if true
137 // callback executed when processing completes
138 // to continue printing, use the following function:
139 // function( config, $table, printStyle ) {
140 // // do something to the table or printStyle string
141 // $.tablesorter.printTable.printOutput( config, $table.html(), printStyle );
142 // }
143 print_callback : null
144 },
145 init: function(table, thisWidget, c) {
146 printTable.init(c);
147 },
148 remove: function(table, c) {
149 printTable.remove(c);
150 }
151
152 });
153
154 })(jQuery);