Added DataTables 1.9.0, minus examples and documentation.
[openemr.git] / library / js / datatables / extras / TableTools / media / as3 / ZeroClipboard.as
blob367b0497d708aa6ff95139ed08f3d4fe52ced0af
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;
17 public class ZeroClipboard extends Sprite {
19 private var domId:String = '';
20 private var button:Sprite;
21 private var clipText:String = 'blank';
22 private var fileName:String = '';
23 private var action:String = 'copy';
24 private var incBom:Boolean = true;
25 private var charSet:String = 'utf8';
28 public function ZeroClipboard() {
29 // constructor, setup event listeners and external interfaces
30 stage.scaleMode = StageScaleMode.EXACT_FIT;
31 flash.system.Security.allowDomain("*");
33 // import flashvars
34 var flashvars:Object = LoaderInfo( this.root.loaderInfo ).parameters;
35 domId = flashvars.id;
37 // invisible button covers entire stage
38 button = new Sprite();
39 button.buttonMode = true;
40 button.useHandCursor = true;
41 button.graphics.beginFill(0x00FF00);
42 button.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
43 button.alpha = 0.0;
44 addChild(button);
46 button.addEventListener(MouseEvent.CLICK, clickHandler);
47 button.addEventListener(MouseEvent.MOUSE_OVER, function(event:Event):void {
48 ExternalInterface.call( 'ZeroClipboard.dispatch', domId, 'mouseOver', null );
49 } );
50 button.addEventListener(MouseEvent.MOUSE_OUT, function(event:Event):void {
51 ExternalInterface.call( 'ZeroClipboard.dispatch', domId, 'mouseOut', null );
52 } );
53 button.addEventListener(MouseEvent.MOUSE_DOWN, function(event:Event):void {
54 ExternalInterface.call( 'ZeroClipboard.dispatch', domId, 'mouseDown', null );
55 } );
56 button.addEventListener(MouseEvent.MOUSE_UP, function(event:Event):void {
57 ExternalInterface.call( 'ZeroClipboard.dispatch', domId, 'mouseUp', null );
58 } );
60 // External functions - readd whenever the stage is made active for IE
61 addCallbacks();
62 stage.addEventListener(Event.ACTIVATE, addCallbacks);
64 // signal to the browser that we are ready
65 ExternalInterface.call( 'ZeroClipboard.dispatch', domId, 'load', null );
68 public function addCallbacks ():void {
69 ExternalInterface.addCallback("setHandCursor", setHandCursor);
70 ExternalInterface.addCallback("clearText", clearText);
71 ExternalInterface.addCallback("setText", setText);
72 ExternalInterface.addCallback("appendText", appendText);
73 ExternalInterface.addCallback("setFileName", setFileName);
74 ExternalInterface.addCallback("setAction", setAction);
75 ExternalInterface.addCallback("setCharSet", setCharSet);
76 ExternalInterface.addCallback("setBomInc", setBomInc);
80 public function setCharSet(newCharSet:String):void {
81 if ( newCharSet == 'UTF16LE' ) {
82 charSet = newCharSet;
83 } else {
84 charSet = 'UTF8';
88 public function setBomInc(newBomInc:Boolean):void {
89 incBom = newBomInc;
92 public function clearText():void {
93 clipText = '';
96 public function appendText(newText:String):void {
97 clipText += newText;
100 public function setText(newText:String):void {
101 clipText = newText;
104 public function setFileName(newFileName:String):void {
105 fileName = newFileName;
108 public function setAction(newAction:String):void {
109 action = newAction;
112 public function setHandCursor(enabled:Boolean):void {
113 // control whether the hand cursor is shown on rollover (true)
114 // or the default arrow cursor (false)
115 button.useHandCursor = enabled;
119 private function clickHandler(event:Event):void {
120 var fileRef:FileReference = new FileReference();
121 fileRef.addEventListener(Event.COMPLETE, saveComplete);
123 if ( action == "save" ) {
124 /* Save as a file */
125 if ( charSet == 'UTF16LE' ) {
126 fileRef.save( strToUTF16LE(clipText), fileName );
127 } else {
128 fileRef.save( strToUTF8(clipText), fileName );
130 } else if ( action == "pdf" ) {
131 fileRef.save( "This instance of ZeroClipboard is not configured for PDF export. "+
132 "Please use the PDF export version.", fileName+".txt" );
133 } else {
134 /* Copy the text to the clipboard. Note charset and BOM have no effect here */
135 System.setClipboard( clipText );
136 ExternalInterface.call( 'ZeroClipboard.dispatch', domId, 'complete', clipText );
141 private function saveComplete(event:Event):void {
142 ExternalInterface.call( 'ZeroClipboard.dispatch', domId, 'complete', clipText );
146 private function getProp( prop:String, opts:Array ):String
148 var i:int, iLen:int;
149 for ( i=0, iLen=opts.length ; i<iLen ; i++ )
151 if ( opts[i].indexOf( prop+":" ) != -1 )
153 return opts[i].replace( prop+":", "" );
156 return "";
161 * Function: strToUTF8
162 * Purpose: Convert a string to the output utf-8
163 * Returns: ByteArray
164 * Inputs: String
166 private function strToUTF8( str:String ):ByteArray {
167 var utf8:ByteArray = new ByteArray();
169 /* BOM first */
170 if ( incBom ) {
171 utf8.writeByte( 0xEF );
172 utf8.writeByte( 0xBB );
173 utf8.writeByte( 0xBF );
175 utf8.writeUTFBytes( str );
177 return utf8;
182 * Function: strToUTF16LE
183 * Purpose: Convert a string to the output utf-16
184 * Returns: ByteArray
185 * Inputs: String
186 * Notes: The fact that this function is needed is a little annoying. Basically, strings in
187 * AS3 are UTF-16 (with surrogate pairs and everything), but characters which take up less
188 * than 8 bytes appear to be stored as only 8 bytes. This function effective adds the
189 * padding required, and the BOM
191 private function strToUTF16LE( str:String ):ByteArray {
192 var utf16:ByteArray = new ByteArray();
193 var iChar:uint;
194 var i:uint=0, iLen:uint = str.length;
196 /* BOM first */
197 if ( incBom ) {
198 utf16.writeByte( 0xFF );
199 utf16.writeByte( 0xFE );
202 while ( i < iLen ) {
203 iChar = str.charCodeAt(i);
205 if ( iChar < 0xFF ) {
206 /* one byte char */
207 utf16.writeByte( iChar );
208 utf16.writeByte( 0 );
209 } else {
210 /* two byte char */
211 utf16.writeByte( iChar & 0x00FF );
212 utf16.writeByte( iChar >> 8 );
215 i++;
218 return utf16;