dataTables.tableTools.js 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230
  1. /*! TableTools 2.2.4
  2. * 2009-2015 SpryMedia Ltd - datatables.net/license
  3. *
  4. * ZeroClipboard 1.0.4
  5. * Author: Joseph Huckaby - MIT licensed
  6. */
  7. /**
  8. * @summary TableTools
  9. * @description Tools and buttons for DataTables
  10. * @version 2.2.4
  11. * @file dataTables.tableTools.js
  12. * @author SpryMedia Ltd (www.sprymedia.co.uk)
  13. * @contact www.sprymedia.co.uk/contact
  14. * @copyright Copyright 2009-2015 SpryMedia Ltd.
  15. *
  16. * This source file is free software, available under the following license:
  17. * MIT license - http://datatables.net/license/mit
  18. *
  19. * This source file is distributed in the hope that it will be useful, but
  20. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  21. * or FITNESS FOR A PARTICULAR PURPOSE. See the license files for details.
  22. *
  23. * For details please refer to: http://www.datatables.net
  24. */
  25. /* Global scope for TableTools for backwards compatibility.
  26. * Will be removed in 2.3
  27. */
  28. var TableTools;
  29. (function(window, document, undefined) {
  30. var factory = function( $, DataTable ) {
  31. "use strict";
  32. //include ZeroClipboard.js
  33. /* ZeroClipboard 1.0.4
  34. * Author: Joseph Huckaby
  35. */
  36. var ZeroClipboard_TableTools = {
  37. version: "1.0.4-TableTools2",
  38. clients: {}, // registered upload clients on page, indexed by id
  39. moviePath: '', // URL to movie
  40. nextId: 1, // ID of next movie
  41. $: function(thingy) {
  42. // simple DOM lookup utility function
  43. if (typeof(thingy) == 'string') {
  44. thingy = document.getElementById(thingy);
  45. }
  46. if (!thingy.addClass) {
  47. // extend element with a few useful methods
  48. thingy.hide = function() { this.style.display = 'none'; };
  49. thingy.show = function() { this.style.display = ''; };
  50. thingy.addClass = function(name) { this.removeClass(name); this.className += ' ' + name; };
  51. thingy.removeClass = function(name) {
  52. this.className = this.className.replace( new RegExp("\\s*" + name + "\\s*"), " ").replace(/^\s+/, '').replace(/\s+$/, '');
  53. };
  54. thingy.hasClass = function(name) {
  55. return !!this.className.match( new RegExp("\\s*" + name + "\\s*") );
  56. };
  57. }
  58. return thingy;
  59. },
  60. setMoviePath: function(path) {
  61. // set path to ZeroClipboard.swf
  62. this.moviePath = path;
  63. },
  64. dispatch: function(id, eventName, args) {
  65. // receive event from flash movie, send to client
  66. var client = this.clients[id];
  67. if (client) {
  68. client.receiveEvent(eventName, args);
  69. }
  70. },
  71. register: function(id, client) {
  72. // register new client to receive events
  73. this.clients[id] = client;
  74. },
  75. getDOMObjectPosition: function(obj) {
  76. // get absolute coordinates for dom element
  77. var info = {
  78. left: 0,
  79. top: 0,
  80. width: obj.width ? obj.width : obj.offsetWidth,
  81. height: obj.height ? obj.height : obj.offsetHeight
  82. };
  83. if ( obj.style.width !== "" ) {
  84. info.width = obj.style.width.replace("px","");
  85. }
  86. if ( obj.style.height !== "" ) {
  87. info.height = obj.style.height.replace("px","");
  88. }
  89. while (obj) {
  90. info.left += obj.offsetLeft;
  91. info.top += obj.offsetTop;
  92. obj = obj.offsetParent;
  93. }
  94. return info;
  95. },
  96. Client: function(elem) {
  97. // constructor for new simple upload client
  98. this.handlers = {};
  99. // unique ID
  100. this.id = ZeroClipboard_TableTools.nextId++;
  101. this.movieId = 'ZeroClipboard_TableToolsMovie_' + this.id;
  102. // register client with singleton to receive flash events
  103. ZeroClipboard_TableTools.register(this.id, this);
  104. // create movie
  105. if (elem) {
  106. this.glue(elem);
  107. }
  108. }
  109. };
  110. ZeroClipboard_TableTools.Client.prototype = {
  111. id: 0, // unique ID for us
  112. ready: false, // whether movie is ready to receive events or not
  113. movie: null, // reference to movie object
  114. clipText: '', // text to copy to clipboard
  115. fileName: '', // default file save name
  116. action: 'copy', // action to perform
  117. handCursorEnabled: true, // whether to show hand cursor, or default pointer cursor
  118. cssEffects: true, // enable CSS mouse effects on dom container
  119. handlers: null, // user event handlers
  120. sized: false,
  121. glue: function(elem, title) {
  122. // glue to DOM element
  123. // elem can be ID or actual DOM element object
  124. this.domElement = ZeroClipboard_TableTools.$(elem);
  125. // float just above object, or zIndex 99 if dom element isn't set
  126. var zIndex = 99;
  127. if (this.domElement.style.zIndex) {
  128. zIndex = parseInt(this.domElement.style.zIndex, 10) + 1;
  129. }
  130. // find X/Y position of domElement
  131. var box = ZeroClipboard_TableTools.getDOMObjectPosition(this.domElement);
  132. // create floating DIV above element
  133. this.div = document.createElement('div');
  134. var style = this.div.style;
  135. style.position = 'absolute';
  136. style.left = '0px';
  137. style.top = '0px';
  138. style.width = (box.width) + 'px';
  139. style.height = box.height + 'px';
  140. style.zIndex = zIndex;
  141. if ( typeof title != "undefined" && title !== "" ) {
  142. this.div.title = title;
  143. }
  144. if ( box.width !== 0 && box.height !== 0 ) {
  145. this.sized = true;
  146. }
  147. // style.backgroundColor = '#f00'; // debug
  148. if ( this.domElement ) {
  149. this.domElement.appendChild(this.div);
  150. this.div.innerHTML = this.getHTML( box.width, box.height ).replace(/&/g, '&');
  151. }
  152. },
  153. positionElement: function() {
  154. var box = ZeroClipboard_TableTools.getDOMObjectPosition(this.domElement);
  155. var style = this.div.style;
  156. style.position = 'absolute';
  157. //style.left = (this.domElement.offsetLeft)+'px';
  158. //style.top = this.domElement.offsetTop+'px';
  159. style.width = box.width + 'px';
  160. style.height = box.height + 'px';
  161. if ( box.width !== 0 && box.height !== 0 ) {
  162. this.sized = true;
  163. } else {
  164. return;
  165. }
  166. var flash = this.div.childNodes[0];
  167. flash.width = box.width;
  168. flash.height = box.height;
  169. },
  170. getHTML: function(width, height) {
  171. // return HTML for movie
  172. var html = '';
  173. var flashvars = 'id=' + this.id +
  174. '&width=' + width +
  175. '&height=' + height;
  176. if (navigator.userAgent.match(/MSIE/)) {
  177. // IE gets an OBJECT tag
  178. var protocol = location.href.match(/^https/i) ? 'https://' : 'http://';
  179. 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>';
  180. }
  181. else {
  182. // all other browsers get an EMBED tag
  183. 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" />';
  184. }
  185. return html;
  186. },
  187. hide: function() {
  188. // temporarily hide floater offscreen
  189. if (this.div) {
  190. this.div.style.left = '-2000px';
  191. }
  192. },
  193. show: function() {
  194. // show ourselves after a call to hide()
  195. this.reposition();
  196. },
  197. destroy: function() {
  198. // destroy control and floater
  199. if (this.domElement && this.div) {
  200. this.hide();
  201. this.div.innerHTML = '';
  202. var body = document.getElementsByTagName('body')[0];
  203. try { body.removeChild( this.div ); } catch(e) {}
  204. this.domElement = null;
  205. this.div = null;
  206. }
  207. },
  208. reposition: function(elem) {
  209. // reposition our floating div, optionally to new container
  210. // warning: container CANNOT change size, only position
  211. if (elem) {
  212. this.domElement = ZeroClipboard_TableTools.$(elem);
  213. if (!this.domElement) {
  214. this.hide();
  215. }
  216. }
  217. if (this.domElement && this.div) {
  218. var box = ZeroClipboard_TableTools.getDOMObjectPosition(this.domElement);
  219. var style = this.div.style;
  220. style.left = '' + box.left + 'px';
  221. style.top = '' + box.top + 'px';
  222. }
  223. },
  224. clearText: function() {
  225. // clear the text to be copy / saved
  226. this.clipText = '';
  227. if (this.ready) {
  228. this.movie.clearText();
  229. }
  230. },
  231. appendText: function(newText) {
  232. // append text to that which is to be copied / saved
  233. this.clipText += newText;
  234. if (this.ready) { this.movie.appendText(newText) ;}
  235. },
  236. setText: function(newText) {
  237. // set text to be copied to be copied / saved
  238. this.clipText = newText;
  239. if (this.ready) { this.movie.setText(newText) ;}
  240. },
  241. setCharSet: function(charSet) {
  242. // set the character set (UTF16LE or UTF8)
  243. this.charSet = charSet;
  244. if (this.ready) { this.movie.setCharSet(charSet) ;}
  245. },
  246. setBomInc: function(bomInc) {
  247. // set if the BOM should be included or not
  248. this.incBom = bomInc;
  249. if (this.ready) { this.movie.setBomInc(bomInc) ;}
  250. },
  251. setFileName: function(newText) {
  252. // set the file name
  253. this.fileName = newText;
  254. if (this.ready) {
  255. this.movie.setFileName(newText);
  256. }
  257. },
  258. setAction: function(newText) {
  259. // set action (save or copy)
  260. this.action = newText;
  261. if (this.ready) {
  262. this.movie.setAction(newText);
  263. }
  264. },
  265. addEventListener: function(eventName, func) {
  266. // add user event listener for event
  267. // event types: load, queueStart, fileStart, fileComplete, queueComplete, progress, error, cancel
  268. eventName = eventName.toString().toLowerCase().replace(/^on/, '');
  269. if (!this.handlers[eventName]) {
  270. this.handlers[eventName] = [];
  271. }
  272. this.handlers[eventName].push(func);
  273. },
  274. setHandCursor: function(enabled) {
  275. // enable hand cursor (true), or default arrow cursor (false)
  276. this.handCursorEnabled = enabled;
  277. if (this.ready) {
  278. this.movie.setHandCursor(enabled);
  279. }
  280. },
  281. setCSSEffects: function(enabled) {
  282. // enable or disable CSS effects on DOM container
  283. this.cssEffects = !!enabled;
  284. },
  285. receiveEvent: function(eventName, args) {
  286. var self;
  287. // receive event from flash
  288. eventName = eventName.toString().toLowerCase().replace(/^on/, '');
  289. // special behavior for certain events
  290. switch (eventName) {
  291. case 'load':
  292. // movie claims it is ready, but in IE this isn't always the case...
  293. // bug fix: Cannot extend EMBED DOM elements in Firefox, must use traditional function
  294. this.movie = document.getElementById(this.movieId);
  295. if (!this.movie) {
  296. self = this;
  297. setTimeout( function() { self.receiveEvent('load', null); }, 1 );
  298. return;
  299. }
  300. // firefox on pc needs a "kick" in order to set these in certain cases
  301. if (!this.ready && navigator.userAgent.match(/Firefox/) && navigator.userAgent.match(/Windows/)) {
  302. self = this;
  303. setTimeout( function() { self.receiveEvent('load', null); }, 100 );
  304. this.ready = true;
  305. return;
  306. }
  307. this.ready = true;
  308. this.movie.clearText();
  309. this.movie.appendText( this.clipText );
  310. this.movie.setFileName( this.fileName );
  311. this.movie.setAction( this.action );
  312. this.movie.setCharSet( this.charSet );
  313. this.movie.setBomInc( this.incBom );
  314. this.movie.setHandCursor( this.handCursorEnabled );
  315. break;
  316. case 'mouseover':
  317. if (this.domElement && this.cssEffects) {
  318. //this.domElement.addClass('hover');
  319. if (this.recoverActive) {
  320. this.domElement.addClass('active');
  321. }
  322. }
  323. break;
  324. case 'mouseout':
  325. if (this.domElement && this.cssEffects) {
  326. this.recoverActive = false;
  327. if (this.domElement.hasClass('active')) {
  328. this.domElement.removeClass('active');
  329. this.recoverActive = true;
  330. }
  331. //this.domElement.removeClass('hover');
  332. }
  333. break;
  334. case 'mousedown':
  335. if (this.domElement && this.cssEffects) {
  336. this.domElement.addClass('active');
  337. }
  338. break;
  339. case 'mouseup':
  340. if (this.domElement && this.cssEffects) {
  341. this.domElement.removeClass('active');
  342. this.recoverActive = false;
  343. }
  344. break;
  345. } // switch eventName
  346. if (this.handlers[eventName]) {
  347. for (var idx = 0, len = this.handlers[eventName].length; idx < len; idx++) {
  348. var func = this.handlers[eventName][idx];
  349. if (typeof(func) == 'function') {
  350. // actual function reference
  351. func(this, args);
  352. }
  353. else if ((typeof(func) == 'object') && (func.length == 2)) {
  354. // PHP style object + method, i.e. [myObject, 'myMethod']
  355. func[0][ func[1] ](this, args);
  356. }
  357. else if (typeof(func) == 'string') {
  358. // name of function
  359. window[func](this, args);
  360. }
  361. } // foreach event handler defined
  362. } // user defined handler for event
  363. }
  364. };
  365. // For the Flash binding to work, ZeroClipboard_TableTools must be on the global
  366. // object list
  367. window.ZeroClipboard_TableTools = ZeroClipboard_TableTools;
  368. //include TableTools.js
  369. /* TableTools
  370. * 2009-2015 SpryMedia Ltd - datatables.net/license
  371. */
  372. /*globals TableTools,ZeroClipboard_TableTools*/
  373. (function($, window, document) {
  374. /**
  375. * TableTools provides flexible buttons and other tools for a DataTables enhanced table
  376. * @class TableTools
  377. * @constructor
  378. * @param {Object} oDT DataTables instance. When using DataTables 1.10 this can
  379. * also be a jQuery collection, jQuery selector, table node, DataTables API
  380. * instance or DataTables settings object.
  381. * @param {Object} oOpts TableTools options
  382. * @param {String} oOpts.sSwfPath ZeroClipboard SWF path
  383. * @param {String} oOpts.sRowSelect Row selection options - 'none', 'single', 'multi' or 'os'
  384. * @param {Function} oOpts.fnPreRowSelect Callback function just prior to row selection
  385. * @param {Function} oOpts.fnRowSelected Callback function just after row selection
  386. * @param {Function} oOpts.fnRowDeselected Callback function when row is deselected
  387. * @param {Array} oOpts.aButtons List of buttons to be used
  388. */
  389. TableTools = function( oDT, oOpts )
  390. {
  391. /* Santiy check that we are a new instance */
  392. if ( ! this instanceof TableTools )
  393. {
  394. alert( "Warning: TableTools must be initialised with the keyword 'new'" );
  395. }
  396. // In 1.10 we can use the API to get the settings object from a number of
  397. // sources
  398. var dtSettings = $.fn.dataTable.Api ?
  399. new $.fn.dataTable.Api( oDT ).settings()[0] :
  400. oDT.fnSettings();
  401. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  402. * Public class variables
  403. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  404. /**
  405. * @namespace Settings object which contains customisable information for TableTools instance
  406. */
  407. this.s = {
  408. /**
  409. * Store 'this' so the instance can be retrieved from the settings object
  410. * @property that
  411. * @type object
  412. * @default this
  413. */
  414. "that": this,
  415. /**
  416. * DataTables settings objects
  417. * @property dt
  418. * @type object
  419. * @default <i>From the oDT init option</i>
  420. */
  421. "dt": dtSettings,
  422. /**
  423. * @namespace Print specific information
  424. */
  425. "print": {
  426. /**
  427. * DataTables draw 'start' point before the printing display was shown
  428. * @property saveStart
  429. * @type int
  430. * @default -1
  431. */
  432. "saveStart": -1,
  433. /**
  434. * DataTables draw 'length' point before the printing display was shown
  435. * @property saveLength
  436. * @type int
  437. * @default -1
  438. */
  439. "saveLength": -1,
  440. /**
  441. * Page scrolling point before the printing display was shown so it can be restored
  442. * @property saveScroll
  443. * @type int
  444. * @default -1
  445. */
  446. "saveScroll": -1,
  447. /**
  448. * Wrapped function to end the print display (to maintain scope)
  449. * @property funcEnd
  450. * @type Function
  451. * @default function () {}
  452. */
  453. "funcEnd": function () {}
  454. },
  455. /**
  456. * A unique ID is assigned to each button in each instance
  457. * @property buttonCounter
  458. * @type int
  459. * @default 0
  460. */
  461. "buttonCounter": 0,
  462. /**
  463. * @namespace Select rows specific information
  464. */
  465. "select": {
  466. /**
  467. * Select type - can be 'none', 'single' or 'multi'
  468. * @property type
  469. * @type string
  470. * @default ""
  471. */
  472. "type": "",
  473. /**
  474. * Array of nodes which are currently selected
  475. * @property selected
  476. * @type array
  477. * @default []
  478. */
  479. "selected": [],
  480. /**
  481. * Function to run before the selection can take place. Will cancel the select if the
  482. * function returns false
  483. * @property preRowSelect
  484. * @type Function
  485. * @default null
  486. */
  487. "preRowSelect": null,
  488. /**
  489. * Function to run when a row is selected
  490. * @property postSelected
  491. * @type Function
  492. * @default null
  493. */
  494. "postSelected": null,
  495. /**
  496. * Function to run when a row is deselected
  497. * @property postDeselected
  498. * @type Function
  499. * @default null
  500. */
  501. "postDeselected": null,
  502. /**
  503. * Indicate if all rows are selected (needed for server-side processing)
  504. * @property all
  505. * @type boolean
  506. * @default false
  507. */
  508. "all": false,
  509. /**
  510. * Class name to add to selected TR nodes
  511. * @property selectedClass
  512. * @type String
  513. * @default ""
  514. */
  515. "selectedClass": ""
  516. },
  517. /**
  518. * Store of the user input customisation object
  519. * @property custom
  520. * @type object
  521. * @default {}
  522. */
  523. "custom": {},
  524. /**
  525. * SWF movie path
  526. * @property swfPath
  527. * @type string
  528. * @default ""
  529. */
  530. "swfPath": "",
  531. /**
  532. * Default button set
  533. * @property buttonSet
  534. * @type array
  535. * @default []
  536. */
  537. "buttonSet": [],
  538. /**
  539. * When there is more than one TableTools instance for a DataTable, there must be a
  540. * master which controls events (row selection etc)
  541. * @property master
  542. * @type boolean
  543. * @default false
  544. */
  545. "master": false,
  546. /**
  547. * Tag names that are used for creating collections and buttons
  548. * @namesapce
  549. */
  550. "tags": {}
  551. };
  552. /**
  553. * @namespace Common and useful DOM elements for the class instance
  554. */
  555. this.dom = {
  556. /**
  557. * DIV element that is create and all TableTools buttons (and their children) put into
  558. * @property container
  559. * @type node
  560. * @default null
  561. */
  562. "container": null,
  563. /**
  564. * The table node to which TableTools will be applied
  565. * @property table
  566. * @type node
  567. * @default null
  568. */
  569. "table": null,
  570. /**
  571. * @namespace Nodes used for the print display
  572. */
  573. "print": {
  574. /**
  575. * Nodes which have been removed from the display by setting them to display none
  576. * @property hidden
  577. * @type array
  578. * @default []
  579. */
  580. "hidden": [],
  581. /**
  582. * The information display saying telling the user about the print display
  583. * @property message
  584. * @type node
  585. * @default null
  586. */
  587. "message": null
  588. },
  589. /**
  590. * @namespace Nodes used for a collection display. This contains the currently used collection
  591. */
  592. "collection": {
  593. /**
  594. * The div wrapper containing the buttons in the collection (i.e. the menu)
  595. * @property collection
  596. * @type node
  597. * @default null
  598. */
  599. "collection": null,
  600. /**
  601. * Background display to provide focus and capture events
  602. * @property background
  603. * @type node
  604. * @default null
  605. */
  606. "background": null
  607. }
  608. };
  609. /**
  610. * @namespace Name space for the classes that this TableTools instance will use
  611. * @extends TableTools.classes
  612. */
  613. this.classes = $.extend( true, {}, TableTools.classes );
  614. if ( this.s.dt.bJUI )
  615. {
  616. $.extend( true, this.classes, TableTools.classes_themeroller );
  617. }
  618. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  619. * Public class methods
  620. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  621. /**
  622. * Retreieve the settings object from an instance
  623. * @method fnSettings
  624. * @returns {object} TableTools settings object
  625. */
  626. this.fnSettings = function () {
  627. return this.s;
  628. };
  629. /* Constructor logic */
  630. if ( typeof oOpts == 'undefined' )
  631. {
  632. oOpts = {};
  633. }
  634. TableTools._aInstances.push( this );
  635. this._fnConstruct( oOpts );
  636. return this;
  637. };
  638. TableTools.prototype = {
  639. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  640. * Public methods
  641. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  642. /**
  643. * Retreieve the settings object from an instance
  644. * @returns {array} List of TR nodes which are currently selected
  645. * @param {boolean} [filtered=false] Get only selected rows which are
  646. * available given the filtering applied to the table. By default
  647. * this is false - i.e. all rows, regardless of filtering are
  648. selected.
  649. */
  650. "fnGetSelected": function ( filtered )
  651. {
  652. var
  653. out = [],
  654. data = this.s.dt.aoData,
  655. displayed = this.s.dt.aiDisplay,
  656. i, iLen;
  657. if ( filtered )
  658. {
  659. // Only consider filtered rows
  660. for ( i=0, iLen=displayed.length ; i<iLen ; i++ )
  661. {
  662. if ( data[ displayed[i] ]._DTTT_selected )
  663. {
  664. out.push( data[ displayed[i] ].nTr );
  665. }
  666. }
  667. }
  668. else
  669. {
  670. // Use all rows
  671. for ( i=0, iLen=data.length ; i<iLen ; i++ )
  672. {
  673. if ( data[i]._DTTT_selected )
  674. {
  675. out.push( data[i].nTr );
  676. }
  677. }
  678. }
  679. return out;
  680. },
  681. /**
  682. * Get the data source objects/arrays from DataTables for the selected rows (same as
  683. * fnGetSelected followed by fnGetData on each row from the table)
  684. * @returns {array} Data from the TR nodes which are currently selected
  685. */
  686. "fnGetSelectedData": function ()
  687. {
  688. var out = [];
  689. var data=this.s.dt.aoData;
  690. var i, iLen;
  691. for ( i=0, iLen=data.length ; i<iLen ; i++ )
  692. {
  693. if ( data[i]._DTTT_selected )
  694. {
  695. out.push( this.s.dt.oInstance.fnGetData(i) );
  696. }
  697. }
  698. return out;
  699. },
  700. /**
  701. * Get the indexes of the selected rows
  702. * @returns {array} List of row indexes
  703. * @param {boolean} [filtered=false] Get only selected rows which are
  704. * available given the filtering applied to the table. By default
  705. * this is false - i.e. all rows, regardless of filtering are
  706. selected.
  707. */
  708. "fnGetSelectedIndexes": function ( filtered )
  709. {
  710. var
  711. out = [],
  712. data = this.s.dt.aoData,
  713. displayed = this.s.dt.aiDisplay,
  714. i, iLen;
  715. if ( filtered )
  716. {
  717. // Only consider filtered rows
  718. for ( i=0, iLen=displayed.length ; i<iLen ; i++ )
  719. {
  720. if ( data[ displayed[i] ]._DTTT_selected )
  721. {
  722. out.push( displayed[i] );
  723. }
  724. }
  725. }
  726. else
  727. {
  728. // Use all rows
  729. for ( i=0, iLen=data.length ; i<iLen ; i++ )
  730. {
  731. if ( data[i]._DTTT_selected )
  732. {
  733. out.push( i );
  734. }
  735. }
  736. }
  737. return out;
  738. },
  739. /**
  740. * Check to see if a current row is selected or not
  741. * @param {Node} n TR node to check if it is currently selected or not
  742. * @returns {Boolean} true if select, false otherwise
  743. */
  744. "fnIsSelected": function ( n )
  745. {
  746. var pos = this.s.dt.oInstance.fnGetPosition( n );
  747. return (this.s.dt.aoData[pos]._DTTT_selected===true) ? true : false;
  748. },
  749. /**
  750. * Select all rows in the table
  751. * @param {boolean} [filtered=false] Select only rows which are available
  752. * given the filtering applied to the table. By default this is false -
  753. * i.e. all rows, regardless of filtering are selected.
  754. */
  755. "fnSelectAll": function ( filtered )
  756. {
  757. this._fnRowSelect( filtered ?
  758. this.s.dt.aiDisplay :
  759. this.s.dt.aoData
  760. );
  761. },
  762. /**
  763. * Deselect all rows in the table
  764. * @param {boolean} [filtered=false] Deselect only rows which are available
  765. * given the filtering applied to the table. By default this is false -
  766. * i.e. all rows, regardless of filtering are deselected.
  767. */
  768. "fnSelectNone": function ( filtered )
  769. {
  770. this._fnRowDeselect( this.fnGetSelectedIndexes(filtered) );
  771. },
  772. /**
  773. * Select row(s)
  774. * @param {node|object|array} n The row(s) to select. Can be a single DOM
  775. * TR node, an array of TR nodes or a jQuery object.
  776. */
  777. "fnSelect": function ( n )
  778. {
  779. if ( this.s.select.type == "single" )
  780. {
  781. this.fnSelectNone();
  782. this._fnRowSelect( n );
  783. }
  784. else
  785. {
  786. this._fnRowSelect( n );
  787. }
  788. },
  789. /**
  790. * Deselect row(s)
  791. * @param {node|object|array} n The row(s) to deselect. Can be a single DOM
  792. * TR node, an array of TR nodes or a jQuery object.
  793. */
  794. "fnDeselect": function ( n )
  795. {
  796. this._fnRowDeselect( n );
  797. },
  798. /**
  799. * Get the title of the document - useful for file names. The title is retrieved from either
  800. * the configuration object's 'title' parameter, or the HTML document title
  801. * @param {Object} oConfig Button configuration object
  802. * @returns {String} Button title
  803. */
  804. "fnGetTitle": function( oConfig )
  805. {
  806. var sTitle = "";
  807. if ( typeof oConfig.sTitle != 'undefined' && oConfig.sTitle !== "" ) {
  808. sTitle = oConfig.sTitle;
  809. } else {
  810. var anTitle = document.getElementsByTagName('title');
  811. if ( anTitle.length > 0 )
  812. {
  813. sTitle = anTitle[0].innerHTML;
  814. }
  815. }
  816. /* Strip characters which the OS will object to - checking for UTF8 support in the scripting
  817. * engine
  818. */
  819. if ( "\u00A1".toString().length < 4 ) {
  820. return sTitle.replace(/[^a-zA-Z0-9_\u00A1-\uFFFF\.,\-_ !\(\)]/g, "");
  821. } else {
  822. return sTitle.replace(/[^a-zA-Z0-9_\.,\-_ !\(\)]/g, "");
  823. }
  824. },
  825. /**
  826. * Calculate a unity array with the column width by proportion for a set of columns to be
  827. * included for a button. This is particularly useful for PDF creation, where we can use the
  828. * column widths calculated by the browser to size the columns in the PDF.
  829. * @param {Object} oConfig Button configuration object
  830. * @returns {Array} Unity array of column ratios
  831. */
  832. "fnCalcColRatios": function ( oConfig )
  833. {
  834. var
  835. aoCols = this.s.dt.aoColumns,
  836. aColumnsInc = this._fnColumnTargets( oConfig.mColumns ),
  837. aColWidths = [],
  838. iWidth = 0, iTotal = 0, i, iLen;
  839. for ( i=0, iLen=aColumnsInc.length ; i<iLen ; i++ )
  840. {
  841. if ( aColumnsInc[i] )
  842. {
  843. iWidth = aoCols[i].nTh.offsetWidth;
  844. iTotal += iWidth;
  845. aColWidths.push( iWidth );
  846. }
  847. }
  848. for ( i=0, iLen=aColWidths.length ; i<iLen ; i++ )
  849. {
  850. aColWidths[i] = aColWidths[i] / iTotal;
  851. }
  852. return aColWidths.join('\t');
  853. },
  854. /**
  855. * Get the information contained in a table as a string
  856. * @param {Object} oConfig Button configuration object
  857. * @returns {String} Table data as a string
  858. */
  859. "fnGetTableData": function ( oConfig )
  860. {
  861. /* In future this could be used to get data from a plain HTML source as well as DataTables */
  862. if ( this.s.dt )
  863. {
  864. return this._fnGetDataTablesData( oConfig );
  865. }
  866. },
  867. /**
  868. * Pass text to a flash button instance, which will be used on the button's click handler
  869. * @param {Object} clip Flash button object
  870. * @param {String} text Text to set
  871. */
  872. "fnSetText": function ( clip, text )
  873. {
  874. this._fnFlashSetText( clip, text );
  875. },
  876. /**
  877. * Resize the flash elements of the buttons attached to this TableTools instance - this is
  878. * useful for when initialising TableTools when it is hidden (display:none) since sizes can't
  879. * be calculated at that time.
  880. */
  881. "fnResizeButtons": function ()
  882. {
  883. for ( var cli in ZeroClipboard_TableTools.clients )
  884. {
  885. if ( cli )
  886. {
  887. var client = ZeroClipboard_TableTools.clients[cli];
  888. if ( typeof client.domElement != 'undefined' &&
  889. client.domElement.parentNode )
  890. {
  891. client.positionElement();
  892. }
  893. }
  894. }
  895. },
  896. /**
  897. * Check to see if any of the ZeroClipboard client's attached need to be resized
  898. */
  899. "fnResizeRequired": function ()
  900. {
  901. for ( var cli in ZeroClipboard_TableTools.clients )
  902. {
  903. if ( cli )
  904. {
  905. var client = ZeroClipboard_TableTools.clients[cli];
  906. if ( typeof client.domElement != 'undefined' &&
  907. client.domElement.parentNode == this.dom.container &&
  908. client.sized === false )
  909. {
  910. return true;
  911. }
  912. }
  913. }
  914. return false;
  915. },
  916. /**
  917. * Programmatically enable or disable the print view
  918. * @param {boolean} [bView=true] Show the print view if true or not given. If false, then
  919. * terminate the print view and return to normal.
  920. * @param {object} [oConfig={}] Configuration for the print view
  921. * @param {boolean} [oConfig.bShowAll=false] Show all rows in the table if true
  922. * @param {string} [oConfig.sInfo] Information message, displayed as an overlay to the
  923. * user to let them know what the print view is.
  924. * @param {string} [oConfig.sMessage] HTML string to show at the top of the document - will
  925. * be included in the printed document.
  926. */
  927. "fnPrint": function ( bView, oConfig )
  928. {
  929. if ( oConfig === undefined )
  930. {
  931. oConfig = {};
  932. }
  933. if ( bView === undefined || bView )
  934. {
  935. this._fnPrintStart( oConfig );
  936. }
  937. else
  938. {
  939. this._fnPrintEnd();
  940. }
  941. },
  942. /**
  943. * Show a message to the end user which is nicely styled
  944. * @param {string} message The HTML string to show to the user
  945. * @param {int} time The duration the message is to be shown on screen for (mS)
  946. */
  947. "fnInfo": function ( message, time ) {
  948. var info = $('<div/>')
  949. .addClass( this.classes.print.info )
  950. .html( message )
  951. .appendTo( 'body' );
  952. setTimeout( function() {
  953. info.fadeOut( "normal", function() {
  954. info.remove();
  955. } );
  956. }, time );
  957. },
  958. /**
  959. * Get the container element of the instance for attaching to the DOM
  960. * @returns {node} DOM node
  961. */
  962. "fnContainer": function () {
  963. return this.dom.container;
  964. },
  965. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  966. * Private methods (they are of course public in JS, but recommended as private)
  967. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  968. /**
  969. * Constructor logic
  970. * @method _fnConstruct
  971. * @param {Object} oOpts Same as TableTools constructor
  972. * @returns void
  973. * @private
  974. */
  975. "_fnConstruct": function ( oOpts )
  976. {
  977. var that = this;
  978. this._fnCustomiseSettings( oOpts );
  979. /* Container element */
  980. this.dom.container = document.createElement( this.s.tags.container );
  981. this.dom.container.className = this.classes.container;
  982. /* Row selection config */
  983. if ( this.s.select.type != 'none' )
  984. {
  985. this._fnRowSelectConfig();
  986. }
  987. /* Buttons */
  988. this._fnButtonDefinations( this.s.buttonSet, this.dom.container );
  989. /* Destructor */
  990. this.s.dt.aoDestroyCallback.push( {
  991. "sName": "TableTools",
  992. "fn": function () {
  993. $(that.s.dt.nTBody)
  994. .off( 'click.DTTT_Select', that.s.custom.sRowSelector )
  995. .off( 'mousedown.DTTT_Select', 'tr' )
  996. .off( 'mouseup.DTTT_Select', 'tr' );
  997. $(that.dom.container).empty();
  998. // Remove the instance
  999. var idx = $.inArray( that, TableTools._aInstances );
  1000. if ( idx !== -1 ) {
  1001. TableTools._aInstances.splice( idx, 1 );
  1002. }
  1003. }
  1004. } );
  1005. },
  1006. /**
  1007. * Take the user defined settings and the default settings and combine them.
  1008. * @method _fnCustomiseSettings
  1009. * @param {Object} oOpts Same as TableTools constructor
  1010. * @returns void
  1011. * @private
  1012. */
  1013. "_fnCustomiseSettings": function ( oOpts )
  1014. {
  1015. /* Is this the master control instance or not? */
  1016. if ( typeof this.s.dt._TableToolsInit == 'undefined' )
  1017. {
  1018. this.s.master = true;
  1019. this.s.dt._TableToolsInit = true;
  1020. }
  1021. /* We can use the table node from comparisons to group controls */
  1022. this.dom.table = this.s.dt.nTable;
  1023. /* Clone the defaults and then the user options */
  1024. this.s.custom = $.extend( {}, TableTools.DEFAULTS, oOpts );
  1025. /* Flash file location */
  1026. this.s.swfPath = this.s.custom.sSwfPath;
  1027. if ( typeof ZeroClipboard_TableTools != 'undefined' )
  1028. {
  1029. ZeroClipboard_TableTools.moviePath = this.s.swfPath;
  1030. }
  1031. /* Table row selecting */
  1032. this.s.select.type = this.s.custom.sRowSelect;
  1033. this.s.select.preRowSelect = this.s.custom.fnPreRowSelect;
  1034. this.s.select.postSelected = this.s.custom.fnRowSelected;
  1035. this.s.select.postDeselected = this.s.custom.fnRowDeselected;
  1036. // Backwards compatibility - allow the user to specify a custom class in the initialiser
  1037. if ( this.s.custom.sSelectedClass )
  1038. {
  1039. this.classes.select.row = this.s.custom.sSelectedClass;
  1040. }
  1041. this.s.tags = this.s.custom.oTags;
  1042. /* Button set */
  1043. this.s.buttonSet = this.s.custom.aButtons;
  1044. },
  1045. /**
  1046. * Take the user input arrays and expand them to be fully defined, and then add them to a given
  1047. * DOM element
  1048. * @method _fnButtonDefinations
  1049. * @param {array} buttonSet Set of user defined buttons
  1050. * @param {node} wrapper Node to add the created buttons to
  1051. * @returns void
  1052. * @private
  1053. */
  1054. "_fnButtonDefinations": function ( buttonSet, wrapper )
  1055. {
  1056. var buttonDef;
  1057. for ( var i=0, iLen=buttonSet.length ; i<iLen ; i++ )
  1058. {
  1059. if ( typeof buttonSet[i] == "string" )
  1060. {
  1061. if ( typeof TableTools.BUTTONS[ buttonSet[i] ] == 'undefined' )
  1062. {
  1063. alert( "TableTools: Warning - unknown button type: "+buttonSet[i] );
  1064. continue;
  1065. }
  1066. buttonDef = $.extend( {}, TableTools.BUTTONS[ buttonSet[i] ], true );
  1067. }
  1068. else
  1069. {
  1070. if ( typeof TableTools.BUTTONS[ buttonSet[i].sExtends ] == 'undefined' )
  1071. {
  1072. alert( "TableTools: Warning - unknown button type: "+buttonSet[i].sExtends );
  1073. continue;
  1074. }
  1075. var o = $.extend( {}, TableTools.BUTTONS[ buttonSet[i].sExtends ], true );
  1076. buttonDef = $.extend( o, buttonSet[i], true );
  1077. }
  1078. var button = this._fnCreateButton(
  1079. buttonDef,
  1080. $(wrapper).hasClass(this.classes.collection.container)
  1081. );
  1082. if ( button ) {
  1083. wrapper.appendChild( button );
  1084. }
  1085. }
  1086. },
  1087. /**
  1088. * Create and configure a TableTools button
  1089. * @method _fnCreateButton
  1090. * @param {Object} oConfig Button configuration object
  1091. * @returns {Node} Button element
  1092. * @private
  1093. */
  1094. "_fnCreateButton": function ( oConfig, bCollectionButton )
  1095. {
  1096. var nButton = this._fnButtonBase( oConfig, bCollectionButton );
  1097. if ( oConfig.sAction.match(/flash/) )
  1098. {
  1099. if ( ! this._fnHasFlash() ) {
  1100. return false;
  1101. }
  1102. this._fnFlashConfig( nButton, oConfig );
  1103. }
  1104. else if ( oConfig.sAction == "text" )
  1105. {
  1106. this._fnTextConfig( nButton, oConfig );
  1107. }
  1108. else if ( oConfig.sAction == "div" )
  1109. {
  1110. this._fnTextConfig( nButton, oConfig );
  1111. }
  1112. else if ( oConfig.sAction == "collection" )
  1113. {
  1114. this._fnTextConfig( nButton, oConfig );
  1115. this._fnCollectionConfig( nButton, oConfig );
  1116. }
  1117. if ( this.s.dt.iTabIndex !== -1 ) {
  1118. $(nButton)
  1119. .attr( 'tabindex', this.s.dt.iTabIndex )
  1120. .attr( 'aria-controls', this.s.dt.sTableId )
  1121. .on( 'keyup.DTTT', function (e) {
  1122. // Trigger the click event on return key when focused.
  1123. // Note that for Flash buttons this has no effect since we
  1124. // can't programmatically trigger the Flash export
  1125. if ( e.keyCode === 13 ) {
  1126. e.stopPropagation();
  1127. $(this).trigger( 'click' );
  1128. }
  1129. } )
  1130. .on( 'mousedown.DTTT', function (e) {
  1131. // On mousedown we want to stop the focus occurring on the
  1132. // button, focus is used only for the keyboard navigation.
  1133. // But using preventDefault for the flash buttons stops the
  1134. // flash action. However, it is not the button that gets
  1135. // focused but the flash element for flash buttons, so this
  1136. // works
  1137. if ( ! oConfig.sAction.match(/flash/) ) {
  1138. e.preventDefault();
  1139. }
  1140. } );
  1141. }
  1142. return nButton;
  1143. },
  1144. /**
  1145. * Create the DOM needed for the button and apply some base properties. All buttons start here
  1146. * @method _fnButtonBase
  1147. * @param {o} oConfig Button configuration object
  1148. * @returns {Node} DIV element for the button
  1149. * @private
  1150. */
  1151. "_fnButtonBase": function ( o, bCollectionButton )
  1152. {
  1153. var sTag, sLiner, sClass;
  1154. if ( bCollectionButton )
  1155. {
  1156. sTag = o.sTag && o.sTag !== "default" ? o.sTag : this.s.tags.collection.button;
  1157. sLiner = o.sLinerTag && o.sLinerTag !== "default" ? o.sLiner : this.s.tags.collection.liner;
  1158. sClass = this.classes.collection.buttons.normal;
  1159. }
  1160. else
  1161. {
  1162. sTag = o.sTag && o.sTag !== "default" ? o.sTag : this.s.tags.button;
  1163. sLiner = o.sLinerTag && o.sLinerTag !== "default" ? o.sLiner : this.s.tags.liner;
  1164. sClass = this.classes.buttons.normal;
  1165. }
  1166. var
  1167. nButton = document.createElement( sTag ),
  1168. nSpan = document.createElement( sLiner ),
  1169. masterS = this._fnGetMasterSettings();
  1170. nButton.className = sClass+" "+o.sButtonClass;
  1171. nButton.setAttribute('id', "ToolTables_"+this.s.dt.sInstance+"_"+masterS.buttonCounter );
  1172. nButton.appendChild( nSpan );
  1173. nSpan.innerHTML = o.sButtonText;
  1174. masterS.buttonCounter++;
  1175. return nButton;
  1176. },
  1177. /**
  1178. * Get the settings object for the master instance. When more than one TableTools instance is
  1179. * assigned to a DataTable, only one of them can be the 'master' (for the select rows). As such,
  1180. * we will typically want to interact with that master for global properties.
  1181. * @method _fnGetMasterSettings
  1182. * @returns {Object} TableTools settings object
  1183. * @private
  1184. */
  1185. "_fnGetMasterSettings": function ()
  1186. {
  1187. if ( this.s.master )
  1188. {
  1189. return this.s;
  1190. }
  1191. else
  1192. {
  1193. /* Look for the master which has the same DT as this one */
  1194. var instances = TableTools._aInstances;
  1195. for ( var i=0, iLen=instances.length ; i<iLen ; i++ )
  1196. {
  1197. if ( this.dom.table == instances[i].s.dt.nTable )
  1198. {
  1199. return instances[i].s;
  1200. }
  1201. }
  1202. }
  1203. },
  1204. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  1205. * Button collection functions
  1206. */
  1207. /**
  1208. * Create a collection button, when activated will present a drop down list of other buttons
  1209. * @param {Node} nButton Button to use for the collection activation
  1210. * @param {Object} oConfig Button configuration object
  1211. * @returns void
  1212. * @private
  1213. */
  1214. "_fnCollectionConfig": function ( nButton, oConfig )
  1215. {
  1216. var nHidden = document.createElement( this.s.tags.collection.container );
  1217. nHidden.style.display = "none";
  1218. nHidden.className = this.classes.collection.container;
  1219. oConfig._collection = nHidden;
  1220. document.body.appendChild( nHidden );
  1221. this._fnButtonDefinations( oConfig.aButtons, nHidden );
  1222. },
  1223. /**
  1224. * Show a button collection
  1225. * @param {Node} nButton Button to use for the collection
  1226. * @param {Object} oConfig Button configuration object
  1227. * @returns void
  1228. * @private
  1229. */
  1230. "_fnCollectionShow": function ( nButton, oConfig )
  1231. {
  1232. var
  1233. that = this,
  1234. oPos = $(nButton).offset(),
  1235. nHidden = oConfig._collection,
  1236. iDivX = oPos.left,
  1237. iDivY = oPos.top + $(nButton).outerHeight(),
  1238. iWinHeight = $(window).height(), iDocHeight = $(document).height(),
  1239. iWinWidth = $(window).width(), iDocWidth = $(document).width();
  1240. nHidden.style.position = "absolute";
  1241. nHidden.style.left = iDivX+"px";
  1242. nHidden.style.top = iDivY+"px";
  1243. nHidden.style.display = "block";
  1244. $(nHidden).css('opacity',0);
  1245. var nBackground = document.createElement('div');
  1246. nBackground.style.position = "absolute";
  1247. nBackground.style.left = "0px";
  1248. nBackground.style.top = "0px";
  1249. nBackground.style.height = ((iWinHeight>iDocHeight)? iWinHeight : iDocHeight) +"px";
  1250. nBackground.style.width = ((iWinWidth>iDocWidth)? iWinWidth : iDocWidth) +"px";
  1251. nBackground.className = this.classes.collection.background;
  1252. $(nBackground).css('opacity',0);
  1253. document.body.appendChild( nBackground );
  1254. document.body.appendChild( nHidden );
  1255. /* Visual corrections to try and keep the collection visible */
  1256. var iDivWidth = $(nHidden).outerWidth();
  1257. var iDivHeight = $(nHidden).outerHeight();
  1258. if ( iDivX + iDivWidth > iDocWidth )
  1259. {
  1260. nHidden.style.left = (iDocWidth-iDivWidth)+"px";
  1261. }
  1262. if ( iDivY + iDivHeight > iDocHeight )
  1263. {
  1264. nHidden.style.top = (iDivY-iDivHeight-$(nButton).outerHeight())+"px";
  1265. }
  1266. this.dom.collection.collection = nHidden;
  1267. this.dom.collection.background = nBackground;
  1268. /* This results in a very small delay for the end user but it allows the animation to be
  1269. * much smoother. If you don't want the animation, then the setTimeout can be removed
  1270. */
  1271. setTimeout( function () {
  1272. $(nHidden).animate({"opacity": 1}, 500);
  1273. $(nBackground).animate({"opacity": 0.25}, 500);
  1274. }, 10 );
  1275. /* Resize the buttons to the Flash contents fit */
  1276. this.fnResizeButtons();
  1277. /* Event handler to remove the collection display */
  1278. $(nBackground).click( function () {
  1279. that._fnCollectionHide.call( that, null, null );
  1280. } );
  1281. },
  1282. /**
  1283. * Hide a button collection
  1284. * @param {Node} nButton Button to use for the collection
  1285. * @param {Object} oConfig Button configuration object
  1286. * @returns void
  1287. * @private
  1288. */
  1289. "_fnCollectionHide": function ( nButton, oConfig )
  1290. {
  1291. if ( oConfig !== null && oConfig.sExtends == 'collection' )
  1292. {
  1293. return;
  1294. }
  1295. if ( this.dom.collection.collection !== null )
  1296. {
  1297. $(this.dom.collection.collection).animate({"opacity": 0}, 500, function (e) {
  1298. this.style.display = "none";
  1299. } );
  1300. $(this.dom.collection.background).animate({"opacity": 0}, 500, function (e) {
  1301. this.parentNode.removeChild( this );
  1302. } );
  1303. this.dom.collection.collection = null;
  1304. this.dom.collection.background = null;
  1305. }
  1306. },
  1307. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  1308. * Row selection functions
  1309. */
  1310. /**
  1311. * Add event handlers to a table to allow for row selection
  1312. * @method _fnRowSelectConfig
  1313. * @returns void
  1314. * @private
  1315. */
  1316. "_fnRowSelectConfig": function ()
  1317. {
  1318. if ( this.s.master )
  1319. {
  1320. var
  1321. that = this,
  1322. i, iLen,
  1323. dt = this.s.dt,
  1324. aoOpenRows = this.s.dt.aoOpenRows;
  1325. $(dt.nTable).addClass( this.classes.select.table );
  1326. // When using OS style selection, we want to cancel the shift text
  1327. // selection, but only when the shift key is used (so you can
  1328. // actually still select text in the table)
  1329. if ( this.s.select.type === 'os' ) {
  1330. $(dt.nTBody).on( 'mousedown.DTTT_Select', 'tr', function(e) {
  1331. if ( e.shiftKey ) {
  1332. $(dt.nTBody)
  1333. .css( '-moz-user-select', 'none' )
  1334. .one('selectstart.DTTT_Select', 'tr', function () {
  1335. return false;
  1336. } );
  1337. }
  1338. } );
  1339. $(dt.nTBody).on( 'mouseup.DTTT_Select', 'tr', function(e) {
  1340. $(dt.nTBody).css( '-moz-user-select', '' );
  1341. } );
  1342. }
  1343. // Row selection
  1344. $(dt.nTBody).on( 'click.DTTT_Select', this.s.custom.sRowSelector, function(e) {
  1345. var row = this.nodeName.toLowerCase() === 'tr' ?
  1346. this :
  1347. $(this).parents('tr')[0];
  1348. var select = that.s.select;
  1349. var pos = that.s.dt.oInstance.fnGetPosition( row );
  1350. /* Sub-table must be ignored (odd that the selector won't do this with >) */
  1351. if ( row.parentNode != dt.nTBody ) {
  1352. return;
  1353. }
  1354. /* Check that we are actually working with a DataTables controlled row */
  1355. if ( dt.oInstance.fnGetData(row) === null ) {
  1356. return;
  1357. }
  1358. // Shift click, ctrl click and simple click handling to make
  1359. // row selection a lot like a file system in desktop OSs
  1360. if ( select.type == 'os' ) {
  1361. if ( e.ctrlKey || e.metaKey ) {
  1362. // Add or remove from the selection
  1363. if ( that.fnIsSelected( row ) ) {
  1364. that._fnRowDeselect( row, e );
  1365. }
  1366. else {
  1367. that._fnRowSelect( row, e );
  1368. }
  1369. }
  1370. else if ( e.shiftKey ) {
  1371. // Add a range of rows, from the last selected row to
  1372. // this one
  1373. var rowIdxs = that.s.dt.aiDisplay.slice(); // visible rows
  1374. var idx1 = $.inArray( select.lastRow, rowIdxs );
  1375. var idx2 = $.inArray( pos, rowIdxs );
  1376. if ( that.fnGetSelected().length === 0 || idx1 === -1 ) {
  1377. // select from top to here - slightly odd, but both
  1378. // Windows and Mac OS do this
  1379. rowIdxs.splice( $.inArray( pos, rowIdxs )+1, rowIdxs.length );
  1380. }
  1381. else {
  1382. // reverse so we can shift click 'up' as well as down
  1383. if ( idx1 > idx2 ) {
  1384. var tmp = idx2;
  1385. idx2 = idx1;
  1386. idx1 = tmp;
  1387. }
  1388. rowIdxs.splice( idx2+1, rowIdxs.length );
  1389. rowIdxs.splice( 0, idx1 );
  1390. }
  1391. if ( ! that.fnIsSelected( row ) ) {
  1392. // Select range
  1393. that._fnRowSelect( rowIdxs, e );
  1394. }
  1395. else {
  1396. // Deselect range - need to keep the clicked on row selected
  1397. rowIdxs.splice( $.inArray( pos, rowIdxs ), 1 );
  1398. that._fnRowDeselect( rowIdxs, e );
  1399. }
  1400. }
  1401. else {
  1402. // No cmd or shift click. Deselect current if selected,
  1403. // or select this row only
  1404. if ( that.fnIsSelected( row ) && that.fnGetSelected().length === 1 ) {
  1405. that._fnRowDeselect( row, e );
  1406. }
  1407. else {
  1408. that.fnSelectNone();
  1409. that._fnRowSelect( row, e );
  1410. }
  1411. }
  1412. }
  1413. else if ( that.fnIsSelected( row ) ) {
  1414. that._fnRowDeselect( row, e );
  1415. }
  1416. else if ( select.type == "single" ) {
  1417. that.fnSelectNone();
  1418. that._fnRowSelect( row, e );
  1419. }
  1420. else if ( select.type == "multi" ) {
  1421. that._fnRowSelect( row, e );
  1422. }
  1423. select.lastRow = pos;
  1424. } );//.on('selectstart', function () { return false; } );
  1425. // Bind a listener to the DataTable for when new rows are created.
  1426. // This allows rows to be visually selected when they should be and
  1427. // deferred rendering is used.
  1428. dt.oApi._fnCallbackReg( dt, 'aoRowCreatedCallback', function (tr, data, index) {
  1429. if ( dt.aoData[index]._DTTT_selected ) {
  1430. $(tr).addClass( that.classes.select.row );
  1431. }
  1432. }, 'TableTools-SelectAll' );
  1433. }
  1434. },
  1435. /**
  1436. * Select rows
  1437. * @param {*} src Rows to select - see _fnSelectData for a description of valid inputs
  1438. * @private
  1439. */
  1440. "_fnRowSelect": function ( src, e )
  1441. {
  1442. var
  1443. that = this,
  1444. data = this._fnSelectData( src ),
  1445. firstTr = data.length===0 ? null : data[0].nTr,
  1446. anSelected = [],
  1447. i, len;
  1448. // Get all the rows that will be selected
  1449. for ( i=0, len=data.length ; i<len ; i++ )
  1450. {
  1451. if ( data[i].nTr )
  1452. {
  1453. anSelected.push( data[i].nTr );
  1454. }
  1455. }
  1456. // User defined pre-selection function
  1457. if ( this.s.select.preRowSelect !== null && !this.s.select.preRowSelect.call(this, e, anSelected, true) )
  1458. {
  1459. return;
  1460. }
  1461. // Mark them as selected
  1462. for ( i=0, len=data.length ; i<len ; i++ )
  1463. {
  1464. data[i]._DTTT_selected = true;
  1465. if ( data[i].nTr )
  1466. {
  1467. $(data[i].nTr).addClass( that.classes.select.row );
  1468. }
  1469. }
  1470. // Post-selection function
  1471. if ( this.s.select.postSelected !== null )
  1472. {
  1473. this.s.select.postSelected.call( this, anSelected );
  1474. }
  1475. TableTools._fnEventDispatch( this, 'select', anSelected, true );
  1476. },
  1477. /**
  1478. * Deselect rows
  1479. * @param {*} src Rows to deselect - see _fnSelectData for a description of valid inputs
  1480. * @private
  1481. */
  1482. "_fnRowDeselect": function ( src, e )
  1483. {
  1484. var
  1485. that = this,
  1486. data = this._fnSelectData( src ),
  1487. firstTr = data.length===0 ? null : data[0].nTr,
  1488. anDeselectedTrs = [],
  1489. i, len;
  1490. // Get all the rows that will be deselected
  1491. for ( i=0, len=data.length ; i<len ; i++ )
  1492. {
  1493. if ( data[i].nTr )
  1494. {
  1495. anDeselectedTrs.push( data[i].nTr );
  1496. }
  1497. }
  1498. // User defined pre-selection function
  1499. if ( this.s.select.preRowSelect !== null && !this.s.select.preRowSelect.call(this, e, anDeselectedTrs, false) )
  1500. {
  1501. return;
  1502. }
  1503. // Mark them as deselected
  1504. for ( i=0, len=data.length ; i<len ; i++ )
  1505. {
  1506. data[i]._DTTT_selected = false;
  1507. if ( data[i].nTr )
  1508. {
  1509. $(data[i].nTr).removeClass( that.classes.select.row );
  1510. }
  1511. }
  1512. // Post-deselection function
  1513. if ( this.s.select.postDeselected !== null )
  1514. {
  1515. this.s.select.postDeselected.call( this, anDeselectedTrs );
  1516. }
  1517. TableTools._fnEventDispatch( this, 'select', anDeselectedTrs, false );
  1518. },
  1519. /**
  1520. * Take a data source for row selection and convert it into aoData points for the DT
  1521. * @param {*} src Can be a single DOM TR node, an array of TR nodes (including a
  1522. * a jQuery object), a single aoData point from DataTables, an array of aoData
  1523. * points or an array of aoData indexes
  1524. * @returns {array} An array of aoData points
  1525. */
  1526. "_fnSelectData": function ( src )
  1527. {
  1528. var out = [], pos, i, iLen;
  1529. if ( src.nodeName )
  1530. {
  1531. // Single node
  1532. pos = this.s.dt.oInstance.fnGetPosition( src );
  1533. out.push( this.s.dt.aoData[pos] );
  1534. }
  1535. else if ( typeof src.length !== 'undefined' )
  1536. {
  1537. // jQuery object or an array of nodes, or aoData points
  1538. for ( i=0, iLen=src.length ; i<iLen ; i++ )
  1539. {
  1540. if ( src[i].nodeName )
  1541. {
  1542. pos = this.s.dt.oInstance.fnGetPosition( src[i] );
  1543. out.push( this.s.dt.aoData[pos] );
  1544. }
  1545. else if ( typeof src[i] === 'number' )
  1546. {
  1547. out.push( this.s.dt.aoData[ src[i] ] );
  1548. }
  1549. else
  1550. {
  1551. out.push( src[i] );
  1552. }
  1553. }
  1554. return out;
  1555. }
  1556. else if ( typeof src === 'number' )
  1557. {
  1558. out.push(this.s.dt.aoData[src]);
  1559. }
  1560. else
  1561. {
  1562. // A single aoData point
  1563. out.push( src );
  1564. }
  1565. return out;
  1566. },
  1567. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  1568. * Text button functions
  1569. */
  1570. /**
  1571. * Configure a text based button for interaction events
  1572. * @method _fnTextConfig
  1573. * @param {Node} nButton Button element which is being considered
  1574. * @param {Object} oConfig Button configuration object
  1575. * @returns void
  1576. * @private
  1577. */
  1578. "_fnTextConfig": function ( nButton, oConfig )
  1579. {
  1580. var that = this;
  1581. if ( oConfig.fnInit !== null )
  1582. {
  1583. oConfig.fnInit.call( this, nButton, oConfig );
  1584. }
  1585. if ( oConfig.sToolTip !== "" )
  1586. {
  1587. nButton.title = oConfig.sToolTip;
  1588. }
  1589. $(nButton).hover( function () {
  1590. if ( oConfig.fnMouseover !== null )
  1591. {
  1592. oConfig.fnMouseover.call( this, nButton, oConfig, null );
  1593. }
  1594. }, function () {
  1595. if ( oConfig.fnMouseout !== null )
  1596. {
  1597. oConfig.fnMouseout.call( this, nButton, oConfig, null );
  1598. }
  1599. } );
  1600. if ( oConfig.fnSelect !== null )
  1601. {
  1602. TableTools._fnEventListen( this, 'select', function (n) {
  1603. oConfig.fnSelect.call( that, nButton, oConfig, n );
  1604. } );
  1605. }
  1606. $(nButton).click( function (e) {
  1607. //e.preventDefault();
  1608. if ( oConfig.fnClick !== null )
  1609. {
  1610. oConfig.fnClick.call( that, nButton, oConfig, null, e );
  1611. }
  1612. /* Provide a complete function to match the behaviour of the flash elements */
  1613. if ( oConfig.fnComplete !== null )
  1614. {
  1615. oConfig.fnComplete.call( that, nButton, oConfig, null, null );
  1616. }
  1617. that._fnCollectionHide( nButton, oConfig );
  1618. } );
  1619. },
  1620. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  1621. * Flash button functions
  1622. */
  1623. /**
  1624. * Check if the Flash plug-in is available
  1625. * @method _fnHasFlash
  1626. * @returns {boolean} `true` if Flash available, `false` otherwise
  1627. * @private
  1628. */
  1629. "_fnHasFlash": function ()
  1630. {
  1631. try {
  1632. var fo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
  1633. if (fo) {
  1634. return true;
  1635. }
  1636. }
  1637. catch (e) {
  1638. if (
  1639. navigator.mimeTypes &&
  1640. navigator.mimeTypes['application/x-shockwave-flash'] !== undefined &&
  1641. navigator.mimeTypes['application/x-shockwave-flash'].enabledPlugin
  1642. ) {
  1643. return true;
  1644. }
  1645. }
  1646. return false;
  1647. },
  1648. /**
  1649. * Configure a flash based button for interaction events
  1650. * @method _fnFlashConfig
  1651. * @param {Node} nButton Button element which is being considered
  1652. * @param {o} oConfig Button configuration object
  1653. * @returns void
  1654. * @private
  1655. */
  1656. "_fnFlashConfig": function ( nButton, oConfig )
  1657. {
  1658. var that = this;
  1659. var flash = new ZeroClipboard_TableTools.Client();
  1660. if ( oConfig.fnInit !== null )
  1661. {
  1662. oConfig.fnInit.call( this, nButton, oConfig );
  1663. }
  1664. flash.setHandCursor( true );
  1665. if ( oConfig.sAction == "flash_save" )
  1666. {
  1667. flash.setAction( 'save' );
  1668. flash.setCharSet( (oConfig.sCharSet=="utf16le") ? 'UTF16LE' : 'UTF8' );
  1669. flash.setBomInc( oConfig.bBomInc );
  1670. flash.setFileName( oConfig.sFileName.replace('*', this.fnGetTitle(oConfig)) );
  1671. }
  1672. else if ( oConfig.sAction == "flash_pdf" )
  1673. {
  1674. flash.setAction( 'pdf' );
  1675. flash.setFileName( oConfig.sFileName.replace('*', this.fnGetTitle(oConfig)) );
  1676. }
  1677. else
  1678. {
  1679. flash.setAction( 'copy' );
  1680. }
  1681. flash.addEventListener('mouseOver', function(client) {
  1682. if ( oConfig.fnMouseover !== null )
  1683. {
  1684. oConfig.fnMouseover.call( that, nButton, oConfig, flash );
  1685. }
  1686. } );
  1687. flash.addEventListener('mouseOut', function(client) {
  1688. if ( oConfig.fnMouseout !== null )
  1689. {
  1690. oConfig.fnMouseout.call( that, nButton, oConfig, flash );
  1691. }
  1692. } );
  1693. flash.addEventListener('mouseDown', function(client) {
  1694. if ( oConfig.fnClick !== null )
  1695. {
  1696. oConfig.fnClick.call( that, nButton, oConfig, flash );
  1697. }
  1698. } );
  1699. flash.addEventListener('complete', function (client, text) {
  1700. if ( oConfig.fnComplete !== null )
  1701. {
  1702. oConfig.fnComplete.call( that, nButton, oConfig, flash, text );
  1703. }
  1704. that._fnCollectionHide( nButton, oConfig );
  1705. } );
  1706. if ( oConfig.fnSelect !== null )
  1707. {
  1708. TableTools._fnEventListen( this, 'select', function (n) {
  1709. oConfig.fnSelect.call( that, nButton, oConfig, n );
  1710. } );
  1711. }
  1712. this._fnFlashGlue( flash, nButton, oConfig.sToolTip );
  1713. },
  1714. /**
  1715. * Wait until the id is in the DOM before we "glue" the swf. Note that this function will call
  1716. * itself (using setTimeout) until it completes successfully
  1717. * @method _fnFlashGlue
  1718. * @param {Object} clip Zero clipboard object
  1719. * @param {Node} node node to glue swf to
  1720. * @param {String} text title of the flash movie
  1721. * @returns void
  1722. * @private
  1723. */
  1724. "_fnFlashGlue": function ( flash, node, text )
  1725. {
  1726. var that = this;
  1727. var id = node.getAttribute('id');
  1728. if ( document.getElementById(id) )
  1729. {
  1730. flash.glue( node, text );
  1731. }
  1732. else
  1733. {
  1734. setTimeout( function () {
  1735. that._fnFlashGlue( flash, node, text );
  1736. }, 100 );
  1737. }
  1738. },
  1739. /**
  1740. * Set the text for the flash clip to deal with
  1741. *
  1742. * This function is required for large information sets. There is a limit on the
  1743. * amount of data that can be transferred between Javascript and Flash in a single call, so
  1744. * we use this method to build up the text in Flash by sending over chunks. It is estimated
  1745. * that the data limit is around 64k, although it is undocumented, and appears to be different
  1746. * between different flash versions. We chunk at 8KiB.
  1747. * @method _fnFlashSetText
  1748. * @param {Object} clip the ZeroClipboard object
  1749. * @param {String} sData the data to be set
  1750. * @returns void
  1751. * @private
  1752. */
  1753. "_fnFlashSetText": function ( clip, sData )
  1754. {
  1755. var asData = this._fnChunkData( sData, 8192 );
  1756. clip.clearText();
  1757. for ( var i=0, iLen=asData.length ; i<iLen ; i++ )
  1758. {
  1759. clip.appendText( asData[i] );
  1760. }
  1761. },
  1762. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  1763. * Data retrieval functions
  1764. */
  1765. /**
  1766. * Convert the mixed columns variable into a boolean array the same size as the columns, which
  1767. * indicates which columns we want to include
  1768. * @method _fnColumnTargets
  1769. * @param {String|Array} mColumns The columns to be included in data retrieval. If a string
  1770. * then it can take the value of "visible" or "hidden" (to include all visible or
  1771. * hidden columns respectively). Or an array of column indexes
  1772. * @returns {Array} A boolean array the length of the columns of the table, which each value
  1773. * indicating if the column is to be included or not
  1774. * @private
  1775. */
  1776. "_fnColumnTargets": function ( mColumns )
  1777. {
  1778. var aColumns = [];
  1779. var dt = this.s.dt;
  1780. var i, iLen;
  1781. var columns = dt.aoColumns;
  1782. var columnCount = columns.length;
  1783. if ( typeof mColumns == "function" )
  1784. {
  1785. var a = mColumns.call( this, dt );
  1786. for ( i=0, iLen=columnCount ; i<iLen ; i++ )
  1787. {
  1788. aColumns.push( $.inArray( i, a ) !== -1 ? true : false );
  1789. }
  1790. }
  1791. else if ( typeof mColumns == "object" )
  1792. {
  1793. for ( i=0, iLen=columnCount ; i<iLen ; i++ )
  1794. {
  1795. aColumns.push( false );
  1796. }
  1797. for ( i=0, iLen=mColumns.length ; i<iLen ; i++ )
  1798. {
  1799. aColumns[ mColumns[i] ] = true;
  1800. }
  1801. }
  1802. else if ( mColumns == "visible" )
  1803. {
  1804. for ( i=0, iLen=columnCount ; i<iLen ; i++ )
  1805. {
  1806. aColumns.push( columns[i].bVisible ? true : false );
  1807. }
  1808. }
  1809. else if ( mColumns == "hidden" )
  1810. {
  1811. for ( i=0, iLen=columnCount ; i<iLen ; i++ )
  1812. {
  1813. aColumns.push( columns[i].bVisible ? false : true );
  1814. }
  1815. }
  1816. else if ( mColumns == "sortable" )
  1817. {
  1818. for ( i=0, iLen=columnCount ; i<iLen ; i++ )
  1819. {
  1820. aColumns.push( columns[i].bSortable ? true : false );
  1821. }
  1822. }
  1823. else /* all */
  1824. {
  1825. for ( i=0, iLen=columnCount ; i<iLen ; i++ )
  1826. {
  1827. aColumns.push( true );
  1828. }
  1829. }
  1830. return aColumns;
  1831. },
  1832. /**
  1833. * New line character(s) depend on the platforms
  1834. * @method method
  1835. * @param {Object} oConfig Button configuration object - only interested in oConfig.sNewLine
  1836. * @returns {String} Newline character
  1837. */
  1838. "_fnNewline": function ( oConfig )
  1839. {
  1840. if ( oConfig.sNewLine == "auto" )
  1841. {
  1842. return navigator.userAgent.match(/Windows/) ? "\r\n" : "\n";
  1843. }
  1844. else
  1845. {
  1846. return oConfig.sNewLine;
  1847. }
  1848. },
  1849. /**
  1850. * Get data from DataTables' internals and format it for output
  1851. * @method _fnGetDataTablesData
  1852. * @param {Object} oConfig Button configuration object
  1853. * @param {String} oConfig.sFieldBoundary Field boundary for the data cells in the string
  1854. * @param {String} oConfig.sFieldSeperator Field separator for the data cells
  1855. * @param {String} oConfig.sNewline New line options
  1856. * @param {Mixed} oConfig.mColumns Which columns should be included in the output
  1857. * @param {Boolean} oConfig.bHeader Include the header
  1858. * @param {Boolean} oConfig.bFooter Include the footer
  1859. * @param {Boolean} oConfig.bSelectedOnly Include only the selected rows in the output
  1860. * @returns {String} Concatenated string of data
  1861. * @private
  1862. */
  1863. "_fnGetDataTablesData": function ( oConfig )
  1864. {
  1865. var i, iLen, j, jLen;
  1866. var aRow, aData=[], sLoopData='', arr;
  1867. var dt = this.s.dt, tr, child;
  1868. var regex = new RegExp(oConfig.sFieldBoundary, "g"); /* Do it here for speed */
  1869. var aColumnsInc = this._fnColumnTargets( oConfig.mColumns );
  1870. var bSelectedOnly = (typeof oConfig.bSelectedOnly != 'undefined') ? oConfig.bSelectedOnly : false;
  1871. /*
  1872. * Header
  1873. */
  1874. if ( oConfig.bHeader )
  1875. {
  1876. aRow = [];
  1877. for ( i=0, iLen=dt.aoColumns.length ; i<iLen ; i++ )
  1878. {
  1879. if ( aColumnsInc[i] )
  1880. {
  1881. sLoopData = dt.aoColumns[i].sTitle.replace(/\n/g," ").replace( /<.*?>/g, "" ).replace(/^\s+|\s+$/g,"");
  1882. sLoopData = this._fnHtmlDecode( sLoopData );
  1883. aRow.push( this._fnBoundData( sLoopData, oConfig.sFieldBoundary, regex ) );
  1884. }
  1885. }
  1886. aData.push( aRow.join(oConfig.sFieldSeperator) );
  1887. }
  1888. bSelectedOnly = true;
  1889. /*
  1890. * Body
  1891. */
  1892. var aDataIndex;
  1893. var aSelected = this.fnGetSelectedIndexes();
  1894. bSelectedOnly = this.s.select.type !== "none" && bSelectedOnly && aSelected.length !== 0;
  1895. if ( bSelectedOnly ) {
  1896. // Use the selected indexes
  1897. aDataIndex = aSelected;
  1898. }
  1899. else if ( DataTable.Api ) {
  1900. // 1.10+ style
  1901. aDataIndex = new DataTable.Api( dt )
  1902. .rows( oConfig.oSelectorOpts )
  1903. .indexes()
  1904. .flatten()
  1905. .toArray();
  1906. }
  1907. else {
  1908. // 1.9- style
  1909. aDataIndex = dt.oInstance
  1910. .$('tr', oConfig.oSelectorOpts)
  1911. .map( function (id, row) {
  1912. return dt.oInstance.fnGetPosition( row );
  1913. } )
  1914. .get();
  1915. }
  1916. for ( j=0, jLen=aDataIndex.length ; j<jLen ; j++ )
  1917. {
  1918. tr = dt.aoData[ aDataIndex[j] ].nTr;
  1919. aRow = [];
  1920. /* Columns */
  1921. for ( i=0, iLen=dt.aoColumns.length ; i<iLen ; i++ )
  1922. {
  1923. if ( aColumnsInc[i] )
  1924. {
  1925. /* Convert to strings (with small optimisation) */
  1926. var mTypeData = dt.oApi._fnGetCellData( dt, aDataIndex[j], i, 'display' );
  1927. if ( oConfig.fnCellRender )
  1928. {
  1929. sLoopData = oConfig.fnCellRender( mTypeData, i, tr, aDataIndex[j] )+"";
  1930. }
  1931. else if ( typeof mTypeData == "string" )
  1932. {
  1933. /* Strip newlines, replace img tags with alt attr. and finally strip html... */
  1934. sLoopData = mTypeData.replace(/\n/g," ");
  1935. sLoopData =
  1936. sLoopData.replace(/<img.*?\s+alt\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s>]+)).*?>/gi,
  1937. '$1$2$3');
  1938. sLoopData = sLoopData.replace( /<.*?>/g, "" );
  1939. }
  1940. else
  1941. {
  1942. sLoopData = mTypeData+"";
  1943. }
  1944. /* Trim and clean the data */
  1945. sLoopData = sLoopData.replace(/^\s+/, '').replace(/\s+$/, '');
  1946. sLoopData = this._fnHtmlDecode( sLoopData );
  1947. /* Bound it and add it to the total data */
  1948. aRow.push( this._fnBoundData( sLoopData, oConfig.sFieldBoundary, regex ) );
  1949. }
  1950. }
  1951. aData.push( aRow.join(oConfig.sFieldSeperator) );
  1952. /* Details rows from fnOpen */
  1953. if ( oConfig.bOpenRows )
  1954. {
  1955. arr = $.grep(dt.aoOpenRows, function(o) { return o.nParent === tr; });
  1956. if ( arr.length === 1 )
  1957. {
  1958. sLoopData = this._fnBoundData( $('td', arr[0].nTr).html(), oConfig.sFieldBoundary, regex );
  1959. aData.push( sLoopData );
  1960. }
  1961. }
  1962. }
  1963. /*
  1964. * Footer
  1965. */
  1966. if ( oConfig.bFooter && dt.nTFoot !== null )
  1967. {
  1968. aRow = [];
  1969. for ( i=0, iLen=dt.aoColumns.length ; i<iLen ; i++ )
  1970. {
  1971. if ( aColumnsInc[i] && dt.aoColumns[i].nTf !== null )
  1972. {
  1973. sLoopData = dt.aoColumns[i].nTf.innerHTML.replace(/\n/g," ").replace( /<.*?>/g, "" );
  1974. sLoopData = this._fnHtmlDecode( sLoopData );
  1975. aRow.push( this._fnBoundData( sLoopData, oConfig.sFieldBoundary, regex ) );
  1976. }
  1977. }
  1978. aData.push( aRow.join(oConfig.sFieldSeperator) );
  1979. }
  1980. var _sLastData = aData.join( this._fnNewline(oConfig) );
  1981. return _sLastData;
  1982. },
  1983. /**
  1984. * Wrap data up with a boundary string
  1985. * @method _fnBoundData
  1986. * @param {String} sData data to bound
  1987. * @param {String} sBoundary bounding char(s)
  1988. * @param {RegExp} regex search for the bounding chars - constructed outside for efficiency
  1989. * in the loop
  1990. * @returns {String} bound data
  1991. * @private
  1992. */
  1993. "_fnBoundData": function ( sData, sBoundary, regex )
  1994. {
  1995. if ( sBoundary === "" )
  1996. {
  1997. return sData;
  1998. }
  1999. else
  2000. {
  2001. return sBoundary + sData.replace(regex, sBoundary+sBoundary) + sBoundary;
  2002. }
  2003. },
  2004. /**
  2005. * Break a string up into an array of smaller strings
  2006. * @method _fnChunkData
  2007. * @param {String} sData data to be broken up
  2008. * @param {Int} iSize chunk size
  2009. * @returns {Array} String array of broken up text
  2010. * @private
  2011. */
  2012. "_fnChunkData": function ( sData, iSize )
  2013. {
  2014. var asReturn = [];
  2015. var iStrlen = sData.length;
  2016. for ( var i=0 ; i<iStrlen ; i+=iSize )
  2017. {
  2018. if ( i+iSize < iStrlen )
  2019. {
  2020. asReturn.push( sData.substring( i, i+iSize ) );
  2021. }
  2022. else
  2023. {
  2024. asReturn.push( sData.substring( i, iStrlen ) );
  2025. }
  2026. }
  2027. return asReturn;
  2028. },
  2029. /**
  2030. * Decode HTML entities
  2031. * @method _fnHtmlDecode
  2032. * @param {String} sData encoded string
  2033. * @returns {String} decoded string
  2034. * @private
  2035. */
  2036. "_fnHtmlDecode": function ( sData )
  2037. {
  2038. if ( sData.indexOf('&') === -1 )
  2039. {
  2040. return sData;
  2041. }
  2042. var n = document.createElement('div');
  2043. return sData.replace( /&([^\s]*?);/g, function( match, match2 ) {
  2044. if ( match.substr(1, 1) === '#' )
  2045. {
  2046. return String.fromCharCode( Number(match2.substr(1)) );
  2047. }
  2048. else
  2049. {
  2050. n.innerHTML = match;
  2051. return n.childNodes[0].nodeValue;
  2052. }
  2053. } );
  2054. },
  2055. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2056. * Printing functions
  2057. */
  2058. /**
  2059. * Show print display
  2060. * @method _fnPrintStart
  2061. * @param {Event} e Event object
  2062. * @param {Object} oConfig Button configuration object
  2063. * @returns void
  2064. * @private
  2065. */
  2066. "_fnPrintStart": function ( oConfig )
  2067. {
  2068. var that = this;
  2069. var oSetDT = this.s.dt;
  2070. /* Parse through the DOM hiding everything that isn't needed for the table */
  2071. this._fnPrintHideNodes( oSetDT.nTable );
  2072. /* Show the whole table */
  2073. this.s.print.saveStart = oSetDT._iDisplayStart;
  2074. this.s.print.saveLength = oSetDT._iDisplayLength;
  2075. if ( oConfig.bShowAll )
  2076. {
  2077. oSetDT._iDisplayStart = 0;
  2078. oSetDT._iDisplayLength = -1;
  2079. if ( oSetDT.oApi._fnCalculateEnd ) {
  2080. oSetDT.oApi._fnCalculateEnd( oSetDT );
  2081. }
  2082. oSetDT.oApi._fnDraw( oSetDT );
  2083. }
  2084. /* Adjust the display for scrolling which might be done by DataTables */
  2085. if ( oSetDT.oScroll.sX !== "" || oSetDT.oScroll.sY !== "" )
  2086. {
  2087. this._fnPrintScrollStart( oSetDT );
  2088. // If the table redraws while in print view, the DataTables scrolling
  2089. // setup would hide the header, so we need to readd it on draw
  2090. $(this.s.dt.nTable).bind('draw.DTTT_Print', function () {
  2091. that._fnPrintScrollStart( oSetDT );
  2092. } );
  2093. }
  2094. /* Remove the other DataTables feature nodes - but leave the table! and info div */
  2095. var anFeature = oSetDT.aanFeatures;
  2096. for ( var cFeature in anFeature )
  2097. {
  2098. if ( cFeature != 'i' && cFeature != 't' && cFeature.length == 1 )
  2099. {
  2100. for ( var i=0, iLen=anFeature[cFeature].length ; i<iLen ; i++ )
  2101. {
  2102. this.dom.print.hidden.push( {
  2103. "node": anFeature[cFeature][i],
  2104. "display": "block"
  2105. } );
  2106. anFeature[cFeature][i].style.display = "none";
  2107. }
  2108. }
  2109. }
  2110. /* Print class can be used for styling */
  2111. $(document.body).addClass( this.classes.print.body );
  2112. /* Show information message to let the user know what is happening */
  2113. if ( oConfig.sInfo !== "" )
  2114. {
  2115. this.fnInfo( oConfig.sInfo, 3000 );
  2116. }
  2117. /* Add a message at the top of the page */
  2118. if ( oConfig.sMessage )
  2119. {
  2120. $('<div/>')
  2121. .addClass( this.classes.print.message )
  2122. .html( oConfig.sMessage )
  2123. .prependTo( 'body' );
  2124. }
  2125. /* Cache the scrolling and the jump to the top of the page */
  2126. this.s.print.saveScroll = $(window).scrollTop();
  2127. window.scrollTo( 0, 0 );
  2128. /* Bind a key event listener to the document for the escape key -
  2129. * it is removed in the callback
  2130. */
  2131. $(document).bind( "keydown.DTTT", function(e) {
  2132. /* Only interested in the escape key */
  2133. if ( e.keyCode == 27 )
  2134. {
  2135. e.preventDefault();
  2136. that._fnPrintEnd.call( that, e );
  2137. }
  2138. } );
  2139. },
  2140. /**
  2141. * Printing is finished, resume normal display
  2142. * @method _fnPrintEnd
  2143. * @param {Event} e Event object
  2144. * @returns void
  2145. * @private
  2146. */
  2147. "_fnPrintEnd": function ( e )
  2148. {
  2149. var that = this;
  2150. var oSetDT = this.s.dt;
  2151. var oSetPrint = this.s.print;
  2152. var oDomPrint = this.dom.print;
  2153. /* Show all hidden nodes */
  2154. this._fnPrintShowNodes();
  2155. /* Restore DataTables' scrolling */
  2156. if ( oSetDT.oScroll.sX !== "" || oSetDT.oScroll.sY !== "" )
  2157. {
  2158. $(this.s.dt.nTable).unbind('draw.DTTT_Print');
  2159. this._fnPrintScrollEnd();
  2160. }
  2161. /* Restore the scroll */
  2162. window.scrollTo( 0, oSetPrint.saveScroll );
  2163. /* Drop the print message */
  2164. $('div.'+this.classes.print.message).remove();
  2165. /* Styling class */
  2166. $(document.body).removeClass( 'DTTT_Print' );
  2167. /* Restore the table length */
  2168. oSetDT._iDisplayStart = oSetPrint.saveStart;
  2169. oSetDT._iDisplayLength = oSetPrint.saveLength;
  2170. if ( oSetDT.oApi._fnCalculateEnd ) {
  2171. oSetDT.oApi._fnCalculateEnd( oSetDT );
  2172. }
  2173. oSetDT.oApi._fnDraw( oSetDT );
  2174. $(document).unbind( "keydown.DTTT" );
  2175. },
  2176. /**
  2177. * Take account of scrolling in DataTables by showing the full table
  2178. * @returns void
  2179. * @private
  2180. */
  2181. "_fnPrintScrollStart": function ()
  2182. {
  2183. var
  2184. oSetDT = this.s.dt,
  2185. nScrollHeadInner = oSetDT.nScrollHead.getElementsByTagName('div')[0],
  2186. nScrollHeadTable = nScrollHeadInner.getElementsByTagName('table')[0],
  2187. nScrollBody = oSetDT.nTable.parentNode,
  2188. nTheadSize, nTfootSize;
  2189. /* Copy the header in the thead in the body table, this way we show one single table when
  2190. * in print view. Note that this section of code is more or less verbatim from DT 1.7.0
  2191. */
  2192. nTheadSize = oSetDT.nTable.getElementsByTagName('thead');
  2193. if ( nTheadSize.length > 0 )
  2194. {
  2195. oSetDT.nTable.removeChild( nTheadSize[0] );
  2196. }
  2197. if ( oSetDT.nTFoot !== null )
  2198. {
  2199. nTfootSize = oSetDT.nTable.getElementsByTagName('tfoot');
  2200. if ( nTfootSize.length > 0 )
  2201. {
  2202. oSetDT.nTable.removeChild( nTfootSize[0] );
  2203. }
  2204. }
  2205. nTheadSize = oSetDT.nTHead.cloneNode(true);
  2206. oSetDT.nTable.insertBefore( nTheadSize, oSetDT.nTable.childNodes[0] );
  2207. if ( oSetDT.nTFoot !== null )
  2208. {
  2209. nTfootSize = oSetDT.nTFoot.cloneNode(true);
  2210. oSetDT.nTable.insertBefore( nTfootSize, oSetDT.nTable.childNodes[1] );
  2211. }
  2212. /* Now adjust the table's viewport so we can actually see it */
  2213. if ( oSetDT.oScroll.sX !== "" )
  2214. {
  2215. oSetDT.nTable.style.width = $(oSetDT.nTable).outerWidth()+"px";
  2216. nScrollBody.style.width = $(oSetDT.nTable).outerWidth()+"px";
  2217. nScrollBody.style.overflow = "visible";
  2218. }
  2219. if ( oSetDT.oScroll.sY !== "" )
  2220. {
  2221. nScrollBody.style.height = $(oSetDT.nTable).outerHeight()+"px";
  2222. nScrollBody.style.overflow = "visible";
  2223. }
  2224. },
  2225. /**
  2226. * Take account of scrolling in DataTables by showing the full table. Note that the redraw of
  2227. * the DataTable that we do will actually deal with the majority of the hard work here
  2228. * @returns void
  2229. * @private
  2230. */
  2231. "_fnPrintScrollEnd": function ()
  2232. {
  2233. var
  2234. oSetDT = this.s.dt,
  2235. nScrollBody = oSetDT.nTable.parentNode;
  2236. if ( oSetDT.oScroll.sX !== "" )
  2237. {
  2238. nScrollBody.style.width = oSetDT.oApi._fnStringToCss( oSetDT.oScroll.sX );
  2239. nScrollBody.style.overflow = "auto";
  2240. }
  2241. if ( oSetDT.oScroll.sY !== "" )
  2242. {
  2243. nScrollBody.style.height = oSetDT.oApi._fnStringToCss( oSetDT.oScroll.sY );
  2244. nScrollBody.style.overflow = "auto";
  2245. }
  2246. },
  2247. /**
  2248. * Resume the display of all TableTools hidden nodes
  2249. * @method _fnPrintShowNodes
  2250. * @returns void
  2251. * @private
  2252. */
  2253. "_fnPrintShowNodes": function ( )
  2254. {
  2255. var anHidden = this.dom.print.hidden;
  2256. for ( var i=0, iLen=anHidden.length ; i<iLen ; i++ )
  2257. {
  2258. anHidden[i].node.style.display = anHidden[i].display;
  2259. }
  2260. anHidden.splice( 0, anHidden.length );
  2261. },
  2262. /**
  2263. * Hide nodes which are not needed in order to display the table. Note that this function is
  2264. * recursive
  2265. * @method _fnPrintHideNodes
  2266. * @param {Node} nNode Element which should be showing in a 'print' display
  2267. * @returns void
  2268. * @private
  2269. */
  2270. "_fnPrintHideNodes": function ( nNode )
  2271. {
  2272. var anHidden = this.dom.print.hidden;
  2273. var nParent = nNode.parentNode;
  2274. var nChildren = nParent.childNodes;
  2275. for ( var i=0, iLen=nChildren.length ; i<iLen ; i++ )
  2276. {
  2277. if ( nChildren[i] != nNode && nChildren[i].nodeType == 1 )
  2278. {
  2279. /* If our node is shown (don't want to show nodes which were previously hidden) */
  2280. var sDisplay = $(nChildren[i]).css("display");
  2281. if ( sDisplay != "none" )
  2282. {
  2283. /* Cache the node and it's previous state so we can restore it */
  2284. anHidden.push( {
  2285. "node": nChildren[i],
  2286. "display": sDisplay
  2287. } );
  2288. nChildren[i].style.display = "none";
  2289. }
  2290. }
  2291. }
  2292. if ( nParent.nodeName.toUpperCase() != "BODY" )
  2293. {
  2294. this._fnPrintHideNodes( nParent );
  2295. }
  2296. }
  2297. };
  2298. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2299. * Static variables
  2300. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  2301. /**
  2302. * Store of all instances that have been created of TableTools, so one can look up other (when
  2303. * there is need of a master)
  2304. * @property _aInstances
  2305. * @type Array
  2306. * @default []
  2307. * @private
  2308. */
  2309. TableTools._aInstances = [];
  2310. /**
  2311. * Store of all listeners and their callback functions
  2312. * @property _aListeners
  2313. * @type Array
  2314. * @default []
  2315. */
  2316. TableTools._aListeners = [];
  2317. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2318. * Static methods
  2319. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  2320. /**
  2321. * Get an array of all the master instances
  2322. * @method fnGetMasters
  2323. * @returns {Array} List of master TableTools instances
  2324. * @static
  2325. */
  2326. TableTools.fnGetMasters = function ()
  2327. {
  2328. var a = [];
  2329. for ( var i=0, iLen=TableTools._aInstances.length ; i<iLen ; i++ )
  2330. {
  2331. if ( TableTools._aInstances[i].s.master )
  2332. {
  2333. a.push( TableTools._aInstances[i] );
  2334. }
  2335. }
  2336. return a;
  2337. };
  2338. /**
  2339. * Get the master instance for a table node (or id if a string is given)
  2340. * @method fnGetInstance
  2341. * @returns {Object} ID of table OR table node, for which we want the TableTools instance
  2342. * @static
  2343. */
  2344. TableTools.fnGetInstance = function ( node )
  2345. {
  2346. if ( typeof node != 'object' )
  2347. {
  2348. node = document.getElementById(node);
  2349. }
  2350. for ( var i=0, iLen=TableTools._aInstances.length ; i<iLen ; i++ )
  2351. {
  2352. if ( TableTools._aInstances[i].s.master && TableTools._aInstances[i].dom.table == node )
  2353. {
  2354. return TableTools._aInstances[i];
  2355. }
  2356. }
  2357. return null;
  2358. };
  2359. /**
  2360. * Add a listener for a specific event
  2361. * @method _fnEventListen
  2362. * @param {Object} that Scope of the listening function (i.e. 'this' in the caller)
  2363. * @param {String} type Event type
  2364. * @param {Function} fn Function
  2365. * @returns void
  2366. * @private
  2367. * @static
  2368. */
  2369. TableTools._fnEventListen = function ( that, type, fn )
  2370. {
  2371. TableTools._aListeners.push( {
  2372. "that": that,
  2373. "type": type,
  2374. "fn": fn
  2375. } );
  2376. };
  2377. /**
  2378. * An event has occurred - look up every listener and fire it off. We check that the event we are
  2379. * going to fire is attached to the same table (using the table node as reference) before firing
  2380. * @method _fnEventDispatch
  2381. * @param {Object} that Scope of the listening function (i.e. 'this' in the caller)
  2382. * @param {String} type Event type
  2383. * @param {Node} node Element that the event occurred on (may be null)
  2384. * @param {boolean} [selected] Indicate if the node was selected (true) or deselected (false)
  2385. * @returns void
  2386. * @private
  2387. * @static
  2388. */
  2389. TableTools._fnEventDispatch = function ( that, type, node, selected )
  2390. {
  2391. var listeners = TableTools._aListeners;
  2392. for ( var i=0, iLen=listeners.length ; i<iLen ; i++ )
  2393. {
  2394. if ( that.dom.table == listeners[i].that.dom.table && listeners[i].type == type )
  2395. {
  2396. listeners[i].fn( node, selected );
  2397. }
  2398. }
  2399. };
  2400. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2401. * Constants
  2402. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  2403. TableTools.buttonBase = {
  2404. // Button base
  2405. "sAction": "text",
  2406. "sTag": "default",
  2407. "sLinerTag": "default",
  2408. "sButtonClass": "DTTT_button_text",
  2409. "sButtonText": "Button text",
  2410. "sTitle": "",
  2411. "sToolTip": "",
  2412. // Common button specific options
  2413. "sCharSet": "utf8",
  2414. "bBomInc": false,
  2415. "sFileName": "*.csv",
  2416. "sFieldBoundary": "",
  2417. "sFieldSeperator": "\t",
  2418. "sNewLine": "auto",
  2419. "mColumns": "all", /* "all", "visible", "hidden" or array of column integers */
  2420. "bHeader": true,
  2421. "bFooter": true,
  2422. "bOpenRows": false,
  2423. "bSelectedOnly": false,
  2424. "oSelectorOpts": undefined, // See http://datatables.net/docs/DataTables/1.9.4/#$ for full options
  2425. // Callbacks
  2426. "fnMouseover": null,
  2427. "fnMouseout": null,
  2428. "fnClick": null,
  2429. "fnSelect": null,
  2430. "fnComplete": null,
  2431. "fnInit": null,
  2432. "fnCellRender": null
  2433. };
  2434. /**
  2435. * @namespace Default button configurations
  2436. */
  2437. TableTools.BUTTONS = {
  2438. "csv": $.extend( {}, TableTools.buttonBase, {
  2439. "sAction": "flash_save",
  2440. "sButtonClass": "DTTT_button_csv",
  2441. "sButtonText": "CSV",
  2442. "sFieldBoundary": '"',
  2443. "sFieldSeperator": ",",
  2444. "fnClick": function( nButton, oConfig, flash ) {
  2445. this.fnSetText( flash, this.fnGetTableData(oConfig) );
  2446. }
  2447. } ),
  2448. "xls": $.extend( {}, TableTools.buttonBase, {
  2449. "sAction": "flash_save",
  2450. "sCharSet": "utf16le",
  2451. "bBomInc": true,
  2452. "sButtonClass": "DTTT_button_xls",
  2453. "sButtonText": "Excel",
  2454. "fnClick": function( nButton, oConfig, flash ) {
  2455. this.fnSetText( flash, this.fnGetTableData(oConfig) );
  2456. }
  2457. } ),
  2458. "copy": $.extend( {}, TableTools.buttonBase, {
  2459. "sAction": "flash_copy",
  2460. "sButtonClass": "DTTT_button_copy",
  2461. "sButtonText": "Copy",
  2462. "fnClick": function( nButton, oConfig, flash ) {
  2463. this.fnSetText( flash, this.fnGetTableData(oConfig) );
  2464. },
  2465. "fnComplete": function(nButton, oConfig, flash, text) {
  2466. var lines = text.split('\n').length;
  2467. if (oConfig.bHeader) lines--;
  2468. if (this.s.dt.nTFoot !== null && oConfig.bFooter) lines--;
  2469. var plural = (lines==1) ? "" : "s";
  2470. this.fnInfo( '<h6>Table copied</h6>'+
  2471. '<p>Copied '+lines+' row'+plural+' to the clipboard.</p>',
  2472. 1500
  2473. );
  2474. }
  2475. } ),
  2476. "pdf": $.extend( {}, TableTools.buttonBase, {
  2477. "sAction": "flash_pdf",
  2478. "sNewLine": "\n",
  2479. "sFileName": "*.pdf",
  2480. "sButtonClass": "DTTT_button_pdf",
  2481. "sButtonText": "PDF",
  2482. "sPdfOrientation": "portrait",
  2483. "sPdfSize": "A4",
  2484. "sPdfMessage": "",
  2485. "fnClick": function( nButton, oConfig, flash ) {
  2486. this.fnSetText( flash,
  2487. "title:"+ this.fnGetTitle(oConfig) +"\n"+
  2488. "message:"+ oConfig.sPdfMessage +"\n"+
  2489. "colWidth:"+ this.fnCalcColRatios(oConfig) +"\n"+
  2490. "orientation:"+ oConfig.sPdfOrientation +"\n"+
  2491. "size:"+ oConfig.sPdfSize +"\n"+
  2492. "--/TableToolsOpts--\n" +
  2493. this.fnGetTableData(oConfig)
  2494. );
  2495. }
  2496. } ),
  2497. "print": $.extend( {}, TableTools.buttonBase, {
  2498. "sInfo": "<h6>Print view</h6><p>Please use your browser's print function to "+
  2499. "print this table. Press escape when finished.</p>",
  2500. "sMessage": null,
  2501. "bShowAll": true,
  2502. "sToolTip": "View print view",
  2503. "sButtonClass": "DTTT_button_print",
  2504. "sButtonText": "Print",
  2505. "fnClick": function ( nButton, oConfig ) {
  2506. this.fnPrint( true, oConfig );
  2507. }
  2508. } ),
  2509. "text": $.extend( {}, TableTools.buttonBase ),
  2510. "select": $.extend( {}, TableTools.buttonBase, {
  2511. "sButtonText": "Select button",
  2512. "fnSelect": function( nButton, oConfig ) {
  2513. if ( this.fnGetSelected().length !== 0 ) {
  2514. $(nButton).removeClass( this.classes.buttons.disabled );
  2515. } else {
  2516. $(nButton).addClass( this.classes.buttons.disabled );
  2517. }
  2518. },
  2519. "fnInit": function( nButton, oConfig ) {
  2520. $(nButton).addClass( this.classes.buttons.disabled );
  2521. }
  2522. } ),
  2523. "select_single": $.extend( {}, TableTools.buttonBase, {
  2524. "sButtonText": "Select button",
  2525. "fnSelect": function( nButton, oConfig ) {
  2526. var iSelected = this.fnGetSelected().length;
  2527. if ( iSelected == 1 ) {
  2528. $(nButton).removeClass( this.classes.buttons.disabled );
  2529. } else {
  2530. $(nButton).addClass( this.classes.buttons.disabled );
  2531. }
  2532. },
  2533. "fnInit": function( nButton, oConfig ) {
  2534. $(nButton).addClass( this.classes.buttons.disabled );
  2535. }
  2536. } ),
  2537. "select_all": $.extend( {}, TableTools.buttonBase, {
  2538. "sButtonText": "Select all",
  2539. "fnClick": function( nButton, oConfig ) {
  2540. this.fnSelectAll();
  2541. },
  2542. "fnSelect": function( nButton, oConfig ) {
  2543. if ( this.fnGetSelected().length == this.s.dt.fnRecordsDisplay() ) {
  2544. $(nButton).addClass( this.classes.buttons.disabled );
  2545. } else {
  2546. $(nButton).removeClass( this.classes.buttons.disabled );
  2547. }
  2548. }
  2549. } ),
  2550. "select_none": $.extend( {}, TableTools.buttonBase, {
  2551. "sButtonText": "Deselect all",
  2552. "fnClick": function( nButton, oConfig ) {
  2553. this.fnSelectNone();
  2554. },
  2555. "fnSelect": function( nButton, oConfig ) {
  2556. if ( this.fnGetSelected().length !== 0 ) {
  2557. $(nButton).removeClass( this.classes.buttons.disabled );
  2558. } else {
  2559. $(nButton).addClass( this.classes.buttons.disabled );
  2560. }
  2561. },
  2562. "fnInit": function( nButton, oConfig ) {
  2563. $(nButton).addClass( this.classes.buttons.disabled );
  2564. }
  2565. } ),
  2566. "ajax": $.extend( {}, TableTools.buttonBase, {
  2567. "sAjaxUrl": "/xhr.php",
  2568. "sButtonText": "Ajax button",
  2569. "fnClick": function( nButton, oConfig ) {
  2570. var sData = this.fnGetTableData(oConfig);
  2571. $.ajax( {
  2572. "url": oConfig.sAjaxUrl,
  2573. "data": [
  2574. { "name": "tableData", "value": sData }
  2575. ],
  2576. "success": oConfig.fnAjaxComplete,
  2577. "dataType": "json",
  2578. "type": "POST",
  2579. "cache": false,
  2580. "error": function () {
  2581. alert( "Error detected when sending table data to server" );
  2582. }
  2583. } );
  2584. },
  2585. "fnAjaxComplete": function( json ) {
  2586. alert( 'Ajax complete' );
  2587. }
  2588. } ),
  2589. "div": $.extend( {}, TableTools.buttonBase, {
  2590. "sAction": "div",
  2591. "sTag": "div",
  2592. "sButtonClass": "DTTT_nonbutton",
  2593. "sButtonText": "Text button"
  2594. } ),
  2595. "collection": $.extend( {}, TableTools.buttonBase, {
  2596. "sAction": "collection",
  2597. "sButtonClass": "DTTT_button_collection",
  2598. "sButtonText": "Collection",
  2599. "fnClick": function( nButton, oConfig ) {
  2600. this._fnCollectionShow(nButton, oConfig);
  2601. }
  2602. } )
  2603. };
  2604. /*
  2605. * on* callback parameters:
  2606. * 1. node - button element
  2607. * 2. object - configuration object for this button
  2608. * 3. object - ZeroClipboard reference (flash button only)
  2609. * 4. string - Returned string from Flash (flash button only - and only on 'complete')
  2610. */
  2611. // Alias to match the other plug-ins styling
  2612. TableTools.buttons = TableTools.BUTTONS;
  2613. /**
  2614. * @namespace Classes used by TableTools - allows the styles to be override easily.
  2615. * Note that when TableTools initialises it will take a copy of the classes object
  2616. * and will use its internal copy for the remainder of its run time.
  2617. */
  2618. TableTools.classes = {
  2619. "container": "DTTT_container",
  2620. "buttons": {
  2621. "normal": "DTTT_button",
  2622. "disabled": "DTTT_disabled"
  2623. },
  2624. "collection": {
  2625. "container": "DTTT_collection",
  2626. "background": "DTTT_collection_background",
  2627. "buttons": {
  2628. "normal": "DTTT_button",
  2629. "disabled": "DTTT_disabled"
  2630. }
  2631. },
  2632. "select": {
  2633. "table": "DTTT_selectable",
  2634. "row": "DTTT_selected selected"
  2635. },
  2636. "print": {
  2637. "body": "DTTT_Print",
  2638. "info": "DTTT_print_info",
  2639. "message": "DTTT_PrintMessage"
  2640. }
  2641. };
  2642. /**
  2643. * @namespace ThemeRoller classes - built in for compatibility with DataTables'
  2644. * bJQueryUI option.
  2645. */
  2646. TableTools.classes_themeroller = {
  2647. "container": "DTTT_container ui-buttonset ui-buttonset-multi",
  2648. "buttons": {
  2649. "normal": "DTTT_button ui-button ui-state-default"
  2650. },
  2651. "collection": {
  2652. "container": "DTTT_collection ui-buttonset ui-buttonset-multi"
  2653. }
  2654. };
  2655. /**
  2656. * @namespace TableTools default settings for initialisation
  2657. */
  2658. TableTools.DEFAULTS = {
  2659. "sSwfPath": "../swf/copy_csv_xls_pdf.swf",
  2660. "sRowSelect": "none",
  2661. "sRowSelector": "tr",
  2662. "sSelectedClass": null,
  2663. "fnPreRowSelect": null,
  2664. "fnRowSelected": null,
  2665. "fnRowDeselected": null,
  2666. "aButtons": [ "copy", "csv", "xls", "pdf", "print" ],
  2667. "oTags": {
  2668. "container": "div",
  2669. "button": "a", // We really want to use buttons here, but Firefox and IE ignore the
  2670. // click on the Flash element in the button (but not mouse[in|out]).
  2671. "liner": "span",
  2672. "collection": {
  2673. "container": "div",
  2674. "button": "a",
  2675. "liner": "span"
  2676. }
  2677. }
  2678. };
  2679. // Alias to match the other plug-ins
  2680. TableTools.defaults = TableTools.DEFAULTS;
  2681. /**
  2682. * Name of this class
  2683. * @constant CLASS
  2684. * @type String
  2685. * @default TableTools
  2686. */
  2687. TableTools.prototype.CLASS = "TableTools";
  2688. /**
  2689. * TableTools version
  2690. * @constant VERSION
  2691. * @type String
  2692. * @default See code
  2693. */
  2694. TableTools.version = "2.2.4";
  2695. // DataTables 1.10 API
  2696. //
  2697. // This will be extended in a big way in in TableTools 3 to provide API methods
  2698. // such as rows().select() and rows.selected() etc, but for the moment the
  2699. // tabletools() method simply returns the instance.
  2700. if ( $.fn.dataTable.Api ) {
  2701. $.fn.dataTable.Api.register( 'tabletools()', function () {
  2702. var tt = null;
  2703. if ( this.context.length > 0 ) {
  2704. tt = TableTools.fnGetInstance( this.context[0].nTable );
  2705. }
  2706. return tt;
  2707. } );
  2708. }
  2709. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2710. * Initialisation
  2711. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  2712. /*
  2713. * Register a new feature with DataTables
  2714. */
  2715. if ( typeof $.fn.dataTable == "function" &&
  2716. typeof $.fn.dataTableExt.fnVersionCheck == "function" &&
  2717. $.fn.dataTableExt.fnVersionCheck('1.9.0') )
  2718. {
  2719. $.fn.dataTableExt.aoFeatures.push( {
  2720. "fnInit": function( oDTSettings ) {
  2721. var init = oDTSettings.oInit;
  2722. var opts = init ?
  2723. init.tableTools || init.oTableTools || {} :
  2724. {};
  2725. return new TableTools( oDTSettings.oInstance, opts ).dom.container;
  2726. },
  2727. "cFeature": "T",
  2728. "sFeature": "TableTools"
  2729. } );
  2730. }
  2731. else
  2732. {
  2733. alert( "Warning: TableTools requires DataTables 1.9.0 or newer - www.datatables.net/download");
  2734. }
  2735. $.fn.DataTable.TableTools = TableTools;
  2736. })(jQuery, window, document);
  2737. /*
  2738. * Register a new feature with DataTables
  2739. */
  2740. if ( typeof $.fn.dataTable == "function" &&
  2741. typeof $.fn.dataTableExt.fnVersionCheck == "function" &&
  2742. $.fn.dataTableExt.fnVersionCheck('1.9.0') )
  2743. {
  2744. $.fn.dataTableExt.aoFeatures.push( {
  2745. "fnInit": function( oDTSettings ) {
  2746. var oOpts = typeof oDTSettings.oInit.oTableTools != 'undefined' ?
  2747. oDTSettings.oInit.oTableTools : {};
  2748. var oTT = new TableTools( oDTSettings.oInstance, oOpts );
  2749. TableTools._aInstances.push( oTT );
  2750. return oTT.dom.container;
  2751. },
  2752. "cFeature": "T",
  2753. "sFeature": "TableTools"
  2754. } );
  2755. }
  2756. else
  2757. {
  2758. alert( "Warning: TableTools 2 requires DataTables 1.9.0 or newer - www.datatables.net/download");
  2759. }
  2760. $.fn.dataTable.TableTools = TableTools;
  2761. $.fn.DataTable.TableTools = TableTools;
  2762. return TableTools;
  2763. }; // /factory
  2764. // Define as an AMD module if possible
  2765. if ( typeof define === 'function' && define.amd ) {
  2766. define( ['jquery', 'datatables'], factory );
  2767. }
  2768. else if ( typeof exports === 'object' ) {
  2769. // Node/CommonJS
  2770. factory( require('jquery'), require('datatables') );
  2771. }
  2772. else if ( jQuery && !jQuery.fn.dataTable.TableTools ) {
  2773. // Otherwise simply initialise as normal, stopping multiple evaluation
  2774. factory( jQuery, jQuery.fn.dataTable );
  2775. }
  2776. })(window, document);