PageRenderTime 23ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/minovate/app/scripts/vendor/datatables/TableTools/as3/ZeroClipboardPdf.as

https://gitlab.com/x33n/dpd-angular-minovate-seed
ActionScript | 310 lines | 228 code | 47 blank | 35 comment | 28 complexity | 98720db8ba435cc943a2fe4e524bec01 MD5 | raw file
  1. /* Compile using: mxmlc --target-player=10.0.0 -static-link-runtime-shared-libraries=true -library-path+=lib ZeroClipboardPdf.as */
  2. package {
  3. import flash.display.Stage;
  4. import flash.display.Sprite;
  5. import flash.display.LoaderInfo;
  6. import flash.display.StageScaleMode;
  7. import flash.events.*;
  8. import flash.display.StageAlign;
  9. import flash.display.StageScaleMode;
  10. import flash.external.ExternalInterface;
  11. import flash.system.Security;
  12. import flash.utils.*;
  13. import flash.system.System;
  14. import flash.net.FileReference;
  15. import flash.net.FileFilter;
  16. /* PDF imports */
  17. import org.alivepdf.pdf.PDF;
  18. import org.alivepdf.data.Grid;
  19. import org.alivepdf.data.GridColumn;
  20. import org.alivepdf.layout.Orientation;
  21. import org.alivepdf.layout.Size;
  22. import org.alivepdf.layout.Unit;
  23. import org.alivepdf.display.Display;
  24. import org.alivepdf.saving.Method;
  25. import org.alivepdf.fonts.FontFamily;
  26. import org.alivepdf.fonts.Style;
  27. import org.alivepdf.fonts.CoreFont;
  28. import org.alivepdf.colors.RGBColor;
  29. public class ZeroClipboard extends Sprite {
  30. private var domId:String = '';
  31. private var button:Sprite;
  32. private var clipText:String = 'blank';
  33. private var fileName:String = '';
  34. private var action:String = 'copy';
  35. private var incBom:Boolean = true;
  36. private var charSet:String = 'utf8';
  37. public function ZeroClipboard() {
  38. // constructor, setup event listeners and external interfaces
  39. stage.scaleMode = StageScaleMode.EXACT_FIT;
  40. flash.system.Security.allowDomain("*");
  41. // import flashvars
  42. var flashvars:Object = LoaderInfo( this.root.loaderInfo ).parameters;
  43. domId = flashvars.id.split("\\").join("\\\\");
  44. // invisible button covers entire stage
  45. button = new Sprite();
  46. button.buttonMode = true;
  47. button.useHandCursor = true;
  48. button.graphics.beginFill(0x00FF00);
  49. button.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
  50. button.alpha = 0.0;
  51. addChild(button);
  52. button.addEventListener(MouseEvent.CLICK, function(event:Event):void {
  53. clickHandler(event);
  54. } );
  55. button.addEventListener(MouseEvent.MOUSE_OVER, function(event:Event):void {
  56. ExternalInterface.call( 'ZeroClipboard_TableTools.dispatch', domId, 'mouseOver', null );
  57. } );
  58. button.addEventListener(MouseEvent.MOUSE_OUT, function(event:Event):void {
  59. ExternalInterface.call( 'ZeroClipboard_TableTools.dispatch', domId, 'mouseOut', null );
  60. } );
  61. button.addEventListener(MouseEvent.MOUSE_DOWN, function(event:Event):void {
  62. ExternalInterface.call( 'ZeroClipboard_TableTools.dispatch', domId, 'mouseDown', null );
  63. } );
  64. button.addEventListener(MouseEvent.MOUSE_UP, function(event:Event):void {
  65. ExternalInterface.call( 'ZeroClipboard_TableTools.dispatch', domId, 'mouseUp', null );
  66. } );
  67. // External functions - readd whenever the stage is made active for IE
  68. addCallbacks();
  69. stage.addEventListener(Event.ACTIVATE, addCallbacks);
  70. // signal to the browser that we are ready
  71. ExternalInterface.call( 'ZeroClipboard_TableTools.dispatch', domId, 'load', null );
  72. }
  73. public function addCallbacks (evt:Event = null):void {
  74. ExternalInterface.addCallback("setHandCursor", setHandCursor);
  75. ExternalInterface.addCallback("clearText", clearText);
  76. ExternalInterface.addCallback("setText", setText);
  77. ExternalInterface.addCallback("appendText", appendText);
  78. ExternalInterface.addCallback("setFileName", setFileName);
  79. ExternalInterface.addCallback("setAction", setAction);
  80. ExternalInterface.addCallback("setCharSet", setCharSet);
  81. ExternalInterface.addCallback("setBomInc", setBomInc);
  82. }
  83. public function setCharSet(newCharSet:String):void {
  84. if ( newCharSet == 'UTF16LE' ) {
  85. charSet = newCharSet;
  86. } else {
  87. charSet = 'UTF8';
  88. }
  89. }
  90. public function setBomInc(newBomInc:Boolean):void {
  91. incBom = newBomInc;
  92. }
  93. public function clearText():void {
  94. clipText = '';
  95. }
  96. public function appendText(newText:String):void {
  97. clipText += newText;
  98. }
  99. public function setText(newText:String):void {
  100. clipText = newText;
  101. }
  102. public function setFileName(newFileName:String):void {
  103. fileName = newFileName;
  104. }
  105. public function setAction(newAction:String):void {
  106. action = newAction;
  107. }
  108. public function setHandCursor(enabled:Boolean):void {
  109. // control whether the hand cursor is shown on rollover (true)
  110. // or the default arrow cursor (false)
  111. button.useHandCursor = enabled;
  112. }
  113. private function clickHandler(event:Event):void {
  114. var fileRef:FileReference = new FileReference();
  115. fileRef.addEventListener(Event.COMPLETE, saveComplete);
  116. if ( action == "save" ) {
  117. /* Save as a file */
  118. if ( charSet == 'UTF16LE' ) {
  119. fileRef.save( strToUTF16LE(clipText), fileName );
  120. } else {
  121. fileRef.save( strToUTF8(clipText), fileName );
  122. }
  123. } else if ( action == "pdf" ) {
  124. /* Save as a PDF */
  125. var pdf:PDF = configPdf();
  126. fileRef.save( pdf.save( Method.LOCAL ), fileName );
  127. } else {
  128. /* Copy the text to the clipboard. Note charset and BOM have no effect here */
  129. System.setClipboard( clipText );
  130. ExternalInterface.call( 'ZeroClipboard_TableTools.dispatch', domId, 'complete', clipText );
  131. }
  132. }
  133. private function saveComplete(event:Event):void {
  134. ExternalInterface.call( 'ZeroClipboard_TableTools.dispatch', domId, 'complete', clipText );
  135. }
  136. private function getProp( prop:String, opts:Array ):String
  137. {
  138. var i:int, iLen:int;
  139. for ( i=0, iLen=opts.length ; i<iLen ; i++ )
  140. {
  141. if ( opts[i].indexOf( prop+":" ) != -1 )
  142. {
  143. return opts[i].replace( prop+":", "" );
  144. }
  145. }
  146. return "";
  147. }
  148. private function configPdf():PDF
  149. {
  150. var
  151. pdf:PDF,
  152. i:int, iLen:int,
  153. splitText:Array = clipText.split("--/TableToolsOpts--\n"),
  154. opts:Array = splitText[0].split("\n"),
  155. dataIn:Array = splitText[1].split("\n"),
  156. aColRatio:Array = getProp( 'colWidth', opts ).split('\t'),
  157. title:String = getProp( 'title', opts ),
  158. message:String = getProp( 'message', opts ),
  159. orientation:String = getProp( 'orientation', opts ),
  160. size:String = getProp( 'size', opts ),
  161. iPageWidth:int = 0,
  162. dataOut:Array = [],
  163. columns:Array = [],
  164. headers:Array,
  165. y:int = 0;
  166. /* Create the PDF */
  167. pdf = new PDF( Orientation[orientation.toUpperCase()], Unit.MM, Size[size.toUpperCase()] );
  168. pdf.setDisplayMode( Display.FULL_WIDTH );
  169. pdf.addPage();
  170. iPageWidth = pdf.getCurrentPage().w-20;
  171. pdf.textStyle( new RGBColor(0), 1 );
  172. /* Add the title / message if there is one */
  173. pdf.setFont( new CoreFont(FontFamily.HELVETICA), 14 );
  174. if ( title != "" )
  175. {
  176. pdf.writeText(11, title+"\n");
  177. }
  178. pdf.setFont( new CoreFont(FontFamily.HELVETICA), 11 );
  179. if ( message != "" )
  180. {
  181. pdf.writeText(11, message+"\n");
  182. }
  183. /* Data setup. Split up the headers, and then construct the columns */
  184. for ( i=0, iLen=dataIn.length ; i<iLen ; i++ )
  185. {
  186. if ( dataIn[i] != "" )
  187. {
  188. dataOut.push( dataIn[i].split("\t") );
  189. }
  190. }
  191. headers = dataOut.shift();
  192. for ( i=0, iLen=headers.length ; i<iLen ; i++ )
  193. {
  194. columns.push( new GridColumn( " \n"+headers[i]+"\n ", i.toString(), aColRatio[i]*iPageWidth, 'C' ) );
  195. }
  196. var grid:Grid = new Grid(
  197. dataOut, /* 1. data */
  198. iPageWidth, /* 2. width */
  199. 100, /* 3. height */
  200. new RGBColor (0xE0E0E0), /* 4. headerColor */
  201. new RGBColor (0xFFFFFF), /* 5. backgroundColor */
  202. true, /* 6. alternateRowColor */
  203. new RGBColor ( 0x0 ), /* 7. borderColor */
  204. .1, /* 8. border alpha */
  205. null, /* 9. joins */
  206. columns /* 10. columns */
  207. );
  208. pdf.addGrid( grid, 0, y );
  209. return pdf;
  210. }
  211. /*
  212. * Function: strToUTF8
  213. * Purpose: Convert a string to the output utf-8
  214. * Returns: ByteArray
  215. * Inputs: String
  216. */
  217. private function strToUTF8( str:String ):ByteArray {
  218. var utf8:ByteArray = new ByteArray();
  219. /* BOM first */
  220. if ( incBom ) {
  221. utf8.writeByte( 0xEF );
  222. utf8.writeByte( 0xBB );
  223. utf8.writeByte( 0xBF );
  224. }
  225. utf8.writeUTFBytes( str );
  226. return utf8;
  227. }
  228. /*
  229. * Function: strToUTF16LE
  230. * Purpose: Convert a string to the output utf-16
  231. * Returns: ByteArray
  232. * Inputs: String
  233. * Notes: The fact that this function is needed is a little annoying. Basically, strings in
  234. * AS3 are UTF-16 (with surrogate pairs and everything), but characters which take up less
  235. * than 8 bytes appear to be stored as only 8 bytes. This function effective adds the
  236. * padding required, and the BOM
  237. */
  238. private function strToUTF16LE( str:String ):ByteArray {
  239. var utf16:ByteArray = new ByteArray();
  240. var iChar:uint;
  241. var i:uint=0, iLen:uint = str.length;
  242. /* BOM first */
  243. if ( incBom ) {
  244. utf16.writeByte( 0xFF );
  245. utf16.writeByte( 0xFE );
  246. }
  247. while ( i < iLen ) {
  248. iChar = str.charCodeAt(i);
  249. if ( iChar < 0xFF ) {
  250. /* one byte char */
  251. utf16.writeByte( iChar );
  252. utf16.writeByte( 0 );
  253. } else {
  254. /* two byte char */
  255. utf16.writeByte( iChar & 0x00FF );
  256. utf16.writeByte( iChar >> 8 );
  257. }
  258. i++;
  259. }
  260. return utf16;
  261. }
  262. }
  263. }