Debian Codegraph @

Buttons/js/buttons.print.js

raw symbols SCIP
1 /*!
2 * Print button for Buttons and DataTables.
3 * 2015 SpryMedia Ltd - datatables.net/license
4 */
5
6 (function($, DataTable) {
7 "use strict";
8
9
10 var _link = document.createElement( 'a' );
11
12 /**
13 * Convert a `link` tag's URL from a relative to an absolute address so it will
14 * work correctly in the popup window which has no base URL.
15 *
16 * @param {node} el Element to convert
17 */
18 var _relToAbs = function( el ) {
19 var url;
20 var clone = $(el).clone()[0];
21 var linkHost;
22
23 if ( clone.nodeName.toLowerCase() === 'link' ) {
24 _link.href = clone.href;
25 linkHost = _link.host;
26
27 // IE doesn't have a trailing slash on the host
28 // Chrome has it on the pathname
29 if ( linkHost.indexOf('/') === -1 && _link.pathname.indexOf('/') !== 0) {
30 linkHost += '/';
31 }
32
33 clone.href = _link.protocol+"//"+linkHost+_link.pathname+_link.search;
34 }
35
36 return clone.outerHTML;
37 };
38
39
40 DataTable.ext.buttons.print = {
41 className: 'buttons-print',
42
43 text: function ( dt ) {
44 return dt.i18n( 'buttons.print', 'Print' );
45 },
46
47 action: function ( e, dt, button, config ) {
48 var data = dt.buttons.exportData( config.exportOptions );
49 var addRow = function ( d, tag ) {
50 var str = '<tr>';
51
52 for ( var i=0, ien=d.length ; i<ien ; i++ ) {
53 str += '<'+tag+'>'+d[i]+'</'+tag+'>';
54 }
55
56 return str + '</tr>';
57 };
58
59 // Construct a table for printing
60 var html = '<table class="'+dt.table().node().className+'">';
61
62 if ( config.header ) {
63 html += '<thead>'+ addRow( data.header, 'th' ) +'</thead>';
64 }
65
66 html += '<tbody>';
67 for ( var i=0, ien=data.body.length ; i<ien ; i++ ) {
68 html += addRow( data.body[i], 'td' );
69 }
70 html += '</tbody>';
71
72 if ( config.footer ) {
73 html += '<thead>'+ addRow( data.footer, 'th' ) +'</thead>';
74 }
75
76 // Open a new window for the printable table
77 var win = window.open( '', '' );
78 var title = config.title.replace( '*', $('title').text() );
79
80 win.document.close();
81
82 // Inject the title and also a copy of the style and link tags from this
83 // document so the table can retain its base styling. Note that we have
84 // to use string manipulation as IE won't allow elements to be created
85 // in the host document and then appended to the new window.
86 var head = '<title>'+title+'</title>';
87 $('style, link').each( function () {
88 head += _relToAbs( this );
89 } );
90
91 $(win.document.head).html( head );
92
93 // Inject the table and other surrounding information
94 $(win.document.body).html(
95 '<h1>'+title+'</h1>'+
96 '<div>'+config.message+'</div>'+
97 html
98 );
99
100 if ( config.customize ) {
101 config.customize( win );
102 }
103
104 setTimeout( function () {
105 if ( config.autoPrint ) {
106 win.print(); // blocking - so close will not
107 win.close(); // execute until this is done
108 }
109 }, 250 );
110 },
111
112 title: '*',
113
114 message: '',
115
116 exportOptions: {},
117
118 header: true,
119
120 footer: false,
121
122 autoPrint: true,
123
124 customize: null
125 };
126
127
128 })(jQuery, jQuery.fn.dataTable);