2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
8 YUI.add('swf', function(Y) {
11 * Embed a Flash applications in a standard manner and communicate with it
12 * via External Interface.
17 SWFDetect = Y.SWFDetect,
23 FLASH_CID = "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000",
24 FLASH_TYPE = "application/x-shockwave-flash",
25 FLASH_VER = "10.0.22",
26 EXPRESS_INSTALL_URL = "http://fpdownload.macromedia.com/pub/flashplayer/update/current/swf/autoUpdater.swf?" + Math.random(),
27 EVENT_HANDLER = "SWF.eventHandler",
28 possibleAttributes = {align:"", allowFullScreen:"", allowNetworking:"", allowScriptAccess:"", base:"", bgcolor:"", menu:"", name:"", quality:"", salign:"", scale:"", tabindex:"", wmode:""};
31 * The SWF utility is a tool for embedding Flash applications in HTML pages.
34 * @requires event-custom, node, swfdetect
38 * Creates the SWF instance and keeps the configuration data
41 * @augments Y.Event.Target
43 * @param {String|HTMLElement} id The id of the element, or the element itself that the SWF will be inserted into.
44 * The width and height of the SWF will be set to the width and height of this container element.
45 * @param {String} swfURL The URL of the SWF to be embedded into the page.
46 * @param {Object} p_oAttributes (optional) Configuration parameters for the Flash application and values for Flashvars
47 * to be passed to the SWF. The p_oAttributes object allows the following additional properties:
49 * <dt>version : String</dt>
50 * <dd>The minimum version of Flash required on the user's machine.</dd>
51 * <dt>fixedAttributes : Object</dt>
52 * <dd>An object literal containing one or more of the following String keys and their values: <code>align,
53 * allowFullScreen, allowNetworking, allowScriptAccess, base, bgcolor, menu, name, quality, salign, scale,
54 * tabindex, wmode.</code> event from the thumb</dd>
58 function SWF (p_oElement /*:String*/, swfURL /*:String*/, p_oAttributes /*:Object*/ ) {
60 this._id = Y.guid("yuiswf");
64 var oElement = Node.one(p_oElement);
66 var flashVersion = p_oAttributes["version"] || FLASH_VER;
67 var flashVersionSplit = (flashVersion + '').split(".");
68 var isFlashVersionRight = SWFDetect.isFlashVersionAtLeast(parseInt(flashVersionSplit[0]), parseInt(flashVersionSplit[1]), parseInt(flashVersionSplit[2]));
69 var canExpressInstall = (SWFDetect.isFlashVersionAtLeast(8,0,0));
70 var shouldExpressInstall = canExpressInstall && !isFlashVersionRight && p_oAttributes["useExpressInstall"];
71 var flashURL = (shouldExpressInstall)?EXPRESS_INSTALL_URL:swfURL;
72 var objstring = '<object ';
74 var flashvarstring = "yId=" + Y.id + "&YUISwfId=" + _id + "&YUIBridgeCallback=" + EVENT_HANDLER + "&allowedDomain=" + document.location.hostname;
76 Y.SWF._instances[_id] = this;
77 if (oElement && (isFlashVersionRight || shouldExpressInstall) && flashURL) {
78 objstring += 'id="' + _id + '" ';
80 objstring += 'classid="' + FLASH_CID + '" ';
83 objstring += 'type="' + FLASH_TYPE + '" data="' + flashURL + '" ';
89 objstring += 'width="' + w + '" height="' + h + '">';
92 objstring += '<param name="movie" value="' + flashURL + '"/>';
95 for (var attribute in p_oAttributes.fixedAttributes) {
96 if (possibleAttributes.hasOwnProperty(attribute)) {
97 objstring += '<param name="' + attribute + '" value="' + p_oAttributes.fixedAttributes[attribute] + '"/>';
101 for (var flashvar in p_oAttributes.flashVars) {
102 var fvar = p_oAttributes.flashVars[flashvar];
103 if (Lang.isString(fvar)) {
104 flashvarstring += "&" + flashvar + "=" + encodeURIComponent(fvar);
108 if (flashvarstring) {
109 objstring += '<param name="flashVars" value="' + flashvarstring + '"/>';
112 objstring += "</object>";
113 oElement.setContent(objstring);
115 this._swf = Node.one("#" + _id);
118 * Fired when the Flash player version on the user's machine is below the required value.
120 * @event wrongflashversion
124 event.type = "wrongflashversion";
125 this.publish("wrongflashversion", {fireOnce:true});
126 this.fire("wrongflashversion", event);
132 * The static collection of all instances of the SWFs on the page.
133 * @property _instances
137 SWF._instances = SWF._instances || {};
141 * Handles an event coming from within the SWF and delegate it
142 * to a specific instance of SWF.
143 * @method eventHandler
144 * @param swfid {String} the id of the SWF dispatching the event
145 * @param event {Object} the event being transmitted.
147 SWF.eventHandler = function (swfid, event) {
148 SWF._instances[swfid]._eventHandler(event);
155 * Propagates a specific event from Flash to JS.
156 * @method _eventHandler
157 * @param event {Object} The event to be propagated from Flash.
160 _eventHandler: function(event)
162 if (event.type === "swfReady")
164 this.publish("swfReady", {fireOnce:true});
165 this.fire("swfReady", event);
167 else if(event.type === "log")
172 this.fire(event.type, event);
177 * Calls a specific function exposed by the SWF's
180 * @param func {String} the name of the function to call
181 * @param args {Object} the set of arguments to pass to the function.
184 callSWF: function (func, args)
189 if (this._swf._node[func]) {
190 return(this._swf._node[func].apply(this._swf._node, args));
197 * Public accessor to the unique name of the SWF instance.
200 * @return {String} Unique name of the SWF instance.
204 return "SWF " + this._id;
208 Y.augment(SWF, Y.EventTarget);