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.

1486 lines
44 KiB

3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*!
  2. * HTML5 export buttons for Buttons and DataTables.
  3. * 2016 SpryMedia Ltd - datatables.net/license
  4. *
  5. * FileSaver.js (1.3.3) - MIT license
  6. * Copyright © 2016 Eli Grey - http://eligrey.com
  7. */
  8. (function( factory ){
  9. if ( typeof define === 'function' && define.amd ) {
  10. // AMD
  11. define( ['jquery', 'datatables.net', 'datatables.net-buttons'], function ( $ ) {
  12. return factory( $, window, document );
  13. } );
  14. }
  15. else if ( typeof exports === 'object' ) {
  16. // CommonJS
  17. module.exports = function (root, $, jszip, pdfmake) {
  18. if ( ! root ) {
  19. // CommonJS environments without a window global must pass a
  20. // root. This will give an error otherwise
  21. root = window;
  22. }
  23. if ( ! $ ) {
  24. $ = typeof window !== 'undefined' ? // jQuery's factory checks for a global window
  25. require('jquery') :
  26. require('jquery')( root );
  27. }
  28. if ( ! $.fn.dataTable ) {
  29. require('datatables.net')(root, $);
  30. }
  31. if ( ! $.fn.dataTable.Buttons ) {
  32. require('datatables.net-buttons')(root, $);
  33. }
  34. return factory( $, root, root.document, jszip, pdfmake );
  35. };
  36. }
  37. else {
  38. // Browser
  39. factory( jQuery, window, document );
  40. }
  41. }(function( $, window, document, jszip, pdfmake, undefined ) {
  42. 'use strict';
  43. var DataTable = $.fn.dataTable;
  44. // Allow the constructor to pass in JSZip and PDFMake from external requires.
  45. // Otherwise, use globally defined variables, if they are available.
  46. var useJszip;
  47. var usePdfmake;
  48. function _jsZip () {
  49. return useJszip || window.JSZip;
  50. }
  51. function _pdfMake () {
  52. return usePdfmake || window.pdfMake;
  53. }
  54. DataTable.Buttons.pdfMake = function (_) {
  55. if ( ! _ ) {
  56. return _pdfMake();
  57. }
  58. usePdfmake = _;
  59. }
  60. DataTable.Buttons.jszip = function (_) {
  61. if ( ! _ ) {
  62. return _jsZip();
  63. }
  64. useJszip = _;
  65. }
  66. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  67. * FileSaver.js dependency
  68. */
  69. /*jslint bitwise: true, indent: 4, laxbreak: true, laxcomma: true, smarttabs: true, plusplus: true */
  70. var _saveAs = (function(view) {
  71. "use strict";
  72. // IE <10 is explicitly unsupported
  73. if (typeof view === "undefined" || typeof navigator !== "undefined" && /MSIE [1-9]\./.test(navigator.userAgent)) {
  74. return;
  75. }
  76. var
  77. doc = view.document
  78. // only get URL when necessary in case Blob.js hasn't overridden it yet
  79. , get_URL = function() {
  80. return view.URL || view.webkitURL || view;
  81. }
  82. , save_link = doc.createElementNS("http://www.w3.org/1999/xhtml", "a")
  83. , can_use_save_link = "download" in save_link
  84. , click = function(node) {
  85. var event = new MouseEvent("click");
  86. node.dispatchEvent(event);
  87. }
  88. , is_safari = /constructor/i.test(view.HTMLElement) || view.safari
  89. , is_chrome_ios =/CriOS\/[\d]+/.test(navigator.userAgent)
  90. , throw_outside = function(ex) {
  91. (view.setImmediate || view.setTimeout)(function() {
  92. throw ex;
  93. }, 0);
  94. }
  95. , force_saveable_type = "application/octet-stream"
  96. // the Blob API is fundamentally broken as there is no "downloadfinished" event to subscribe to
  97. , arbitrary_revoke_timeout = 1000 * 40 // in ms
  98. , revoke = function(file) {
  99. var revoker = function() {
  100. if (typeof file === "string") { // file is an object URL
  101. get_URL().revokeObjectURL(file);
  102. } else { // file is a File
  103. file.remove();
  104. }
  105. };
  106. setTimeout(revoker, arbitrary_revoke_timeout);
  107. }
  108. , dispatch = function(filesaver, event_types, event) {
  109. event_types = [].concat(event_types);
  110. var i = event_types.length;
  111. while (i--) {
  112. var listener = filesaver["on" + event_types[i]];
  113. if (typeof listener === "function") {
  114. try {
  115. listener.call(filesaver, event || filesaver);
  116. } catch (ex) {
  117. throw_outside(ex);
  118. }
  119. }
  120. }
  121. }
  122. , auto_bom = function(blob) {
  123. // prepend BOM for UTF-8 XML and text/* types (including HTML)
  124. // note: your browser will automatically convert UTF-16 U+FEFF to EF BB BF
  125. if (/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
  126. return new Blob([String.fromCharCode(0xFEFF), blob], {type: blob.type});
  127. }
  128. return blob;
  129. }
  130. , FileSaver = function(blob, name, no_auto_bom) {
  131. if (!no_auto_bom) {
  132. blob = auto_bom(blob);
  133. }
  134. // First try a.download, then web filesystem, then object URLs
  135. var
  136. filesaver = this
  137. , type = blob.type
  138. , force = type === force_saveable_type
  139. , object_url
  140. , dispatch_all = function() {
  141. dispatch(filesaver, "writestart progress write writeend".split(" "));
  142. }
  143. // on any filesys errors revert to saving with object URLs
  144. , fs_error = function() {
  145. if ((is_chrome_ios || (force && is_safari)) && view.FileReader) {
  146. // Safari doesn't allow downloading of blob urls
  147. var reader = new FileReader();
  148. reader.onloadend = function() {
  149. var url = is_chrome_ios ? reader.result : reader.result.replace(/^data:[^;]*;/, 'data:attachment/file;');
  150. var popup = view.open(url, '_blank');
  151. if(!popup) view.location.href = url;
  152. url=undefined; // release reference before dispatching
  153. filesaver.readyState = filesaver.DONE;
  154. dispatch_all();
  155. };
  156. reader.readAsDataURL(blob);
  157. filesaver.readyState = filesaver.INIT;
  158. return;
  159. }
  160. // don't create more object URLs than needed
  161. if (!object_url) {
  162. object_url = get_URL().createObjectURL(blob);
  163. }
  164. if (force) {
  165. view.location.href = object_url;
  166. } else {
  167. var opened = view.open(object_url, "_blank");
  168. if (!opened) {
  169. // Apple does not allow window.open, see https://developer.apple.com/library/safari/documentation/Tools/Conceptual/SafariExtensionGuide/WorkingwithWindowsandTabs/WorkingwithWindowsandTabs.html
  170. view.location.href = object_url;
  171. }
  172. }
  173. filesaver.readyState = filesaver.DONE;
  174. dispatch_all();
  175. revoke(object_url);
  176. }
  177. ;
  178. filesaver.readyState = filesaver.INIT;
  179. if (can_use_save_link) {
  180. object_url = get_URL().createObjectURL(blob);
  181. setTimeout(function() {
  182. save_link.href = object_url;
  183. save_link.download = name;
  184. click(save_link);
  185. dispatch_all();
  186. revoke(object_url);
  187. filesaver.readyState = filesaver.DONE;
  188. });
  189. return;
  190. }
  191. fs_error();
  192. }
  193. , FS_proto = FileSaver.prototype
  194. , saveAs = function(blob, name, no_auto_bom) {
  195. return new FileSaver(blob, name || blob.name || "download", no_auto_bom);
  196. }
  197. ;
  198. // IE 10+ (native saveAs)
  199. if (typeof navigator !== "undefined" && navigator.msSaveOrOpenBlob) {
  200. return function(blob, name, no_auto_bom) {
  201. name = name || blob.name || "download";
  202. if (!no_auto_bom) {
  203. blob = auto_bom(blob);
  204. }
  205. return navigator.msSaveOrOpenBlob(blob, name);
  206. };
  207. }
  208. FS_proto.abort = function(){};
  209. FS_proto.readyState = FS_proto.INIT = 0;
  210. FS_proto.WRITING = 1;
  211. FS_proto.DONE = 2;
  212. FS_proto.error =
  213. FS_proto.onwritestart =
  214. FS_proto.onprogress =
  215. FS_proto.onwrite =
  216. FS_proto.onabort =
  217. FS_proto.onerror =
  218. FS_proto.onwriteend =
  219. null;
  220. return saveAs;
  221. }(
  222. typeof self !== "undefined" && self
  223. || typeof window !== "undefined" && window
  224. || this.content
  225. ));
  226. // Expose file saver on the DataTables API. Can't attach to `DataTables.Buttons`
  227. // since this file can be loaded before Button's core!
  228. DataTable.fileSave = _saveAs;
  229. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  230. * Local (private) functions
  231. */
  232. /**
  233. * Get the sheet name for Excel exports.
  234. *
  235. * @param {object} config Button configuration
  236. */
  237. var _sheetname = function ( config )
  238. {
  239. var sheetName = 'Sheet1';
  240. if ( config.sheetName ) {
  241. sheetName = config.sheetName.replace(/[\[\]\*\/\\\?\:]/g, '');
  242. }
  243. return sheetName;
  244. };
  245. /**
  246. * Get the newline character(s)
  247. *
  248. * @param {object} config Button configuration
  249. * @return {string} Newline character
  250. */
  251. var _newLine = function ( config )
  252. {
  253. return config.newline ?
  254. config.newline :
  255. navigator.userAgent.match(/Windows/) ?
  256. '\r\n' :
  257. '\n';
  258. };
  259. /**
  260. * Combine the data from the `buttons.exportData` method into a string that
  261. * will be used in the export file.
  262. *
  263. * @param {DataTable.Api} dt DataTables API instance
  264. * @param {object} config Button configuration
  265. * @return {object} The data to export
  266. */
  267. var _exportData = function ( dt, config )
  268. {
  269. var newLine = _newLine( config );
  270. var data = dt.buttons.exportData( config.exportOptions );
  271. var boundary = config.fieldBoundary;
  272. var separator = config.fieldSeparator;
  273. var reBoundary = new RegExp( boundary, 'g' );
  274. var escapeChar = config.escapeChar !== undefined ?
  275. config.escapeChar :
  276. '\\';
  277. var join = function ( a ) {
  278. var s = '';
  279. // If there is a field boundary, then we might need to escape it in
  280. // the source data
  281. for ( var i=0, ien=a.length ; i<ien ; i++ ) {
  282. if ( i > 0 ) {
  283. s += separator;
  284. }
  285. s += boundary ?
  286. boundary + ('' + a[i]).replace( reBoundary, escapeChar+boundary ) + boundary :
  287. a[i];
  288. }
  289. return s;
  290. };
  291. var header = config.header ? join( data.header )+newLine : '';
  292. var footer = config.footer && data.footer ? newLine+join( data.footer ) : '';
  293. var body = [];
  294. for ( var i=0, ien=data.body.length ; i<ien ; i++ ) {
  295. body.push( join( data.body[i] ) );
  296. }
  297. return {
  298. str: header + body.join( newLine ) + footer,
  299. rows: body.length
  300. };
  301. };
  302. /**
  303. * Older versions of Safari (prior to tech preview 18) don't support the
  304. * download option required.
  305. *
  306. * @return {Boolean} `true` if old Safari
  307. */
  308. var _isDuffSafari = function ()
  309. {
  310. var safari = navigator.userAgent.indexOf('Safari') !== -1 &&
  311. navigator.userAgent.indexOf('Chrome') === -1 &&
  312. navigator.userAgent.indexOf('Opera') === -1;
  313. if ( ! safari ) {
  314. return false;
  315. }
  316. var version = navigator.userAgent.match( /AppleWebKit\/(\d+\.\d+)/ );
  317. if ( version && version.length > 1 && version[1]*1 < 603.1 ) {
  318. return true;
  319. }
  320. return false;
  321. };
  322. /**
  323. * Convert from numeric position to letter for column names in Excel
  324. * @param {int} n Column number
  325. * @return {string} Column letter(s) name
  326. */
  327. function createCellPos( n ){
  328. var ordA = 'A'.charCodeAt(0);
  329. var ordZ = 'Z'.charCodeAt(0);
  330. var len = ordZ - ordA + 1;
  331. var s = "";
  332. while( n >= 0 ) {
  333. s = String.fromCharCode(n % len + ordA) + s;
  334. n = Math.floor(n / len) - 1;
  335. }
  336. return s;
  337. }
  338. try {
  339. var _serialiser = new XMLSerializer();
  340. var _ieExcel;
  341. }
  342. catch (t) {}
  343. /**
  344. * Recursively add XML files from an object's structure to a ZIP file. This
  345. * allows the XSLX file to be easily defined with an object's structure matching
  346. * the files structure.
  347. *
  348. * @param {JSZip} zip ZIP package
  349. * @param {object} obj Object to add (recursive)
  350. */
  351. function _addToZip( zip, obj ) {
  352. if ( _ieExcel === undefined ) {
  353. // Detect if we are dealing with IE's _awful_ serialiser by seeing if it
  354. // drop attributes
  355. _ieExcel = _serialiser
  356. .serializeToString(
  357. ( new window.DOMParser() ).parseFromString( excelStrings['xl/worksheets/sheet1.xml'], 'text/xml' )
  358. )
  359. .indexOf( 'xmlns:r' ) === -1;
  360. }
  361. $.each( obj, function ( name, val ) {
  362. if ( $.isPlainObject( val ) ) {
  363. var newDir = zip.folder( name );
  364. _addToZip( newDir, val );
  365. }
  366. else {
  367. if ( _ieExcel ) {
  368. // IE's XML serialiser will drop some name space attributes from
  369. // from the root node, so we need to save them. Do this by
  370. // replacing the namespace nodes with a regular attribute that
  371. // we convert back when serialised. Edge does not have this
  372. // issue
  373. var worksheet = val.childNodes[0];
  374. var i, ien;
  375. var attrs = [];
  376. for ( i=worksheet.attributes.length-1 ; i>=0 ; i-- ) {
  377. var attrName = worksheet.attributes[i].nodeName;
  378. var attrValue = worksheet.attributes[i].nodeValue;
  379. if ( attrName.indexOf( ':' ) !== -1 ) {
  380. attrs.push( { name: attrName, value: attrValue } );
  381. worksheet.removeAttribute( attrName );
  382. }
  383. }
  384. for ( i=0, ien=attrs.length ; i<ien ; i++ ) {
  385. var attr = val.createAttribute( attrs[i].name.replace( ':', '_dt_b_namespace_token_' ) );
  386. attr.value = attrs[i].value;
  387. worksheet.setAttributeNode( attr );
  388. }
  389. }
  390. var str = _serialiser.serializeToString(val);
  391. // Fix IE's XML
  392. if ( _ieExcel ) {
  393. // IE doesn't include the XML declaration
  394. if ( str.indexOf( '<?xml' ) === -1 ) {
  395. str = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+str;
  396. }
  397. // Return namespace attributes to being as such
  398. str = str.replace( /_dt_b_namespace_token_/g, ':' );
  399. // Remove testing name space that IE puts into the space preserve attr
  400. str = str.replace( /xmlns:NS[\d]+="" NS[\d]+:/g, '' );
  401. }
  402. // Safari, IE and Edge will put empty name space attributes onto
  403. // various elements making them useless. This strips them out
  404. str = str.replace( /<([^<>]*?) xmlns=""([^<>]*?)>/g, '<$1 $2>' );
  405. zip.file( name, str );
  406. }
  407. } );
  408. }
  409. /**
  410. * Create an XML node and add any children, attributes, etc without needing to
  411. * be verbose in the DOM.
  412. *
  413. * @param {object} doc XML document
  414. * @param {string} nodeName Node name
  415. * @param {object} opts Options - can be `attr` (attributes), `children`
  416. * (child nodes) and `text` (text content)
  417. * @return {node} Created node
  418. */
  419. function _createNode( doc, nodeName, opts ) {
  420. var tempNode = doc.createElement( nodeName );
  421. if ( opts ) {
  422. if ( opts.attr ) {
  423. $(tempNode).attr( opts.attr );
  424. }
  425. if ( opts.children ) {
  426. $.each( opts.children, function ( key, value ) {
  427. tempNode.appendChild( value );
  428. } );
  429. }
  430. if ( opts.text !== null && opts.text !== undefined ) {
  431. tempNode.appendChild( doc.createTextNode( opts.text ) );
  432. }
  433. }
  434. return tempNode;
  435. }
  436. /**
  437. * Get the width for an Excel column based on the contents of that column
  438. * @param {object} data Data for export
  439. * @param {int} col Column index
  440. * @return {int} Column width
  441. */
  442. function _excelColWidth( data, col ) {
  443. var max = data.header[col].length;
  444. var len, lineSplit, str;
  445. if ( data.footer && data.footer[col].length > max ) {
  446. max = data.footer[col].length;
  447. }
  448. for ( var i=0, ien=data.body.length ; i<ien ; i++ ) {
  449. var point = data.body[i][col];
  450. str = point !== null && point !== undefined ?
  451. point.toString() :
  452. '';
  453. // If there is a newline character, workout the width of the column
  454. // based on the longest line in the string
  455. if ( str.indexOf('\n') !== -1 ) {
  456. lineSplit = str.split('\n');
  457. lineSplit.sort( function (a, b) {
  458. return b.length - a.length;
  459. } );
  460. len = lineSplit[0].length;
  461. }
  462. else {
  463. len = str.length;
  464. }
  465. if ( len > max ) {
  466. max = len;
  467. }
  468. // Max width rather than having potentially massive column widths
  469. if ( max > 40 ) {
  470. return 54; // 40 * 1.35
  471. }
  472. }
  473. max *= 1.35;
  474. // And a min width
  475. return max > 6 ? max : 6;
  476. }
  477. // Excel - Pre-defined strings to build a basic XLSX file
  478. var excelStrings = {
  479. "_rels/.rels":
  480. '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+
  481. '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">'+
  482. '<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>'+
  483. '</Relationships>',
  484. "xl/_rels/workbook.xml.rels":
  485. '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+
  486. '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">'+
  487. '<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/>'+
  488. '<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>'+
  489. '</Relationships>',
  490. "[Content_Types].xml":
  491. '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+
  492. '<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">'+
  493. '<Default Extension="xml" ContentType="application/xml" />'+
  494. '<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml" />'+
  495. '<Default Extension="jpeg" ContentType="image/jpeg" />'+
  496. '<Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml" />'+
  497. '<Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml" />'+
  498. '<Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml" />'+
  499. '</Types>',
  500. "xl/workbook.xml":
  501. '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+
  502. '<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">'+
  503. '<fileVersion appName="xl" lastEdited="5" lowestEdited="5" rupBuild="24816"/>'+
  504. '<workbookPr showInkAnnotation="0" autoCompressPictures="0"/>'+
  505. '<bookViews>'+
  506. '<workbookView xWindow="0" yWindow="0" windowWidth="25600" windowHeight="19020" tabRatio="500"/>'+
  507. '</bookViews>'+
  508. '<sheets>'+
  509. '<sheet name="Sheet1" sheetId="1" r:id="rId1"/>'+
  510. '</sheets>'+
  511. '<definedNames/>'+
  512. '</workbook>',
  513. "xl/worksheets/sheet1.xml":
  514. '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+
  515. '<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac">'+
  516. '<sheetData/>'+
  517. '<mergeCells count="0"/>'+
  518. '</worksheet>',
  519. "xl/styles.xml":
  520. '<?xml version="1.0" encoding="UTF-8"?>'+
  521. '<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac">'+
  522. '<numFmts count="6">'+
  523. '<numFmt numFmtId="164" formatCode="#,##0.00_-\ [$$-45C]"/>'+
  524. '<numFmt numFmtId="165" formatCode="&quot;£&quot;#,##0.00"/>'+
  525. '<numFmt numFmtId="166" formatCode="[$€-2]\ #,##0.00"/>'+
  526. '<numFmt numFmtId="167" formatCode="0.0%"/>'+
  527. '<numFmt numFmtId="168" formatCode="#,##0;(#,##0)"/>'+
  528. '<numFmt numFmtId="169" formatCode="#,##0.00;(#,##0.00)"/>'+
  529. '</numFmts>'+
  530. '<fonts count="5" x14ac:knownFonts="1">'+
  531. '<font>'+
  532. '<sz val="11" />'+
  533. '<name val="Calibri" />'+
  534. '</font>'+
  535. '<font>'+
  536. '<sz val="11" />'+
  537. '<name val="Calibri" />'+
  538. '<color rgb="FFFFFFFF" />'+
  539. '</font>'+
  540. '<font>'+
  541. '<sz val="11" />'+
  542. '<name val="Calibri" />'+
  543. '<b />'+
  544. '</font>'+
  545. '<font>'+
  546. '<sz val="11" />'+
  547. '<name val="Calibri" />'+
  548. '<i />'+
  549. '</font>'+
  550. '<font>'+
  551. '<sz val="11" />'+
  552. '<name val="Calibri" />'+
  553. '<u />'+
  554. '</font>'+
  555. '</fonts>'+
  556. '<fills count="6">'+
  557. '<fill>'+
  558. '<patternFill patternType="none" />'+
  559. '</fill>'+
  560. '<fill>'+ // Excel appears to use this as a dotted background regardless of values but
  561. '<patternFill patternType="none" />'+ // to be valid to the schema, use a patternFill
  562. '</fill>'+
  563. '<fill>'+
  564. '<patternFill patternType="solid">'+
  565. '<fgColor rgb="FFD9D9D9" />'+
  566. '<bgColor indexed="64" />'+
  567. '</patternFill>'+
  568. '</fill>'+
  569. '<fill>'+
  570. '<patternFill patternType="solid">'+
  571. '<fgColor rgb="FFD99795" />'+
  572. '<bgColor indexed="64" />'+
  573. '</patternFill>'+
  574. '</fill>'+
  575. '<fill>'+
  576. '<patternFill patternType="solid">'+
  577. '<fgColor rgb="ffc6efce" />'+
  578. '<bgColor indexed="64" />'+
  579. '</patternFill>'+
  580. '</fill>'+
  581. '<fill>'+
  582. '<patternFill patternType="solid">'+
  583. '<fgColor rgb="ffc6cfef" />'+
  584. '<bgColor indexed="64" />'+
  585. '</patternFill>'+
  586. '</fill>'+
  587. '</fills>'+
  588. '<borders count="2">'+
  589. '<border>'+
  590. '<left />'+
  591. '<right />'+
  592. '<top />'+
  593. '<bottom />'+
  594. '<diagonal />'+
  595. '</border>'+
  596. '<border diagonalUp="false" diagonalDown="false">'+
  597. '<left style="thin">'+
  598. '<color auto="1" />'+
  599. '</left>'+
  600. '<right style="thin">'+
  601. '<color auto="1" />'+
  602. '</right>'+
  603. '<top style="thin">'+
  604. '<color auto="1" />'+
  605. '</top>'+
  606. '<bottom style="thin">'+
  607. '<color auto="1" />'+
  608. '</bottom>'+
  609. '<diagonal />'+
  610. '</border>'+
  611. '</borders>'+
  612. '<cellStyleXfs count="1">'+
  613. '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" />'+
  614. '</cellStyleXfs>'+
  615. '<cellXfs count="68">'+
  616. '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  617. '<xf numFmtId="0" fontId="1" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  618. '<xf numFmtId="0" fontId="2" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  619. '<xf numFmtId="0" fontId="3" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  620. '<xf numFmtId="0" fontId="4" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  621. '<xf numFmtId="0" fontId="0" fillId="2" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  622. '<xf numFmtId="0" fontId="1" fillId="2" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  623. '<xf numFmtId="0" fontId="2" fillId="2" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  624. '<xf numFmtId="0" fontId="3" fillId="2" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  625. '<xf numFmtId="0" fontId="4" fillId="2" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  626. '<xf numFmtId="0" fontId="0" fillId="3" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  627. '<xf numFmtId="0" fontId="1" fillId="3" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  628. '<xf numFmtId="0" fontId="2" fillId="3" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  629. '<xf numFmtId="0" fontId="3" fillId="3" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  630. '<xf numFmtId="0" fontId="4" fillId="3" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  631. '<xf numFmtId="0" fontId="0" fillId="4" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  632. '<xf numFmtId="0" fontId="1" fillId="4" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  633. '<xf numFmtId="0" fontId="2" fillId="4" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  634. '<xf numFmtId="0" fontId="3" fillId="4" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  635. '<xf numFmtId="0" fontId="4" fillId="4" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  636. '<xf numFmtId="0" fontId="0" fillId="5" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  637. '<xf numFmtId="0" fontId="1" fillId="5" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  638. '<xf numFmtId="0" fontId="2" fillId="5" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  639. '<xf numFmtId="0" fontId="3" fillId="5" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  640. '<xf numFmtId="0" fontId="4" fillId="5" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
  641. '<xf numFmtId="0" fontId="0" fillId="0" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  642. '<xf numFmtId="0" fontId="1" fillId="0" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  643. '<xf numFmtId="0" fontId="2" fillId="0" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  644. '<xf numFmtId="0" fontId="3" fillId="0" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  645. '<xf numFmtId="0" fontId="4" fillId="0" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  646. '<xf numFmtId="0" fontId="0" fillId="2" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  647. '<xf numFmtId="0" fontId="1" fillId="2" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  648. '<xf numFmtId="0" fontId="2" fillId="2" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  649. '<xf numFmtId="0" fontId="3" fillId="2" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  650. '<xf numFmtId="0" fontId="4" fillId="2" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  651. '<xf numFmtId="0" fontId="0" fillId="3" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  652. '<xf numFmtId="0" fontId="1" fillId="3" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  653. '<xf numFmtId="0" fontId="2" fillId="3" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  654. '<xf numFmtId="0" fontId="3" fillId="3" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  655. '<xf numFmtId="0" fontId="4" fillId="3" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  656. '<xf numFmtId="0" fontId="0" fillId="4" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  657. '<xf numFmtId="0" fontId="1" fillId="4" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  658. '<xf numFmtId="0" fontId="2" fillId="4" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  659. '<xf numFmtId="0" fontId="3" fillId="4" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  660. '<xf numFmtId="0" fontId="4" fillId="4" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  661. '<xf numFmtId="0" fontId="0" fillId="5" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  662. '<xf numFmtId="0" fontId="1" fillId="5" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  663. '<xf numFmtId="0" fontId="2" fillId="5" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  664. '<xf numFmtId="0" fontId="3" fillId="5" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  665. '<xf numFmtId="0" fontId="4" fillId="5" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
  666. '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyAlignment="1">'+
  667. '<alignment horizontal="left"/>'+
  668. '</xf>'+
  669. '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyAlignment="1">'+
  670. '<alignment horizontal="center"/>'+
  671. '</xf>'+
  672. '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyAlignment="1">'+
  673. '<alignment horizontal="right"/>'+
  674. '</xf>'+
  675. '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyAlignment="1">'+
  676. '<alignment horizontal="fill"/>'+
  677. '</xf>'+
  678. '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyAlignment="1">'+
  679. '<alignment textRotation="90"/>'+
  680. '</xf>'+
  681. '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyAlignment="1">'+
  682. '<alignment wrapText="1"/>'+
  683. '</xf>'+
  684. '<xf numFmtId="9" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  685. '<xf numFmtId="164" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  686. '<xf numFmtId="165" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  687. '<xf numFmtId="166" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  688. '<xf numFmtId="167" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  689. '<xf numFmtId="168" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  690. '<xf numFmtId="169" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  691. '<xf numFmtId="3" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  692. '<xf numFmtId="4" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  693. '<xf numFmtId="1" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  694. '<xf numFmtId="2" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  695. '<xf numFmtId="14" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
  696. '</cellXfs>'+
  697. '<cellStyles count="1">'+
  698. '<cellStyle name="Normal" xfId="0" builtinId="0" />'+
  699. '</cellStyles>'+
  700. '<dxfs count="0" />'+
  701. '<tableStyles count="0" defaultTableStyle="TableStyleMedium9" defaultPivotStyle="PivotStyleMedium4" />'+
  702. '</styleSheet>'
  703. };
  704. // Note we could use 3 `for` loops for the styles, but when gzipped there is
  705. // virtually no difference in size, since the above can be easily compressed
  706. // Pattern matching for special number formats. Perhaps this should be exposed
  707. // via an API in future?
  708. // Ref: section 3.8.30 - built in formatters in open spreadsheet
  709. // https://www.ecma-international.org/news/TC45_current_work/Office%20Open%20XML%20Part%204%20-%20Markup%20Language%20Reference.pdf
  710. var _excelSpecials = [
  711. { match: /^\-?\d+\.\d%$/, style: 60, fmt: function (d) { return d/100; } }, // Percent with d.p.
  712. { match: /^\-?\d+\.?\d*%$/, style: 56, fmt: function (d) { return d/100; } }, // Percent
  713. { match: /^\-?\$[\d,]+.?\d*$/, style: 57 }, // Dollars
  714. { match: /^\-?£[\d,]+.?\d*$/, style: 58 }, // Pounds
  715. { match: /^\-?€[\d,]+.?\d*$/, style: 59 }, // Euros
  716. { match: /^\-?\d+$/, style: 65 }, // Numbers without thousand separators
  717. { match: /^\-?\d+\.\d{2}$/, style: 66 }, // Numbers 2 d.p. without thousands separators
  718. { match: /^\([\d,]+\)$/, style: 61, fmt: function (d) { return -1 * d.replace(/[\(\)]/g, ''); } }, // Negative numbers indicated by brackets
  719. { match: /^\([\d,]+\.\d{2}\)$/, style: 62, fmt: function (d) { return -1 * d.replace(/[\(\)]/g, ''); } }, // Negative numbers indicated by brackets - 2d.p.
  720. { match: /^\-?[\d,]+$/, style: 63 }, // Numbers with thousand separators
  721. { match: /^\-?[\d,]+\.\d{2}$/, style: 64 },
  722. { match: /^[\d]{4}\-[01][\d]\-[0123][\d]$/, style: 67, fmt: function (d) {return Math.round(25569 + (Date.parse(d) / (86400 * 1000)));}} //Date yyyy-mm-dd
  723. ];
  724. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  725. * Buttons
  726. */
  727. //
  728. // Copy to clipboard
  729. //
  730. DataTable.ext.buttons.copyHtml5 = {
  731. className: 'buttons-copy buttons-html5',
  732. text: function ( dt ) {
  733. return dt.i18n( 'buttons.copy', 'Copy' );
  734. },
  735. action: function ( e, dt, button, config ) {
  736. this.processing( true );
  737. var that = this;
  738. var exportData = _exportData( dt, config );
  739. var info = dt.buttons.exportInfo( config );
  740. var newline = _newLine(config);
  741. var output = exportData.str;
  742. var hiddenDiv = $('<div/>')
  743. .css( {
  744. height: 1,
  745. width: 1,
  746. overflow: 'hidden',
  747. position: 'fixed',
  748. top: 0,
  749. left: 0
  750. } );
  751. if ( info.title ) {
  752. output = info.title + newline + newline + output;
  753. }
  754. if ( info.messageTop ) {
  755. output = info.messageTop + newline + newline + output;
  756. }
  757. if ( info.messageBottom ) {
  758. output = output + newline + newline + info.messageBottom;
  759. }
  760. if ( config.customize ) {
  761. output = config.customize( output, config, dt );
  762. }
  763. var textarea = $('<textarea readonly/>')
  764. .val( output )
  765. .appendTo( hiddenDiv );
  766. // For browsers that support the copy execCommand, try to use it
  767. if ( document.queryCommandSupported('copy') ) {
  768. hiddenDiv.appendTo( dt.table().container() );
  769. textarea[0].focus();
  770. textarea[0].select();
  771. try {
  772. var successful = document.execCommand( 'copy' );
  773. hiddenDiv.remove();
  774. if (successful) {
  775. dt.buttons.info(
  776. dt.i18n( 'buttons.copyTitle', 'Copy to clipboard' ),
  777. dt.i18n( 'buttons.copySuccess', {
  778. 1: 'Copied one row to clipboard',
  779. _: 'Copied %d rows to clipboard'
  780. }, exportData.rows ),
  781. 2000
  782. );
  783. this.processing( false );
  784. return;
  785. }
  786. }
  787. catch (t) {}
  788. }
  789. // Otherwise we show the text box and instruct the user to use it
  790. var message = $('<span>'+dt.i18n( 'buttons.copyKeys',
  791. 'Press <i>ctrl</i> or <i>\u2318</i> + <i>C</i> to copy the table data<br>to your system clipboard.<br><br>'+
  792. 'To cancel, click this message or press escape.' )+'</span>'
  793. )
  794. .append( hiddenDiv );
  795. dt.buttons.info( dt.i18n( 'buttons.copyTitle', 'Copy to clipboard' ), message, 0 );
  796. // Select the text so when the user activates their system clipboard
  797. // it will copy that text
  798. textarea[0].focus();
  799. textarea[0].select();
  800. // Event to hide the message when the user is done
  801. var container = $(message).closest('.dt-button-info');
  802. var close = function () {
  803. container.off( 'click.buttons-copy' );
  804. $(document).off( '.buttons-copy' );
  805. dt.buttons.info( false );
  806. };
  807. container.on( 'click.buttons-copy', close );
  808. $(document)
  809. .on( 'keydown.buttons-copy', function (e) {
  810. if ( e.keyCode === 27 ) { // esc
  811. close();
  812. that.processing( false );
  813. }
  814. } )
  815. .on( 'copy.buttons-copy cut.buttons-copy', function () {
  816. close();
  817. that.processing( false );
  818. } );
  819. },
  820. exportOptions: {},
  821. fieldSeparator: '\t',
  822. fieldBoundary: '',
  823. header: true,
  824. footer: false,
  825. title: '*',
  826. messageTop: '*',
  827. messageBottom: '*'
  828. };
  829. //
  830. // CSV export
  831. //
  832. DataTable.ext.buttons.csvHtml5 = {
  833. bom: false,
  834. className: 'buttons-csv buttons-html5',
  835. available: function () {
  836. return window.FileReader !== undefined && window.Blob;
  837. },
  838. text: function ( dt ) {
  839. return dt.i18n( 'buttons.csv', 'CSV' );
  840. },
  841. action: function ( e, dt, button, config ) {
  842. this.processing( true );
  843. // Set the text
  844. var output = _exportData( dt, config ).str;
  845. var info = dt.buttons.exportInfo(config);
  846. var charset = config.charset;
  847. if ( config.customize ) {
  848. output = config.customize( output, config, dt );
  849. }
  850. if ( charset !== false ) {
  851. if ( ! charset ) {
  852. charset = document.characterSet || document.charset;
  853. }
  854. if ( charset ) {
  855. charset = ';charset='+charset;
  856. }
  857. }
  858. else {
  859. charset = '';
  860. }
  861. if ( config.bom ) {
  862. output = String.fromCharCode(0xFEFF) + output;
  863. }
  864. _saveAs(
  865. new Blob( [output], {type: 'text/csv'+charset} ),
  866. info.filename,
  867. true
  868. );
  869. this.processing( false );
  870. },
  871. filename: '*',
  872. extension: '.csv',
  873. exportOptions: {},
  874. fieldSeparator: ',',
  875. fieldBoundary: '"',
  876. escapeChar: '"',
  877. charset: null,
  878. header: true,
  879. footer: false
  880. };
  881. //
  882. // Excel (xlsx) export
  883. //
  884. DataTable.ext.buttons.excelHtml5 = {
  885. className: 'buttons-excel buttons-html5',
  886. available: function () {
  887. return window.FileReader !== undefined && _jsZip() !== undefined && ! _isDuffSafari() && _serialiser;
  888. },
  889. text: function ( dt ) {
  890. return dt.i18n( 'buttons.excel', 'Excel' );
  891. },
  892. action: function ( e, dt, button, config ) {
  893. this.processing( true );
  894. var that = this;
  895. var rowPos = 0;
  896. var dataStartRow, dataEndRow;
  897. var getXml = function ( type ) {
  898. var str = excelStrings[ type ];
  899. //str = str.replace( /xmlns:/g, 'xmlns_' ).replace( /mc:/g, 'mc_' );
  900. return $.parseXML( str );
  901. };
  902. var rels = getXml('xl/worksheets/sheet1.xml');
  903. var relsGet = rels.getElementsByTagName( "sheetData" )[0];
  904. var xlsx = {
  905. _rels: {
  906. ".rels": getXml('_rels/.rels')
  907. },
  908. xl: {
  909. _rels: {
  910. "workbook.xml.rels": getXml('xl/_rels/workbook.xml.rels')
  911. },
  912. "workbook.xml": getXml('xl/workbook.xml'),
  913. "styles.xml": getXml('xl/styles.xml'),
  914. "worksheets": {
  915. "sheet1.xml": rels
  916. }
  917. },
  918. "[Content_Types].xml": getXml('[Content_Types].xml')
  919. };
  920. var data = dt.buttons.exportData( config.exportOptions );
  921. var currentRow, rowNode;
  922. var addRow = function ( row ) {
  923. currentRow = rowPos+1;
  924. rowNode = _createNode( rels, "row", { attr: {r:currentRow} } );
  925. for ( var i=0, ien=row.length ; i<ien ; i++ ) {
  926. // Concat both the Cell Columns as a letter and the Row of the cell.
  927. var cellId = createCellPos(i) + '' + currentRow;
  928. var cell = null;
  929. // For null, undefined of blank cell, continue so it doesn't create the _createNode
  930. if ( row[i] === null || row[i] === undefined || row[i] === '' ) {
  931. if ( config.createEmptyCells === true ) {
  932. row[i] = '';
  933. }
  934. else {
  935. continue;
  936. }
  937. }
  938. var originalContent = row[i];
  939. row[i] = typeof row[i].trim === 'function'
  940. ? row[i].trim()
  941. : row[i];
  942. // Special number formatting options
  943. for ( var j=0, jen=_excelSpecials.length ; j<jen ; j++ ) {
  944. var special = _excelSpecials[j];
  945. // TODO Need to provide the ability for the specials to say
  946. // if they are returning a string, since at the moment it is
  947. // assumed to be a number
  948. if ( row[i].match && ! row[i].match(/^0\d+/) && row[i].match( special.match ) ) {
  949. var val = row[i].replace(/[^\d\.\-]/g, '');
  950. if ( special.fmt ) {
  951. val = special.fmt( val );
  952. }
  953. cell = _createNode( rels, 'c', {
  954. attr: {
  955. r: cellId,
  956. s: special.style
  957. },
  958. children: [
  959. _createNode( rels, 'v', { text: val } )
  960. ]
  961. } );
  962. break;
  963. }
  964. }
  965. if ( ! cell ) {
  966. if ( typeof row[i] === 'number' || (
  967. row[i].match &&
  968. row[i].match(/^-?\d+(\.\d+)?([eE]\-?\d+)?$/) && // Includes exponential format
  969. ! row[i].match(/^0\d+/) )
  970. ) {
  971. // Detect numbers - don't match numbers with leading zeros
  972. // or a negative anywhere but the start
  973. cell = _createNode( rels, 'c', {
  974. attr: {
  975. t: 'n',
  976. r: cellId
  977. },
  978. children: [
  979. _createNode( rels, 'v', { text: row[i] } )
  980. ]
  981. } );
  982. }
  983. else {
  984. // String output - replace non standard characters for text output
  985. var text = ! originalContent.replace ?
  986. originalContent :
  987. originalContent.replace(/[\x00-\x09\x0B\x0C\x0E-\x1F\x7F-\x9F]/g, '');
  988. cell = _createNode( rels, 'c', {
  989. attr: {
  990. t: 'inlineStr',
  991. r: cellId
  992. },
  993. children:{
  994. row: _createNode( rels, 'is', {
  995. children: {
  996. row: _createNode( rels, 't', {
  997. text: text,
  998. attr: {
  999. 'xml:space': 'preserve'
  1000. }
  1001. } )
  1002. }
  1003. } )
  1004. }
  1005. } );
  1006. }
  1007. }
  1008. rowNode.appendChild( cell );
  1009. }
  1010. relsGet.appendChild(rowNode);
  1011. rowPos++;
  1012. };
  1013. if ( config.customizeData ) {
  1014. config.customizeData( data );
  1015. }
  1016. var mergeCells = function ( row, colspan ) {
  1017. var mergeCells = $('mergeCells', rels);
  1018. mergeCells[0].appendChild( _createNode( rels, 'mergeCell', {
  1019. attr: {
  1020. ref: 'A'+row+':'+createCellPos(colspan)+row
  1021. }
  1022. } ) );
  1023. mergeCells.attr( 'count', parseFloat(mergeCells.attr( 'count' ))+1 );
  1024. $('row:eq('+(row-1)+') c', rels).attr( 's', '51' ); // centre
  1025. };
  1026. // Title and top messages
  1027. var exportInfo = dt.buttons.exportInfo( config );
  1028. if ( exportInfo.title ) {
  1029. addRow( [exportInfo.title], rowPos );
  1030. mergeCells( rowPos, data.header.length-1 );
  1031. }
  1032. if ( exportInfo.messageTop ) {
  1033. addRow( [exportInfo.messageTop], rowPos );
  1034. mergeCells( rowPos, data.header.length-1 );
  1035. }
  1036. // Table itself
  1037. if ( config.header ) {
  1038. addRow( data.header, rowPos );
  1039. $('row:last c', rels).attr( 's', '2' ); // bold
  1040. }
  1041. dataStartRow = rowPos;
  1042. for ( var n=0, ie=data.body.length ; n<ie ; n++ ) {
  1043. addRow( data.body[n], rowPos );
  1044. }
  1045. dataEndRow = rowPos;
  1046. if ( config.footer && data.footer ) {
  1047. addRow( data.footer, rowPos);
  1048. $('row:last c', rels).attr( 's', '2' ); // bold
  1049. }
  1050. // Below the table
  1051. if ( exportInfo.messageBottom ) {
  1052. addRow( [exportInfo.messageBottom], rowPos );
  1053. mergeCells( rowPos, data.header.length-1 );
  1054. }
  1055. // Set column widths
  1056. var cols = _createNode( rels, 'cols' );
  1057. $('worksheet', rels).prepend( cols );
  1058. for ( var i=0, ien=data.header.length ; i<ien ; i++ ) {
  1059. cols.appendChild( _createNode( rels, 'col', {
  1060. attr: {
  1061. min: i+1,
  1062. max: i+1,
  1063. width: _excelColWidth( data, i ),
  1064. customWidth: 1
  1065. }
  1066. } ) );
  1067. }
  1068. // Workbook modifications
  1069. var workbook = xlsx.xl['workbook.xml'];
  1070. $( 'sheets sheet', workbook ).attr( 'name', _sheetname( config ) );
  1071. // Auto filter for columns
  1072. if ( config.autoFilter ) {
  1073. $('mergeCells', rels).before( _createNode( rels, 'autoFilter', {
  1074. attr: {
  1075. ref: 'A'+dataStartRow+':'+createCellPos(data.header.length-1)+dataEndRow
  1076. }
  1077. } ) );
  1078. $('definedNames', workbook).append( _createNode( workbook, 'definedName', {
  1079. attr: {
  1080. name: '_xlnm._FilterDatabase',
  1081. localSheetId: '0',
  1082. hidden: 1
  1083. },
  1084. text: _sheetname(config)+'!$A$'+dataStartRow+':'+createCellPos(data.header.length-1)+dataEndRow
  1085. } ) );
  1086. }
  1087. // Let the developer customise the document if they want to
  1088. if ( config.customize ) {
  1089. config.customize( xlsx, config, dt );
  1090. }
  1091. // Excel doesn't like an empty mergeCells tag
  1092. if ( $('mergeCells', rels).children().length === 0 ) {
  1093. $('mergeCells', rels).remove();
  1094. }
  1095. var jszip = _jsZip();
  1096. var zip = new jszip();
  1097. var zipConfig = {
  1098. compression: "DEFLATE",
  1099. type: 'blob',
  1100. mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
  1101. };
  1102. _addToZip( zip, xlsx );
  1103. // Modern Excel has a 218 character limit on the file name + path of the file (why!?)
  1104. // https://support.microsoft.com/en-us/office/excel-specifications-and-limits-1672b34d-7043-467e-8e27-269d656771c3
  1105. // So we truncate to allow for this.
  1106. var filename = exportInfo.filename;
  1107. if (filename > 175) {
  1108. filename = filename.substr(0, 175);
  1109. }
  1110. if ( zip.generateAsync ) {
  1111. // JSZip 3+
  1112. zip
  1113. .generateAsync( zipConfig )
  1114. .then( function ( blob ) {
  1115. _saveAs( blob, filename );
  1116. that.processing( false );
  1117. } );
  1118. }
  1119. else {
  1120. // JSZip 2.5
  1121. _saveAs(
  1122. zip.generate( zipConfig ),
  1123. filename
  1124. );
  1125. this.processing( false );
  1126. }
  1127. },
  1128. filename: '*',
  1129. extension: '.xlsx',
  1130. exportOptions: {},
  1131. header: true,
  1132. footer: false,
  1133. title: '*',
  1134. messageTop: '*',
  1135. messageBottom: '*',
  1136. createEmptyCells: false,
  1137. autoFilter: false,
  1138. sheetName: ''
  1139. };
  1140. //
  1141. // PDF export - using pdfMake - http://pdfmake.org
  1142. //
  1143. DataTable.ext.buttons.pdfHtml5 = {
  1144. className: 'buttons-pdf buttons-html5',
  1145. available: function () {
  1146. return window.FileReader !== undefined && _pdfMake();
  1147. },
  1148. text: function ( dt ) {
  1149. return dt.i18n( 'buttons.pdf', 'PDF' );
  1150. },
  1151. action: function ( e, dt, button, config ) {
  1152. this.processing( true );
  1153. var that = this;
  1154. var data = dt.buttons.exportData( config.exportOptions );
  1155. var info = dt.buttons.exportInfo( config );
  1156. var rows = [];
  1157. if ( config.header ) {
  1158. rows.push( $.map( data.header, function ( d ) {
  1159. return {
  1160. text: typeof d === 'string' ? d : d+'',
  1161. style: 'tableHeader'
  1162. };
  1163. } ) );
  1164. }
  1165. for ( var i=0, ien=data.body.length ; i<ien ; i++ ) {
  1166. rows.push( $.map( data.body[i], function ( d ) {
  1167. if ( d === null || d === undefined ) {
  1168. d = '';
  1169. }
  1170. return {
  1171. text: typeof d === 'string' ? d : d+'',
  1172. style: i % 2 ? 'tableBodyEven' : 'tableBodyOdd'
  1173. };
  1174. } ) );
  1175. }
  1176. if ( config.footer && data.footer) {
  1177. rows.push( $.map( data.footer, function ( d ) {
  1178. return {
  1179. text: typeof d === 'string' ? d : d+'',
  1180. style: 'tableFooter'
  1181. };
  1182. } ) );
  1183. }
  1184. var doc = {
  1185. pageSize: config.pageSize,
  1186. pageOrientation: config.orientation,
  1187. content: [
  1188. {
  1189. table: {
  1190. headerRows: 1,
  1191. body: rows
  1192. },
  1193. layout: 'noBorders'
  1194. }
  1195. ],
  1196. styles: {
  1197. tableHeader: {
  1198. bold: true,
  1199. fontSize: 11,
  1200. color: 'white',
  1201. fillColor: '#2d4154',
  1202. alignment: 'center'
  1203. },
  1204. tableBodyEven: {},
  1205. tableBodyOdd: {
  1206. fillColor: '#f3f3f3'
  1207. },
  1208. tableFooter: {
  1209. bold: true,
  1210. fontSize: 11,
  1211. color: 'white',
  1212. fillColor: '#2d4154'
  1213. },
  1214. title: {
  1215. alignment: 'center',
  1216. fontSize: 15
  1217. },
  1218. message: {}
  1219. },
  1220. defaultStyle: {
  1221. fontSize: 10
  1222. }
  1223. };
  1224. if ( info.messageTop ) {
  1225. doc.content.unshift( {
  1226. text: info.messageTop,
  1227. style: 'message',
  1228. margin: [ 0, 0, 0, 12 ]
  1229. } );
  1230. }
  1231. if ( info.messageBottom ) {
  1232. doc.content.push( {
  1233. text: info.messageBottom,
  1234. style: 'message',
  1235. margin: [ 0, 0, 0, 12 ]
  1236. } );
  1237. }
  1238. if ( info.title ) {
  1239. doc.content.unshift( {
  1240. text: info.title,
  1241. style: 'title',
  1242. margin: [ 0, 0, 0, 12 ]
  1243. } );
  1244. }
  1245. if ( config.customize ) {
  1246. config.customize( doc, config, dt );
  1247. }
  1248. var pdf = _pdfMake().createPdf( doc );
  1249. if ( config.download === 'open' && ! _isDuffSafari() ) {
  1250. pdf.open();
  1251. }
  1252. else {
  1253. pdf.download( info.filename );
  1254. }
  1255. this.processing( false );
  1256. },
  1257. title: '*',
  1258. filename: '*',
  1259. extension: '.pdf',
  1260. exportOptions: {},
  1261. orientation: 'portrait',
  1262. pageSize: 'A4',
  1263. header: true,
  1264. footer: false,
  1265. messageTop: '*',
  1266. messageBottom: '*',
  1267. customize: null,
  1268. download: 'download'
  1269. };
  1270. return DataTable;
  1271. }));