source: public/vendors/dataTable/Buttons-1.6.1/js/buttons.flash.js@ 13ff930

develop
Last change on this file since 13ff930 was c6b84df, checked in by beratkjufliju <kufliju@…>, 3 years ago

added fileTypes controller, notifications, excel export, edited views

  • Property mode set to 100644
File size: 45.1 KB
Line 
1/*!
2 * Flash export buttons for Buttons and DataTables.
3 * 2015-2017 SpryMedia Ltd - datatables.net/license
4 *
5 * ZeroClipbaord - MIT license
6 * Copyright (c) 2012 Joseph Huckaby
7 */
8
9(function( factory ){
10 if ( typeof define === 'function' && define.amd ) {
11 // AMD
12 define( ['jquery', 'datatables.net', 'datatables.net-buttons'], function ( $ ) {
13 return factory( $, window, document );
14 } );
15 }
16 else if ( typeof exports === 'object' ) {
17 // CommonJS
18 module.exports = function (root, $) {
19 if ( ! root ) {
20 root = window;
21 }
22
23 if ( ! $ || ! $.fn.dataTable ) {
24 $ = require('datatables.net')(root, $).$;
25 }
26
27 if ( ! $.fn.dataTable.Buttons ) {
28 require('datatables.net-buttons')(root, $);
29 }
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';
40var DataTable = $.fn.dataTable;
41
42
43/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
44 * ZeroClipboard dependency
45 */
46
47/*
48 * ZeroClipboard 1.0.4 with modifications
49 * Author: Joseph Huckaby
50 * License: MIT
51 *
52 * Copyright (c) 2012 Joseph Huckaby
53 */
54var ZeroClipboard_TableTools = {
55 version: "1.0.4-TableTools2",
56 clients: {}, // registered upload clients on page, indexed by id
57 moviePath: '', // URL to movie
58 nextId: 1, // ID of next movie
59
60 $: function(thingy) {
61 // simple DOM lookup utility function
62 if (typeof(thingy) == 'string') {
63 thingy = document.getElementById(thingy);
64 }
65 if (!thingy.addClass) {
66 // extend element with a few useful methods
67 thingy.hide = function() { this.style.display = 'none'; };
68 thingy.show = function() { this.style.display = ''; };
69 thingy.addClass = function(name) { this.removeClass(name); this.className += ' ' + name; };
70 thingy.removeClass = function(name) {
71 this.className = this.className.replace( new RegExp("\\s*" + name + "\\s*"), " ").replace(/^\s+/, '').replace(/\s+$/, '');
72 };
73 thingy.hasClass = function(name) {
74 return !!this.className.match( new RegExp("\\s*" + name + "\\s*") );
75 };
76 }
77 return thingy;
78 },
79
80 setMoviePath: function(path) {
81 // set path to ZeroClipboard.swf
82 this.moviePath = path;
83 },
84
85 dispatch: function(id, eventName, args) {
86 // receive event from flash movie, send to client
87 var client = this.clients[id];
88 if (client) {
89 client.receiveEvent(eventName, args);
90 }
91 },
92
93 log: function ( str ) {
94 console.log( 'Flash: '+str );
95 },
96
97 register: function(id, client) {
98 // register new client to receive events
99 this.clients[id] = client;
100 },
101
102 getDOMObjectPosition: function(obj) {
103 // get absolute coordinates for dom element
104 var info = {
105 left: 0,
106 top: 0,
107 width: obj.width ? obj.width : obj.offsetWidth,
108 height: obj.height ? obj.height : obj.offsetHeight
109 };
110
111 if ( obj.style.width !== "" ) {
112 info.width = obj.style.width.replace("px","");
113 }
114
115 if ( obj.style.height !== "" ) {
116 info.height = obj.style.height.replace("px","");
117 }
118
119 while (obj) {
120 info.left += obj.offsetLeft;
121 info.top += obj.offsetTop;
122 obj = obj.offsetParent;
123 }
124
125 return info;
126 },
127
128 Client: function(elem) {
129 // constructor for new simple upload client
130 this.handlers = {};
131
132 // unique ID
133 this.id = ZeroClipboard_TableTools.nextId++;
134 this.movieId = 'ZeroClipboard_TableToolsMovie_' + this.id;
135
136 // register client with singleton to receive flash events
137 ZeroClipboard_TableTools.register(this.id, this);
138
139 // create movie
140 if (elem) {
141 this.glue(elem);
142 }
143 }
144};
145
146ZeroClipboard_TableTools.Client.prototype = {
147
148 id: 0, // unique ID for us
149 ready: false, // whether movie is ready to receive events or not
150 movie: null, // reference to movie object
151 clipText: '', // text to copy to clipboard
152 fileName: '', // default file save name
153 action: 'copy', // action to perform
154 handCursorEnabled: true, // whether to show hand cursor, or default pointer cursor
155 cssEffects: true, // enable CSS mouse effects on dom container
156 handlers: null, // user event handlers
157 sized: false,
158 sheetName: '', // default sheet name for excel export
159
160 glue: function(elem, title) {
161 // glue to DOM element
162 // elem can be ID or actual DOM element object
163 this.domElement = ZeroClipboard_TableTools.$(elem);
164
165 // float just above object, or zIndex 99 if dom element isn't set
166 var zIndex = 99;
167 if (this.domElement.style.zIndex) {
168 zIndex = parseInt(this.domElement.style.zIndex, 10) + 1;
169 }
170
171 // find X/Y position of domElement
172 var box = ZeroClipboard_TableTools.getDOMObjectPosition(this.domElement);
173
174 // create floating DIV above element
175 this.div = document.createElement('div');
176 var style = this.div.style;
177 style.position = 'absolute';
178 style.left = '0px';
179 style.top = '0px';
180 style.width = (box.width) + 'px';
181 style.height = box.height + 'px';
182 style.zIndex = zIndex;
183
184 if ( typeof title != "undefined" && title !== "" ) {
185 this.div.title = title;
186 }
187 if ( box.width !== 0 && box.height !== 0 ) {
188 this.sized = true;
189 }
190
191 // style.backgroundColor = '#f00'; // debug
192 if ( this.domElement ) {
193 this.domElement.appendChild(this.div);
194 this.div.innerHTML = this.getHTML( box.width, box.height ).replace(/&/g, '&amp;');
195 }
196 },
197
198 positionElement: function() {
199 var box = ZeroClipboard_TableTools.getDOMObjectPosition(this.domElement);
200 var style = this.div.style;
201
202 style.position = 'absolute';
203 //style.left = (this.domElement.offsetLeft)+'px';
204 //style.top = this.domElement.offsetTop+'px';
205 style.width = box.width + 'px';
206 style.height = box.height + 'px';
207
208 if ( box.width !== 0 && box.height !== 0 ) {
209 this.sized = true;
210 } else {
211 return;
212 }
213
214 var flash = this.div.childNodes[0];
215 flash.width = box.width;
216 flash.height = box.height;
217 },
218
219 getHTML: function(width, height) {
220 // return HTML for movie
221 var html = '';
222 var flashvars = 'id=' + this.id +
223 '&width=' + width +
224 '&height=' + height;
225
226 if (navigator.userAgent.match(/MSIE/)) {
227 // IE gets an OBJECT tag
228 var protocol = location.href.match(/^https/i) ? 'https://' : 'http://';
229 html += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="'+protocol+'download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" width="'+width+'" height="'+height+'" id="'+this.movieId+'" align="middle"><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="false" /><param name="movie" value="'+ZeroClipboard_TableTools.moviePath+'" /><param name="loop" value="false" /><param name="menu" value="false" /><param name="quality" value="best" /><param name="bgcolor" value="#ffffff" /><param name="flashvars" value="'+flashvars+'"/><param name="wmode" value="transparent"/></object>';
230 }
231 else {
232 // all other browsers get an EMBED tag
233 html += '<embed id="'+this.movieId+'" src="'+ZeroClipboard_TableTools.moviePath+'" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="'+width+'" height="'+height+'" name="'+this.movieId+'" align="middle" allowScriptAccess="always" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="'+flashvars+'" wmode="transparent" />';
234 }
235 return html;
236 },
237
238 hide: function() {
239 // temporarily hide floater offscreen
240 if (this.div) {
241 this.div.style.left = '-2000px';
242 }
243 },
244
245 show: function() {
246 // show ourselves after a call to hide()
247 this.reposition();
248 },
249
250 destroy: function() {
251 // destroy control and floater
252 var that = this;
253
254 if (this.domElement && this.div) {
255 $(this.div).remove();
256
257 this.domElement = null;
258 this.div = null;
259
260 $.each( ZeroClipboard_TableTools.clients, function ( id, client ) {
261 if ( client === that ) {
262 delete ZeroClipboard_TableTools.clients[ id ];
263 }
264 } );
265 }
266 },
267
268 reposition: function(elem) {
269 // reposition our floating div, optionally to new container
270 // warning: container CANNOT change size, only position
271 if (elem) {
272 this.domElement = ZeroClipboard_TableTools.$(elem);
273 if (!this.domElement) {
274 this.hide();
275 }
276 }
277
278 if (this.domElement && this.div) {
279 var box = ZeroClipboard_TableTools.getDOMObjectPosition(this.domElement);
280 var style = this.div.style;
281 style.left = '' + box.left + 'px';
282 style.top = '' + box.top + 'px';
283 }
284 },
285
286 clearText: function() {
287 // clear the text to be copy / saved
288 this.clipText = '';
289 if (this.ready) {
290 this.movie.clearText();
291 }
292 },
293
294 appendText: function(newText) {
295 // append text to that which is to be copied / saved
296 this.clipText += newText;
297 if (this.ready) { this.movie.appendText(newText) ;}
298 },
299
300 setText: function(newText) {
301 // set text to be copied to be copied / saved
302 this.clipText = newText;
303 if (this.ready) { this.movie.setText(newText) ;}
304 },
305
306 setFileName: function(newText) {
307 // set the file name
308 this.fileName = newText;
309 if (this.ready) {
310 this.movie.setFileName(newText);
311 }
312 },
313
314 setSheetData: function(data) {
315 // set the xlsx sheet data
316 if (this.ready) {
317 this.movie.setSheetData( JSON.stringify( data ) );
318 }
319 },
320
321 setAction: function(newText) {
322 // set action (save or copy)
323 this.action = newText;
324 if (this.ready) {
325 this.movie.setAction(newText);
326 }
327 },
328
329 addEventListener: function(eventName, func) {
330 // add user event listener for event
331 // event types: load, queueStart, fileStart, fileComplete, queueComplete, progress, error, cancel
332 eventName = eventName.toString().toLowerCase().replace(/^on/, '');
333 if (!this.handlers[eventName]) {
334 this.handlers[eventName] = [];
335 }
336 this.handlers[eventName].push(func);
337 },
338
339 setHandCursor: function(enabled) {
340 // enable hand cursor (true), or default arrow cursor (false)
341 this.handCursorEnabled = enabled;
342 if (this.ready) {
343 this.movie.setHandCursor(enabled);
344 }
345 },
346
347 setCSSEffects: function(enabled) {
348 // enable or disable CSS effects on DOM container
349 this.cssEffects = !!enabled;
350 },
351
352 receiveEvent: function(eventName, args) {
353 var self;
354
355 // receive event from flash
356 eventName = eventName.toString().toLowerCase().replace(/^on/, '');
357
358 // special behavior for certain events
359 switch (eventName) {
360 case 'load':
361 // movie claims it is ready, but in IE this isn't always the case...
362 // bug fix: Cannot extend EMBED DOM elements in Firefox, must use traditional function
363 this.movie = document.getElementById(this.movieId);
364 if (!this.movie) {
365 self = this;
366 setTimeout( function() { self.receiveEvent('load', null); }, 1 );
367 return;
368 }
369
370 // firefox on pc needs a "kick" in order to set these in certain cases
371 if (!this.ready && navigator.userAgent.match(/Firefox/) && navigator.userAgent.match(/Windows/)) {
372 self = this;
373 setTimeout( function() { self.receiveEvent('load', null); }, 100 );
374 this.ready = true;
375 return;
376 }
377
378 this.ready = true;
379 this.movie.clearText();
380 this.movie.appendText( this.clipText );
381 this.movie.setFileName( this.fileName );
382 this.movie.setAction( this.action );
383 this.movie.setHandCursor( this.handCursorEnabled );
384 break;
385
386 case 'mouseover':
387 if (this.domElement && this.cssEffects) {
388 //this.domElement.addClass('hover');
389 if (this.recoverActive) {
390 this.domElement.addClass('active');
391 }
392 }
393 break;
394
395 case 'mouseout':
396 if (this.domElement && this.cssEffects) {
397 this.recoverActive = false;
398 if (this.domElement.hasClass('active')) {
399 this.domElement.removeClass('active');
400 this.recoverActive = true;
401 }
402 //this.domElement.removeClass('hover');
403 }
404 break;
405
406 case 'mousedown':
407 if (this.domElement && this.cssEffects) {
408 this.domElement.addClass('active');
409 }
410 break;
411
412 case 'mouseup':
413 if (this.domElement && this.cssEffects) {
414 this.domElement.removeClass('active');
415 this.recoverActive = false;
416 }
417 break;
418 } // switch eventName
419
420 if (this.handlers[eventName]) {
421 for (var idx = 0, len = this.handlers[eventName].length; idx < len; idx++) {
422 var func = this.handlers[eventName][idx];
423
424 if (typeof(func) == 'function') {
425 // actual function reference
426 func(this, args);
427 }
428 else if ((typeof(func) == 'object') && (func.length == 2)) {
429 // PHP style object + method, i.e. [myObject, 'myMethod']
430 func[0][ func[1] ](this, args);
431 }
432 else if (typeof(func) == 'string') {
433 // name of function
434 window[func](this, args);
435 }
436 } // foreach event handler defined
437 } // user defined handler for event
438 }
439};
440
441ZeroClipboard_TableTools.hasFlash = function ()
442{
443 try {
444 var fo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
445 if (fo) {
446 return true;
447 }
448 }
449 catch (e) {
450 if (
451 navigator.mimeTypes &&
452 navigator.mimeTypes['application/x-shockwave-flash'] !== undefined &&
453 navigator.mimeTypes['application/x-shockwave-flash'].enabledPlugin
454 ) {
455 return true;
456 }
457 }
458
459 return false;
460};
461
462// For the Flash binding to work, ZeroClipboard_TableTools must be on the global
463// object list
464window.ZeroClipboard_TableTools = ZeroClipboard_TableTools;
465
466
467
468/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
469 * Local (private) functions
470 */
471
472/**
473 * If a Buttons instance is initlaised before it is placed into the DOM, Flash
474 * won't be able to bind to it, so we need to wait until it is available, this
475 * method abstracts that out.
476 *
477 * @param {ZeroClipboard} flash ZeroClipboard instance
478 * @param {jQuery} node Button
479 */
480var _glue = function ( flash, node )
481{
482 var id = node.attr('id');
483
484 if ( node.parents('html').length ) {
485 flash.glue( node[0], '' );
486 }
487 else {
488 setTimeout( function () {
489 _glue( flash, node );
490 }, 500 );
491 }
492};
493
494/**
495 * Get the sheet name for Excel exports.
496 *
497 * @param {object} config Button configuration
498 */
499var _sheetname = function ( config )
500{
501 var sheetName = 'Sheet1';
502
503 if ( config.sheetName ) {
504 sheetName = config.sheetName.replace(/[\[\]\*\/\\\?\:]/g, '');
505 }
506
507 return sheetName;
508};
509
510/**
511 * Set the flash text. This has to be broken up into chunks as the Javascript /
512 * Flash bridge has a size limit. There is no indication in the Flash
513 * documentation what this is, and it probably depends upon the browser.
514 * Experimentation shows that the point is around 50k when data starts to get
515 * lost, so an 8K limit used here is safe.
516 *
517 * @param {ZeroClipboard} flash ZeroClipboard instance
518 * @param {string} data Data to send to Flash
519 */
520var _setText = function ( flash, data )
521{
522 var parts = data.match(/[\s\S]{1,8192}/g) || [];
523
524 flash.clearText();
525 for ( var i=0, len=parts.length ; i<len ; i++ )
526 {
527 flash.appendText( parts[i] );
528 }
529};
530
531/**
532 * Get the newline character(s)
533 *
534 * @param {object} config Button configuration
535 * @return {string} Newline character
536 */
537var _newLine = function ( config )
538{
539 return config.newline ?
540 config.newline :
541 navigator.userAgent.match(/Windows/) ?
542 '\r\n' :
543 '\n';
544};
545
546/**
547 * Combine the data from the `buttons.exportData` method into a string that
548 * will be used in the export file.
549 *
550 * @param {DataTable.Api} dt DataTables API instance
551 * @param {object} config Button configuration
552 * @return {object} The data to export
553 */
554var _exportData = function ( dt, config )
555{
556 var newLine = _newLine( config );
557 var data = dt.buttons.exportData( config.exportOptions );
558 var boundary = config.fieldBoundary;
559 var separator = config.fieldSeparator;
560 var reBoundary = new RegExp( boundary, 'g' );
561 var escapeChar = config.escapeChar !== undefined ?
562 config.escapeChar :
563 '\\';
564 var join = function ( a ) {
565 var s = '';
566
567 // If there is a field boundary, then we might need to escape it in
568 // the source data
569 for ( var i=0, ien=a.length ; i<ien ; i++ ) {
570 if ( i > 0 ) {
571 s += separator;
572 }
573
574 s += boundary ?
575 boundary + ('' + a[i]).replace( reBoundary, escapeChar+boundary ) + boundary :
576 a[i];
577 }
578
579 return s;
580 };
581
582 var header = config.header ? join( data.header )+newLine : '';
583 var footer = config.footer && data.footer ? newLine+join( data.footer ) : '';
584 var body = [];
585
586 for ( var i=0, ien=data.body.length ; i<ien ; i++ ) {
587 body.push( join( data.body[i] ) );
588 }
589
590 return {
591 str: header + body.join( newLine ) + footer,
592 rows: body.length
593 };
594};
595
596
597// Basic initialisation for the buttons is common between them
598var flashButton = {
599 available: function () {
600 return ZeroClipboard_TableTools.hasFlash();
601 },
602
603 init: function ( dt, button, config ) {
604 // Insert the Flash movie
605 ZeroClipboard_TableTools.moviePath = DataTable.Buttons.swfPath;
606 var flash = new ZeroClipboard_TableTools.Client();
607
608 flash.setHandCursor( true );
609 flash.addEventListener('mouseDown', function(client) {
610 config._fromFlash = true;
611 dt.button( button[0] ).trigger();
612 config._fromFlash = false;
613 } );
614
615 _glue( flash, button );
616
617 config._flash = flash;
618 },
619
620 destroy: function ( dt, button, config ) {
621 config._flash.destroy();
622 },
623
624 fieldSeparator: ',',
625
626 fieldBoundary: '"',
627
628 exportOptions: {},
629
630 title: '*',
631
632 messageTop: '*',
633
634 messageBottom: '*',
635
636 filename: '*',
637
638 extension: '.csv',
639
640 header: true,
641
642 footer: false
643};
644
645
646/**
647 * Convert from numeric position to letter for column names in Excel
648 * @param {int} n Column number
649 * @return {string} Column letter(s) name
650 */
651function createCellPos( n ){
652 var ordA = 'A'.charCodeAt(0);
653 var ordZ = 'Z'.charCodeAt(0);
654 var len = ordZ - ordA + 1;
655 var s = "";
656
657 while( n >= 0 ) {
658 s = String.fromCharCode(n % len + ordA) + s;
659 n = Math.floor(n / len) - 1;
660 }
661
662 return s;
663}
664
665/**
666 * Create an XML node and add any children, attributes, etc without needing to
667 * be verbose in the DOM.
668 *
669 * @param {object} doc XML document
670 * @param {string} nodeName Node name
671 * @param {object} opts Options - can be `attr` (attributes), `children`
672 * (child nodes) and `text` (text content)
673 * @return {node} Created node
674 */
675function _createNode( doc, nodeName, opts ){
676 var tempNode = doc.createElement( nodeName );
677
678 if ( opts ) {
679 if ( opts.attr ) {
680 $(tempNode).attr( opts.attr );
681 }
682
683 if ( opts.children ) {
684 $.each( opts.children, function ( key, value ) {
685 tempNode.appendChild( value );
686 } );
687 }
688
689 if ( opts.text !== null && opts.text !== undefined ) {
690 tempNode.appendChild( doc.createTextNode( opts.text ) );
691 }
692 }
693
694 return tempNode;
695}
696
697/**
698 * Get the width for an Excel column based on the contents of that column
699 * @param {object} data Data for export
700 * @param {int} col Column index
701 * @return {int} Column width
702 */
703function _excelColWidth( data, col ) {
704 var max = data.header[col].length;
705 var len, lineSplit, str;
706
707 if ( data.footer && data.footer[col].length > max ) {
708 max = data.footer[col].length;
709 }
710
711 for ( var i=0, ien=data.body.length ; i<ien ; i++ ) {
712 var point = data.body[i][col];
713 str = point !== null && point !== undefined ?
714 point.toString() :
715 '';
716
717 // If there is a newline character, workout the width of the column
718 // based on the longest line in the string
719 if ( str.indexOf('\n') !== -1 ) {
720 lineSplit = str.split('\n');
721 lineSplit.sort( function (a, b) {
722 return b.length - a.length;
723 } );
724
725 len = lineSplit[0].length;
726 }
727 else {
728 len = str.length;
729 }
730
731 if ( len > max ) {
732 max = len;
733 }
734
735 // Max width rather than having potentially massive column widths
736 if ( max > 40 ) {
737 return 52; // 40 * 1.3
738 }
739 }
740
741 max *= 1.3;
742
743 // And a min width
744 return max > 6 ? max : 6;
745}
746
747 var _serialiser = "";
748 if (typeof window.XMLSerializer === 'undefined') {
749 _serialiser = new function () {
750 this.serializeToString = function (input) {
751 return input.xml
752 }
753 };
754 } else {
755 _serialiser = new XMLSerializer();
756 }
757
758 var _ieExcel;
759
760
761/**
762 * Convert XML folders in an object to strings
763 * @param {object} obj XLSX document object
764 */
765function _xlsxToStrings( obj ) {
766 if ( _ieExcel === undefined ) {
767 // Detect if we are dealing with IE's _awful_ serialiser by seeing if it
768 // drop attributes
769 _ieExcel = _serialiser
770 .serializeToString(
771 $.parseXML( excelStrings['xl/worksheets/sheet1.xml'] )
772 )
773 .indexOf( 'xmlns:r' ) === -1;
774 }
775
776 $.each( obj, function ( name, val ) {
777 if ( $.isPlainObject( val ) ) {
778 _xlsxToStrings( val );
779 }
780 else {
781 if ( _ieExcel ) {
782 // IE's XML serialiser will drop some name space attributes from
783 // from the root node, so we need to save them. Do this by
784 // replacing the namespace nodes with a regular attribute that
785 // we convert back when serialised. Edge does not have this
786 // issue
787 var worksheet = val.childNodes[0];
788 var i, ien;
789 var attrs = [];
790
791 for ( i=worksheet.attributes.length-1 ; i>=0 ; i-- ) {
792 var attrName = worksheet.attributes[i].nodeName;
793 var attrValue = worksheet.attributes[i].nodeValue;
794
795 if ( attrName.indexOf( ':' ) !== -1 ) {
796 attrs.push( { name: attrName, value: attrValue } );
797
798 worksheet.removeAttribute( attrName );
799 }
800 }
801
802 for ( i=0, ien=attrs.length ; i<ien ; i++ ) {
803 var attr = val.createAttribute( attrs[i].name.replace( ':', '_dt_b_namespace_token_' ) );
804 attr.value = attrs[i].value;
805 worksheet.setAttributeNode( attr );
806 }
807 }
808
809 var str = _serialiser.serializeToString(val);
810
811 // Fix IE's XML
812 if ( _ieExcel ) {
813 // IE doesn't include the XML declaration
814 if ( str.indexOf( '<?xml' ) === -1 ) {
815 str = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+str;
816 }
817
818 // Return namespace attributes to being as such
819 str = str.replace( /_dt_b_namespace_token_/g, ':' );
820 }
821
822 // Safari, IE and Edge will put empty name space attributes onto
823 // various elements making them useless. This strips them out
824 str = str.replace( /<([^<>]*?) xmlns=""([^<>]*?)>/g, '<$1 $2>' );
825
826 obj[ name ] = str;
827 }
828 } );
829}
830
831// Excel - Pre-defined strings to build a basic XLSX file
832var excelStrings = {
833 "_rels/.rels":
834 '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+
835 '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">'+
836 '<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>'+
837 '</Relationships>',
838
839 "xl/_rels/workbook.xml.rels":
840 '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+
841 '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">'+
842 '<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/>'+
843 '<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>'+
844 '</Relationships>',
845
846 "[Content_Types].xml":
847 '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+
848 '<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">'+
849 '<Default Extension="xml" ContentType="application/xml" />'+
850 '<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml" />'+
851 '<Default Extension="jpeg" ContentType="image/jpeg" />'+
852 '<Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml" />'+
853 '<Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml" />'+
854 '<Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml" />'+
855 '</Types>',
856
857 "xl/workbook.xml":
858 '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+
859 '<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">'+
860 '<fileVersion appName="xl" lastEdited="5" lowestEdited="5" rupBuild="24816"/>'+
861 '<workbookPr showInkAnnotation="0" autoCompressPictures="0"/>'+
862 '<bookViews>'+
863 '<workbookView xWindow="0" yWindow="0" windowWidth="25600" windowHeight="19020" tabRatio="500"/>'+
864 '</bookViews>'+
865 '<sheets>'+
866 '<sheet name="" sheetId="1" r:id="rId1"/>'+
867 '</sheets>'+
868 '</workbook>',
869
870 "xl/worksheets/sheet1.xml":
871 '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+
872 '<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">'+
873 '<sheetData/>'+
874 '<mergeCells count="0"/>'+
875 '</worksheet>',
876
877 "xl/styles.xml":
878 '<?xml version="1.0" encoding="UTF-8"?>'+
879 '<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">'+
880 '<numFmts count="6">'+
881 '<numFmt numFmtId="164" formatCode="#,##0.00_-\ [$$-45C]"/>'+
882 '<numFmt numFmtId="165" formatCode="&quot;£&quot;#,##0.00"/>'+
883 '<numFmt numFmtId="166" formatCode="[$€-2]\ #,##0.00"/>'+
884 '<numFmt numFmtId="167" formatCode="0.0%"/>'+
885 '<numFmt numFmtId="168" formatCode="#,##0;(#,##0)"/>'+
886 '<numFmt numFmtId="169" formatCode="#,##0.00;(#,##0.00)"/>'+
887 '</numFmts>'+
888 '<fonts count="5" x14ac:knownFonts="1">'+
889 '<font>'+
890 '<sz val="11" />'+
891 '<name val="Calibri" />'+
892 '</font>'+
893 '<font>'+
894 '<sz val="11" />'+
895 '<name val="Calibri" />'+
896 '<color rgb="FFFFFFFF" />'+
897 '</font>'+
898 '<font>'+
899 '<sz val="11" />'+
900 '<name val="Calibri" />'+
901 '<b />'+
902 '</font>'+
903 '<font>'+
904 '<sz val="11" />'+
905 '<name val="Calibri" />'+
906 '<i />'+
907 '</font>'+
908 '<font>'+
909 '<sz val="11" />'+
910 '<name val="Calibri" />'+
911 '<u />'+
912 '</font>'+
913 '</fonts>'+
914 '<fills count="6">'+
915 '<fill>'+
916 '<patternFill patternType="none" />'+
917 '</fill>'+
918 '<fill>'+ // Excel appears to use this as a dotted background regardless of values but
919 '<patternFill patternType="none" />'+ // to be valid to the schema, use a patternFill
920 '</fill>'+
921 '<fill>'+
922 '<patternFill patternType="solid">'+
923 '<fgColor rgb="FFD9D9D9" />'+
924 '<bgColor indexed="64" />'+
925 '</patternFill>'+
926 '</fill>'+
927 '<fill>'+
928 '<patternFill patternType="solid">'+
929 '<fgColor rgb="FFD99795" />'+
930 '<bgColor indexed="64" />'+
931 '</patternFill>'+
932 '</fill>'+
933 '<fill>'+
934 '<patternFill patternType="solid">'+
935 '<fgColor rgb="ffc6efce" />'+
936 '<bgColor indexed="64" />'+
937 '</patternFill>'+
938 '</fill>'+
939 '<fill>'+
940 '<patternFill patternType="solid">'+
941 '<fgColor rgb="ffc6cfef" />'+
942 '<bgColor indexed="64" />'+
943 '</patternFill>'+
944 '</fill>'+
945 '</fills>'+
946 '<borders count="2">'+
947 '<border>'+
948 '<left />'+
949 '<right />'+
950 '<top />'+
951 '<bottom />'+
952 '<diagonal />'+
953 '</border>'+
954 '<border diagonalUp="false" diagonalDown="false">'+
955 '<left style="thin">'+
956 '<color auto="1" />'+
957 '</left>'+
958 '<right style="thin">'+
959 '<color auto="1" />'+
960 '</right>'+
961 '<top style="thin">'+
962 '<color auto="1" />'+
963 '</top>'+
964 '<bottom style="thin">'+
965 '<color auto="1" />'+
966 '</bottom>'+
967 '<diagonal />'+
968 '</border>'+
969 '</borders>'+
970 '<cellStyleXfs count="1">'+
971 '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" />'+
972 '</cellStyleXfs>'+
973 '<cellXfs count="61">'+
974 '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
975 '<xf numFmtId="0" fontId="1" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
976 '<xf numFmtId="0" fontId="2" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
977 '<xf numFmtId="0" fontId="3" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
978 '<xf numFmtId="0" fontId="4" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
979 '<xf numFmtId="0" fontId="0" fillId="2" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
980 '<xf numFmtId="0" fontId="1" fillId="2" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
981 '<xf numFmtId="0" fontId="2" fillId="2" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
982 '<xf numFmtId="0" fontId="3" fillId="2" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
983 '<xf numFmtId="0" fontId="4" fillId="2" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
984 '<xf numFmtId="0" fontId="0" fillId="3" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
985 '<xf numFmtId="0" fontId="1" fillId="3" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
986 '<xf numFmtId="0" fontId="2" fillId="3" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
987 '<xf numFmtId="0" fontId="3" fillId="3" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
988 '<xf numFmtId="0" fontId="4" fillId="3" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
989 '<xf numFmtId="0" fontId="0" fillId="4" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
990 '<xf numFmtId="0" fontId="1" fillId="4" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
991 '<xf numFmtId="0" fontId="2" fillId="4" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
992 '<xf numFmtId="0" fontId="3" fillId="4" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
993 '<xf numFmtId="0" fontId="4" fillId="4" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
994 '<xf numFmtId="0" fontId="0" fillId="5" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
995 '<xf numFmtId="0" fontId="1" fillId="5" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
996 '<xf numFmtId="0" fontId="2" fillId="5" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
997 '<xf numFmtId="0" fontId="3" fillId="5" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
998 '<xf numFmtId="0" fontId="4" fillId="5" borderId="0" applyFont="1" applyFill="1" applyBorder="1"/>'+
999 '<xf numFmtId="0" fontId="0" fillId="0" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
1000 '<xf numFmtId="0" fontId="1" fillId="0" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
1001 '<xf numFmtId="0" fontId="2" fillId="0" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
1002 '<xf numFmtId="0" fontId="3" fillId="0" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
1003 '<xf numFmtId="0" fontId="4" fillId="0" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
1004 '<xf numFmtId="0" fontId="0" fillId="2" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
1005 '<xf numFmtId="0" fontId="1" fillId="2" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
1006 '<xf numFmtId="0" fontId="2" fillId="2" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
1007 '<xf numFmtId="0" fontId="3" fillId="2" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
1008 '<xf numFmtId="0" fontId="4" fillId="2" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
1009 '<xf numFmtId="0" fontId="0" fillId="3" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
1010 '<xf numFmtId="0" fontId="1" fillId="3" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
1011 '<xf numFmtId="0" fontId="2" fillId="3" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
1012 '<xf numFmtId="0" fontId="3" fillId="3" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
1013 '<xf numFmtId="0" fontId="4" fillId="3" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
1014 '<xf numFmtId="0" fontId="0" fillId="4" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
1015 '<xf numFmtId="0" fontId="1" fillId="4" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
1016 '<xf numFmtId="0" fontId="2" fillId="4" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
1017 '<xf numFmtId="0" fontId="3" fillId="4" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
1018 '<xf numFmtId="0" fontId="4" fillId="4" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
1019 '<xf numFmtId="0" fontId="0" fillId="5" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
1020 '<xf numFmtId="0" fontId="1" fillId="5" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
1021 '<xf numFmtId="0" fontId="2" fillId="5" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
1022 '<xf numFmtId="0" fontId="3" fillId="5" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
1023 '<xf numFmtId="0" fontId="4" fillId="5" borderId="1" applyFont="1" applyFill="1" applyBorder="1"/>'+
1024 '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyAlignment="1">'+
1025 '<alignment horizontal="left"/>'+
1026 '</xf>'+
1027 '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyAlignment="1">'+
1028 '<alignment horizontal="center"/>'+
1029 '</xf>'+
1030 '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyAlignment="1">'+
1031 '<alignment horizontal="right"/>'+
1032 '</xf>'+
1033 '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyAlignment="1">'+
1034 '<alignment horizontal="fill"/>'+
1035 '</xf>'+
1036 '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyAlignment="1">'+
1037 '<alignment textRotation="90"/>'+
1038 '</xf>'+
1039 '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyAlignment="1">'+
1040 '<alignment wrapText="1"/>'+
1041 '</xf>'+
1042 '<xf numFmtId="9" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
1043 '<xf numFmtId="164" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
1044 '<xf numFmtId="165" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
1045 '<xf numFmtId="166" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
1046 '<xf numFmtId="167" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
1047 '<xf numFmtId="168" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
1048 '<xf numFmtId="169" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
1049 '<xf numFmtId="3" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
1050 '<xf numFmtId="4" fontId="0" fillId="0" borderId="0" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyNumberFormat="1"/>'+
1051 '</cellXfs>'+
1052 '<cellStyles count="1">'+
1053 '<cellStyle name="Normal" xfId="0" builtinId="0" />'+
1054 '</cellStyles>'+
1055 '<dxfs count="0" />'+
1056 '<tableStyles count="0" defaultTableStyle="TableStyleMedium9" defaultPivotStyle="PivotStyleMedium4" />'+
1057 '</styleSheet>'
1058};
1059// Note we could use 3 `for` loops for the styles, but when gzipped there is
1060// virtually no difference in size, since the above can be easily compressed
1061
1062// Pattern matching for special number formats. Perhaps this should be exposed
1063// via an API in future?
1064var _excelSpecials = [
1065 { match: /^\-?\d+\.\d%$/, style: 60, fmt: function (d) { return d/100; } }, // Precent with d.p.
1066 { match: /^\-?\d+\.?\d*%$/, style: 56, fmt: function (d) { return d/100; } }, // Percent
1067 { match: /^\-?\$[\d,]+.?\d*$/, style: 57 }, // Dollars
1068 { match: /^\-?£[\d,]+.?\d*$/, style: 58 }, // Pounds
1069 { match: /^\-?€[\d,]+.?\d*$/, style: 59 }, // Euros
1070 { match: /^\([\d,]+\)$/, style: 61, fmt: function (d) { return -1 * d.replace(/[\(\)]/g, ''); } }, // Negative numbers indicated by brackets
1071 { match: /^\([\d,]+\.\d{2}\)$/, style: 62, fmt: function (d) { return -1 * d.replace(/[\(\)]/g, ''); } }, // Negative numbers indicated by brackets - 2d.p.
1072 { match: /^[\d,]+$/, style: 63 }, // Numbers with thousand separators
1073 { match: /^[\d,]+\.\d{2}$/, style: 64 } // Numbers with 2d.p. and thousands separators
1074];
1075
1076
1077
1078/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1079 * DataTables options and methods
1080 */
1081
1082// Set the default SWF path
1083DataTable.Buttons.swfPath = '//cdn.datatables.net/buttons/'+DataTable.Buttons.version+'/swf/flashExport.swf';
1084
1085// Method to allow Flash buttons to be resized when made visible - as they are
1086// of zero height and width if initialised hidden
1087DataTable.Api.register( 'buttons.resize()', function () {
1088 $.each( ZeroClipboard_TableTools.clients, function ( i, client ) {
1089 if ( client.domElement !== undefined && client.domElement.parentNode ) {
1090 client.positionElement();
1091 }
1092 } );
1093} );
1094
1095
1096/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1097 * Button definitions
1098 */
1099
1100// Copy to clipboard
1101DataTable.ext.buttons.copyFlash = $.extend( {}, flashButton, {
1102 className: 'buttons-copy buttons-flash',
1103
1104 text: function ( dt ) {
1105 return dt.i18n( 'buttons.copy', 'Copy' );
1106 },
1107
1108 action: function ( e, dt, button, config ) {
1109 // Check that the trigger did actually occur due to a Flash activation
1110 if ( ! config._fromFlash ) {
1111 return;
1112 }
1113
1114 this.processing( true );
1115
1116 var flash = config._flash;
1117 var exportData = _exportData( dt, config );
1118 var info = dt.buttons.exportInfo( config );
1119 var newline = _newLine(config);
1120 var output = exportData.str;
1121
1122 if ( info.title ) {
1123 output = info.title + newline + newline + output;
1124 }
1125
1126 if ( info.messageTop ) {
1127 output = info.messageTop + newline + newline + output;
1128 }
1129
1130 if ( info.messageBottom ) {
1131 output = output + newline + newline + info.messageBottom;
1132 }
1133
1134 if ( config.customize ) {
1135 output = config.customize( output, config, dt );
1136 }
1137
1138 flash.setAction( 'copy' );
1139 _setText( flash, output );
1140
1141 this.processing( false );
1142
1143 dt.buttons.info(
1144 dt.i18n( 'buttons.copyTitle', 'Copy to clipboard' ),
1145 dt.i18n( 'buttons.copySuccess', {
1146 _: 'Copied %d rows to clipboard',
1147 1: 'Copied 1 row to clipboard'
1148 }, data.rows ),
1149 3000
1150 );
1151 },
1152
1153 fieldSeparator: '\t',
1154
1155 fieldBoundary: ''
1156} );
1157
1158// CSV save file
1159DataTable.ext.buttons.csvFlash = $.extend( {}, flashButton, {
1160 className: 'buttons-csv buttons-flash',
1161
1162 text: function ( dt ) {
1163 return dt.i18n( 'buttons.csv', 'CSV' );
1164 },
1165
1166 action: function ( e, dt, button, config ) {
1167 // Set the text
1168 var flash = config._flash;
1169 var data = _exportData( dt, config );
1170 var info = dt.buttons.exportInfo( config );
1171 var output = config.customize ?
1172 config.customize( data.str, config, dt ) :
1173 data.str;
1174
1175 flash.setAction( 'csv' );
1176 flash.setFileName( info.filename );
1177 _setText( flash, output );
1178 },
1179
1180 escapeChar: '"'
1181} );
1182
1183// Excel save file - this is really a CSV file using UTF-8 that Excel can read
1184DataTable.ext.buttons.excelFlash = $.extend( {}, flashButton, {
1185 className: 'buttons-excel buttons-flash',
1186
1187 text: function ( dt ) {
1188 return dt.i18n( 'buttons.excel', 'Excel' );
1189 },
1190
1191 action: function ( e, dt, button, config ) {
1192 this.processing( true );
1193
1194 var flash = config._flash;
1195 var rowPos = 0;
1196 var rels = $.parseXML( excelStrings['xl/worksheets/sheet1.xml'] ) ; //Parses xml
1197 var relsGet = rels.getElementsByTagName( "sheetData" )[0];
1198
1199 var xlsx = {
1200 _rels: {
1201 ".rels": $.parseXML( excelStrings['_rels/.rels'] )
1202 },
1203 xl: {
1204 _rels: {
1205 "workbook.xml.rels": $.parseXML( excelStrings['xl/_rels/workbook.xml.rels'] )
1206 },
1207 "workbook.xml": $.parseXML( excelStrings['xl/workbook.xml'] ),
1208 "styles.xml": $.parseXML( excelStrings['xl/styles.xml'] ),
1209 "worksheets": {
1210 "sheet1.xml": rels
1211 }
1212
1213 },
1214 "[Content_Types].xml": $.parseXML( excelStrings['[Content_Types].xml'])
1215 };
1216
1217 var data = dt.buttons.exportData( config.exportOptions );
1218 var currentRow, rowNode;
1219 var addRow = function ( row ) {
1220 currentRow = rowPos+1;
1221 rowNode = _createNode( rels, "row", { attr: {r:currentRow} } );
1222
1223 for ( var i=0, ien=row.length ; i<ien ; i++ ) {
1224 // Concat both the Cell Columns as a letter and the Row of the cell.
1225 var cellId = createCellPos(i) + '' + currentRow;
1226 var cell = null;
1227
1228 // For null, undefined of blank cell, continue so it doesn't create the _createNode
1229 if ( row[i] === null || row[i] === undefined || row[i] === '' ) {
1230 if ( config.createEmptyCells === true ) {
1231 row[i] = '';
1232 }
1233 else {
1234 continue;
1235 }
1236 }
1237
1238 row[i] = $.trim( row[i] );
1239
1240 // Special number formatting options
1241 for ( var j=0, jen=_excelSpecials.length ; j<jen ; j++ ) {
1242 var special = _excelSpecials[j];
1243
1244 // TODO Need to provide the ability for the specials to say
1245 // if they are returning a string, since at the moment it is
1246 // assumed to be a number
1247 if ( row[i].match && ! row[i].match(/^0\d+/) && row[i].match( special.match ) ) {
1248 var val = row[i].replace(/[^\d\.\-]/g, '');
1249
1250 if ( special.fmt ) {
1251 val = special.fmt( val );
1252 }
1253
1254 cell = _createNode( rels, 'c', {
1255 attr: {
1256 r: cellId,
1257 s: special.style
1258 },
1259 children: [
1260 _createNode( rels, 'v', { text: val } )
1261 ]
1262 } );
1263
1264 break;
1265 }
1266 }
1267
1268 if ( ! cell ) {
1269 if ( typeof row[i] === 'number' || (
1270 row[i].match &&
1271 row[i].match(/^-?\d+(\.\d+)?$/) &&
1272 ! row[i].match(/^0\d+/) )
1273 ) {
1274 // Detect numbers - don't match numbers with leading zeros
1275 // or a negative anywhere but the start
1276 cell = _createNode( rels, 'c', {
1277 attr: {
1278 t: 'n',
1279 r: cellId
1280 },
1281 children: [
1282 _createNode( rels, 'v', { text: row[i] } )
1283 ]
1284 } );
1285 }
1286 else {
1287 // String output - replace non standard characters for text output
1288 var text = ! row[i].replace ?
1289 row[i] :
1290 row[i].replace(/[\x00-\x09\x0B\x0C\x0E-\x1F\x7F-\x9F]/g, '');
1291
1292 cell = _createNode( rels, 'c', {
1293 attr: {
1294 t: 'inlineStr',
1295 r: cellId
1296 },
1297 children:{
1298 row: _createNode( rels, 'is', {
1299 children: {
1300 row: _createNode( rels, 't', {
1301 text: text
1302 } )
1303 }
1304 } )
1305 }
1306 } );
1307 }
1308 }
1309
1310 rowNode.appendChild( cell );
1311 }
1312
1313 relsGet.appendChild(rowNode);
1314 rowPos++;
1315 };
1316
1317 $( 'sheets sheet', xlsx.xl['workbook.xml'] ).attr( 'name', _sheetname( config ) );
1318
1319 if ( config.customizeData ) {
1320 config.customizeData( data );
1321 }
1322
1323 var mergeCells = function ( row, colspan ) {
1324 var mergeCells = $('mergeCells', rels);
1325
1326 mergeCells[0].appendChild( _createNode( rels, 'mergeCell', {
1327 attr: {
1328 ref: 'A'+row+':'+createCellPos(colspan)+row
1329 }
1330 } ) );
1331 mergeCells.attr( 'count', mergeCells.attr( 'count' )+1 );
1332 $('row:eq('+(row-1)+') c', rels).attr( 's', '51' ); // centre
1333 };
1334
1335 // Title and top messages
1336 var exportInfo = dt.buttons.exportInfo( config );
1337 if ( exportInfo.title ) {
1338 addRow( [exportInfo.title], rowPos );
1339 mergeCells( rowPos, data.header.length-1 );
1340 }
1341
1342 if ( exportInfo.messageTop ) {
1343 addRow( [exportInfo.messageTop], rowPos );
1344 mergeCells( rowPos, data.header.length-1 );
1345 }
1346
1347 // Table itself
1348 if ( config.header ) {
1349 addRow( data.header, rowPos );
1350 $('row:last c', rels).attr( 's', '2' ); // bold
1351 }
1352
1353 for ( var n=0, ie=data.body.length ; n<ie ; n++ ) {
1354 addRow( data.body[n], rowPos );
1355 }
1356
1357 if ( config.footer && data.footer ) {
1358 addRow( data.footer, rowPos);
1359 $('row:last c', rels).attr( 's', '2' ); // bold
1360 }
1361
1362 // Below the table
1363 if ( exportInfo.messageBottom ) {
1364 addRow( [exportInfo.messageBottom], rowPos );
1365 mergeCells( rowPos, data.header.length-1 );
1366 }
1367
1368 // Set column widths
1369 var cols = _createNode( rels, 'cols' );
1370 $('worksheet', rels).prepend( cols );
1371
1372 for ( var i=0, ien=data.header.length ; i<ien ; i++ ) {
1373 cols.appendChild( _createNode( rels, 'col', {
1374 attr: {
1375 min: i+1,
1376 max: i+1,
1377 width: _excelColWidth( data, i ),
1378 customWidth: 1
1379 }
1380 } ) );
1381 }
1382
1383 // Let the developer customise the document if they want to
1384 if ( config.customize ) {
1385 config.customize( xlsx, config, dt );
1386 }
1387
1388 _xlsxToStrings( xlsx );
1389
1390 flash.setAction( 'excel' );
1391 flash.setFileName( exportInfo.filename );
1392 flash.setSheetData( xlsx );
1393 _setText( flash, '' );
1394
1395 this.processing( false );
1396 },
1397
1398 extension: '.xlsx',
1399
1400 createEmptyCells: false
1401} );
1402
1403
1404
1405// PDF export
1406DataTable.ext.buttons.pdfFlash = $.extend( {}, flashButton, {
1407 className: 'buttons-pdf buttons-flash',
1408
1409 text: function ( dt ) {
1410 return dt.i18n( 'buttons.pdf', 'PDF' );
1411 },
1412
1413 action: function ( e, dt, button, config ) {
1414 this.processing( true );
1415
1416 // Set the text
1417 var flash = config._flash;
1418 var data = dt.buttons.exportData( config.exportOptions );
1419 var info = dt.buttons.exportInfo( config );
1420 var totalWidth = dt.table().node().offsetWidth;
1421
1422 // Calculate the column width ratios for layout of the table in the PDF
1423 var ratios = dt.columns( config.columns ).indexes().map( function ( idx ) {
1424 return dt.column( idx ).header().offsetWidth / totalWidth;
1425 } );
1426
1427 flash.setAction( 'pdf' );
1428 flash.setFileName( info.filename );
1429
1430 _setText( flash, JSON.stringify( {
1431 title: info.title || '',
1432 messageTop: info.messageTop || '',
1433 messageBottom: info.messageBottom || '',
1434 colWidth: ratios.toArray(),
1435 orientation: config.orientation,
1436 size: config.pageSize,
1437 header: config.header ? data.header : null,
1438 footer: config.footer ? data.footer : null,
1439 body: data.body
1440 } ) );
1441
1442 this.processing( false );
1443 },
1444
1445 extension: '.pdf',
1446
1447 orientation: 'portrait',
1448
1449 pageSize: 'A4',
1450
1451 newline: '\n'
1452} );
1453
1454
1455return DataTable.Buttons;
1456}));
Note: See TracBrowser for help on using the repository browser.