/app/webroot/js/lib/zeroClipboard/src/flash/ZeroClipboard.as

https://github.com/thiagosilr/plock · ActionScript · 189 lines · 83 code · 30 blank · 76 comment · 4 complexity · a7bd3f1b92af92bdf92af19e12006024 MD5 · raw file

  1. package {
  2. import flash.display.Stage;
  3. import flash.display.Sprite;
  4. import flash.display.LoaderInfo;
  5. import flash.events.*;
  6. import flash.external.ExternalInterface;
  7. import flash.utils.*;
  8. import flash.system.Capabilities;
  9. // ZeroClipboard
  10. //
  11. // The ZeroClipboard class creates a simple sprite button that will put
  12. // text in your clipboard when clicked
  13. //
  14. // returns nothing
  15. public class ZeroClipboard extends Sprite {
  16. // The button sprite
  17. private var button:Sprite;
  18. // The text in the clipboard
  19. private var clipText:String = "";
  20. // constructor, setup event listeners and external interfaces
  21. public function ZeroClipboard() {
  22. // Align the stage to top left
  23. stage.align = "TL";
  24. stage.scaleMode = "noScale";
  25. // Get the flashvars
  26. var flashvars:Object = LoaderInfo( this.root.loaderInfo ).parameters;
  27. // Allow the swf object to be run on any domain, for when the site hosts the file on a separate server
  28. if (flashvars.trustedDomain) {
  29. flash.system.Security.allowDomain(flashvars.trustedDomain.split("\\").join("\\\\"));
  30. }
  31. // invisible button covers entire stage
  32. button = new Sprite();
  33. button.buttonMode = true;
  34. button.useHandCursor = false;
  35. button.graphics.beginFill(0xCCFF00);
  36. button.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
  37. button.alpha = 0.0;
  38. addChild(button);
  39. // Adding the event listeners
  40. button.addEventListener(MouseEvent.CLICK, mouseClick);
  41. button.addEventListener(MouseEvent.MOUSE_OVER, mouseOver);
  42. button.addEventListener(MouseEvent.MOUSE_OUT, mouseOut);
  43. button.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
  44. button.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
  45. // external functions
  46. ExternalInterface.addCallback("setHandCursor", setHandCursor);
  47. ExternalInterface.addCallback("setText", setText);
  48. ExternalInterface.addCallback("setSize", setSize);
  49. // signal to the browser that we are ready
  50. ExternalInterface.call( 'ZeroClipboard.dispatch', 'load', metaData());
  51. }
  52. // mouseClick
  53. //
  54. // The mouseClick private function handles clearing the clipboard, and
  55. // setting new clip text. It gets this from the clipText private variable.
  56. // Once the text has been placed in the clipboard, It then signals to the
  57. // Javascript that it is done.
  58. //
  59. // returns nothing
  60. private function mouseClick(event:MouseEvent): void {
  61. // Linux currently doesn't use the correct clipboard buffer with the new
  62. // Flash 10 API, so we need to use this until we can figure out an alternative
  63. flash.system.System.setClipboard(clipText);
  64. // signal to the page it is done
  65. ExternalInterface.call( 'ZeroClipboard.dispatch', 'complete', metaData(event, {
  66. text: clipText.split("\\").join("\\\\")
  67. }));
  68. // reset the text
  69. clipText = "";
  70. }
  71. // mouseOver
  72. //
  73. // The mouseOver function signals to the page that the button is being hovered.
  74. //
  75. // returns nothing
  76. private function mouseOver(event:MouseEvent): void {
  77. ExternalInterface.call( 'ZeroClipboard.dispatch', 'mouseOver', metaData(event) );
  78. }
  79. // mouseOut
  80. //
  81. // The mouseOut function signals to the page that the button is not being hovered.
  82. //
  83. // returns nothing
  84. private function mouseOut(event:MouseEvent): void {
  85. ExternalInterface.call( 'ZeroClipboard.dispatch', 'mouseOut', metaData(event) );
  86. }
  87. // mouseDown
  88. //
  89. // The mouseDown function signals to the page that the button has a mouse button down.
  90. //
  91. // returns nothing
  92. private function mouseDown(event:MouseEvent): void {
  93. ExternalInterface.call( 'ZeroClipboard.dispatch', 'mouseDown', metaData(event) );
  94. // if the clipText hasn't been set
  95. if (!clipText) {
  96. // request data from the page
  97. ExternalInterface.call( 'ZeroClipboard.dispatch', 'dataRequested', metaData(event) );
  98. }
  99. }
  100. // mouseUp
  101. //
  102. // The mouseUp function signals to the page that the mouse button has been lifted
  103. //
  104. // returns nothing
  105. private function mouseUp(event:MouseEvent): void {
  106. ExternalInterface.call( 'ZeroClipboard.dispatch', 'mouseUp', metaData(event) );
  107. }
  108. // metaData
  109. //
  110. // The metaData function will take a mouseEvent, and an extra object to
  111. // create a meta object of more info. This will let the page know if
  112. // certain modifier keys are down
  113. //
  114. // returns an Object of extra event data
  115. private function metaData(event:MouseEvent = void, extra:Object = void):Object {
  116. // create the default options, contains flash version
  117. var normalOptions:Object = {
  118. flashVersion : Capabilities.version
  119. }
  120. // if an event is passed in, return what modifier keys are pressed
  121. if (event) {
  122. normalOptions.altKey = event.altKey;
  123. normalOptions.ctrlKey = event.ctrlKey;
  124. normalOptions.shiftKey = event.shiftKey;
  125. }
  126. // for everything in the extra object, add it to the normal options
  127. for(var i:String in extra) {
  128. normalOptions[i] = extra[i];
  129. }
  130. return normalOptions;
  131. }
  132. // setText
  133. //
  134. // setText gets the clipboard text to be copied from the javascript.
  135. //
  136. // returns nothing
  137. public function setText(newText:String): void {
  138. // set the maximum number of files allowed
  139. clipText = newText;
  140. }
  141. // setHandCursor
  142. //
  143. // setHandCursor will make the button cursor be a hand on hover.
  144. //
  145. // returns nothing
  146. public function setHandCursor(enabled:Boolean): void {
  147. button.useHandCursor = enabled;
  148. }
  149. // setSize
  150. //
  151. // Sets the size of the button to equal the size of the hovered object.
  152. //
  153. // returns nothing
  154. public function setSize(width:Number, height:Number): void {
  155. button.width = width;
  156. button.height = height;
  157. }
  158. }
  159. }