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