Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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