OpenSencillo  2016.106
Long live the simplicity of PHP
 All Data Structures Namespaces Files Functions Pages
ZeroClipboard.as
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 
17  public class ZeroClipboard extends Sprite {
18 
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';
26 
27 
28  public function ZeroClipboard() {
29  // constructor, setup event listeners and external interfaces
30  stage.scaleMode = StageScaleMode.EXACT_FIT;
31  flash.system.Security.allowDomain("*");
32 
33  // import flashvars
34  var flashvars:Object = LoaderInfo( this.root.loaderInfo ).parameters;
35  domId = flashvars.id.split("\\").join("\\\\");
36 
37  // Validate id to prevent scripting attacks. The id given is an integer
38  if ( domId !== parseInt( domId, 10 ).toString() ) {
39  throw new Error( 'Invalid DOM id' );
40  }
41 
42  // invisible button covers entire stage
43  button = new Sprite();
44  button.buttonMode = true;
45  button.useHandCursor = true;
46  button.graphics.beginFill(0x00FF00);
47  button.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
48  button.alpha = 0.0;
49  addChild(button);
50 
51  button.addEventListener(MouseEvent.CLICK, clickHandler);
52  button.addEventListener(MouseEvent.MOUSE_OVER, function(event:Event):void {
53  ExternalInterface.call( 'ZeroClipboard_TableTools.dispatch', domId, 'mouseOver', null );
54  } );
55  button.addEventListener(MouseEvent.MOUSE_OUT, function(event:Event):void {
56  ExternalInterface.call( 'ZeroClipboard_TableTools.dispatch', domId, 'mouseOut', null );
57  } );
58  button.addEventListener(MouseEvent.MOUSE_DOWN, function(event:Event):void {
59  ExternalInterface.call( 'ZeroClipboard_TableTools.dispatch', domId, 'mouseDown', null );
60  } );
61  button.addEventListener(MouseEvent.MOUSE_UP, function(event:Event):void {
62  ExternalInterface.call( 'ZeroClipboard_TableTools.dispatch', domId, 'mouseUp', null );
63  } );
64 
65  // External functions - readd whenever the stage is made active for IE
66  addCallbacks();
67  stage.addEventListener(Event.ACTIVATE, addCallbacks);
68 
69  // signal to the browser that we are ready
70  ExternalInterface.call( 'ZeroClipboard_TableTools.dispatch', domId, 'load', null );
71  }
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 
84 
85  public function setCharSet(newCharSet:String):void {
86  if ( newCharSet == 'UTF16LE' ) {
87  charSet = newCharSet;
88  } else {
89  charSet = 'UTF8';
90  }
91  }
92 
93  public function setBomInc(newBomInc:Boolean):void {
94  incBom = newBomInc;
95  }
96 
97  public function clearText():void {
98  clipText = '';
99  }
100 
101  public function appendText(newText:String):void {
102  clipText += newText;
103  }
104 
105  public function setText(newText:String):void {
106  clipText = newText;
107  }
108 
109  public function setFileName(newFileName:String):void {
110  fileName = newFileName;
111  }
112 
113  public function setAction(newAction:String):void {
114  action = newAction;
115  }
116 
117  public function setHandCursor(enabled:Boolean):void {
118  // control whether the hand cursor is shown on rollover (true)
119  // or the default arrow cursor (false)
120  button.useHandCursor = enabled;
121  }
122 
123 
124  private function clickHandler(event:Event):void {
125  var fileRef:FileReference = new FileReference();
126  fileRef.addEventListener(Event.COMPLETE, saveComplete);
127 
128  if ( action == "save" ) {
129  /* Save as a file */
130  if ( charSet == 'UTF16LE' ) {
131  fileRef.save( strToUTF16LE(clipText), fileName );
132  } else {
133  fileRef.save( strToUTF8(clipText), fileName );
134  }
135  } else if ( action == "pdf" ) {
136  fileRef.save( "This instance of ZeroClipboard is not configured for PDF export. "+
137  "Please use the PDF export version.", fileName+".txt" );
138  } else {
139  /* Copy the text to the clipboard. Note charset and BOM have no effect here */
140  System.setClipboard( clipText );
141  ExternalInterface.call( 'ZeroClipboard_TableTools.dispatch', domId, 'complete', clipText );
142  }
143  }
144 
145 
146  private function saveComplete(event:Event):void {
147  ExternalInterface.call( 'ZeroClipboard_TableTools.dispatch', domId, 'complete', clipText );
148  }
149 
150 
151  private function getProp( prop:String, opts:Array ):String
152  {
153  var i:int, iLen:int;
154  for ( i=0, iLen=opts.length ; i<iLen ; i++ )
155  {
156  if ( opts[i].indexOf( prop+":" ) != -1 )
157  {
158  return opts[i].replace( prop+":", "" );
159  }
160  }
161  return "";
162  }
163 
164 
165  /*
166  * Function: strToUTF8
167  * Purpose: Convert a string to the output utf-8
168  * Returns: ByteArray
169  * Inputs: String
170  */
171  private function strToUTF8( str:String ):ByteArray {
172  var utf8:ByteArray = new ByteArray();
173 
174  /* BOM first */
175  if ( incBom ) {
176  utf8.writeByte( 0xEF );
177  utf8.writeByte( 0xBB );
178  utf8.writeByte( 0xBF );
179  }
180  utf8.writeUTFBytes( str );
181 
182  return utf8;
183  }
184 
185 
186  /*
187  * Function: strToUTF16LE
188  * Purpose: Convert a string to the output utf-16
189  * Returns: ByteArray
190  * Inputs: String
191  * Notes: The fact that this function is needed is a little annoying. Basically, strings in
192  * AS3 are UTF-16 (with surrogate pairs and everything), but characters which take up less
193  * than 8 bytes appear to be stored as only 8 bytes. This function effective adds the
194  * padding required, and the BOM
195  */
196  private function strToUTF16LE( str:String ):ByteArray {
197  var utf16:ByteArray = new ByteArray();
198  var iChar:uint;
199  var i:uint=0, iLen:uint = str.length;
200 
201  /* BOM first */
202  if ( incBom ) {
203  utf16.writeByte( 0xFF );
204  utf16.writeByte( 0xFE );
205  }
206 
207  while ( i < iLen ) {
208  iChar = str.charCodeAt(i);
209 
210  if ( iChar < 0xFF ) {
211  /* one byte char */
212  utf16.writeByte( iChar );
213  utf16.writeByte( 0 );
214  } else {
215  /* two byte char */
216  utf16.writeByte( iChar & 0x00FF );
217  utf16.writeByte( iChar >> 8 );
218  }
219 
220  i++;
221  }
222 
223  return utf16;
224  }
225  }
226 }