PageRenderTime 47ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/production/js/datatables/tools/as3/ZeroClipboard.as

https://gitlab.com/jbeta/gentelella
ActionScript | 221 lines | 154 code | 37 blank | 30 comment | 20 complexity | d9718eff7befe02a826602d9af54d241 MD5 | raw file
  1. /* Compile using: mxmlc --target-player=10.0.0 ZeroClipboard.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. public class ZeroClipboard extends Sprite {
  17. private var domId:String = '';
  18. private var button:Sprite;
  19. private var clipText:String = 'blank';
  20. private var fileName:String = '';
  21. private var action:String = 'copy';
  22. private var incBom:Boolean = true;
  23. private var charSet:String = 'utf8';
  24. public function ZeroClipboard() {
  25. // constructor, setup event listeners and external interfaces
  26. stage.scaleMode = StageScaleMode.EXACT_FIT;
  27. flash.system.Security.allowDomain("*");
  28. // import flashvars
  29. var flashvars:Object = LoaderInfo( this.root.loaderInfo ).parameters;
  30. domId = flashvars.id.split("\\").join("\\\\");
  31. // invisible button covers entire stage
  32. button = new Sprite();
  33. button.buttonMode = true;
  34. button.useHandCursor = true;
  35. button.graphics.beginFill(0x00FF00);
  36. button.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
  37. button.alpha = 0.0;
  38. addChild(button);
  39. button.addEventListener(MouseEvent.CLICK, clickHandler);
  40. button.addEventListener(MouseEvent.MOUSE_OVER, function(event:Event):void {
  41. ExternalInterface.call( 'ZeroClipboard_TableTools.dispatch', domId, 'mouseOver', null );
  42. } );
  43. button.addEventListener(MouseEvent.MOUSE_OUT, function(event:Event):void {
  44. ExternalInterface.call( 'ZeroClipboard_TableTools.dispatch', domId, 'mouseOut', null );
  45. } );
  46. button.addEventListener(MouseEvent.MOUSE_DOWN, function(event:Event):void {
  47. ExternalInterface.call( 'ZeroClipboard_TableTools.dispatch', domId, 'mouseDown', null );
  48. } );
  49. button.addEventListener(MouseEvent.MOUSE_UP, function(event:Event):void {
  50. ExternalInterface.call( 'ZeroClipboard_TableTools.dispatch', domId, 'mouseUp', null );
  51. } );
  52. // External functions - readd whenever the stage is made active for IE
  53. addCallbacks();
  54. stage.addEventListener(Event.ACTIVATE, addCallbacks);
  55. // signal to the browser that we are ready
  56. ExternalInterface.call( 'ZeroClipboard_TableTools.dispatch', domId, 'load', null );
  57. }
  58. public function addCallbacks (evt:Event = null):void {
  59. ExternalInterface.addCallback("setHandCursor", setHandCursor);
  60. ExternalInterface.addCallback("clearText", clearText);
  61. ExternalInterface.addCallback("setText", setText);
  62. ExternalInterface.addCallback("appendText", appendText);
  63. ExternalInterface.addCallback("setFileName", setFileName);
  64. ExternalInterface.addCallback("setAction", setAction);
  65. ExternalInterface.addCallback("setCharSet", setCharSet);
  66. ExternalInterface.addCallback("setBomInc", setBomInc);
  67. }
  68. public function setCharSet(newCharSet:String):void {
  69. if ( newCharSet == 'UTF16LE' ) {
  70. charSet = newCharSet;
  71. } else {
  72. charSet = 'UTF8';
  73. }
  74. }
  75. public function setBomInc(newBomInc:Boolean):void {
  76. incBom = newBomInc;
  77. }
  78. public function clearText():void {
  79. clipText = '';
  80. }
  81. public function appendText(newText:String):void {
  82. clipText += newText;
  83. }
  84. public function setText(newText:String):void {
  85. clipText = newText;
  86. }
  87. public function setFileName(newFileName:String):void {
  88. fileName = newFileName;
  89. }
  90. public function setAction(newAction:String):void {
  91. action = newAction;
  92. }
  93. public function setHandCursor(enabled:Boolean):void {
  94. // control whether the hand cursor is shown on rollover (true)
  95. // or the default arrow cursor (false)
  96. button.useHandCursor = enabled;
  97. }
  98. private function clickHandler(event:Event):void {
  99. var fileRef:FileReference = new FileReference();
  100. fileRef.addEventListener(Event.COMPLETE, saveComplete);
  101. if ( action == "save" ) {
  102. /* Save as a file */
  103. if ( charSet == 'UTF16LE' ) {
  104. fileRef.save( strToUTF16LE(clipText), fileName );
  105. } else {
  106. fileRef.save( strToUTF8(clipText), fileName );
  107. }
  108. } else if ( action == "pdf" ) {
  109. fileRef.save( "This instance of ZeroClipboard is not configured for PDF export. "+
  110. "Please use the PDF export version.", fileName+".txt" );
  111. } else {
  112. /* Copy the text to the clipboard. Note charset and BOM have no effect here */
  113. System.setClipboard( clipText );
  114. ExternalInterface.call( 'ZeroClipboard_TableTools.dispatch', domId, 'complete', clipText );
  115. }
  116. }
  117. private function saveComplete(event:Event):void {
  118. ExternalInterface.call( 'ZeroClipboard_TableTools.dispatch', domId, 'complete', clipText );
  119. }
  120. private function getProp( prop:String, opts:Array ):String
  121. {
  122. var i:int, iLen:int;
  123. for ( i=0, iLen=opts.length ; i<iLen ; i++ )
  124. {
  125. if ( opts[i].indexOf( prop+":" ) != -1 )
  126. {
  127. return opts[i].replace( prop+":", "" );
  128. }
  129. }
  130. return "";
  131. }
  132. /*
  133. * Function: strToUTF8
  134. * Purpose: Convert a string to the output utf-8
  135. * Returns: ByteArray
  136. * Inputs: String
  137. */
  138. private function strToUTF8( str:String ):ByteArray {
  139. var utf8:ByteArray = new ByteArray();
  140. /* BOM first */
  141. if ( incBom ) {
  142. utf8.writeByte( 0xEF );
  143. utf8.writeByte( 0xBB );
  144. utf8.writeByte( 0xBF );
  145. }
  146. utf8.writeUTFBytes( str );
  147. return utf8;
  148. }
  149. /*
  150. * Function: strToUTF16LE
  151. * Purpose: Convert a string to the output utf-16
  152. * Returns: ByteArray
  153. * Inputs: String
  154. * Notes: The fact that this function is needed is a little annoying. Basically, strings in
  155. * AS3 are UTF-16 (with surrogate pairs and everything), but characters which take up less
  156. * than 8 bytes appear to be stored as only 8 bytes. This function effective adds the
  157. * padding required, and the BOM
  158. */
  159. private function strToUTF16LE( str:String ):ByteArray {
  160. var utf16:ByteArray = new ByteArray();
  161. var iChar:uint;
  162. var i:uint=0, iLen:uint = str.length;
  163. /* BOM first */
  164. if ( incBom ) {
  165. utf16.writeByte( 0xFF );
  166. utf16.writeByte( 0xFE );
  167. }
  168. while ( i < iLen ) {
  169. iChar = str.charCodeAt(i);
  170. if ( iChar < 0xFF ) {
  171. /* one byte char */
  172. utf16.writeByte( iChar );
  173. utf16.writeByte( 0 );
  174. } else {
  175. /* two byte char */
  176. utf16.writeByte( iChar & 0x00FF );
  177. utf16.writeByte( iChar >> 8 );
  178. }
  179. i++;
  180. }
  181. return utf16;
  182. }
  183. }
  184. }