3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
7 YUI.add('swf', function(Y) {
10 * Embed a Flash applications in a standard manner and communicate with it
11 * via External Interface.
16 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 p_oAttributes = p_oAttributes || {};
68 var flashVersion = p_oAttributes.version || FLASH_VER;
70 var flashVersionSplit = (flashVersion + '').split(".");
71 var isFlashVersionRight = SWFDetect.isFlashVersionAtLeast(parseInt(flashVersionSplit[0], 10), parseInt(flashVersionSplit[1], 10), parseInt(flashVersionSplit[2], 10));
72 var canExpressInstall = (SWFDetect.isFlashVersionAtLeast(8,0,0));
73 var shouldExpressInstall = canExpressInstall && !isFlashVersionRight && p_oAttributes.useExpressInstall;
74 var flashURL = (shouldExpressInstall)?EXPRESS_INSTALL_URL:swfURL;
75 var objstring = '<object ';
77 var flashvarstring = "yId=" + Y.id + "&YUISwfId=" + _id + "&YUIBridgeCallback=" + EVENT_HANDLER + "&allowedDomain=" + document.location.hostname;
79 Y.SWF._instances[_id] = this;
80 if (oElement && (isFlashVersionRight || shouldExpressInstall) && flashURL) {
81 objstring += 'id="' + _id + '" ';
83 objstring += 'classid="' + FLASH_CID + '" ';
85 objstring += 'type="' + FLASH_TYPE + '" data="' + Escape.html(flashURL) + '" ';
91 objstring += 'width="' + w + '" height="' + h + '">';
94 objstring += '<param name="movie" value="' + Escape.html(flashURL) + '"/>';
97 for (var attribute in p_oAttributes.fixedAttributes) {
98 if (possibleAttributes.hasOwnProperty(attribute)) {
99 objstring += '<param name="' + Escape.html(attribute) + '" value="' + Escape.html(p_oAttributes.fixedAttributes[attribute]) + '"/>';
103 for (var flashvar in p_oAttributes.flashVars) {
104 var fvar = p_oAttributes.flashVars[flashvar];
105 if (Lang.isString(fvar)) {
106 flashvarstring += "&" + Escape.html(flashvar) + "=" + Escape.html(encodeURIComponent(fvar));
110 if (flashvarstring) {
111 objstring += '<param name="flashVars" value="' + flashvarstring + '"/>';
114 objstring += "</object>";
115 oElement.setContent(objstring);
117 this._swf = Node.one("#" + _id);
120 * Fired when the Flash player version on the user's machine is
121 * below the required value.
123 * @event wrongflashversion
126 event.type = "wrongflashversion";
127 this.publish("wrongflashversion", {fireOnce:true});
128 this.fire("wrongflashversion", event);
134 * The static collection of all instances of the SWFs on the page.
135 * @property _instances
139 SWF._instances = SWF._instances || {};
143 * Handles an event coming from within the SWF and delegate it
144 * to a specific instance of SWF.
145 * @method eventHandler
146 * @param swfid {String} the id of the SWF dispatching the event
147 * @param event {Object} the event being transmitted.
149 SWF.eventHandler = function (swfid, event) {
150 SWF._instances[swfid]._eventHandler(event);
156 * Propagates a specific event from Flash to JS.
157 * @method _eventHandler
158 * @param event {Object} The event to be propagated from Flash.
160 _eventHandler: function(event) {
161 if (event.type === "swfReady") {
162 this.publish("swfReady", {fireOnce:true});
163 this.fire("swfReady", event);
164 } else if(event.type === "log") {
166 this.fire(event.type, event);
171 * Calls a specific function exposed by the SWF's
174 * @param func {String} the name of the function to call
175 * @param args {Array} the set of arguments to pass to the function.
178 callSWF: function (func, args)
183 if (this._swf._node[func]) {
184 return(this._swf._node[func].apply(this._swf._node, args));
191 * Public accessor to the unique name of the SWF instance.
194 * @return {String} Unique name of the SWF instance.
198 return "SWF " + this._id;
202 Y.augment(SWF, Y.EventTarget);
207 }, '3.5.0' ,{requires:['event-custom','node','swfdetect','escape']});