JavaScriptProxy.as 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. JavaScriptProxy.as
  3. Part of the Flash / JavaScript Integration Kit
  4. http://www.macromedia.com/go/flashjavascript
  5. Created by:
  6. Mike Chambers
  7. http://weblogs.macromedia.com/mesh/
  8. mesh@macromedia.com
  9. Christian Cantrell
  10. http://weblogs.macromedia.com/cantrell/
  11. cantrell@macromedia.com
  12. ----
  13. Macromedia(r) Flash(r)./ JavaScript Integration Kit License
  14. Copyright (c) 2005 Macromedia, inc. All rights reserved.
  15. Redistribution and use in source and binary forms, with or without modification,
  16. are permitted provided that the following conditions are met:
  17. 1. Redistributions of source code must retain the above copyright notice, this
  18. list of conditions and the following disclaimer.
  19. 2. Redistributions in binary form must reproduce the above copyright notice,
  20. this list of conditions and the following disclaimer in the documentation and/or
  21. other materials provided with the distribution.
  22. 3. The end-user documentation included with the redistribution, if any, must
  23. include the following acknowledgment:
  24. "This product includes software developed by Macromedia, Inc.
  25. (http://www.macromedia.com)."
  26. Alternately, this acknowledgment may appear in the software itself, if and
  27. wherever such third-party acknowledgments normally appear.
  28. 4. The name Macromedia must not be used to endorse or promote products derived
  29. from this software without prior written permission. For written permission,
  30. please contact devrelations@macromedia.com.
  31. 5. Products derived from this software may not be called "Macromedia" or
  32. “Macromedia Flash”, nor may "Macromedia" or “Macromedia Flash” appear in their
  33. name.
  34. THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MACROMEDIA OR
  37. ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  38. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  39. OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  40. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  42. OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  43. DAMAGE.
  44. */
  45. //import the serializer class
  46. import com.macromedia.javascript.JavaScriptSerializer;
  47. class com.macromedia.javascript.JavaScriptProxy
  48. {
  49. private var instance:Object;
  50. private var lcId:String;
  51. private var receiving_lc:LocalConnection
  52. /*
  53. Constructor for Class
  54. Public
  55. Takes two arguments.
  56. lcId : REQUIRED : an id passed in from HTML / Javascript that is used to
  57. communicate with the gateway swf. The same id must be passed into
  58. the gateway swf.
  59. instance : the object / class instance that function call will be proxied to.
  60. This is required if function calls will be made from JavaScript
  61. to Flash
  62. */
  63. function JavaScriptProxy(lcId:String, instance:Object)
  64. {
  65. //if either argument is undefined, JavaScript to Flash calls won't work.
  66. //So we just return.
  67. //
  68. //Flash to JavaScript calls will still work
  69. if(lcId == undefined || instance == undefined)
  70. {
  71. return;
  72. }
  73. this.instance = instance;
  74. this.lcId = lcId;
  75. receiving_lc = new LocalConnection();
  76. //the incoming function call will occur in the scope of receiving_lc, so we have
  77. //to set a property to let us get back to the correct scope.
  78. receiving_lc.controller = this;
  79. receiving_lc.callFlash = callFlash;
  80. //listen for incoming function calls
  81. receiving_lc.connect(this.lcId);
  82. }
  83. /*
  84. callFlash
  85. Private
  86. This is called by the FlashProxy in JavaScript to make a functon call into
  87. the Flash content.
  88. */
  89. private function callFlash(args:Array):Void
  90. {
  91. //get a reference to the correct scope (this method is called in the scope
  92. //of the local connection object)
  93. var con:Object = this["controller"];
  94. var functionName:Object = args.shift();
  95. var f:Function = con.instance[functionName];
  96. //call the function in the correct scope, passing the arguments
  97. f.apply(con.instance, args);
  98. }
  99. /*
  100. This proxies function calls to the server, which allows you to call JavaScript
  101. functions as if they were functions on JavaScriptProxy instance.
  102. i.e.
  103. var j:JavaScriptProxy = new JavaScriptProxy();
  104. j.jsFunction("foo", [1, 2]);
  105. */
  106. public function __resolve(functionName:String):Function
  107. {
  108. var f:Function = function()
  109. {
  110. arguments.splice(0,0, functionName);
  111. var f:Function = call;
  112. f.apply(this, arguments);
  113. };
  114. return f;
  115. }
  116. /*
  117. call
  118. public
  119. This is used to call functions within JavaScript.
  120. functionName : A string of the name of the function being called in JavaScript.
  121. a1, a2 ... an : subsequesnt arguments will be passed to the JavaScript function.
  122. Example:
  123. var j:JavaScriptProxy = new JavaScriptProxy();
  124. j.call("jsFunction", "foo", [1, 2]);
  125. */
  126. public function call(functionName:String):Void
  127. {
  128. var len:Number = arguments.length;
  129. var argsString:String = "";
  130. //Serialize the arguments
  131. for(var i:Number = 0; i < len; i++)
  132. {
  133. argsString += JavaScriptSerializer.serializeItem(arguments[i]);
  134. if(i != len - 1)
  135. {
  136. argsString += ",";
  137. }
  138. }
  139. //Created the javascript URL
  140. var callString:String = "javascript:FlashProxy.callJS(" + argsString + ");";
  141. //call out into the HTML / JavaScript environment
  142. getURL(callString);
  143. }
  144. }