datatable-tabletools.js 130 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214
  1. /*! TableTools 2.2.3
  2. * 2009-2014 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.3
  11. * @file dataTables.tableTools.js
  12. * @author SpryMedia Ltd (www.sprymedia.co.uk)
  13. * @contact www.sprymedia.co.uk/contact
  14. * @copyright Copyright 2009-2014 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-2014 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).off( 'click.DTTT_Select', 'tr' );
  994. $(that.dom.container).empty();
  995. // Remove the instance
  996. var idx = $.inArray( that, TableTools._aInstances );
  997. if ( idx !== -1 ) {
  998. TableTools._aInstances.splice( idx, 1 );
  999. }
  1000. }
  1001. } );
  1002. },
  1003. /**
  1004. * Take the user defined settings and the default settings and combine them.
  1005. * @method _fnCustomiseSettings
  1006. * @param {Object} oOpts Same as TableTools constructor
  1007. * @returns void
  1008. * @private
  1009. */
  1010. "_fnCustomiseSettings": function ( oOpts )
  1011. {
  1012. /* Is this the master control instance or not? */
  1013. if ( typeof this.s.dt._TableToolsInit == 'undefined' )
  1014. {
  1015. this.s.master = true;
  1016. this.s.dt._TableToolsInit = true;
  1017. }
  1018. /* We can use the table node from comparisons to group controls */
  1019. this.dom.table = this.s.dt.nTable;
  1020. /* Clone the defaults and then the user options */
  1021. this.s.custom = $.extend( {}, TableTools.DEFAULTS, oOpts );
  1022. /* Flash file location */
  1023. this.s.swfPath = this.s.custom.sSwfPath;
  1024. if ( typeof ZeroClipboard_TableTools != 'undefined' )
  1025. {
  1026. ZeroClipboard_TableTools.moviePath = this.s.swfPath;
  1027. }
  1028. /* Table row selecting */
  1029. this.s.select.type = this.s.custom.sRowSelect;
  1030. this.s.select.preRowSelect = this.s.custom.fnPreRowSelect;
  1031. this.s.select.postSelected = this.s.custom.fnRowSelected;
  1032. this.s.select.postDeselected = this.s.custom.fnRowDeselected;
  1033. // Backwards compatibility - allow the user to specify a custom class in the initialiser
  1034. if ( this.s.custom.sSelectedClass )
  1035. {
  1036. this.classes.select.row = this.s.custom.sSelectedClass;
  1037. }
  1038. this.s.tags = this.s.custom.oTags;
  1039. /* Button set */
  1040. this.s.buttonSet = this.s.custom.aButtons;
  1041. },
  1042. /**
  1043. * Take the user input arrays and expand them to be fully defined, and then add them to a given
  1044. * DOM element
  1045. * @method _fnButtonDefinations
  1046. * @param {array} buttonSet Set of user defined buttons
  1047. * @param {node} wrapper Node to add the created buttons to
  1048. * @returns void
  1049. * @private
  1050. */
  1051. "_fnButtonDefinations": function ( buttonSet, wrapper )
  1052. {
  1053. var buttonDef;
  1054. for ( var i=0, iLen=buttonSet.length ; i<iLen ; i++ )
  1055. {
  1056. if ( typeof buttonSet[i] == "string" )
  1057. {
  1058. if ( typeof TableTools.BUTTONS[ buttonSet[i] ] == 'undefined' )
  1059. {
  1060. alert( "TableTools: Warning - unknown button type: "+buttonSet[i] );
  1061. continue;
  1062. }
  1063. buttonDef = $.extend( {}, TableTools.BUTTONS[ buttonSet[i] ], true );
  1064. }
  1065. else
  1066. {
  1067. if ( typeof TableTools.BUTTONS[ buttonSet[i].sExtends ] == 'undefined' )
  1068. {
  1069. alert( "TableTools: Warning - unknown button type: "+buttonSet[i].sExtends );
  1070. continue;
  1071. }
  1072. var o = $.extend( {}, TableTools.BUTTONS[ buttonSet[i].sExtends ], true );
  1073. buttonDef = $.extend( o, buttonSet[i], true );
  1074. }
  1075. var button = this._fnCreateButton(
  1076. buttonDef,
  1077. $(wrapper).hasClass(this.classes.collection.container)
  1078. );
  1079. if ( button ) {
  1080. wrapper.appendChild( button );
  1081. }
  1082. }
  1083. },
  1084. /**
  1085. * Create and configure a TableTools button
  1086. * @method _fnCreateButton
  1087. * @param {Object} oConfig Button configuration object
  1088. * @returns {Node} Button element
  1089. * @private
  1090. */
  1091. "_fnCreateButton": function ( oConfig, bCollectionButton )
  1092. {
  1093. var nButton = this._fnButtonBase( oConfig, bCollectionButton );
  1094. if ( oConfig.sAction.match(/flash/) )
  1095. {
  1096. if ( ! this._fnHasFlash() ) {
  1097. return false;
  1098. }
  1099. this._fnFlashConfig( nButton, oConfig );
  1100. }
  1101. else if ( oConfig.sAction == "text" )
  1102. {
  1103. this._fnTextConfig( nButton, oConfig );
  1104. }
  1105. else if ( oConfig.sAction == "div" )
  1106. {
  1107. this._fnTextConfig( nButton, oConfig );
  1108. }
  1109. else if ( oConfig.sAction == "collection" )
  1110. {
  1111. this._fnTextConfig( nButton, oConfig );
  1112. this._fnCollectionConfig( nButton, oConfig );
  1113. }
  1114. if ( this.s.dt.iTabIndex !== -1 ) {
  1115. $(nButton)
  1116. .attr( 'tabindex', this.s.dt.iTabIndex )
  1117. .attr( 'aria-controls', this.s.dt.sTableId )
  1118. .on( 'keyup.DTTT', function (e) {
  1119. // Trigger the click event on return key when focused.
  1120. // Note that for Flash buttons this has no effect since we
  1121. // can't programmatically trigger the Flash export
  1122. if ( e.keyCode === 13 ) {
  1123. e.stopPropagation();
  1124. $(this).trigger( 'click' );
  1125. }
  1126. } )
  1127. .on( 'mousedown.DTTT', function (e) {
  1128. // On mousedown we want to stop the focus occurring on the
  1129. // button, focus is used only for the keyboard navigation.
  1130. // But using preventDefault for the flash buttons stops the
  1131. // flash action. However, it is not the button that gets
  1132. // focused but the flash element for flash buttons, so this
  1133. // works
  1134. if ( ! oConfig.sAction.match(/flash/) ) {
  1135. e.preventDefault();
  1136. }
  1137. } );
  1138. }
  1139. return nButton;
  1140. },
  1141. /**
  1142. * Create the DOM needed for the button and apply some base properties. All buttons start here
  1143. * @method _fnButtonBase
  1144. * @param {o} oConfig Button configuration object
  1145. * @returns {Node} DIV element for the button
  1146. * @private
  1147. */
  1148. "_fnButtonBase": function ( o, bCollectionButton )
  1149. {
  1150. var sTag, sLiner, sClass;
  1151. if ( bCollectionButton )
  1152. {
  1153. sTag = o.sTag && o.sTag !== "default" ? o.sTag : this.s.tags.collection.button;
  1154. sLiner = o.sLinerTag && o.sLinerTag !== "default" ? o.sLiner : this.s.tags.collection.liner;
  1155. sClass = this.classes.collection.buttons.normal;
  1156. }
  1157. else
  1158. {
  1159. sTag = o.sTag && o.sTag !== "default" ? o.sTag : this.s.tags.button;
  1160. sLiner = o.sLinerTag && o.sLinerTag !== "default" ? o.sLiner : this.s.tags.liner;
  1161. sClass = this.classes.buttons.normal;
  1162. }
  1163. var
  1164. nButton = document.createElement( sTag ),
  1165. nSpan = document.createElement( sLiner ),
  1166. masterS = this._fnGetMasterSettings();
  1167. nButton.className = sClass+" "+o.sButtonClass;
  1168. nButton.setAttribute('id', "ToolTables_"+this.s.dt.sInstance+"_"+masterS.buttonCounter );
  1169. nButton.appendChild( nSpan );
  1170. nSpan.innerHTML = o.sButtonText;
  1171. masterS.buttonCounter++;
  1172. return nButton;
  1173. },
  1174. /**
  1175. * Get the settings object for the master instance. When more than one TableTools instance is
  1176. * assigned to a DataTable, only one of them can be the 'master' (for the select rows). As such,
  1177. * we will typically want to interact with that master for global properties.
  1178. * @method _fnGetMasterSettings
  1179. * @returns {Object} TableTools settings object
  1180. * @private
  1181. */
  1182. "_fnGetMasterSettings": function ()
  1183. {
  1184. if ( this.s.master )
  1185. {
  1186. return this.s;
  1187. }
  1188. else
  1189. {
  1190. /* Look for the master which has the same DT as this one */
  1191. var instances = TableTools._aInstances;
  1192. for ( var i=0, iLen=instances.length ; i<iLen ; i++ )
  1193. {
  1194. if ( this.dom.table == instances[i].s.dt.nTable )
  1195. {
  1196. return instances[i].s;
  1197. }
  1198. }
  1199. }
  1200. },
  1201. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  1202. * Button collection functions
  1203. */
  1204. /**
  1205. * Create a collection button, when activated will present a drop down list of other buttons
  1206. * @param {Node} nButton Button to use for the collection activation
  1207. * @param {Object} oConfig Button configuration object
  1208. * @returns void
  1209. * @private
  1210. */
  1211. "_fnCollectionConfig": function ( nButton, oConfig )
  1212. {
  1213. var nHidden = document.createElement( this.s.tags.collection.container );
  1214. nHidden.style.display = "none";
  1215. nHidden.className = this.classes.collection.container;
  1216. oConfig._collection = nHidden;
  1217. document.body.appendChild( nHidden );
  1218. this._fnButtonDefinations( oConfig.aButtons, nHidden );
  1219. },
  1220. /**
  1221. * Show a button collection
  1222. * @param {Node} nButton Button to use for the collection
  1223. * @param {Object} oConfig Button configuration object
  1224. * @returns void
  1225. * @private
  1226. */
  1227. "_fnCollectionShow": function ( nButton, oConfig )
  1228. {
  1229. var
  1230. that = this,
  1231. oPos = $(nButton).offset(),
  1232. nHidden = oConfig._collection,
  1233. iDivX = oPos.left,
  1234. iDivY = oPos.top + $(nButton).outerHeight(),
  1235. iWinHeight = $(window).height(), iDocHeight = $(document).height(),
  1236. iWinWidth = $(window).width(), iDocWidth = $(document).width();
  1237. nHidden.style.position = "absolute";
  1238. nHidden.style.left = iDivX+"px";
  1239. nHidden.style.top = iDivY+"px";
  1240. nHidden.style.display = "block";
  1241. $(nHidden).css('opacity',0);
  1242. var nBackground = document.createElement('div');
  1243. nBackground.style.position = "absolute";
  1244. nBackground.style.left = "0px";
  1245. nBackground.style.top = "0px";
  1246. nBackground.style.height = ((iWinHeight>iDocHeight)? iWinHeight : iDocHeight) +"px";
  1247. nBackground.style.width = ((iWinWidth>iDocWidth)? iWinWidth : iDocWidth) +"px";
  1248. nBackground.className = this.classes.collection.background;
  1249. $(nBackground).css('opacity',0);
  1250. document.body.appendChild( nBackground );
  1251. document.body.appendChild( nHidden );
  1252. /* Visual corrections to try and keep the collection visible */
  1253. var iDivWidth = $(nHidden).outerWidth();
  1254. var iDivHeight = $(nHidden).outerHeight();
  1255. if ( iDivX + iDivWidth > iDocWidth )
  1256. {
  1257. nHidden.style.left = (iDocWidth-iDivWidth)+"px";
  1258. }
  1259. if ( iDivY + iDivHeight > iDocHeight )
  1260. {
  1261. nHidden.style.top = (iDivY-iDivHeight-$(nButton).outerHeight())+"px";
  1262. }
  1263. this.dom.collection.collection = nHidden;
  1264. this.dom.collection.background = nBackground;
  1265. /* This results in a very small delay for the end user but it allows the animation to be
  1266. * much smoother. If you don't want the animation, then the setTimeout can be removed
  1267. */
  1268. setTimeout( function () {
  1269. $(nHidden).animate({"opacity": 1}, 500);
  1270. $(nBackground).animate({"opacity": 0.25}, 500);
  1271. }, 10 );
  1272. /* Resize the buttons to the Flash contents fit */
  1273. this.fnResizeButtons();
  1274. /* Event handler to remove the collection display */
  1275. $(nBackground).click( function () {
  1276. that._fnCollectionHide.call( that, null, null );
  1277. } );
  1278. },
  1279. /**
  1280. * Hide a button collection
  1281. * @param {Node} nButton Button to use for the collection
  1282. * @param {Object} oConfig Button configuration object
  1283. * @returns void
  1284. * @private
  1285. */
  1286. "_fnCollectionHide": function ( nButton, oConfig )
  1287. {
  1288. if ( oConfig !== null && oConfig.sExtends == 'collection' )
  1289. {
  1290. return;
  1291. }
  1292. if ( this.dom.collection.collection !== null )
  1293. {
  1294. $(this.dom.collection.collection).animate({"opacity": 0}, 500, function (e) {
  1295. this.style.display = "none";
  1296. } );
  1297. $(this.dom.collection.background).animate({"opacity": 0}, 500, function (e) {
  1298. this.parentNode.removeChild( this );
  1299. } );
  1300. this.dom.collection.collection = null;
  1301. this.dom.collection.background = null;
  1302. }
  1303. },
  1304. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  1305. * Row selection functions
  1306. */
  1307. /**
  1308. * Add event handlers to a table to allow for row selection
  1309. * @method _fnRowSelectConfig
  1310. * @returns void
  1311. * @private
  1312. */
  1313. "_fnRowSelectConfig": function ()
  1314. {
  1315. if ( this.s.master )
  1316. {
  1317. var
  1318. that = this,
  1319. i, iLen,
  1320. dt = this.s.dt,
  1321. aoOpenRows = this.s.dt.aoOpenRows;
  1322. $(dt.nTable).addClass( this.classes.select.table );
  1323. // When using OS style selection, we want to cancel the shift text
  1324. // selection, but only when the shift key is used (so you can
  1325. // actually still select text in the table)
  1326. if ( this.s.select.type === 'os' ) {
  1327. $(dt.nTBody).on( 'mousedown.DTTT_Select', 'tr', function(e) {
  1328. if ( e.shiftKey ) {
  1329. $(dt.nTBody)
  1330. .css( '-moz-user-select', 'none' )
  1331. .one('selectstart.DTTT_Select', 'tr', function () {
  1332. return false;
  1333. } );
  1334. }
  1335. } );
  1336. $(dt.nTBody).on( 'mouseup.DTTT_Select', 'tr', function(e) {
  1337. $(dt.nTBody).css( '-moz-user-select', '' );
  1338. } );
  1339. }
  1340. // Row selection
  1341. $(dt.nTBody).on( 'click.DTTT_Select', this.s.custom.sRowSelector, function(e) {
  1342. var row = this.nodeName.toLowerCase() === 'tr' ?
  1343. this :
  1344. $(this).parents('tr')[0];
  1345. var select = that.s.select;
  1346. var pos = that.s.dt.oInstance.fnGetPosition( row );
  1347. /* Sub-table must be ignored (odd that the selector won't do this with >) */
  1348. if ( row.parentNode != dt.nTBody ) {
  1349. return;
  1350. }
  1351. /* Check that we are actually working with a DataTables controlled row */
  1352. if ( dt.oInstance.fnGetData(row) === null ) {
  1353. return;
  1354. }
  1355. // Shift click, ctrl click and simple click handling to make
  1356. // row selection a lot like a file system in desktop OSs
  1357. if ( select.type == 'os' ) {
  1358. if ( e.ctrlKey || e.metaKey ) {
  1359. // Add or remove from the selection
  1360. if ( that.fnIsSelected( row ) ) {
  1361. that._fnRowDeselect( row, e );
  1362. }
  1363. else {
  1364. that._fnRowSelect( row, e );
  1365. }
  1366. }
  1367. else if ( e.shiftKey ) {
  1368. // Add a range of rows, from the last selected row to
  1369. // this one
  1370. var rowIdxs = that.s.dt.aiDisplay.slice(); // visible rows
  1371. var idx1 = $.inArray( select.lastRow, rowIdxs );
  1372. var idx2 = $.inArray( pos, rowIdxs );
  1373. if ( that.fnGetSelected().length === 0 || idx1 === -1 ) {
  1374. // select from top to here - slightly odd, but both
  1375. // Windows and Mac OS do this
  1376. rowIdxs.splice( $.inArray( pos, rowIdxs )+1, rowIdxs.length );
  1377. }
  1378. else {
  1379. // reverse so we can shift click 'up' as well as down
  1380. if ( idx1 > idx2 ) {
  1381. var tmp = idx2;
  1382. idx2 = idx1;
  1383. idx1 = tmp;
  1384. }
  1385. rowIdxs.splice( idx2+1, rowIdxs.length );
  1386. rowIdxs.splice( 0, idx1 );
  1387. }
  1388. if ( ! that.fnIsSelected( row ) ) {
  1389. // Select range
  1390. that._fnRowSelect( rowIdxs, e );
  1391. }
  1392. else {
  1393. // Deselect range - need to keep the clicked on row selected
  1394. rowIdxs.splice( $.inArray( pos, rowIdxs ), 1 );
  1395. that._fnRowDeselect( rowIdxs, e );
  1396. }
  1397. }
  1398. else {
  1399. // No cmd or shift click. Deselect current if selected,
  1400. // or select this row only
  1401. if ( that.fnIsSelected( row ) && that.fnGetSelected().length === 1 ) {
  1402. that._fnRowDeselect( row, e );
  1403. }
  1404. else {
  1405. that.fnSelectNone();
  1406. that._fnRowSelect( row, e );
  1407. }
  1408. }
  1409. }
  1410. else if ( that.fnIsSelected( row ) ) {
  1411. that._fnRowDeselect( row, e );
  1412. }
  1413. else if ( select.type == "single" ) {
  1414. that.fnSelectNone();
  1415. that._fnRowSelect( row, e );
  1416. }
  1417. else if ( select.type == "multi" ) {
  1418. that._fnRowSelect( row, e );
  1419. }
  1420. select.lastRow = pos;
  1421. } );//.on('selectstart', function () { return false; } );
  1422. // Bind a listener to the DataTable for when new rows are created.
  1423. // This allows rows to be visually selected when they should be and
  1424. // deferred rendering is used.
  1425. dt.oApi._fnCallbackReg( dt, 'aoRowCreatedCallback', function (tr, data, index) {
  1426. if ( dt.aoData[index]._DTTT_selected ) {
  1427. $(tr).addClass( that.classes.select.row );
  1428. }
  1429. }, 'TableTools-SelectAll' );
  1430. }
  1431. },
  1432. /**
  1433. * Select rows
  1434. * @param {*} src Rows to select - see _fnSelectData for a description of valid inputs
  1435. * @private
  1436. */
  1437. "_fnRowSelect": function ( src, e )
  1438. {
  1439. var
  1440. that = this,
  1441. data = this._fnSelectData( src ),
  1442. firstTr = data.length===0 ? null : data[0].nTr,
  1443. anSelected = [],
  1444. i, len;
  1445. // Get all the rows that will be selected
  1446. for ( i=0, len=data.length ; i<len ; i++ )
  1447. {
  1448. if ( data[i].nTr )
  1449. {
  1450. anSelected.push( data[i].nTr );
  1451. }
  1452. }
  1453. // User defined pre-selection function
  1454. if ( this.s.select.preRowSelect !== null && !this.s.select.preRowSelect.call(this, e, anSelected, true) )
  1455. {
  1456. return;
  1457. }
  1458. // Mark them as selected
  1459. for ( i=0, len=data.length ; i<len ; i++ )
  1460. {
  1461. data[i]._DTTT_selected = true;
  1462. if ( data[i].nTr )
  1463. {
  1464. $(data[i].nTr).addClass( that.classes.select.row );
  1465. }
  1466. }
  1467. // Post-selection function
  1468. if ( this.s.select.postSelected !== null )
  1469. {
  1470. this.s.select.postSelected.call( this, anSelected );
  1471. }
  1472. TableTools._fnEventDispatch( this, 'select', anSelected, true );
  1473. },
  1474. /**
  1475. * Deselect rows
  1476. * @param {*} src Rows to deselect - see _fnSelectData for a description of valid inputs
  1477. * @private
  1478. */
  1479. "_fnRowDeselect": function ( src, e )
  1480. {
  1481. var
  1482. that = this,
  1483. data = this._fnSelectData( src ),
  1484. firstTr = data.length===0 ? null : data[0].nTr,
  1485. anDeselectedTrs = [],
  1486. i, len;
  1487. // Get all the rows that will be deselected
  1488. for ( i=0, len=data.length ; i<len ; i++ )
  1489. {
  1490. if ( data[i].nTr )
  1491. {
  1492. anDeselectedTrs.push( data[i].nTr );
  1493. }
  1494. }
  1495. // User defined pre-selection function
  1496. if ( this.s.select.preRowSelect !== null && !this.s.select.preRowSelect.call(this, e, anDeselectedTrs, false) )
  1497. {
  1498. return;
  1499. }
  1500. // Mark them as deselected
  1501. for ( i=0, len=data.length ; i<len ; i++ )
  1502. {
  1503. data[i]._DTTT_selected = false;
  1504. if ( data[i].nTr )
  1505. {
  1506. $(data[i].nTr).removeClass( that.classes.select.row );
  1507. }
  1508. }
  1509. // Post-deselection function
  1510. if ( this.s.select.postDeselected !== null )
  1511. {
  1512. this.s.select.postDeselected.call( this, anDeselectedTrs );
  1513. }
  1514. TableTools._fnEventDispatch( this, 'select', anDeselectedTrs, false );
  1515. },
  1516. /**
  1517. * Take a data source for row selection and convert it into aoData points for the DT
  1518. * @param {*} src Can be a single DOM TR node, an array of TR nodes (including a
  1519. * a jQuery object), a single aoData point from DataTables, an array of aoData
  1520. * points or an array of aoData indexes
  1521. * @returns {array} An array of aoData points
  1522. */
  1523. "_fnSelectData": function ( src )
  1524. {
  1525. var out = [], pos, i, iLen;
  1526. if ( src.nodeName )
  1527. {
  1528. // Single node
  1529. pos = this.s.dt.oInstance.fnGetPosition( src );
  1530. out.push( this.s.dt.aoData[pos] );
  1531. }
  1532. else if ( typeof src.length !== 'undefined' )
  1533. {
  1534. // jQuery object or an array of nodes, or aoData points
  1535. for ( i=0, iLen=src.length ; i<iLen ; i++ )
  1536. {
  1537. if ( src[i].nodeName )
  1538. {
  1539. pos = this.s.dt.oInstance.fnGetPosition( src[i] );
  1540. out.push( this.s.dt.aoData[pos] );
  1541. }
  1542. else if ( typeof src[i] === 'number' )
  1543. {
  1544. out.push( this.s.dt.aoData[ src[i] ] );
  1545. }
  1546. else
  1547. {
  1548. out.push( src[i] );
  1549. }
  1550. }
  1551. return out;
  1552. }
  1553. else
  1554. {
  1555. // A single aoData point
  1556. out.push( src );
  1557. }
  1558. return out;
  1559. },
  1560. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  1561. * Text button functions
  1562. */
  1563. /**
  1564. * Configure a text based button for interaction events
  1565. * @method _fnTextConfig
  1566. * @param {Node} nButton Button element which is being considered
  1567. * @param {Object} oConfig Button configuration object
  1568. * @returns void
  1569. * @private
  1570. */
  1571. "_fnTextConfig": function ( nButton, oConfig )
  1572. {
  1573. var that = this;
  1574. if ( oConfig.fnInit !== null )
  1575. {
  1576. oConfig.fnInit.call( this, nButton, oConfig );
  1577. }
  1578. if ( oConfig.sToolTip !== "" )
  1579. {
  1580. nButton.title = oConfig.sToolTip;
  1581. }
  1582. $(nButton).hover( function () {
  1583. if ( oConfig.fnMouseover !== null )
  1584. {
  1585. oConfig.fnMouseover.call( this, nButton, oConfig, null );
  1586. }
  1587. }, function () {
  1588. if ( oConfig.fnMouseout !== null )
  1589. {
  1590. oConfig.fnMouseout.call( this, nButton, oConfig, null );
  1591. }
  1592. } );
  1593. if ( oConfig.fnSelect !== null )
  1594. {
  1595. TableTools._fnEventListen( this, 'select', function (n) {
  1596. oConfig.fnSelect.call( that, nButton, oConfig, n );
  1597. } );
  1598. }
  1599. $(nButton).click( function (e) {
  1600. //e.preventDefault();
  1601. if ( oConfig.fnClick !== null )
  1602. {
  1603. oConfig.fnClick.call( that, nButton, oConfig, null, e );
  1604. }
  1605. /* Provide a complete function to match the behaviour of the flash elements */
  1606. if ( oConfig.fnComplete !== null )
  1607. {
  1608. oConfig.fnComplete.call( that, nButton, oConfig, null, null );
  1609. }
  1610. that._fnCollectionHide( nButton, oConfig );
  1611. } );
  1612. },
  1613. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  1614. * Flash button functions
  1615. */
  1616. /**
  1617. * Check if the Flash plug-in is available
  1618. * @method _fnHasFlash
  1619. * @returns {boolean} `true` if Flash available, `false` otherwise
  1620. * @private
  1621. */
  1622. "_fnHasFlash": function ()
  1623. {
  1624. try {
  1625. var fo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
  1626. if (fo) {
  1627. return true;
  1628. }
  1629. }
  1630. catch (e) {
  1631. if (
  1632. navigator.mimeTypes &&
  1633. navigator.mimeTypes['application/x-shockwave-flash'] !== undefined &&
  1634. navigator.mimeTypes['application/x-shockwave-flash'].enabledPlugin
  1635. ) {
  1636. return true;
  1637. }
  1638. }
  1639. return false;
  1640. },
  1641. /**
  1642. * Configure a flash based button for interaction events
  1643. * @method _fnFlashConfig
  1644. * @param {Node} nButton Button element which is being considered
  1645. * @param {o} oConfig Button configuration object
  1646. * @returns void
  1647. * @private
  1648. */
  1649. "_fnFlashConfig": function ( nButton, oConfig )
  1650. {
  1651. var that = this;
  1652. var flash = new ZeroClipboard_TableTools.Client();
  1653. if ( oConfig.fnInit !== null )
  1654. {
  1655. oConfig.fnInit.call( this, nButton, oConfig );
  1656. }
  1657. flash.setHandCursor( true );
  1658. if ( oConfig.sAction == "flash_save" )
  1659. {
  1660. flash.setAction( 'save' );
  1661. flash.setCharSet( (oConfig.sCharSet=="utf16le") ? 'UTF16LE' : 'UTF8' );
  1662. flash.setBomInc( oConfig.bBomInc );
  1663. flash.setFileName( oConfig.sFileName.replace('*', this.fnGetTitle(oConfig)) );
  1664. }
  1665. else if ( oConfig.sAction == "flash_pdf" )
  1666. {
  1667. flash.setAction( 'pdf' );
  1668. flash.setFileName( oConfig.sFileName.replace('*', this.fnGetTitle(oConfig)) );
  1669. }
  1670. else
  1671. {
  1672. flash.setAction( 'copy' );
  1673. }
  1674. flash.addEventListener('mouseOver', function(client) {
  1675. if ( oConfig.fnMouseover !== null )
  1676. {
  1677. oConfig.fnMouseover.call( that, nButton, oConfig, flash );
  1678. }
  1679. } );
  1680. flash.addEventListener('mouseOut', function(client) {
  1681. if ( oConfig.fnMouseout !== null )
  1682. {
  1683. oConfig.fnMouseout.call( that, nButton, oConfig, flash );
  1684. }
  1685. } );
  1686. flash.addEventListener('mouseDown', function(client) {
  1687. if ( oConfig.fnClick !== null )
  1688. {
  1689. oConfig.fnClick.call( that, nButton, oConfig, flash );
  1690. }
  1691. } );
  1692. flash.addEventListener('complete', function (client, text) {
  1693. if ( oConfig.fnComplete !== null )
  1694. {
  1695. oConfig.fnComplete.call( that, nButton, oConfig, flash, text );
  1696. }
  1697. that._fnCollectionHide( nButton, oConfig );
  1698. } );
  1699. this._fnFlashGlue( flash, nButton, oConfig.sToolTip );
  1700. },
  1701. /**
  1702. * Wait until the id is in the DOM before we "glue" the swf. Note that this function will call
  1703. * itself (using setTimeout) until it completes successfully
  1704. * @method _fnFlashGlue
  1705. * @param {Object} clip Zero clipboard object
  1706. * @param {Node} node node to glue swf to
  1707. * @param {String} text title of the flash movie
  1708. * @returns void
  1709. * @private
  1710. */
  1711. "_fnFlashGlue": function ( flash, node, text )
  1712. {
  1713. var that = this;
  1714. var id = node.getAttribute('id');
  1715. if ( document.getElementById(id) )
  1716. {
  1717. flash.glue( node, text );
  1718. }
  1719. else
  1720. {
  1721. setTimeout( function () {
  1722. that._fnFlashGlue( flash, node, text );
  1723. }, 100 );
  1724. }
  1725. },
  1726. /**
  1727. * Set the text for the flash clip to deal with
  1728. *
  1729. * This function is required for large information sets. There is a limit on the
  1730. * amount of data that can be transferred between Javascript and Flash in a single call, so
  1731. * we use this method to build up the text in Flash by sending over chunks. It is estimated
  1732. * that the data limit is around 64k, although it is undocumented, and appears to be different
  1733. * between different flash versions. We chunk at 8KiB.
  1734. * @method _fnFlashSetText
  1735. * @param {Object} clip the ZeroClipboard object
  1736. * @param {String} sData the data to be set
  1737. * @returns void
  1738. * @private
  1739. */
  1740. "_fnFlashSetText": function ( clip, sData )
  1741. {
  1742. var asData = this._fnChunkData( sData, 8192 );
  1743. clip.clearText();
  1744. for ( var i=0, iLen=asData.length ; i<iLen ; i++ )
  1745. {
  1746. clip.appendText( asData[i] );
  1747. }
  1748. },
  1749. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  1750. * Data retrieval functions
  1751. */
  1752. /**
  1753. * Convert the mixed columns variable into a boolean array the same size as the columns, which
  1754. * indicates which columns we want to include
  1755. * @method _fnColumnTargets
  1756. * @param {String|Array} mColumns The columns to be included in data retrieval. If a string
  1757. * then it can take the value of "visible" or "hidden" (to include all visible or
  1758. * hidden columns respectively). Or an array of column indexes
  1759. * @returns {Array} A boolean array the length of the columns of the table, which each value
  1760. * indicating if the column is to be included or not
  1761. * @private
  1762. */
  1763. "_fnColumnTargets": function ( mColumns )
  1764. {
  1765. var aColumns = [];
  1766. var dt = this.s.dt;
  1767. var i, iLen;
  1768. var columns = dt.aoColumns;
  1769. var columnCount = columns.length;
  1770. if ( typeof mColumns == "function" )
  1771. {
  1772. var a = mColumns.call( this, dt );
  1773. for ( i=0, iLen=columnCount ; i<iLen ; i++ )
  1774. {
  1775. aColumns.push( $.inArray( i, a ) !== -1 ? true : false );
  1776. }
  1777. }
  1778. else if ( typeof mColumns == "object" )
  1779. {
  1780. for ( i=0, iLen=columnCount ; i<iLen ; i++ )
  1781. {
  1782. aColumns.push( false );
  1783. }
  1784. for ( i=0, iLen=mColumns.length ; i<iLen ; i++ )
  1785. {
  1786. aColumns[ mColumns[i] ] = true;
  1787. }
  1788. }
  1789. else if ( mColumns == "visible" )
  1790. {
  1791. for ( i=0, iLen=columnCount ; i<iLen ; i++ )
  1792. {
  1793. aColumns.push( columns[i].bVisible ? true : false );
  1794. }
  1795. }
  1796. else if ( mColumns == "hidden" )
  1797. {
  1798. for ( i=0, iLen=columnCount ; i<iLen ; i++ )
  1799. {
  1800. aColumns.push( columns[i].bVisible ? false : true );
  1801. }
  1802. }
  1803. else if ( mColumns == "sortable" )
  1804. {
  1805. for ( i=0, iLen=columnCount ; i<iLen ; i++ )
  1806. {
  1807. aColumns.push( columns[i].bSortable ? true : false );
  1808. }
  1809. }
  1810. else /* all */
  1811. {
  1812. for ( i=0, iLen=columnCount ; i<iLen ; i++ )
  1813. {
  1814. aColumns.push( true );
  1815. }
  1816. }
  1817. return aColumns;
  1818. },
  1819. /**
  1820. * New line character(s) depend on the platforms
  1821. * @method method
  1822. * @param {Object} oConfig Button configuration object - only interested in oConfig.sNewLine
  1823. * @returns {String} Newline character
  1824. */
  1825. "_fnNewline": function ( oConfig )
  1826. {
  1827. if ( oConfig.sNewLine == "auto" )
  1828. {
  1829. return navigator.userAgent.match(/Windows/) ? "\r\n" : "\n";
  1830. }
  1831. else
  1832. {
  1833. return oConfig.sNewLine;
  1834. }
  1835. },
  1836. /**
  1837. * Get data from DataTables' internals and format it for output
  1838. * @method _fnGetDataTablesData
  1839. * @param {Object} oConfig Button configuration object
  1840. * @param {String} oConfig.sFieldBoundary Field boundary for the data cells in the string
  1841. * @param {String} oConfig.sFieldSeperator Field separator for the data cells
  1842. * @param {String} oConfig.sNewline New line options
  1843. * @param {Mixed} oConfig.mColumns Which columns should be included in the output
  1844. * @param {Boolean} oConfig.bHeader Include the header
  1845. * @param {Boolean} oConfig.bFooter Include the footer
  1846. * @param {Boolean} oConfig.bSelectedOnly Include only the selected rows in the output
  1847. * @returns {String} Concatenated string of data
  1848. * @private
  1849. */
  1850. "_fnGetDataTablesData": function ( oConfig )
  1851. {
  1852. var i, iLen, j, jLen;
  1853. var aRow, aData=[], sLoopData='', arr;
  1854. var dt = this.s.dt, tr, child;
  1855. var regex = new RegExp(oConfig.sFieldBoundary, "g"); /* Do it here for speed */
  1856. var aColumnsInc = this._fnColumnTargets( oConfig.mColumns );
  1857. var bSelectedOnly = (typeof oConfig.bSelectedOnly != 'undefined') ? oConfig.bSelectedOnly : false;
  1858. /*
  1859. * Header
  1860. */
  1861. if ( oConfig.bHeader )
  1862. {
  1863. aRow = [];
  1864. for ( i=0, iLen=dt.aoColumns.length ; i<iLen ; i++ )
  1865. {
  1866. if ( aColumnsInc[i] )
  1867. {
  1868. sLoopData = dt.aoColumns[i].sTitle.replace(/\n/g," ").replace( /<.*?>/g, "" ).replace(/^\s+|\s+$/g,"");
  1869. sLoopData = this._fnHtmlDecode( sLoopData );
  1870. aRow.push( this._fnBoundData( sLoopData, oConfig.sFieldBoundary, regex ) );
  1871. }
  1872. }
  1873. aData.push( aRow.join(oConfig.sFieldSeperator) );
  1874. }
  1875. bSelectedOnly = true;
  1876. /*
  1877. * Body
  1878. */
  1879. var aDataIndex;
  1880. var aSelected = this.fnGetSelectedIndexes();
  1881. bSelectedOnly = this.s.select.type !== "none" && bSelectedOnly && aSelected.length !== 0;
  1882. if ( bSelectedOnly ) {
  1883. // Use the selected indexes
  1884. aDataIndex = aSelected;
  1885. }
  1886. else if ( DataTable.Api ) {
  1887. // 1.10+ style
  1888. aDataIndex = new DataTable.Api( dt )
  1889. .rows( oConfig.oSelectorOpts )
  1890. .indexes()
  1891. .flatten()
  1892. .toArray();
  1893. }
  1894. else {
  1895. // 1.9- style
  1896. aDataIndex = dt.oInstance
  1897. .$('tr', oConfig.oSelectorOpts)
  1898. .map( function (id, row) {
  1899. return dt.oInstance.fnGetPosition( row );
  1900. } )
  1901. .get();
  1902. }
  1903. for ( j=0, jLen=aDataIndex.length ; j<jLen ; j++ )
  1904. {
  1905. tr = dt.aoData[ aDataIndex[j] ].nTr;
  1906. aRow = [];
  1907. /* Columns */
  1908. for ( i=0, iLen=dt.aoColumns.length ; i<iLen ; i++ )
  1909. {
  1910. if ( aColumnsInc[i] )
  1911. {
  1912. /* Convert to strings (with small optimisation) */
  1913. var mTypeData = dt.oApi._fnGetCellData( dt, aDataIndex[j], i, 'display' );
  1914. if ( oConfig.fnCellRender )
  1915. {
  1916. sLoopData = oConfig.fnCellRender( mTypeData, i, tr, aDataIndex[j] )+"";
  1917. }
  1918. else if ( typeof mTypeData == "string" )
  1919. {
  1920. /* Strip newlines, replace img tags with alt attr. and finally strip html... */
  1921. sLoopData = mTypeData.replace(/\n/g," ");
  1922. sLoopData =
  1923. sLoopData.replace(/<img.*?\s+alt\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s>]+)).*?>/gi,
  1924. '$1$2$3');
  1925. sLoopData = sLoopData.replace( /<.*?>/g, "" );
  1926. }
  1927. else
  1928. {
  1929. sLoopData = mTypeData+"";
  1930. }
  1931. /* Trim and clean the data */
  1932. sLoopData = sLoopData.replace(/^\s+/, '').replace(/\s+$/, '');
  1933. sLoopData = this._fnHtmlDecode( sLoopData );
  1934. /* Bound it and add it to the total data */
  1935. aRow.push( this._fnBoundData( sLoopData, oConfig.sFieldBoundary, regex ) );
  1936. }
  1937. }
  1938. aData.push( aRow.join(oConfig.sFieldSeperator) );
  1939. /* Details rows from fnOpen */
  1940. if ( oConfig.bOpenRows )
  1941. {
  1942. arr = $.grep(dt.aoOpenRows, function(o) { return o.nParent === tr; });
  1943. if ( arr.length === 1 )
  1944. {
  1945. sLoopData = this._fnBoundData( $('td', arr[0].nTr).html(), oConfig.sFieldBoundary, regex );
  1946. aData.push( sLoopData );
  1947. }
  1948. }
  1949. }
  1950. /*
  1951. * Footer
  1952. */
  1953. if ( oConfig.bFooter && dt.nTFoot !== null )
  1954. {
  1955. aRow = [];
  1956. for ( i=0, iLen=dt.aoColumns.length ; i<iLen ; i++ )
  1957. {
  1958. if ( aColumnsInc[i] && dt.aoColumns[i].nTf !== null )
  1959. {
  1960. sLoopData = dt.aoColumns[i].nTf.innerHTML.replace(/\n/g," ").replace( /<.*?>/g, "" );
  1961. sLoopData = this._fnHtmlDecode( sLoopData );
  1962. aRow.push( this._fnBoundData( sLoopData, oConfig.sFieldBoundary, regex ) );
  1963. }
  1964. }
  1965. aData.push( aRow.join(oConfig.sFieldSeperator) );
  1966. }
  1967. var _sLastData = aData.join( this._fnNewline(oConfig) );
  1968. return _sLastData;
  1969. },
  1970. /**
  1971. * Wrap data up with a boundary string
  1972. * @method _fnBoundData
  1973. * @param {String} sData data to bound
  1974. * @param {String} sBoundary bounding char(s)
  1975. * @param {RegExp} regex search for the bounding chars - constructed outside for efficiency
  1976. * in the loop
  1977. * @returns {String} bound data
  1978. * @private
  1979. */
  1980. "_fnBoundData": function ( sData, sBoundary, regex )
  1981. {
  1982. if ( sBoundary === "" )
  1983. {
  1984. return sData;
  1985. }
  1986. else
  1987. {
  1988. return sBoundary + sData.replace(regex, sBoundary+sBoundary) + sBoundary;
  1989. }
  1990. },
  1991. /**
  1992. * Break a string up into an array of smaller strings
  1993. * @method _fnChunkData
  1994. * @param {String} sData data to be broken up
  1995. * @param {Int} iSize chunk size
  1996. * @returns {Array} String array of broken up text
  1997. * @private
  1998. */
  1999. "_fnChunkData": function ( sData, iSize )
  2000. {
  2001. var asReturn = [];
  2002. var iStrlen = sData.length;
  2003. for ( var i=0 ; i<iStrlen ; i+=iSize )
  2004. {
  2005. if ( i+iSize < iStrlen )
  2006. {
  2007. asReturn.push( sData.substring( i, i+iSize ) );
  2008. }
  2009. else
  2010. {
  2011. asReturn.push( sData.substring( i, iStrlen ) );
  2012. }
  2013. }
  2014. return asReturn;
  2015. },
  2016. /**
  2017. * Decode HTML entities
  2018. * @method _fnHtmlDecode
  2019. * @param {String} sData encoded string
  2020. * @returns {String} decoded string
  2021. * @private
  2022. */
  2023. "_fnHtmlDecode": function ( sData )
  2024. {
  2025. if ( sData.indexOf('&') === -1 )
  2026. {
  2027. return sData;
  2028. }
  2029. var n = document.createElement('div');
  2030. return sData.replace( /&([^\s]*?);/g, function( match, match2 ) {
  2031. if ( match.substr(1, 1) === '#' )
  2032. {
  2033. return String.fromCharCode( Number(match2.substr(1)) );
  2034. }
  2035. else
  2036. {
  2037. n.innerHTML = match;
  2038. return n.childNodes[0].nodeValue;
  2039. }
  2040. } );
  2041. },
  2042. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2043. * Printing functions
  2044. */
  2045. /**
  2046. * Show print display
  2047. * @method _fnPrintStart
  2048. * @param {Event} e Event object
  2049. * @param {Object} oConfig Button configuration object
  2050. * @returns void
  2051. * @private
  2052. */
  2053. "_fnPrintStart": function ( oConfig )
  2054. {
  2055. var that = this;
  2056. var oSetDT = this.s.dt;
  2057. /* Parse through the DOM hiding everything that isn't needed for the table */
  2058. this._fnPrintHideNodes( oSetDT.nTable );
  2059. /* Show the whole table */
  2060. this.s.print.saveStart = oSetDT._iDisplayStart;
  2061. this.s.print.saveLength = oSetDT._iDisplayLength;
  2062. if ( oConfig.bShowAll )
  2063. {
  2064. oSetDT._iDisplayStart = 0;
  2065. oSetDT._iDisplayLength = -1;
  2066. if ( oSetDT.oApi._fnCalculateEnd ) {
  2067. oSetDT.oApi._fnCalculateEnd( oSetDT );
  2068. }
  2069. oSetDT.oApi._fnDraw( oSetDT );
  2070. }
  2071. /* Adjust the display for scrolling which might be done by DataTables */
  2072. if ( oSetDT.oScroll.sX !== "" || oSetDT.oScroll.sY !== "" )
  2073. {
  2074. this._fnPrintScrollStart( oSetDT );
  2075. // If the table redraws while in print view, the DataTables scrolling
  2076. // setup would hide the header, so we need to readd it on draw
  2077. $(this.s.dt.nTable).bind('draw.DTTT_Print', function () {
  2078. that._fnPrintScrollStart( oSetDT );
  2079. } );
  2080. }
  2081. /* Remove the other DataTables feature nodes - but leave the table! and info div */
  2082. var anFeature = oSetDT.aanFeatures;
  2083. for ( var cFeature in anFeature )
  2084. {
  2085. if ( cFeature != 'i' && cFeature != 't' && cFeature.length == 1 )
  2086. {
  2087. for ( var i=0, iLen=anFeature[cFeature].length ; i<iLen ; i++ )
  2088. {
  2089. this.dom.print.hidden.push( {
  2090. "node": anFeature[cFeature][i],
  2091. "display": "block"
  2092. } );
  2093. anFeature[cFeature][i].style.display = "none";
  2094. }
  2095. }
  2096. }
  2097. /* Print class can be used for styling */
  2098. $(document.body).addClass( this.classes.print.body );
  2099. /* Show information message to let the user know what is happening */
  2100. if ( oConfig.sInfo !== "" )
  2101. {
  2102. this.fnInfo( oConfig.sInfo, 3000 );
  2103. }
  2104. /* Add a message at the top of the page */
  2105. if ( oConfig.sMessage )
  2106. {
  2107. $('<div/>')
  2108. .addClass( this.classes.print.message )
  2109. .html( oConfig.sMessage )
  2110. .prependTo( 'body' );
  2111. }
  2112. /* Cache the scrolling and the jump to the top of the page */
  2113. this.s.print.saveScroll = $(window).scrollTop();
  2114. window.scrollTo( 0, 0 );
  2115. /* Bind a key event listener to the document for the escape key -
  2116. * it is removed in the callback
  2117. */
  2118. $(document).bind( "keydown.DTTT", function(e) {
  2119. /* Only interested in the escape key */
  2120. if ( e.keyCode == 27 )
  2121. {
  2122. e.preventDefault();
  2123. that._fnPrintEnd.call( that, e );
  2124. }
  2125. } );
  2126. },
  2127. /**
  2128. * Printing is finished, resume normal display
  2129. * @method _fnPrintEnd
  2130. * @param {Event} e Event object
  2131. * @returns void
  2132. * @private
  2133. */
  2134. "_fnPrintEnd": function ( e )
  2135. {
  2136. var that = this;
  2137. var oSetDT = this.s.dt;
  2138. var oSetPrint = this.s.print;
  2139. var oDomPrint = this.dom.print;
  2140. /* Show all hidden nodes */
  2141. this._fnPrintShowNodes();
  2142. /* Restore DataTables' scrolling */
  2143. if ( oSetDT.oScroll.sX !== "" || oSetDT.oScroll.sY !== "" )
  2144. {
  2145. $(this.s.dt.nTable).unbind('draw.DTTT_Print');
  2146. this._fnPrintScrollEnd();
  2147. }
  2148. /* Restore the scroll */
  2149. window.scrollTo( 0, oSetPrint.saveScroll );
  2150. /* Drop the print message */
  2151. $('div.'+this.classes.print.message).remove();
  2152. /* Styling class */
  2153. $(document.body).removeClass( 'DTTT_Print' );
  2154. /* Restore the table length */
  2155. oSetDT._iDisplayStart = oSetPrint.saveStart;
  2156. oSetDT._iDisplayLength = oSetPrint.saveLength;
  2157. if ( oSetDT.oApi._fnCalculateEnd ) {
  2158. oSetDT.oApi._fnCalculateEnd( oSetDT );
  2159. }
  2160. oSetDT.oApi._fnDraw( oSetDT );
  2161. $(document).unbind( "keydown.DTTT" );
  2162. },
  2163. /**
  2164. * Take account of scrolling in DataTables by showing the full table
  2165. * @returns void
  2166. * @private
  2167. */
  2168. "_fnPrintScrollStart": function ()
  2169. {
  2170. var
  2171. oSetDT = this.s.dt,
  2172. nScrollHeadInner = oSetDT.nScrollHead.getElementsByTagName('div')[0],
  2173. nScrollHeadTable = nScrollHeadInner.getElementsByTagName('table')[0],
  2174. nScrollBody = oSetDT.nTable.parentNode,
  2175. nTheadSize, nTfootSize;
  2176. /* Copy the header in the thead in the body table, this way we show one single table when
  2177. * in print view. Note that this section of code is more or less verbatim from DT 1.7.0
  2178. */
  2179. nTheadSize = oSetDT.nTable.getElementsByTagName('thead');
  2180. if ( nTheadSize.length > 0 )
  2181. {
  2182. oSetDT.nTable.removeChild( nTheadSize[0] );
  2183. }
  2184. if ( oSetDT.nTFoot !== null )
  2185. {
  2186. nTfootSize = oSetDT.nTable.getElementsByTagName('tfoot');
  2187. if ( nTfootSize.length > 0 )
  2188. {
  2189. oSetDT.nTable.removeChild( nTfootSize[0] );
  2190. }
  2191. }
  2192. nTheadSize = oSetDT.nTHead.cloneNode(true);
  2193. oSetDT.nTable.insertBefore( nTheadSize, oSetDT.nTable.childNodes[0] );
  2194. if ( oSetDT.nTFoot !== null )
  2195. {
  2196. nTfootSize = oSetDT.nTFoot.cloneNode(true);
  2197. oSetDT.nTable.insertBefore( nTfootSize, oSetDT.nTable.childNodes[1] );
  2198. }
  2199. /* Now adjust the table's viewport so we can actually see it */
  2200. if ( oSetDT.oScroll.sX !== "" )
  2201. {
  2202. oSetDT.nTable.style.width = $(oSetDT.nTable).outerWidth()+"px";
  2203. nScrollBody.style.width = $(oSetDT.nTable).outerWidth()+"px";
  2204. nScrollBody.style.overflow = "visible";
  2205. }
  2206. if ( oSetDT.oScroll.sY !== "" )
  2207. {
  2208. nScrollBody.style.height = $(oSetDT.nTable).outerHeight()+"px";
  2209. nScrollBody.style.overflow = "visible";
  2210. }
  2211. },
  2212. /**
  2213. * Take account of scrolling in DataTables by showing the full table. Note that the redraw of
  2214. * the DataTable that we do will actually deal with the majority of the hard work here
  2215. * @returns void
  2216. * @private
  2217. */
  2218. "_fnPrintScrollEnd": function ()
  2219. {
  2220. var
  2221. oSetDT = this.s.dt,
  2222. nScrollBody = oSetDT.nTable.parentNode;
  2223. if ( oSetDT.oScroll.sX !== "" )
  2224. {
  2225. nScrollBody.style.width = oSetDT.oApi._fnStringToCss( oSetDT.oScroll.sX );
  2226. nScrollBody.style.overflow = "auto";
  2227. }
  2228. if ( oSetDT.oScroll.sY !== "" )
  2229. {
  2230. nScrollBody.style.height = oSetDT.oApi._fnStringToCss( oSetDT.oScroll.sY );
  2231. nScrollBody.style.overflow = "auto";
  2232. }
  2233. },
  2234. /**
  2235. * Resume the display of all TableTools hidden nodes
  2236. * @method _fnPrintShowNodes
  2237. * @returns void
  2238. * @private
  2239. */
  2240. "_fnPrintShowNodes": function ( )
  2241. {
  2242. var anHidden = this.dom.print.hidden;
  2243. for ( var i=0, iLen=anHidden.length ; i<iLen ; i++ )
  2244. {
  2245. anHidden[i].node.style.display = anHidden[i].display;
  2246. }
  2247. anHidden.splice( 0, anHidden.length );
  2248. },
  2249. /**
  2250. * Hide nodes which are not needed in order to display the table. Note that this function is
  2251. * recursive
  2252. * @method _fnPrintHideNodes
  2253. * @param {Node} nNode Element which should be showing in a 'print' display
  2254. * @returns void
  2255. * @private
  2256. */
  2257. "_fnPrintHideNodes": function ( nNode )
  2258. {
  2259. var anHidden = this.dom.print.hidden;
  2260. var nParent = nNode.parentNode;
  2261. var nChildren = nParent.childNodes;
  2262. for ( var i=0, iLen=nChildren.length ; i<iLen ; i++ )
  2263. {
  2264. if ( nChildren[i] != nNode && nChildren[i].nodeType == 1 )
  2265. {
  2266. /* If our node is shown (don't want to show nodes which were previously hidden) */
  2267. var sDisplay = $(nChildren[i]).css("display");
  2268. if ( sDisplay != "none" )
  2269. {
  2270. /* Cache the node and it's previous state so we can restore it */
  2271. anHidden.push( {
  2272. "node": nChildren[i],
  2273. "display": sDisplay
  2274. } );
  2275. nChildren[i].style.display = "none";
  2276. }
  2277. }
  2278. }
  2279. if ( nParent.nodeName.toUpperCase() != "BODY" )
  2280. {
  2281. this._fnPrintHideNodes( nParent );
  2282. }
  2283. }
  2284. };
  2285. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2286. * Static variables
  2287. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  2288. /**
  2289. * Store of all instances that have been created of TableTools, so one can look up other (when
  2290. * there is need of a master)
  2291. * @property _aInstances
  2292. * @type Array
  2293. * @default []
  2294. * @private
  2295. */
  2296. TableTools._aInstances = [];
  2297. /**
  2298. * Store of all listeners and their callback functions
  2299. * @property _aListeners
  2300. * @type Array
  2301. * @default []
  2302. */
  2303. TableTools._aListeners = [];
  2304. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2305. * Static methods
  2306. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  2307. /**
  2308. * Get an array of all the master instances
  2309. * @method fnGetMasters
  2310. * @returns {Array} List of master TableTools instances
  2311. * @static
  2312. */
  2313. TableTools.fnGetMasters = function ()
  2314. {
  2315. var a = [];
  2316. for ( var i=0, iLen=TableTools._aInstances.length ; i<iLen ; i++ )
  2317. {
  2318. if ( TableTools._aInstances[i].s.master )
  2319. {
  2320. a.push( TableTools._aInstances[i] );
  2321. }
  2322. }
  2323. return a;
  2324. };
  2325. /**
  2326. * Get the master instance for a table node (or id if a string is given)
  2327. * @method fnGetInstance
  2328. * @returns {Object} ID of table OR table node, for which we want the TableTools instance
  2329. * @static
  2330. */
  2331. TableTools.fnGetInstance = function ( node )
  2332. {
  2333. if ( typeof node != 'object' )
  2334. {
  2335. node = document.getElementById(node);
  2336. }
  2337. for ( var i=0, iLen=TableTools._aInstances.length ; i<iLen ; i++ )
  2338. {
  2339. if ( TableTools._aInstances[i].s.master && TableTools._aInstances[i].dom.table == node )
  2340. {
  2341. return TableTools._aInstances[i];
  2342. }
  2343. }
  2344. return null;
  2345. };
  2346. /**
  2347. * Add a listener for a specific event
  2348. * @method _fnEventListen
  2349. * @param {Object} that Scope of the listening function (i.e. 'this' in the caller)
  2350. * @param {String} type Event type
  2351. * @param {Function} fn Function
  2352. * @returns void
  2353. * @private
  2354. * @static
  2355. */
  2356. TableTools._fnEventListen = function ( that, type, fn )
  2357. {
  2358. TableTools._aListeners.push( {
  2359. "that": that,
  2360. "type": type,
  2361. "fn": fn
  2362. } );
  2363. };
  2364. /**
  2365. * An event has occurred - look up every listener and fire it off. We check that the event we are
  2366. * going to fire is attached to the same table (using the table node as reference) before firing
  2367. * @method _fnEventDispatch
  2368. * @param {Object} that Scope of the listening function (i.e. 'this' in the caller)
  2369. * @param {String} type Event type
  2370. * @param {Node} node Element that the event occurred on (may be null)
  2371. * @param {boolean} [selected] Indicate if the node was selected (true) or deselected (false)
  2372. * @returns void
  2373. * @private
  2374. * @static
  2375. */
  2376. TableTools._fnEventDispatch = function ( that, type, node, selected )
  2377. {
  2378. var listeners = TableTools._aListeners;
  2379. for ( var i=0, iLen=listeners.length ; i<iLen ; i++ )
  2380. {
  2381. if ( that.dom.table == listeners[i].that.dom.table && listeners[i].type == type )
  2382. {
  2383. listeners[i].fn( node, selected );
  2384. }
  2385. }
  2386. };
  2387. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2388. * Constants
  2389. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  2390. TableTools.buttonBase = {
  2391. // Button base
  2392. "sAction": "text",
  2393. "sTag": "default",
  2394. "sLinerTag": "default",
  2395. "sButtonClass": "DTTT_button_text",
  2396. "sButtonText": "Button text",
  2397. "sTitle": "",
  2398. "sToolTip": "",
  2399. // Common button specific options
  2400. "sCharSet": "utf8",
  2401. "bBomInc": false,
  2402. "sFileName": "*.csv",
  2403. "sFieldBoundary": "",
  2404. "sFieldSeperator": "\t",
  2405. "sNewLine": "auto",
  2406. "mColumns": "all", /* "all", "visible", "hidden" or array of column integers */
  2407. "bHeader": true,
  2408. "bFooter": true,
  2409. "bOpenRows": false,
  2410. "bSelectedOnly": false,
  2411. "oSelectorOpts": undefined, // See http://datatables.net/docs/DataTables/1.9.4/#$ for full options
  2412. // Callbacks
  2413. "fnMouseover": null,
  2414. "fnMouseout": null,
  2415. "fnClick": null,
  2416. "fnSelect": null,
  2417. "fnComplete": null,
  2418. "fnInit": null,
  2419. "fnCellRender": null
  2420. };
  2421. /**
  2422. * @namespace Default button configurations
  2423. */
  2424. TableTools.BUTTONS = {
  2425. "csv": $.extend( {}, TableTools.buttonBase, {
  2426. "sAction": "flash_save",
  2427. "sButtonClass": "DTTT_button_csv",
  2428. "sButtonText": "CSV",
  2429. "sFieldBoundary": '"',
  2430. "sFieldSeperator": ",",
  2431. "fnClick": function( nButton, oConfig, flash ) {
  2432. this.fnSetText( flash, this.fnGetTableData(oConfig) );
  2433. }
  2434. } ),
  2435. "xls": $.extend( {}, TableTools.buttonBase, {
  2436. "sAction": "flash_save",
  2437. "sCharSet": "utf16le",
  2438. "bBomInc": true,
  2439. "sButtonClass": "DTTT_button_xls",
  2440. "sButtonText": "Excel",
  2441. "fnClick": function( nButton, oConfig, flash ) {
  2442. this.fnSetText( flash, this.fnGetTableData(oConfig) );
  2443. }
  2444. } ),
  2445. "copy": $.extend( {}, TableTools.buttonBase, {
  2446. "sAction": "flash_copy",
  2447. "sButtonClass": "DTTT_button_copy",
  2448. "sButtonText": "Copy",
  2449. "fnClick": function( nButton, oConfig, flash ) {
  2450. this.fnSetText( flash, this.fnGetTableData(oConfig) );
  2451. },
  2452. "fnComplete": function(nButton, oConfig, flash, text) {
  2453. var lines = text.split('\n').length;
  2454. if (oConfig.bHeader) lines--;
  2455. if (this.s.dt.nTFoot !== null && oConfig.bFooter) lines--;
  2456. var plural = (lines==1) ? "" : "s";
  2457. this.fnInfo( '<h6>Table copied</h6>'+
  2458. '<p>Copied '+lines+' row'+plural+' to the clipboard.</p>',
  2459. 1500
  2460. );
  2461. }
  2462. } ),
  2463. "pdf": $.extend( {}, TableTools.buttonBase, {
  2464. "sAction": "flash_pdf",
  2465. "sNewLine": "\n",
  2466. "sFileName": "*.pdf",
  2467. "sButtonClass": "DTTT_button_pdf",
  2468. "sButtonText": "PDF",
  2469. "sPdfOrientation": "portrait",
  2470. "sPdfSize": "A4",
  2471. "sPdfMessage": "",
  2472. "fnClick": function( nButton, oConfig, flash ) {
  2473. this.fnSetText( flash,
  2474. "title:"+ this.fnGetTitle(oConfig) +"\n"+
  2475. "message:"+ oConfig.sPdfMessage +"\n"+
  2476. "colWidth:"+ this.fnCalcColRatios(oConfig) +"\n"+
  2477. "orientation:"+ oConfig.sPdfOrientation +"\n"+
  2478. "size:"+ oConfig.sPdfSize +"\n"+
  2479. "--/TableToolsOpts--\n" +
  2480. this.fnGetTableData(oConfig)
  2481. );
  2482. }
  2483. } ),
  2484. "print": $.extend( {}, TableTools.buttonBase, {
  2485. "sInfo": "<h6>Print view</h6><p>Please use your browser's print function to "+
  2486. "print this table. Press escape when finished.</p>",
  2487. "sMessage": null,
  2488. "bShowAll": true,
  2489. "sToolTip": "View print view",
  2490. "sButtonClass": "DTTT_button_print",
  2491. "sButtonText": "Print",
  2492. "fnClick": function ( nButton, oConfig ) {
  2493. this.fnPrint( true, oConfig );
  2494. }
  2495. } ),
  2496. "text": $.extend( {}, TableTools.buttonBase ),
  2497. "select": $.extend( {}, TableTools.buttonBase, {
  2498. "sButtonText": "Select button",
  2499. "fnSelect": function( nButton, oConfig ) {
  2500. if ( this.fnGetSelected().length !== 0 ) {
  2501. $(nButton).removeClass( this.classes.buttons.disabled );
  2502. } else {
  2503. $(nButton).addClass( this.classes.buttons.disabled );
  2504. }
  2505. },
  2506. "fnInit": function( nButton, oConfig ) {
  2507. $(nButton).addClass( this.classes.buttons.disabled );
  2508. }
  2509. } ),
  2510. "select_single": $.extend( {}, TableTools.buttonBase, {
  2511. "sButtonText": "Select button",
  2512. "fnSelect": function( nButton, oConfig ) {
  2513. var iSelected = this.fnGetSelected().length;
  2514. if ( iSelected == 1 ) {
  2515. $(nButton).removeClass( this.classes.buttons.disabled );
  2516. } else {
  2517. $(nButton).addClass( this.classes.buttons.disabled );
  2518. }
  2519. },
  2520. "fnInit": function( nButton, oConfig ) {
  2521. $(nButton).addClass( this.classes.buttons.disabled );
  2522. }
  2523. } ),
  2524. "select_all": $.extend( {}, TableTools.buttonBase, {
  2525. "sButtonText": "Select all",
  2526. "fnClick": function( nButton, oConfig ) {
  2527. this.fnSelectAll();
  2528. },
  2529. "fnSelect": function( nButton, oConfig ) {
  2530. if ( this.fnGetSelected().length == this.s.dt.fnRecordsDisplay() ) {
  2531. $(nButton).addClass( this.classes.buttons.disabled );
  2532. } else {
  2533. $(nButton).removeClass( this.classes.buttons.disabled );
  2534. }
  2535. }
  2536. } ),
  2537. "select_none": $.extend( {}, TableTools.buttonBase, {
  2538. "sButtonText": "Deselect all",
  2539. "fnClick": function( nButton, oConfig ) {
  2540. this.fnSelectNone();
  2541. },
  2542. "fnSelect": function( nButton, oConfig ) {
  2543. if ( this.fnGetSelected().length !== 0 ) {
  2544. $(nButton).removeClass( this.classes.buttons.disabled );
  2545. } else {
  2546. $(nButton).addClass( this.classes.buttons.disabled );
  2547. }
  2548. },
  2549. "fnInit": function( nButton, oConfig ) {
  2550. $(nButton).addClass( this.classes.buttons.disabled );
  2551. }
  2552. } ),
  2553. "ajax": $.extend( {}, TableTools.buttonBase, {
  2554. "sAjaxUrl": "/xhr.php",
  2555. "sButtonText": "Ajax button",
  2556. "fnClick": function( nButton, oConfig ) {
  2557. var sData = this.fnGetTableData(oConfig);
  2558. $.ajax( {
  2559. "url": oConfig.sAjaxUrl,
  2560. "data": [
  2561. { "name": "tableData", "value": sData }
  2562. ],
  2563. "success": oConfig.fnAjaxComplete,
  2564. "dataType": "json",
  2565. "type": "POST",
  2566. "cache": false,
  2567. "error": function () {
  2568. alert( "Error detected when sending table data to server" );
  2569. }
  2570. } );
  2571. },
  2572. "fnAjaxComplete": function( json ) {
  2573. alert( 'Ajax complete' );
  2574. }
  2575. } ),
  2576. "div": $.extend( {}, TableTools.buttonBase, {
  2577. "sAction": "div",
  2578. "sTag": "div",
  2579. "sButtonClass": "DTTT_nonbutton",
  2580. "sButtonText": "Text button"
  2581. } ),
  2582. "collection": $.extend( {}, TableTools.buttonBase, {
  2583. "sAction": "collection",
  2584. "sButtonClass": "DTTT_button_collection",
  2585. "sButtonText": "Collection",
  2586. "fnClick": function( nButton, oConfig ) {
  2587. this._fnCollectionShow(nButton, oConfig);
  2588. }
  2589. } )
  2590. };
  2591. /*
  2592. * on* callback parameters:
  2593. * 1. node - button element
  2594. * 2. object - configuration object for this button
  2595. * 3. object - ZeroClipboard reference (flash button only)
  2596. * 4. string - Returned string from Flash (flash button only - and only on 'complete')
  2597. */
  2598. // Alias to match the other plug-ins styling
  2599. TableTools.buttons = TableTools.BUTTONS;
  2600. /**
  2601. * @namespace Classes used by TableTools - allows the styles to be override easily.
  2602. * Note that when TableTools initialises it will take a copy of the classes object
  2603. * and will use its internal copy for the remainder of its run time.
  2604. */
  2605. TableTools.classes = {
  2606. "container": "DTTT_container",
  2607. "buttons": {
  2608. "normal": "DTTT_button",
  2609. "disabled": "DTTT_disabled"
  2610. },
  2611. "collection": {
  2612. "container": "DTTT_collection",
  2613. "background": "DTTT_collection_background",
  2614. "buttons": {
  2615. "normal": "DTTT_button",
  2616. "disabled": "DTTT_disabled"
  2617. }
  2618. },
  2619. "select": {
  2620. "table": "DTTT_selectable",
  2621. "row": "DTTT_selected selected"
  2622. },
  2623. "print": {
  2624. "body": "DTTT_Print",
  2625. "info": "DTTT_print_info",
  2626. "message": "DTTT_PrintMessage"
  2627. }
  2628. };
  2629. /**
  2630. * @namespace ThemeRoller classes - built in for compatibility with DataTables'
  2631. * bJQueryUI option.
  2632. */
  2633. TableTools.classes_themeroller = {
  2634. "container": "DTTT_container ui-buttonset ui-buttonset-multi",
  2635. "buttons": {
  2636. "normal": "DTTT_button ui-button ui-state-default"
  2637. },
  2638. "collection": {
  2639. "container": "DTTT_collection ui-buttonset ui-buttonset-multi"
  2640. }
  2641. };
  2642. /**
  2643. * @namespace TableTools default settings for initialisation
  2644. */
  2645. TableTools.DEFAULTS = {
  2646. "sSwfPath": "../swf/copy_csv_xls_pdf.swf",
  2647. "sRowSelect": "none",
  2648. "sRowSelector": "tr",
  2649. "sSelectedClass": null,
  2650. "fnPreRowSelect": null,
  2651. "fnRowSelected": null,
  2652. "fnRowDeselected": null,
  2653. "aButtons": [ "copy", "csv", "xls", "pdf", "print" ],
  2654. "oTags": {
  2655. "container": "div",
  2656. "button": "a", // We really want to use buttons here, but Firefox and IE ignore the
  2657. // click on the Flash element in the button (but not mouse[in|out]).
  2658. "liner": "span",
  2659. "collection": {
  2660. "container": "div",
  2661. "button": "a",
  2662. "liner": "span"
  2663. }
  2664. }
  2665. };
  2666. // Alias to match the other plug-ins
  2667. TableTools.defaults = TableTools.DEFAULTS;
  2668. /**
  2669. * Name of this class
  2670. * @constant CLASS
  2671. * @type String
  2672. * @default TableTools
  2673. */
  2674. TableTools.prototype.CLASS = "TableTools";
  2675. /**
  2676. * TableTools version
  2677. * @constant VERSION
  2678. * @type String
  2679. * @default See code
  2680. */
  2681. TableTools.version = "2.2.3";
  2682. // DataTables 1.10 API
  2683. //
  2684. // This will be extended in a big way in in TableTools 3 to provide API methods
  2685. // such as rows().select() and rows.selected() etc, but for the moment the
  2686. // tabletools() method simply returns the instance.
  2687. if ( $.fn.dataTable.Api ) {
  2688. $.fn.dataTable.Api.register( 'tabletools()', function () {
  2689. var tt = null;
  2690. if ( this.context.length > 0 ) {
  2691. tt = TableTools.fnGetInstance( this.context[0].nTable );
  2692. }
  2693. return tt;
  2694. } );
  2695. }
  2696. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2697. * Initialisation
  2698. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  2699. /*
  2700. * Register a new feature with DataTables
  2701. */
  2702. if ( typeof $.fn.dataTable == "function" &&
  2703. typeof $.fn.dataTableExt.fnVersionCheck == "function" &&
  2704. $.fn.dataTableExt.fnVersionCheck('1.9.0') )
  2705. {
  2706. $.fn.dataTableExt.aoFeatures.push( {
  2707. "fnInit": function( oDTSettings ) {
  2708. var init = oDTSettings.oInit;
  2709. var opts = init ?
  2710. init.tableTools || init.oTableTools || {} :
  2711. {};
  2712. return new TableTools( oDTSettings.oInstance, opts ).dom.container;
  2713. },
  2714. "cFeature": "T",
  2715. "sFeature": "TableTools"
  2716. } );
  2717. }
  2718. else
  2719. {
  2720. alert( "Warning: TableTools requires DataTables 1.9.0 or newer - www.datatables.net/download");
  2721. }
  2722. $.fn.DataTable.TableTools = TableTools;
  2723. })(jQuery, window, document);
  2724. /*
  2725. * Register a new feature with DataTables
  2726. */
  2727. if ( typeof $.fn.dataTable == "function" &&
  2728. typeof $.fn.dataTableExt.fnVersionCheck == "function" &&
  2729. $.fn.dataTableExt.fnVersionCheck('1.9.0') )
  2730. {
  2731. $.fn.dataTableExt.aoFeatures.push( {
  2732. "fnInit": function( oDTSettings ) {
  2733. var oOpts = typeof oDTSettings.oInit.oTableTools != 'undefined' ?
  2734. oDTSettings.oInit.oTableTools : {};
  2735. var oTT = new TableTools( oDTSettings.oInstance, oOpts );
  2736. TableTools._aInstances.push( oTT );
  2737. return oTT.dom.container;
  2738. },
  2739. "cFeature": "T",
  2740. "sFeature": "TableTools"
  2741. } );
  2742. }
  2743. else
  2744. {
  2745. alert( "Warning: TableTools 2 requires DataTables 1.9.0 or newer - www.datatables.net/download");
  2746. }
  2747. $.fn.dataTable.TableTools = TableTools;
  2748. $.fn.DataTable.TableTools = TableTools;
  2749. return TableTools;
  2750. }; // /factory
  2751. // Define as an AMD module if possible
  2752. if ( typeof define === 'function' && define.amd ) {
  2753. define( ['jquery', 'datatables'], factory );
  2754. }
  2755. else if ( typeof exports === 'object' ) {
  2756. // Node/CommonJS
  2757. factory( require('jquery'), require('datatables') );
  2758. }
  2759. else if ( jQuery && !jQuery.fn.dataTable.TableTools ) {
  2760. // Otherwise simply initialise as normal, stopping multiple evaluation
  2761. factory( jQuery, jQuery.fn.dataTable );
  2762. }
  2763. })(window, document);