1 /* Compile using: mxmlc --target-player=10.0.0 ZeroClipboard.as */
3 import flash
.display
.Stage;
4 import flash
.display
.Sprite;
5 import flash
.display
.LoaderInfo;
6 import flash
.display
.StageScaleMode;
8 import flash
.display
.StageAlign;
9 import flash
.display
.StageScaleMode;
10 import flash
.external
.ExternalInterface;
11 import flash
.system
.Security;
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
("*");
34 var flashvars
:Object = LoaderInfo( this.root
.loaderInfo
).parameters
;
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
);
46 button
.addEventListener
(MouseEvent.CLICK
, clickHandler
);
47 button
.addEventListener
(MouseEvent.MOUSE_OVER
, function(event
:Event):void {
48 ExternalInterface.call
( 'ZeroClipboard.dispatch', domId
, 'mouseOver', null );
50 button
.addEventListener
(MouseEvent.MOUSE_OUT
, function(event
:Event):void {
51 ExternalInterface.call
( 'ZeroClipboard.dispatch', domId
, 'mouseOut', null );
53 button
.addEventListener
(MouseEvent.MOUSE_DOWN
, function(event
:Event):void {
54 ExternalInterface.call
( 'ZeroClipboard.dispatch', domId
, 'mouseDown', null );
56 button
.addEventListener
(MouseEvent.MOUSE_UP
, function(event
:Event):void {
57 ExternalInterface.call
( 'ZeroClipboard.dispatch', domId
, 'mouseUp', null );
60 // External functions - readd whenever the stage is made active for IE
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' ) {
88 public function setBomInc
(newBomInc
:Boolean):void {
92 public function clearText
():void {
96 public function appendText
(newText
:String):void {
100 public function setText
(newText
:String):void {
104 public function setFileName
(newFileName
:String):void {
105 fileName
= newFileName
;
108 public function setAction
(newAction
:String):void {
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" ) {
125 if ( charSet
== 'UTF16LE' ) {
126 fileRef
.save
( strToUTF16LE
(clipText
), fileName
);
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" );
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
149 for ( i
=0, iLen
=opts
.length
; i
<iLen
; i
++ )
151 if ( opts
[i
].indexOf
( prop
+":" ) != -1 )
153 return opts
[i
].replace
( prop
+":", "" );
161 * Function: strToUTF8
162 * Purpose: Convert a string to the output utf-8
166 private function strToUTF8
( str
:String ):ByteArray {
167 var utf8
:ByteArray = new ByteArray();
171 utf8
.writeByte
( 0xEF );
172 utf8
.writeByte
( 0xBB );
173 utf8
.writeByte
( 0xBF );
175 utf8
.writeUTFBytes
( str
);
182 * Function: strToUTF16LE
183 * Purpose: Convert a string to the output utf-16
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();
194 var i
:uint=0, iLen
:uint = str
.length
;
198 utf16
.writeByte
( 0xFF );
199 utf16
.writeByte
( 0xFE );
203 iChar
= str
.charCodeAt
(i
);
205 if ( iChar
< 0xFF ) {
207 utf16
.writeByte
( iChar
);
208 utf16
.writeByte
( 0 );
211 utf16
.writeByte
( iChar
& 0x00FF );
212 utf16
.writeByte
( iChar
>> 8 );