JavaScriptFlashGateway.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. Macromedia(r) Flash(r) JavaScript Integration Kit License
  3. Copyright (c) 2005 Macromedia, inc. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without modification,
  5. are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice, this
  7. list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation and/or
  10. other materials provided with the distribution.
  11. 3. The end-user documentation included with the redistribution, if any, must
  12. include the following acknowledgment:
  13. "This product includes software developed by Macromedia, Inc.
  14. (http://www.macromedia.com)."
  15. Alternately, this acknowledgment may appear in the software itself, if and
  16. wherever such third-party acknowledgments normally appear.
  17. 4. The name Macromedia must not be used to endorse or promote products derived
  18. from this software without prior written permission. For written permission,
  19. please contact devrelations@macromedia.com.
  20. 5. Products derived from this software may not be called "Macromedia" or
  21. "Macromedia Flash", nor may "Macromedia" or "Macromedia Flash" appear in their
  22. name.
  23. THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  24. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  25. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MACROMEDIA OR
  26. ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  28. OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  30. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  32. DAMAGE.
  33. --
  34. This code is part of the Flash / JavaScript Integration Kit:
  35. http://www.macromedia.com/go/flashjavascript/
  36. Created by:
  37. Christian Cantrell
  38. http://weblogs.macromedia.com/cantrell/
  39. mailto:cantrell@macromedia.com
  40. Mike Chambers
  41. http://weblogs.macromedia.com/mesh/
  42. mailto:mesh@macromedia.com
  43. Macromedia
  44. */
  45. /**
  46. * Create a new Exception object.
  47. * name: The name of the exception.
  48. * message: The exception message.
  49. */
  50. function Exception(name, message)
  51. {
  52. if (name)
  53. this.name = name;
  54. if (message)
  55. this.message = message;
  56. }
  57. /**
  58. * Set the name of the exception.
  59. */
  60. Exception.prototype.setName = function(name)
  61. {
  62. this.name = name;
  63. }
  64. /**
  65. * Get the exception's name.
  66. */
  67. Exception.prototype.getName = function()
  68. {
  69. return this.name;
  70. }
  71. /**
  72. * Set a message on the exception.
  73. */
  74. Exception.prototype.setMessage = function(msg)
  75. {
  76. this.message = msg;
  77. }
  78. /**
  79. * Get the exception message.
  80. */
  81. Exception.prototype.getMessage = function()
  82. {
  83. return this.message;
  84. }
  85. /**
  86. * Generates a browser-specific Flash tag. Create a new instance, set whatever
  87. * properties you need, then call either toString() to get the tag as a string, or
  88. * call write() to write the tag out.
  89. */
  90. /**
  91. * Creates a new instance of the FlashTag.
  92. * src: The path to the SWF file.
  93. * width: The width of your Flash content.
  94. * height: the height of your Flash content.
  95. */
  96. function FlashTag(src, width, height)
  97. {
  98. this.src = src;
  99. this.width = width;
  100. this.height = height;
  101. this.version = '7,0,14,0';
  102. this.id = null;
  103. this.bgcolor = 'ffffff';
  104. this.flashVars = null;
  105. }
  106. /**
  107. * Sets the Flash version used in the Flash tag.
  108. */
  109. FlashTag.prototype.setVersion = function(v)
  110. {
  111. this.version = v;
  112. }
  113. /**
  114. * Sets the ID used in the Flash tag.
  115. */
  116. FlashTag.prototype.setId = function(id)
  117. {
  118. this.id = id;
  119. }
  120. /**
  121. * Sets the background color used in the Flash tag.
  122. */
  123. FlashTag.prototype.setBgcolor = function(bgc)
  124. {
  125. this.bgcolor = bgc;
  126. }
  127. /**
  128. * Sets any variables to be passed into the Flash content.
  129. */
  130. FlashTag.prototype.setFlashvars = function(fv)
  131. {
  132. this.flashVars = fv;
  133. }
  134. /**
  135. * Get the Flash tag as a string.
  136. */
  137. FlashTag.prototype.toString = function()
  138. {
  139. var ie = (navigator.appName.indexOf ("Microsoft") != -1) ? 1 : 0;
  140. var flashTag = new String();
  141. if (ie)
  142. {
  143. flashTag += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ';
  144. if (this.id != null)
  145. {
  146. flashTag += 'id="'+this.id+'" ';
  147. }
  148. flashTag += 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version='+this.version+'" ';
  149. flashTag += 'width="'+this.width+'" ';
  150. flashTag += 'height="'+this.height+'">';
  151. flashTag += '<param name="movie" value="'+this.src+'"/>';
  152. flashTag += '<param name="quality" value="high"/>';
  153. flashTag += '<param name="bgcolor" value="#'+this.bgcolor+'"/>';
  154. if (this.flashVars != null)
  155. {
  156. flashTag += '<param name="flashvars" value="'+this.flashVars+'"/>';
  157. }
  158. flashTag += '</object>';
  159. }
  160. else
  161. {
  162. flashTag += '<embed src="'+this.src+'" ';
  163. flashTag += 'quality="high" ';
  164. flashTag += 'bgcolor="#'+this.bgcolor+'" ';
  165. flashTag += 'width="'+this.width+'" ';
  166. flashTag += 'height="'+this.height+'" ';
  167. flashTag += 'type="application/x-shockwave-flash" ';
  168. if (this.flashVars != null)
  169. {
  170. flashTag += 'flashvars="'+this.flashVars+'" ';
  171. }
  172. if (this.id != null)
  173. {
  174. flashTag += 'name="'+this.id+'" ';
  175. }
  176. flashTag += 'pluginspage="http://www.macromedia.com/go/getflashplayer">';
  177. flashTag += '</embed>';
  178. }
  179. return flashTag;
  180. }
  181. /**
  182. * Write the Flash tag out. Pass in a reference to the document to write to.
  183. */
  184. FlashTag.prototype.write = function(doc)
  185. {
  186. doc.write(this.toString());
  187. }
  188. /**
  189. * The FlashSerializer serializes JavaScript variables of types object, array, string,
  190. * number, date, boolean, null or undefined into XML.
  191. */
  192. /**
  193. * Create a new instance of the FlashSerializer.
  194. * useCdata: Whether strings should be treated as character data. If false, strings are simply XML encoded.
  195. */
  196. function FlashSerializer(useCdata)
  197. {
  198. this.useCdata = useCdata;
  199. }
  200. /**
  201. * Serialize an array into a format that can be deserialized in Flash. Supported data types are object,
  202. * array, string, number, date, boolean, null, and undefined. Returns a string of serialized data.
  203. */
  204. FlashSerializer.prototype.serialize = function(args)
  205. {
  206. var qs = new String();
  207. for (var i = 0; i < args.length; ++i)
  208. {
  209. switch(typeof(args[i]))
  210. {
  211. case 'undefined':
  212. qs += 't'+(i)+'=undf';
  213. break;
  214. case 'string':
  215. qs += 't'+(i)+'=str&d'+(i)+'='+escape(args[i]);
  216. break;
  217. case 'number':
  218. qs += 't'+(i)+'=num&d'+(i)+'='+escape(args[i]);
  219. break;
  220. case 'boolean':
  221. qs += 't'+(i)+'=bool&d'+(i)+'='+escape(args[i]);
  222. break;
  223. case 'object':
  224. if (args[i] == null)
  225. {
  226. qs += 't'+(i)+'=null';
  227. }
  228. else if (args[i] instanceof Date)
  229. {
  230. qs += 't'+(i)+'=date&d'+(i)+'='+escape(args[i].getTime());
  231. }
  232. else // array or object
  233. {
  234. try
  235. {
  236. qs += 't'+(i)+'=xser&d'+(i)+'='+escape(this._serializeXML(args[i]));
  237. }
  238. catch (exception)
  239. {
  240. throw new Exception("FlashSerializationException",
  241. "The following error occurred during complex object serialization: " + exception.getMessage());
  242. }
  243. }
  244. break;
  245. default:
  246. throw new Exception("FlashSerializationException",
  247. "You can only serialize strings, numbers, booleans, dates, objects, arrays, nulls, and undefined.");
  248. }
  249. if (i != (args.length - 1))
  250. {
  251. qs += '&';
  252. }
  253. }
  254. return qs;
  255. }
  256. /**
  257. * Private
  258. */
  259. FlashSerializer.prototype._serializeXML = function(obj)
  260. {
  261. var doc = new Object();
  262. doc.xml = '<fp>';
  263. this._serializeNode(obj, doc, null);
  264. doc.xml += '</fp>';
  265. return doc.xml;
  266. }
  267. /**
  268. * Private
  269. */
  270. FlashSerializer.prototype._serializeNode = function(obj, doc, name)
  271. {
  272. switch(typeof(obj))
  273. {
  274. case 'undefined':
  275. doc.xml += '<undf'+this._addName(name)+'/>';
  276. break;
  277. case 'string':
  278. doc.xml += '<str'+this._addName(name)+'>'+this._escapeXml(obj)+'</str>';
  279. break;
  280. case 'number':
  281. doc.xml += '<num'+this._addName(name)+'>'+obj+'</num>';
  282. break;
  283. case 'boolean':
  284. doc.xml += '<bool'+this._addName(name)+' val="'+obj+'"/>';
  285. break;
  286. case 'object':
  287. if (obj == null)
  288. {
  289. doc.xml += '<null'+this._addName(name)+'/>';
  290. }
  291. else if (obj instanceof Date)
  292. {
  293. doc.xml += '<date'+this._addName(name)+'>'+obj.getTime()+'</date>';
  294. }
  295. else if (obj instanceof Array)
  296. {
  297. doc.xml += '<array'+this._addName(name)+'>';
  298. for (var i = 0; i < obj.length; ++i)
  299. {
  300. this._serializeNode(obj[i], doc, null);
  301. }
  302. doc.xml += '</array>';
  303. }
  304. else
  305. {
  306. doc.xml += '<obj'+this._addName(name)+'>';
  307. for (var n in obj)
  308. {
  309. if (typeof(obj[n]) == 'function')
  310. continue;
  311. this._serializeNode(obj[n], doc, n);
  312. }
  313. doc.xml += '</obj>';
  314. }
  315. break;
  316. default:
  317. throw new Exception("FlashSerializationException",
  318. "You can only serialize strings, numbers, booleans, objects, dates, arrays, nulls and undefined");
  319. break;
  320. }
  321. }
  322. /**
  323. * Private
  324. */
  325. FlashSerializer.prototype._addName= function(name)
  326. {
  327. if (name != null)
  328. {
  329. return ' name="'+name+'"';
  330. }
  331. return '';
  332. }
  333. /**
  334. * Private
  335. */
  336. FlashSerializer.prototype._escapeXml = function(str)
  337. {
  338. if (this.useCdata)
  339. return '<![CDATA['+str+']]>';
  340. else
  341. return str.replace(/&/g,'&amp;').replace(/</g,'&lt;');
  342. }
  343. /**
  344. * The FlashProxy object is what proxies function calls between JavaScript and Flash.
  345. * It handles all argument serialization issues.
  346. */
  347. /**
  348. * Instantiates a new FlashProxy object. Pass in a uniqueID and the name (including the path)
  349. * of the Flash proxy SWF. The ID is the same ID that needs to be passed into your Flash content as lcId.
  350. */
  351. function FlashProxy(uid, proxySwfName)
  352. {
  353. this.uid = uid;
  354. this.proxySwfName = proxySwfName;
  355. this.flashSerializer = new FlashSerializer(false);
  356. }
  357. /**
  358. * Call a function in your Flash content. Arguments should be:
  359. * 1. ActionScript function name to call,
  360. * 2. any number of additional arguments of type object,
  361. * array, string, number, boolean, date, null, or undefined.
  362. */
  363. FlashProxy.prototype.call = function()
  364. {
  365. if (arguments.length == 0)
  366. {
  367. throw new Exception("Flash Proxy Exception",
  368. "The first argument should be the function name followed by any number of additional arguments.");
  369. }
  370. var qs = 'lcId=' + escape(this.uid) + '&functionName=' + escape(arguments[0]);
  371. if (arguments.length > 1)
  372. {
  373. var justArgs = new Array();
  374. for (var i = 1; i < arguments.length; ++i)
  375. {
  376. justArgs.push(arguments[i]);
  377. }
  378. qs += ('&' + this.flashSerializer.serialize(justArgs));
  379. }
  380. var divName = '_flash_proxy_' + this.uid;
  381. if(!document.getElementById(divName))
  382. {
  383. var newTarget = document.createElement("div");
  384. newTarget.id = divName;
  385. document.body.appendChild(newTarget);
  386. }
  387. var target = document.getElementById(divName);
  388. var ft = new FlashTag(this.proxySwfName, 1, 1);
  389. ft.setVersion('6,0,65,0');
  390. ft.setFlashvars(qs);
  391. target.innerHTML = ft.toString();
  392. }
  393. /**
  394. * This is the function that proxies function calls from Flash to JavaScript.
  395. * It is called implicitly.
  396. */
  397. FlashProxy.callJS = function()
  398. {
  399. var functionToCall = eval(arguments[0]);
  400. var argArray = new Array();
  401. for (var i = 1; i < arguments.length; ++i)
  402. {
  403. argArray.push(arguments[i]);
  404. }
  405. functionToCall.apply(functionToCall, argArray);
  406. }