提交 | 用户 | 时间
|
58d006
|
1 |
/*! |
A |
2 |
* Flash export buttons for Buttons and DataTables. |
|
3 |
* 2015 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'; |
|
40 |
var 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 |
*/ |
|
54 |
var 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 |
register: function(id, client) { |
|
94 |
// register new client to receive events |
|
95 |
this.clients[id] = client; |
|
96 |
}, |
|
97 |
|
|
98 |
getDOMObjectPosition: function(obj) { |
|
99 |
// get absolute coordinates for dom element |
|
100 |
var info = { |
|
101 |
left: 0, |
|
102 |
top: 0, |
|
103 |
width: obj.width ? obj.width : obj.offsetWidth, |
|
104 |
height: obj.height ? obj.height : obj.offsetHeight |
|
105 |
}; |
|
106 |
|
|
107 |
if ( obj.style.width !== "" ) { |
|
108 |
info.width = obj.style.width.replace("px",""); |
|
109 |
} |
|
110 |
|
|
111 |
if ( obj.style.height !== "" ) { |
|
112 |
info.height = obj.style.height.replace("px",""); |
|
113 |
} |
|
114 |
|
|
115 |
while (obj) { |
|
116 |
info.left += obj.offsetLeft; |
|
117 |
info.top += obj.offsetTop; |
|
118 |
obj = obj.offsetParent; |
|
119 |
} |
|
120 |
|
|
121 |
return info; |
|
122 |
}, |
|
123 |
|
|
124 |
Client: function(elem) { |
|
125 |
// constructor for new simple upload client |
|
126 |
this.handlers = {}; |
|
127 |
|
|
128 |
// unique ID |
|
129 |
this.id = ZeroClipboard_TableTools.nextId++; |
|
130 |
this.movieId = 'ZeroClipboard_TableToolsMovie_' + this.id; |
|
131 |
|
|
132 |
// register client with singleton to receive flash events |
|
133 |
ZeroClipboard_TableTools.register(this.id, this); |
|
134 |
|
|
135 |
// create movie |
|
136 |
if (elem) { |
|
137 |
this.glue(elem); |
|
138 |
} |
|
139 |
} |
|
140 |
}; |
|
141 |
|
|
142 |
ZeroClipboard_TableTools.Client.prototype = { |
|
143 |
|
|
144 |
id: 0, // unique ID for us |
|
145 |
ready: false, // whether movie is ready to receive events or not |
|
146 |
movie: null, // reference to movie object |
|
147 |
clipText: '', // text to copy to clipboard |
|
148 |
fileName: '', // default file save name |
|
149 |
action: 'copy', // action to perform |
|
150 |
handCursorEnabled: true, // whether to show hand cursor, or default pointer cursor |
|
151 |
cssEffects: true, // enable CSS mouse effects on dom container |
|
152 |
handlers: null, // user event handlers |
|
153 |
sized: false, |
|
154 |
|
|
155 |
glue: function(elem, title) { |
|
156 |
// glue to DOM element |
|
157 |
// elem can be ID or actual DOM element object |
|
158 |
this.domElement = ZeroClipboard_TableTools.$(elem); |
|
159 |
|
|
160 |
// float just above object, or zIndex 99 if dom element isn't set |
|
161 |
var zIndex = 99; |
|
162 |
if (this.domElement.style.zIndex) { |
|
163 |
zIndex = parseInt(this.domElement.style.zIndex, 10) + 1; |
|
164 |
} |
|
165 |
|
|
166 |
// find X/Y position of domElement |
|
167 |
var box = ZeroClipboard_TableTools.getDOMObjectPosition(this.domElement); |
|
168 |
|
|
169 |
// create floating DIV above element |
|
170 |
this.div = document.createElement('div'); |
|
171 |
var style = this.div.style; |
|
172 |
style.position = 'absolute'; |
|
173 |
style.left = '0px'; |
|
174 |
style.top = '0px'; |
|
175 |
style.width = (box.width) + 'px'; |
|
176 |
style.height = box.height + 'px'; |
|
177 |
style.zIndex = zIndex; |
|
178 |
|
|
179 |
if ( typeof title != "undefined" && title !== "" ) { |
|
180 |
this.div.title = title; |
|
181 |
} |
|
182 |
if ( box.width !== 0 && box.height !== 0 ) { |
|
183 |
this.sized = true; |
|
184 |
} |
|
185 |
|
|
186 |
// style.backgroundColor = '#f00'; // debug |
|
187 |
if ( this.domElement ) { |
|
188 |
this.domElement.appendChild(this.div); |
|
189 |
this.div.innerHTML = this.getHTML( box.width, box.height ).replace(/&/g, '&'); |
|
190 |
} |
|
191 |
}, |
|
192 |
|
|
193 |
positionElement: function() { |
|
194 |
var box = ZeroClipboard_TableTools.getDOMObjectPosition(this.domElement); |
|
195 |
var style = this.div.style; |
|
196 |
|
|
197 |
style.position = 'absolute'; |
|
198 |
//style.left = (this.domElement.offsetLeft)+'px'; |
|
199 |
//style.top = this.domElement.offsetTop+'px'; |
|
200 |
style.width = box.width + 'px'; |
|
201 |
style.height = box.height + 'px'; |
|
202 |
|
|
203 |
if ( box.width !== 0 && box.height !== 0 ) { |
|
204 |
this.sized = true; |
|
205 |
} else { |
|
206 |
return; |
|
207 |
} |
|
208 |
|
|
209 |
var flash = this.div.childNodes[0]; |
|
210 |
flash.width = box.width; |
|
211 |
flash.height = box.height; |
|
212 |
}, |
|
213 |
|
|
214 |
getHTML: function(width, height) { |
|
215 |
// return HTML for movie |
|
216 |
var html = ''; |
|
217 |
var flashvars = 'id=' + this.id + |
|
218 |
'&width=' + width + |
|
219 |
'&height=' + height; |
|
220 |
|
|
221 |
if (navigator.userAgent.match(/MSIE/)) { |
|
222 |
// IE gets an OBJECT tag |
|
223 |
var protocol = location.href.match(/^https/i) ? 'https://' : 'http://'; |
|
224 |
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>'; |
|
225 |
} |
|
226 |
else { |
|
227 |
// all other browsers get an EMBED tag |
|
228 |
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" />'; |
|
229 |
} |
|
230 |
return html; |
|
231 |
}, |
|
232 |
|
|
233 |
hide: function() { |
|
234 |
// temporarily hide floater offscreen |
|
235 |
if (this.div) { |
|
236 |
this.div.style.left = '-2000px'; |
|
237 |
} |
|
238 |
}, |
|
239 |
|
|
240 |
show: function() { |
|
241 |
// show ourselves after a call to hide() |
|
242 |
this.reposition(); |
|
243 |
}, |
|
244 |
|
|
245 |
destroy: function() { |
|
246 |
// destroy control and floater |
|
247 |
var that = this; |
|
248 |
|
|
249 |
if (this.domElement && this.div) { |
|
250 |
$(this.div).remove(); |
|
251 |
|
|
252 |
this.domElement = null; |
|
253 |
this.div = null; |
|
254 |
|
|
255 |
$.each( ZeroClipboard_TableTools.clients, function ( id, client ) { |
|
256 |
if ( client === that ) { |
|
257 |
delete ZeroClipboard_TableTools.clients[ id ]; |
|
258 |
} |
|
259 |
} ); |
|
260 |
} |
|
261 |
}, |
|
262 |
|
|
263 |
reposition: function(elem) { |
|
264 |
// reposition our floating div, optionally to new container |
|
265 |
// warning: container CANNOT change size, only position |
|
266 |
if (elem) { |
|
267 |
this.domElement = ZeroClipboard_TableTools.$(elem); |
|
268 |
if (!this.domElement) { |
|
269 |
this.hide(); |
|
270 |
} |
|
271 |
} |
|
272 |
|
|
273 |
if (this.domElement && this.div) { |
|
274 |
var box = ZeroClipboard_TableTools.getDOMObjectPosition(this.domElement); |
|
275 |
var style = this.div.style; |
|
276 |
style.left = '' + box.left + 'px'; |
|
277 |
style.top = '' + box.top + 'px'; |
|
278 |
} |
|
279 |
}, |
|
280 |
|
|
281 |
clearText: function() { |
|
282 |
// clear the text to be copy / saved |
|
283 |
this.clipText = ''; |
|
284 |
if (this.ready) { |
|
285 |
this.movie.clearText(); |
|
286 |
} |
|
287 |
}, |
|
288 |
|
|
289 |
appendText: function(newText) { |
|
290 |
// append text to that which is to be copied / saved |
|
291 |
this.clipText += newText; |
|
292 |
if (this.ready) { this.movie.appendText(newText) ;} |
|
293 |
}, |
|
294 |
|
|
295 |
setText: function(newText) { |
|
296 |
// set text to be copied to be copied / saved |
|
297 |
this.clipText = newText; |
|
298 |
if (this.ready) { this.movie.setText(newText) ;} |
|
299 |
}, |
|
300 |
|
|
301 |
setFileName: function(newText) { |
|
302 |
// set the file name |
|
303 |
this.fileName = newText; |
|
304 |
if (this.ready) { |
|
305 |
this.movie.setFileName(newText); |
|
306 |
} |
|
307 |
}, |
|
308 |
|
|
309 |
setAction: function(newText) { |
|
310 |
// set action (save or copy) |
|
311 |
this.action = newText; |
|
312 |
if (this.ready) { |
|
313 |
this.movie.setAction(newText); |
|
314 |
} |
|
315 |
}, |
|
316 |
|
|
317 |
addEventListener: function(eventName, func) { |
|
318 |
// add user event listener for event |
|
319 |
// event types: load, queueStart, fileStart, fileComplete, queueComplete, progress, error, cancel |
|
320 |
eventName = eventName.toString().toLowerCase().replace(/^on/, ''); |
|
321 |
if (!this.handlers[eventName]) { |
|
322 |
this.handlers[eventName] = []; |
|
323 |
} |
|
324 |
this.handlers[eventName].push(func); |
|
325 |
}, |
|
326 |
|
|
327 |
setHandCursor: function(enabled) { |
|
328 |
// enable hand cursor (true), or default arrow cursor (false) |
|
329 |
this.handCursorEnabled = enabled; |
|
330 |
if (this.ready) { |
|
331 |
this.movie.setHandCursor(enabled); |
|
332 |
} |
|
333 |
}, |
|
334 |
|
|
335 |
setCSSEffects: function(enabled) { |
|
336 |
// enable or disable CSS effects on DOM container |
|
337 |
this.cssEffects = !!enabled; |
|
338 |
}, |
|
339 |
|
|
340 |
receiveEvent: function(eventName, args) { |
|
341 |
var self; |
|
342 |
|
|
343 |
// receive event from flash |
|
344 |
eventName = eventName.toString().toLowerCase().replace(/^on/, ''); |
|
345 |
|
|
346 |
// special behavior for certain events |
|
347 |
switch (eventName) { |
|
348 |
case 'load': |
|
349 |
// movie claims it is ready, but in IE this isn't always the case... |
|
350 |
// bug fix: Cannot extend EMBED DOM elements in Firefox, must use traditional function |
|
351 |
this.movie = document.getElementById(this.movieId); |
|
352 |
if (!this.movie) { |
|
353 |
self = this; |
|
354 |
setTimeout( function() { self.receiveEvent('load', null); }, 1 ); |
|
355 |
return; |
|
356 |
} |
|
357 |
|
|
358 |
// firefox on pc needs a "kick" in order to set these in certain cases |
|
359 |
if (!this.ready && navigator.userAgent.match(/Firefox/) && navigator.userAgent.match(/Windows/)) { |
|
360 |
self = this; |
|
361 |
setTimeout( function() { self.receiveEvent('load', null); }, 100 ); |
|
362 |
this.ready = true; |
|
363 |
return; |
|
364 |
} |
|
365 |
|
|
366 |
this.ready = true; |
|
367 |
this.movie.clearText(); |
|
368 |
this.movie.appendText( this.clipText ); |
|
369 |
this.movie.setFileName( this.fileName ); |
|
370 |
this.movie.setAction( this.action ); |
|
371 |
this.movie.setHandCursor( this.handCursorEnabled ); |
|
372 |
break; |
|
373 |
|
|
374 |
case 'mouseover': |
|
375 |
if (this.domElement && this.cssEffects) { |
|
376 |
//this.domElement.addClass('hover'); |
|
377 |
if (this.recoverActive) { |
|
378 |
this.domElement.addClass('active'); |
|
379 |
} |
|
380 |
} |
|
381 |
break; |
|
382 |
|
|
383 |
case 'mouseout': |
|
384 |
if (this.domElement && this.cssEffects) { |
|
385 |
this.recoverActive = false; |
|
386 |
if (this.domElement.hasClass('active')) { |
|
387 |
this.domElement.removeClass('active'); |
|
388 |
this.recoverActive = true; |
|
389 |
} |
|
390 |
//this.domElement.removeClass('hover'); |
|
391 |
} |
|
392 |
break; |
|
393 |
|
|
394 |
case 'mousedown': |
|
395 |
if (this.domElement && this.cssEffects) { |
|
396 |
this.domElement.addClass('active'); |
|
397 |
} |
|
398 |
break; |
|
399 |
|
|
400 |
case 'mouseup': |
|
401 |
if (this.domElement && this.cssEffects) { |
|
402 |
this.domElement.removeClass('active'); |
|
403 |
this.recoverActive = false; |
|
404 |
} |
|
405 |
break; |
|
406 |
} // switch eventName |
|
407 |
|
|
408 |
if (this.handlers[eventName]) { |
|
409 |
for (var idx = 0, len = this.handlers[eventName].length; idx < len; idx++) { |
|
410 |
var func = this.handlers[eventName][idx]; |
|
411 |
|
|
412 |
if (typeof(func) == 'function') { |
|
413 |
// actual function reference |
|
414 |
func(this, args); |
|
415 |
} |
|
416 |
else if ((typeof(func) == 'object') && (func.length == 2)) { |
|
417 |
// PHP style object + method, i.e. [myObject, 'myMethod'] |
|
418 |
func[0][ func[1] ](this, args); |
|
419 |
} |
|
420 |
else if (typeof(func) == 'string') { |
|
421 |
// name of function |
|
422 |
window[func](this, args); |
|
423 |
} |
|
424 |
} // foreach event handler defined |
|
425 |
} // user defined handler for event |
|
426 |
} |
|
427 |
}; |
|
428 |
|
|
429 |
ZeroClipboard_TableTools.hasFlash = function () |
|
430 |
{ |
|
431 |
try { |
|
432 |
var fo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash'); |
|
433 |
if (fo) { |
|
434 |
return true; |
|
435 |
} |
|
436 |
} |
|
437 |
catch (e) { |
|
438 |
if ( |
|
439 |
navigator.mimeTypes && |
|
440 |
navigator.mimeTypes['application/x-shockwave-flash'] !== undefined && |
|
441 |
navigator.mimeTypes['application/x-shockwave-flash'].enabledPlugin |
|
442 |
) { |
|
443 |
return true; |
|
444 |
} |
|
445 |
} |
|
446 |
|
|
447 |
return false; |
|
448 |
}; |
|
449 |
|
|
450 |
// For the Flash binding to work, ZeroClipboard_TableTools must be on the global |
|
451 |
// object list |
|
452 |
window.ZeroClipboard_TableTools = ZeroClipboard_TableTools; |
|
453 |
|
|
454 |
|
|
455 |
|
|
456 |
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
|
457 |
* Local (private) functions |
|
458 |
*/ |
|
459 |
|
|
460 |
/** |
|
461 |
* If a Buttons instance is initlaised before it is placed into the DOM, Flash |
|
462 |
* won't be able to bind to it, so we need to wait until it is available, this |
|
463 |
* method abstracts that out. |
|
464 |
* |
|
465 |
* @param {ZeroClipboard} flash ZeroClipboard instance |
|
466 |
* @param {jQuery} node Button |
|
467 |
*/ |
|
468 |
var _glue = function ( flash, node ) |
|
469 |
{ |
|
470 |
var id = node.attr('id'); |
|
471 |
|
|
472 |
if ( node.parents('html').length ) { |
|
473 |
flash.glue( node[0], '' ); |
|
474 |
} |
|
475 |
else { |
|
476 |
setTimeout( function () { |
|
477 |
_glue( flash, node ); |
|
478 |
}, 500 ); |
|
479 |
} |
|
480 |
}; |
|
481 |
|
|
482 |
/** |
|
483 |
* Get the file name for an exported file. |
|
484 |
* |
|
485 |
* @param {object} config Button configuration |
|
486 |
* @param {boolean} incExtension Include the file name extension |
|
487 |
*/ |
|
488 |
var _filename = function ( config, incExtension ) |
|
489 |
{ |
|
490 |
// Backwards compatibility |
|
491 |
var filename = config.filename === '*' && config.title !== '*' && config.title !== undefined ? |
|
492 |
config.title : |
|
493 |
config.filename; |
|
494 |
|
|
495 |
if ( filename.indexOf( '*' ) !== -1 ) { |
|
496 |
filename = filename.replace( '*', $('title').text() ); |
|
497 |
} |
|
498 |
|
|
499 |
// Strip characters which the OS will object to |
|
500 |
filename = filename.replace(/[^a-zA-Z0-9_\u00A1-\uFFFF\.,\-_ !\(\)]/g, ""); |
|
501 |
|
|
502 |
return incExtension === undefined || incExtension === true ? |
|
503 |
filename+config.extension : |
|
504 |
filename; |
|
505 |
}; |
|
506 |
|
|
507 |
/** |
|
508 |
* Get the title for an exported file. |
|
509 |
* |
|
510 |
* @param {object} config Button configuration |
|
511 |
*/ |
|
512 |
var _title = function ( config ) |
|
513 |
{ |
|
514 |
var title = config.title; |
|
515 |
|
|
516 |
return title.indexOf( '*' ) !== -1 ? |
|
517 |
title.replace( '*', $('title').text() ) : |
|
518 |
title; |
|
519 |
}; |
|
520 |
|
|
521 |
/** |
|
522 |
* Set the flash text. This has to be broken up into chunks as the Javascript / |
|
523 |
* Flash bridge has a size limit. There is no indication in the Flash |
|
524 |
* documentation what this is, and it probably depends upon the browser. |
|
525 |
* Experimentation shows that the point is around 50k when data starts to get |
|
526 |
* lost, so an 8K limit used here is safe. |
|
527 |
* |
|
528 |
* @param {ZeroClipboard} flash ZeroClipboard instance |
|
529 |
* @param {string} data Data to send to Flash |
|
530 |
*/ |
|
531 |
var _setText = function ( flash, data ) |
|
532 |
{ |
|
533 |
var parts = data.match(/[\s\S]{1,8192}/g) || []; |
|
534 |
|
|
535 |
flash.clearText(); |
|
536 |
for ( var i=0, len=parts.length ; i<len ; i++ ) |
|
537 |
{ |
|
538 |
flash.appendText( parts[i] ); |
|
539 |
} |
|
540 |
}; |
|
541 |
|
|
542 |
/** |
|
543 |
* Get the newline character(s) |
|
544 |
* |
|
545 |
* @param {object} config Button configuration |
|
546 |
* @return {string} Newline character |
|
547 |
*/ |
|
548 |
var _newLine = function ( config ) |
|
549 |
{ |
|
550 |
return config.newline ? |
|
551 |
config.newline : |
|
552 |
navigator.userAgent.match(/Windows/) ? |
|
553 |
'\r\n' : |
|
554 |
'\n'; |
|
555 |
}; |
|
556 |
|
|
557 |
/** |
|
558 |
* Combine the data from the `buttons.exportData` method into a string that |
|
559 |
* will be used in the export file. |
|
560 |
* |
|
561 |
* @param {DataTable.Api} dt DataTables API instance |
|
562 |
* @param {object} config Button configuration |
|
563 |
* @return {object} The data to export |
|
564 |
*/ |
|
565 |
var _exportData = function ( dt, config ) |
|
566 |
{ |
|
567 |
var newLine = _newLine( config ); |
|
568 |
var data = dt.buttons.exportData( config.exportOptions ); |
|
569 |
var boundary = config.fieldBoundary; |
|
570 |
var separator = config.fieldSeparator; |
|
571 |
var reBoundary = new RegExp( boundary, 'g' ); |
|
572 |
var escapeChar = config.escapeChar !== undefined ? |
|
573 |
config.escapeChar : |
|
574 |
'\\'; |
|
575 |
var join = function ( a ) { |
|
576 |
var s = ''; |
|
577 |
|
|
578 |
// If there is a field boundary, then we might need to escape it in |
|
579 |
// the source data |
|
580 |
for ( var i=0, ien=a.length ; i<ien ; i++ ) { |
|
581 |
if ( i > 0 ) { |
|
582 |
s += separator; |
|
583 |
} |
|
584 |
|
|
585 |
s += boundary ? |
|
586 |
boundary + ('' + a[i]).replace( reBoundary, escapeChar+boundary ) + boundary : |
|
587 |
a[i]; |
|
588 |
} |
|
589 |
|
|
590 |
return s; |
|
591 |
}; |
|
592 |
|
|
593 |
var header = config.header ? join( data.header )+newLine : ''; |
|
594 |
var footer = config.footer ? newLine+join( data.footer ) : ''; |
|
595 |
var body = []; |
|
596 |
|
|
597 |
for ( var i=0, ien=data.body.length ; i<ien ; i++ ) { |
|
598 |
body.push( join( data.body[i] ) ); |
|
599 |
} |
|
600 |
|
|
601 |
return { |
|
602 |
str: header + body.join( newLine ) + footer, |
|
603 |
rows: body.length |
|
604 |
}; |
|
605 |
}; |
|
606 |
|
|
607 |
|
|
608 |
// Basic initialisation for the buttons is common between them |
|
609 |
var flashButton = { |
|
610 |
available: function () { |
|
611 |
return ZeroClipboard_TableTools.hasFlash(); |
|
612 |
}, |
|
613 |
|
|
614 |
init: function ( dt, button, config ) { |
|
615 |
// Insert the Flash movie |
|
616 |
ZeroClipboard_TableTools.moviePath = DataTable.Buttons.swfPath; |
|
617 |
var flash = new ZeroClipboard_TableTools.Client(); |
|
618 |
|
|
619 |
flash.setHandCursor( true ); |
|
620 |
flash.addEventListener('mouseDown', function(client) { |
|
621 |
config._fromFlash = true; |
|
622 |
dt.button( button[0] ).trigger(); |
|
623 |
config._fromFlash = false; |
|
624 |
} ); |
|
625 |
|
|
626 |
_glue( flash, button ); |
|
627 |
|
|
628 |
config._flash = flash; |
|
629 |
}, |
|
630 |
|
|
631 |
destroy: function ( dt, button, config ) { |
|
632 |
config._flash.destroy(); |
|
633 |
}, |
|
634 |
|
|
635 |
fieldSeparator: ',', |
|
636 |
|
|
637 |
fieldBoundary: '"', |
|
638 |
|
|
639 |
exportOptions: {}, |
|
640 |
|
|
641 |
title: '*', |
|
642 |
|
|
643 |
filename: '*', |
|
644 |
|
|
645 |
extension: '.csv', |
|
646 |
|
|
647 |
header: true, |
|
648 |
|
|
649 |
footer: false |
|
650 |
}; |
|
651 |
|
|
652 |
|
|
653 |
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
|
654 |
* DataTables options and methods |
|
655 |
*/ |
|
656 |
|
|
657 |
// Set the default SWF path |
|
658 |
DataTable.Buttons.swfPath = '//cdn.datatables.net/buttons/1.0.0/swf/flashExport.swf'; |
|
659 |
|
|
660 |
// Method to allow Flash buttons to be resized when made visible - as they are |
|
661 |
// of zero height and width if initialised hidden |
|
662 |
DataTable.Api.register( 'buttons.resize()', function () { |
|
663 |
$.each( ZeroClipboard_TableTools.clients, function ( i, client ) { |
|
664 |
if ( client.domElement !== undefined && client.domElement.parentNode ) { |
|
665 |
client.positionElement(); |
|
666 |
} |
|
667 |
} ); |
|
668 |
} ); |
|
669 |
|
|
670 |
|
|
671 |
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
|
672 |
* Button definitions |
|
673 |
*/ |
|
674 |
|
|
675 |
// Copy to clipboard |
|
676 |
DataTable.ext.buttons.copyFlash = $.extend( {}, flashButton, { |
|
677 |
className: 'buttons-copy buttons-flash', |
|
678 |
|
|
679 |
text: function ( dt ) { |
|
680 |
return dt.i18n( 'buttons.copy', 'Copy' ); |
|
681 |
}, |
|
682 |
|
|
683 |
action: function ( e, dt, button, config ) { |
|
684 |
// Check that the trigger did actually occur due to a Flash activation |
|
685 |
if ( ! config._fromFlash ) { |
|
686 |
return; |
|
687 |
} |
|
688 |
|
|
689 |
var flash = config._flash; |
|
690 |
var data = _exportData( dt, config ); |
|
691 |
|
|
692 |
flash.setAction( 'copy' ); |
|
693 |
_setText( flash, data.str ); |
|
694 |
|
|
695 |
dt.buttons.info( |
|
696 |
dt.i18n( 'buttons.copyTitle', 'Copy to clipboard' ), |
|
697 |
dt.i18n( 'buttons.copyInfo', { |
|
698 |
_: 'Copied %d rows to clipboard', |
|
699 |
1: 'Copied 1 row to clipboard' |
|
700 |
}, data.rows ), |
|
701 |
3000 |
|
702 |
); |
|
703 |
}, |
|
704 |
|
|
705 |
fieldSeparator: '\t', |
|
706 |
|
|
707 |
fieldBoundary: '' |
|
708 |
} ); |
|
709 |
|
|
710 |
// CSV save file |
|
711 |
DataTable.ext.buttons.csvFlash = $.extend( {}, flashButton, { |
|
712 |
className: 'buttons-csv buttons-flash', |
|
713 |
|
|
714 |
text: function ( dt ) { |
|
715 |
return dt.i18n( 'buttons.csv', 'CSV' ); |
|
716 |
}, |
|
717 |
|
|
718 |
action: function ( e, dt, button, config ) { |
|
719 |
// Set the text |
|
720 |
var flash = config._flash; |
|
721 |
var data = _exportData( dt, config ); |
|
722 |
|
|
723 |
flash.setAction( 'csv' ); |
|
724 |
flash.setFileName( _filename( config ) ); |
|
725 |
_setText( flash, data.str ); |
|
726 |
}, |
|
727 |
|
|
728 |
escapeChar: '"' |
|
729 |
} ); |
|
730 |
|
|
731 |
// Excel save file - this is really a CSV file using UTF-8 that Excel can read |
|
732 |
DataTable.ext.buttons.excelFlash = $.extend( {}, flashButton, { |
|
733 |
className: 'buttons-excel buttons-flash', |
|
734 |
|
|
735 |
text: function ( dt ) { |
|
736 |
return dt.i18n( 'buttons.excel', 'Excel' ); |
|
737 |
}, |
|
738 |
|
|
739 |
action: function ( e, dt, button, config ) { |
|
740 |
// Set the text |
|
741 |
var xml = ''; |
|
742 |
var flash = config._flash; |
|
743 |
var data = dt.buttons.exportData( config.exportOptions ); |
|
744 |
var addRow = function ( row ) { |
|
745 |
var cells = []; |
|
746 |
|
|
747 |
for ( var i=0, ien=row.length ; i<ien ; i++ ) { |
|
748 |
if ( row[i] === null || row[i] === undefined ) { |
|
749 |
row[i] = ''; |
|
750 |
} |
|
751 |
|
|
752 |
cells.push( typeof row[i] === 'number' || (row[i].match && row[i].match(/^-?[0-9\.]+$/) && row[i].charAt(0) !== '0') ? |
|
753 |
'<c t="n"><v>'+row[i]+'</v></c>' : |
|
754 |
'<c t="inlineStr"><is><t>'+( |
|
755 |
! row[i].replace ? |
|
756 |
row[i] : |
|
757 |
row[i] |
|
758 |
.replace(/&(?!amp;)/g, '&') |
|
759 |
.replace(/[\x00-\x1F\x7F-\x9F]/g, ''))+ // remove control characters |
|
760 |
'</t></is></c>' // they are not valid in XML |
|
761 |
); |
|
762 |
} |
|
763 |
|
|
764 |
return '<row>'+cells.join('')+'</row>'; |
|
765 |
}; |
|
766 |
|
|
767 |
if ( config.header ) { |
|
768 |
xml += addRow( data.header ); |
|
769 |
} |
|
770 |
|
|
771 |
for ( var i=0, ien=data.body.length ; i<ien ; i++ ) { |
|
772 |
xml += addRow( data.body[i] ); |
|
773 |
} |
|
774 |
|
|
775 |
if ( config.footer ) { |
|
776 |
xml += addRow( data.footer ); |
|
777 |
} |
|
778 |
|
|
779 |
flash.setAction( 'excel' ); |
|
780 |
flash.setFileName( _filename( config ) ); |
|
781 |
_setText( flash, xml ); |
|
782 |
}, |
|
783 |
|
|
784 |
extension: '.xlsx' |
|
785 |
} ); |
|
786 |
|
|
787 |
// PDF export |
|
788 |
DataTable.ext.buttons.pdfFlash = $.extend( {}, flashButton, { |
|
789 |
className: 'buttons-pdf buttons-flash', |
|
790 |
|
|
791 |
text: function ( dt ) { |
|
792 |
return dt.i18n( 'buttons.pdf', 'PDF' ); |
|
793 |
}, |
|
794 |
|
|
795 |
action: function ( e, dt, button, config ) { |
|
796 |
// Set the text |
|
797 |
var flash = config._flash; |
|
798 |
var data = dt.buttons.exportData( config.exportOptions ); |
|
799 |
var totalWidth = dt.table().node().offsetWidth; |
|
800 |
|
|
801 |
// Calculate the column width ratios for layout of the table in the PDF |
|
802 |
var ratios = dt.columns( config.columns ).indexes().map( function ( idx ) { |
|
803 |
return dt.column( idx ).header().offsetWidth / totalWidth; |
|
804 |
} ); |
|
805 |
|
|
806 |
flash.setAction( 'pdf' ); |
|
807 |
flash.setFileName( _title( config ) ); |
|
808 |
|
|
809 |
_setText( flash, JSON.stringify( { |
|
810 |
title: _filename(config, false), |
|
811 |
message: config.message, |
|
812 |
colWidth: ratios.toArray(), |
|
813 |
orientation: config.orientation, |
|
814 |
size: config.pageSize, |
|
815 |
header: config.header ? data.header : null, |
|
816 |
footer: config.footer ? data.footer : null, |
|
817 |
body: data.body |
|
818 |
} ) ); |
|
819 |
}, |
|
820 |
|
|
821 |
extension: '.pdf', |
|
822 |
|
|
823 |
orientation: 'portrait', |
|
824 |
|
|
825 |
pageSize: 'A4', |
|
826 |
|
|
827 |
message: '', |
|
828 |
|
|
829 |
newline: '\n' |
|
830 |
} ); |
|
831 |
|
|
832 |
|
|
833 |
return DataTable.Buttons; |
|
834 |
})); |