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;
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';
30 stage.scaleMode = StageScaleMode.EXACT_FIT;
31 flash.system.Security.allowDomain(
"*");
34 var flashvars:Object = LoaderInfo( this.root.loaderInfo ).parameters;
35 domId = flashvars.id.split(
"\\").join(
"\\\\");
38 if ( domId !== parseInt( domId, 10 ).toString() ) {
39 throw new Error(
'Invalid DOM id' );
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);
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 );
55 button.addEventListener(MouseEvent.MOUSE_OUT,
function(event:Event):
void {
56 ExternalInterface.call(
'ZeroClipboard_TableTools.dispatch', domId,
'mouseOut', null );
58 button.addEventListener(MouseEvent.MOUSE_DOWN,
function(event:Event):
void {
59 ExternalInterface.call(
'ZeroClipboard_TableTools.dispatch', domId,
'mouseDown', null );
61 button.addEventListener(MouseEvent.MOUSE_UP,
function(event:Event):
void {
62 ExternalInterface.call(
'ZeroClipboard_TableTools.dispatch', domId,
'mouseUp', null );
67 stage.addEventListener(Event.ACTIVATE, addCallbacks);
70 ExternalInterface.call(
'ZeroClipboard_TableTools.dispatch', domId,
'load', null );
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);
85 public function setCharSet(newCharSet:String):void {
86 if ( newCharSet ==
'UTF16LE' ) {
93 public function setBomInc(newBomInc:Boolean):void {
97 public function clearText():void {
101 public function appendText(newText:String):void {
105 public function setText(newText:String):void {
109 public function setFileName(newFileName:String):void {
110 fileName = newFileName;
113 public function setAction(newAction:String):void {
117 public function setHandCursor(enabled:Boolean):void {
120 button.useHandCursor = enabled;
124 private function clickHandler(event:Event):void {
125 var fileRef:FileReference =
new FileReference();
126 fileRef.addEventListener(Event.COMPLETE, saveComplete);
128 if ( action ==
"save" ) {
130 if ( charSet ==
'UTF16LE' ) {
131 fileRef.save( strToUTF16LE(clipText), fileName );
133 fileRef.save( strToUTF8(clipText), fileName );
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" );
140 System.setClipboard( clipText );
141 ExternalInterface.call(
'ZeroClipboard_TableTools.dispatch', domId,
'complete', clipText );
146 private function saveComplete(event:Event):void {
147 ExternalInterface.call(
'ZeroClipboard_TableTools.dispatch', domId,
'complete', clipText );
151 private function getProp( prop:String, opts:Array ):String
154 for ( i=0, iLen=opts.length ; i<iLen ; i++ )
156 if ( opts[i].indexOf( prop+
":" ) != -1 )
158 return opts[i].replace( prop+
":",
"" );
171 private function strToUTF8( str:String ):ByteArray {
172 var utf8:ByteArray =
new ByteArray();
176 utf8.writeByte( 0xEF );
177 utf8.writeByte( 0xBB );
178 utf8.writeByte( 0xBF );
180 utf8.writeUTFBytes( str );
196 private function strToUTF16LE( str:String ):ByteArray {
197 var utf16:ByteArray =
new ByteArray();
199 var i:uint=0, iLen:uint = str.length;
203 utf16.writeByte( 0xFF );
204 utf16.writeByte( 0xFE );
208 iChar = str.charCodeAt(i);
210 if ( iChar < 0xFF ) {
212 utf16.writeByte( iChar );
213 utf16.writeByte( 0 );
216 utf16.writeByte( iChar & 0x00FF );
217 utf16.writeByte( iChar >> 8 );