PageRenderTime 34ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/source/core/aw/external/jsinterface/JSArguments.as

http://jsinterface.googlecode.com/
ActionScript | 317 lines | 87 code | 17 blank | 213 comment | 22 complexity | 543b9dda5102c7efb09cee6e5acf466c MD5 | raw file
  1. package aw.external.jsinterface{
  2. import flash.utils.Dictionary;
  3. /**
  4. * ItÂ’s a class used upon calling functions from Flash Player environment in JavaScript environment.
  5. * In the usual case a function from Flash Player environment called in JavaScript environment gets
  6. * the whole set of arguments transferred to the function in JavaScript. But such function cannot
  7. * get access to the target object in the scope of which it was executed. JSArguments contains a list
  8. * of function arguments and a reference to the wrapper object of the JavaScript object in the scope
  9. * of which it was executed.
  10. *
  11. * For the function to receive the JSArguments objects the method should be registered with JSArguments.register();
  12. * @example
  13. <listing version="3.0">
  14. package{
  15. import aw.external.JSInterface;
  16. import aw.external.jsinterface.JSArguments;
  17. import flash.display.Sprite;
  18. public class JSArgumentsExample extends Sprite{
  19. public function JSArgumentsExample():void{
  20. super();
  21. JSInterface.initialize(this);
  22. JSArguments.registerByTarget(this, this.test);
  23. // setup method
  24. JSInterface.window.test = this.test;
  25. // call method
  26. JSInterface.window.test();
  27. }
  28. protected function test(args:JSArguments):void{
  29. trace(args.target); // [object Window]
  30. }
  31. }
  32. }
  33. </listing>
  34. *
  35. * @public
  36. * @author Galaburda a_[w] Oleg http://www.actualwave.com
  37. */
  38. public dynamic class JSArguments extends Array{
  39. /**
  40. *
  41. *
  42. *
  43. * @private (constant)
  44. * @langversion ActionScript 3.0
  45. * @playerversion Flash 9.0.28.0
  46. */
  47. static private const _argumented:Dictionary = new Dictionary(true);
  48. /**
  49. *
  50. *
  51. *
  52. * @private (protected)
  53. * @see aw.external.jsinterface.JSDynamic
  54. * @langversion ActionScript 3.0
  55. * @playerversion Flash 9.0.28.0
  56. */
  57. protected var _target:JSDynamic;
  58. /**
  59. *
  60. *
  61. *
  62. * @public
  63. * @param args
  64. * @param target
  65. * @return void
  66. * @see aw.external.jsinterface.JSDynamic
  67. * @langversion ActionScript 3.0
  68. * @playerversion Flash 9.0.28.0
  69. */
  70. public function JSArguments(args:Array=null, target:JSDynamic=null):void{
  71. super();
  72. setArguments(args, target);
  73. }
  74. /**
  75. * JSDynamic object, wrapper of JavaScript object in the scope of which was performed this function.
  76. *
  77. *
  78. * @public (AS3,getter)
  79. * @return JSDynamic
  80. * @see aw.external.jsinterface.JSDynamic
  81. * @langversion ActionScript 3.0
  82. * @playerversion Flash 9.0.28.0
  83. */
  84. AS3 function get target():JSDynamic{
  85. return this._target;
  86. }
  87. /**
  88. * Set parameters of the object. The method is performed before sending the object to the function.
  89. *
  90. *
  91. * @public (AS3)
  92. * @param args
  93. * @param target
  94. * @return void
  95. * @see aw.external.jsinterface.JSDynamic
  96. * @langversion ActionScript 3.0
  97. * @playerversion Flash 9.0.28.0
  98. */
  99. AS3 function setArguments(args:Array, target:JSDynamic=null):void{
  100. this._target = target;
  101. if(args) this.push.apply(this, args);
  102. }
  103. /**
  104. * Registers the method that should accept a JSArguments object as an argument.
  105. * This method allows indicating a class derivative from JSArguments, instances
  106. * of which will be transferred to the indicated method. The functions registered
  107. * with JSArguments.register() method will be automatically removed with the object
  108. * when Garbage Collector will be clearing the memory.
  109. *
  110. * @public
  111. * @param target The object that owns the method.
  112. * @param name Maybe Function - refers to a method or a String or QName - the name of the method of transmited object.
  113. * @param definition Class which extends JSArguments, copies of which will be passed to the function.
  114. * @return void
  115. * @langversion ActionScript 3.0
  116. * @playerversion Flash 9.0.28.0
  117. */
  118. static public function registerByTarget(target:Object, name:*, definition:Class=null):void{
  119. var list:Dictionary;
  120. if(target in _argumented) list = _argumented[target];
  121. else _argumented[target] = list = new Dictionary();
  122. if(!definition) definition = JSArguments;
  123. list[name is Function ? name : target[name]] = definition;
  124. }
  125. /**
  126. * Register method by the link. A function registered by this
  127. * method can be removed only by hand, otherwise it will never be removed.
  128. *
  129. *
  130. * @public
  131. * @param method Link to the method.
  132. * @param definition Class which extends JSArguments, copies of which will be passed to the function.
  133. * @return void
  134. * @langversion ActionScript 3.0
  135. * @playerversion Flash 9.0.28.0
  136. */
  137. static public function register(method:Function, definition:Class=null):void{
  138. var list:Dictionary;
  139. var target:Object = null;
  140. if(target in _argumented) list = _argumented[target];
  141. else _argumented[target] = list = new Dictionary();
  142. if(!definition) definition = JSArguments;
  143. list[method] = definition;
  144. }
  145. /**
  146. * Register a list of methods of one object which hosts as an argument object JSArguments
  147. *
  148. *
  149. * @public
  150. * @param target The object which owns transmitted methods.
  151. * @param args A list of methods or their names.
  152. * @return void
  153. * @langversion ActionScript 3.0
  154. * @playerversion Flash 9.0.28.0
  155. */
  156. static public function registerList(target:Object, ...args:Array):void{
  157. var list:Dictionary;
  158. if(target in _argumented) list = _argumented[target];
  159. else _argumented[target] = list = new Dictionary();
  160. for each(var name:* in args) list[name is Function ? name : target[name]] = JSArguments;
  161. }
  162. /**
  163. * Checks whether the method is registered.
  164. *
  165. *
  166. * @public
  167. * @param target The object that owns the method.
  168. * @param name Maybe Function - refers to a method or a String or QName - the name of the method of transmited object.
  169. * @return Boolean
  170. * @langversion ActionScript 3.0
  171. * @playerversion Flash 9.0.28.0
  172. */
  173. static public function isRegisteredByTarget(target:Object, name:*):Boolean{
  174. return target in _argumented && (name is Function ? name : target[name]) in _argumented[target];
  175. }
  176. /**
  177. *
  178. *
  179. * @public
  180. * @param method Link to the method.
  181. * @return Boolean
  182. * @langversion ActionScript 3.0
  183. * @playerversion Flash 9.0.28.0
  184. */
  185. static public function isRegistered(method:Function):Boolean{
  186. for each(var list:Dictionary in _argumented){
  187. if(method in list) return true;
  188. }
  189. return false;
  190. }
  191. /**
  192. * Returns the class, copies of which will be transferred using this method. If the method was not registered, it will return NULL.
  193. *
  194. *
  195. * @public
  196. * @param target The object that owns the method.
  197. * @param name Maybe Function - refers to a method or a String or QName - the name of the method of transmited object.
  198. * @return Class
  199. * @langversion ActionScript 3.0
  200. * @playerversion Flash 9.0.28.0
  201. */
  202. static public function getDefinitionByTarget(target:Object, name:*):Class{
  203. if(target in _argumented){
  204. var list:Dictionary = _argumented[target];
  205. var method:Function = name is Function ? name : target[name];
  206. if(method in list) return list[method];
  207. }
  208. return null;
  209. }
  210. /**
  211. *
  212. *
  213. * @public
  214. * @param method Link to the method.
  215. * @return Class
  216. * @langversion ActionScript 3.0
  217. * @playerversion Flash 9.0.28.0
  218. */
  219. static public function getDefinition(method:Function):Class{
  220. for each(var list:Dictionary in _argumented){
  221. if(method in list) return list[method];
  222. }
  223. return null;
  224. }
  225. /**
  226. * Removes method registration.
  227. *
  228. *
  229. * @public
  230. * @param target The object that owns the method.
  231. * @param name Maybe Function - refers to a method or a String or QName - the name of the method of transmited object.
  232. * @return Boolean
  233. * @langversion ActionScript 3.0
  234. * @playerversion Flash 9.0.28.0
  235. */
  236. static public function unregisterByTarget(target:Object, name:*):Boolean{
  237. if(target in _argumented){
  238. return delete _argumented[target][name is Function ? name : target[name]];
  239. }
  240. return false;
  241. }
  242. /**
  243. *
  244. *
  245. * @public
  246. * @param method Link to the method.
  247. * @return Boolean
  248. * @langversion ActionScript 3.0
  249. * @playerversion Flash 9.0.28.0
  250. */
  251. static public function unregister(method:Function):Boolean{
  252. for each(var list:Dictionary in _argumented){
  253. if(method in list) return delete list[method];
  254. }
  255. return false;
  256. }
  257. /**
  258. * Creates a copy of JSArguments by passed parameters.
  259. *
  260. *
  261. * @private
  262. * @param method Link to the method.
  263. * @param args The list of arguments of function.
  264. * @param target The object concerning which function has been called.
  265. * @return JSArguments
  266. * @see aw.external.jsinterface.JSDynamic
  267. * @see aw.external.jsinterface.JSArguments
  268. * @langversion ActionScript 3.0
  269. * @playerversion Flash 9.0.28.0
  270. */
  271. static public function create(method:Function, args:Array, target:JSDynamic):JSArguments{
  272. var definition:Class = getDefinition(method);
  273. var result:JSArguments;
  274. if(!definition || definition==JSArguments) result = new JSArguments(args, target);
  275. else{
  276. result = new definition();
  277. result.setArguments(args, target);
  278. }
  279. return result;
  280. }
  281. /**
  282. * If the transferred method is registered, will return JSArguments object and if is not present - the same list of arguments.
  283. *
  284. *
  285. * @private
  286. * @param method Link to the method.
  287. * @param args The list of arguments of function.
  288. * @param target ?????? ???????????? ???????? ???? ??????? ???????.
  289. * @return Array
  290. * @see aw.external.jsinterface.JSDynamic
  291. * @langversion ActionScript 3.0
  292. * @playerversion Flash 9.0.28.0
  293. */
  294. static public function convert(method:Function, args:Array, target:JSDynamic):Array{
  295. return isRegistered(method) ? create(method, args, target) : args;
  296. }
  297. }
  298. }