Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3234 mandeep.dh 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;
36
 
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);
45
 
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
			} );
59
 
60
			/* External functions
61
			 * See the PDF version for why this is done this way :-(
62
			 */
63
			addCallbacks();
64
			setInterval( addCallbacks, 1000 );
65
 
66
			// signal to the browser that we are ready
67
			ExternalInterface.call( 'ZeroClipboard.dispatch', domId, 'load', null );
68
		}
69
 
70
		public function addCallbacks ():void {
71
			ExternalInterface.addCallback("setHandCursor", setHandCursor);
72
			ExternalInterface.addCallback("clearText", clearText);
73
			ExternalInterface.addCallback("setText", setText);
74
			ExternalInterface.addCallback("appendText", appendText);
75
			ExternalInterface.addCallback("setFileName", setFileName);
76
			ExternalInterface.addCallback("setAction", setAction);
77
			ExternalInterface.addCallback("setCharSet", setCharSet);
78
			ExternalInterface.addCallback("setBomInc", setBomInc);
79
		}
80
 
81
 
82
		public function setCharSet(newCharSet:String):void {
83
			if ( newCharSet == 'UTF16LE' ) {
84
				charSet = newCharSet;
85
			} else {
86
				charSet = 'UTF8';
87
			}
88
		}
89
 
90
		public function setBomInc(newBomInc:Boolean):void {
91
			incBom = newBomInc;
92
		}
93
 
94
		public function clearText():void {
95
			clipText = '';
96
		}
97
 
98
		public function appendText(newText:String):void {
99
			clipText += newText;
100
		}
101
 
102
		public function setText(newText:String):void {
103
			clipText = newText;
104
		}
105
 
106
		public function setFileName(newFileName:String):void {
107
			fileName = newFileName;
108
		}
109
 
110
		public function setAction(newAction:String):void {
111
			action = newAction;
112
		}
113
 
114
		public function setHandCursor(enabled:Boolean):void {
115
			// control whether the hand cursor is shown on rollover (true)
116
			// or the default arrow cursor (false)
117
			button.useHandCursor = enabled;
118
		}
119
 
120
 
121
		private function clickHandler(event:Event):void {
122
			var fileRef:FileReference = new FileReference();
123
			fileRef.addEventListener(Event.COMPLETE, saveComplete);
124
 
125
			if ( action == "save" ) {
126
				/* Save as a file */
127
				if ( charSet == 'UTF16LE' ) {
128
					fileRef.save( strToUTF16LE(clipText), fileName );
129
				} else {
130
					fileRef.save( strToUTF8(clipText), fileName );
131
				}
132
			} else if ( action == "pdf" ) {
133
					fileRef.save( "This instance of ZeroClipboard is not configured for PDF export. "+
134
						"Please use the PDF export version.", fileName+".txt" );
135
			} else {
136
				/* Copy the text to the clipboard. Note charset and BOM have no effect here */
137
				System.setClipboard( clipText );
138
				ExternalInterface.call( 'ZeroClipboard.dispatch', domId, 'complete', clipText );
139
			}
140
		}
141
 
142
 
143
		private function saveComplete(event:Event):void {
144
			ExternalInterface.call( 'ZeroClipboard.dispatch', domId, 'complete', clipText );
145
		}
146
 
147
 
148
		private function getProp( prop:String, opts:Array ):String
149
		{
150
			var i:int, iLen:int;
151
			for ( i=0, iLen=opts.length ; i<iLen ; i++ )
152
			{
153
				if ( opts[i].indexOf( prop+":" ) != -1 )
154
				{
155
					return opts[i].replace( prop+":", "" );
156
				}
157
			}
158
			return "";
159
		}
160
 
161
 
162
		/*
163
		 * Function: strToUTF8
164
		 * Purpose:  Convert a string to the output utf-8
165
		 * Returns:  ByteArray
166
		 * Inputs:   String
167
		 */
168
		private function strToUTF8( str:String ):ByteArray {
169
			var utf8:ByteArray = new ByteArray();
170
 
171
			/* BOM first */
172
			if ( incBom ) {
173
				utf8.writeByte( 0xEF );
174
				utf8.writeByte( 0xBB );
175
				utf8.writeByte( 0xBF );
176
			}
177
			utf8.writeUTFBytes( str );
178
 
179
			return utf8;
180
		}
181
 
182
 
183
		/*
184
		 * Function: strToUTF16LE
185
		 * Purpose:  Convert a string to the output utf-16
186
		 * Returns:  ByteArray
187
		 * Inputs:   String
188
		 * Notes:    The fact that this function is needed is a little annoying. Basically, strings in
189
		 *   AS3 are UTF-16 (with surrogate pairs and everything), but characters which take up less
190
		 *   than 8 bytes appear to be stored as only 8 bytes. This function effective adds the 
191
		 *   padding required, and the BOM
192
		 */
193
		private function strToUTF16LE( str:String ):ByteArray {
194
			var utf16:ByteArray = new ByteArray();
195
			var iChar:uint;
196
			var i:uint=0, iLen:uint = str.length;
197
 
198
			/* BOM first */
199
			if ( incBom ) {
200
				utf16.writeByte( 0xFF );
201
				utf16.writeByte( 0xFE );
202
			}
203
 
204
			while ( i < iLen ) {
205
				iChar = str.charCodeAt(i);
206
 
207
				if ( iChar < 0xFF ) {
208
					/* one byte char */
209
					utf16.writeByte( iChar );
210
					utf16.writeByte( 0 );
211
				} else {
212
					/* two byte char */
213
					utf16.writeByte( iChar & 0x00FF );
214
					utf16.writeByte( iChar >> 8 );
215
				}
216
 
217
				i++;
218
			}
219
 
220
			return utf16;
221
		}
222
	}
223
}