You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

230 lines
5.5 KiB

3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*!
  2. * Print button for Buttons and DataTables.
  3. * 2016 SpryMedia Ltd - datatables.net/license
  4. */
  5. (function( factory ){
  6. if ( typeof define === 'function' && define.amd ) {
  7. // AMD
  8. define( ['jquery', 'datatables.net', 'datatables.net-buttons'], function ( $ ) {
  9. return factory( $, window, document );
  10. } );
  11. }
  12. else if ( typeof exports === 'object' ) {
  13. // CommonJS
  14. module.exports = function (root, $) {
  15. if ( ! root ) {
  16. // CommonJS environments without a window global must pass a
  17. // root. This will give an error otherwise
  18. root = window;
  19. }
  20. if ( ! $ ) {
  21. $ = typeof window !== 'undefined' ? // jQuery's factory checks for a global window
  22. require('jquery') :
  23. require('jquery')( root );
  24. }
  25. if ( ! $.fn.dataTable ) {
  26. require('datatables.net')(root, $);
  27. }
  28. if ( ! $.fn.dataTable.Buttons ) {
  29. require('datatables.net-buttons')(root, $);
  30. }
  31. return factory( $, root, root.document );
  32. };
  33. }
  34. else {
  35. // Browser
  36. factory( jQuery, window, document );
  37. }
  38. }(function( $, window, document, undefined ) {
  39. 'use strict';
  40. var DataTable = $.fn.dataTable;
  41. var _link = document.createElement( 'a' );
  42. /**
  43. * Clone link and style tags, taking into account the need to change the source
  44. * path.
  45. *
  46. * @param {node} el Element to convert
  47. */
  48. var _styleToAbs = function( el ) {
  49. var url;
  50. var clone = $(el).clone()[0];
  51. var linkHost;
  52. if ( clone.nodeName.toLowerCase() === 'link' ) {
  53. clone.href = _relToAbs( clone.href );
  54. }
  55. return clone.outerHTML;
  56. };
  57. /**
  58. * Convert a URL from a relative to an absolute address so it will work
  59. * correctly in the popup window which has no base URL.
  60. *
  61. * @param {string} href URL
  62. */
  63. var _relToAbs = function( href ) {
  64. // Assign to a link on the original page so the browser will do all the
  65. // hard work of figuring out where the file actually is
  66. _link.href = href;
  67. var linkHost = _link.host;
  68. // IE doesn't have a trailing slash on the host
  69. // Chrome has it on the pathname
  70. if ( linkHost.indexOf('/') === -1 && _link.pathname.indexOf('/') !== 0) {
  71. linkHost += '/';
  72. }
  73. return _link.protocol+"//"+linkHost+_link.pathname+_link.search;
  74. };
  75. DataTable.ext.buttons.print = {
  76. className: 'buttons-print',
  77. text: function ( dt ) {
  78. return dt.i18n( 'buttons.print', 'Print' );
  79. },
  80. action: function ( e, dt, button, config ) {
  81. var data = dt.buttons.exportData(
  82. $.extend( {decodeEntities: false}, config.exportOptions ) // XSS protection
  83. );
  84. var exportInfo = dt.buttons.exportInfo( config );
  85. var columnClasses = dt
  86. .columns( config.exportOptions.columns )
  87. .flatten()
  88. .map( function (idx) {
  89. return dt.settings()[0].aoColumns[dt.column(idx).index()].sClass;
  90. } )
  91. .toArray();
  92. var addRow = function ( d, tag ) {
  93. var str = '<tr>';
  94. for ( var i=0, ien=d.length ; i<ien ; i++ ) {
  95. // null and undefined aren't useful in the print output
  96. var dataOut = d[i] === null || d[i] === undefined ?
  97. '' :
  98. d[i];
  99. var classAttr = columnClasses[i] ?
  100. 'class="'+columnClasses[i]+'"' :
  101. '';
  102. str += '<'+tag+' '+classAttr+'>'+dataOut+'</'+tag+'>';
  103. }
  104. return str + '</tr>';
  105. };
  106. // Construct a table for printing
  107. var html = '<table class="'+dt.table().node().className+'">';
  108. if ( config.header ) {
  109. html += '<thead>'+ addRow( data.header, 'th' ) +'</thead>';
  110. }
  111. html += '<tbody>';
  112. for ( var i=0, ien=data.body.length ; i<ien ; i++ ) {
  113. html += addRow( data.body[i], 'td' );
  114. }
  115. html += '</tbody>';
  116. if ( config.footer && data.footer ) {
  117. html += '<tfoot>'+ addRow( data.footer, 'th' ) +'</tfoot>';
  118. }
  119. html += '</table>';
  120. // Open a new window for the printable table
  121. var win = window.open( '', '' );
  122. if (! win) {
  123. dt.buttons.info(
  124. dt.i18n( 'buttons.printErrorTitle', 'Unable to open print view' ),
  125. dt.i18n( 'buttons.printErrorMsg', 'Please allow popups in your browser for this site to be able to view the print view.' ),
  126. 5000
  127. );
  128. return;
  129. }
  130. win.document.close();
  131. // Inject the title and also a copy of the style and link tags from this
  132. // document so the table can retain its base styling. Note that we have
  133. // to use string manipulation as IE won't allow elements to be created
  134. // in the host document and then appended to the new window.
  135. var head = '<title>'+exportInfo.title+'</title>';
  136. $('style, link').each( function () {
  137. head += _styleToAbs( this );
  138. } );
  139. try {
  140. win.document.head.innerHTML = head; // Work around for Edge
  141. }
  142. catch (e) {
  143. $(win.document.head).html( head ); // Old IE
  144. }
  145. // Inject the table and other surrounding information
  146. win.document.body.innerHTML =
  147. '<h1>'+exportInfo.title+'</h1>'+
  148. '<div>'+(exportInfo.messageTop || '')+'</div>'+
  149. html+
  150. '<div>'+(exportInfo.messageBottom || '')+'</div>';
  151. $(win.document.body).addClass('dt-print-view');
  152. $('img', win.document.body).each( function ( i, img ) {
  153. img.setAttribute( 'src', _relToAbs( img.getAttribute('src') ) );
  154. } );
  155. if ( config.customize ) {
  156. config.customize( win, config, dt );
  157. }
  158. // Allow stylesheets time to load
  159. var autoPrint = function () {
  160. if ( config.autoPrint ) {
  161. win.print(); // blocking - so close will not
  162. win.close(); // execute until this is done
  163. }
  164. };
  165. if ( navigator.userAgent.match(/Trident\/\d.\d/) ) { // IE needs to call this without a setTimeout
  166. autoPrint();
  167. }
  168. else {
  169. win.setTimeout( autoPrint, 1000 );
  170. }
  171. },
  172. title: '*',
  173. messageTop: '*',
  174. messageBottom: '*',
  175. exportOptions: {},
  176. header: true,
  177. footer: false,
  178. autoPrint: true,
  179. customize: null
  180. };
  181. return DataTable;
  182. }));