Merge branch 'MDL-32509' of git://github.com/danpoltawski/moodle
[moodle.git] / lib / yui / 3.5.0 / build / swf / swf.js
blob1b543be04e70ec8871f57c7f61d24c095f0f6b26
1 /*
2 YUI 3.5.0 (build 5089)
3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
6 */
7 YUI.add('swf', function(Y) {
9 /**
10  * Embed a Flash applications in a standard manner and communicate with it
11  * via External Interface.
12  * @module swf
13  */
15     var Event = Y.Event,
16         SWFDetect = Y.SWFDetect,
17         Lang = Y.Lang,
18         uA = Y.UA,
19         Node = Y.Node,
20         Escape = Y.Escape,
22         // private
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:""};
30         /**
31          * The SWF utility is a tool for embedding Flash applications in HTML pages.
32          * @module swf
33          * @title SWF Utility
34          * @requires event-custom, node, swfdetect
35          */
37         /**
38          * Creates the SWF instance and keeps the configuration data
39          *
40          * @class SWF
41          * @augments Y.Event.Target
42          * @constructor
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:
48          *        <dl>
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>
55          *        </dl>
56          */
58 function SWF (p_oElement /*:String*/, swfURL /*:String*/, p_oAttributes /*:Object*/ ) {
60     this._id = Y.guid("yuiswf");
63     var _id = this._id;
64     var oElement = Node.one(p_oElement);
65     
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 ';
76     var w, h;
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 + '" ';
82         if (uA.ie) {
83             objstring += 'classid="' + FLASH_CID + '" ';
84         } else {
85             objstring += 'type="' + FLASH_TYPE + '" data="' + Escape.html(flashURL) + '" ';
86         }
88         w = "100%";
89         h = "100%";
91         objstring += 'width="' + w + '" height="' + h + '">';
93         if (uA.ie) {
94             objstring += '<param name="movie" value="' + Escape.html(flashURL) + '"/>';
95         }
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]) + '"/>';
100             }
101         }
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));
107             }
108         }
110         if (flashvarstring) {
111             objstring += '<param name="flashVars" value="' + flashvarstring + '"/>';
112         }
114         objstring += "</object>";
115         oElement.setContent(objstring);
117         this._swf = Node.one("#" + _id);
118     } else {
119         /**
120          * Fired when the Flash player version on the user's machine is
121          * below the required value.
122          *
123          * @event wrongflashversion
124          */
125         var event = {};
126         event.type = "wrongflashversion";
127         this.publish("wrongflashversion", {fireOnce:true});
128         this.fire("wrongflashversion", event);
129     }
133  * @private
134  * The static collection of all instances of the SWFs on the page.
135  * @property _instances
136  * @type Object
137  */
139 SWF._instances = SWF._instances || {};
142  * @private
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.
148  */
149 SWF.eventHandler = function (swfid, event) {
150     SWF._instances[swfid]._eventHandler(event);
153 SWF.prototype = {
154     /**
155      * @private
156      * Propagates a specific event from Flash to JS.
157      * @method _eventHandler
158      * @param event {Object} The event to be propagated from Flash.
159      */
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") {
165         } else {
166             this.fire(event.type, event);
167         }
168     },
170         /**
171      * Calls a specific function exposed by the SWF's
172      * ExternalInterface.
173      * @method callSWF
174      * @param func {String} the name of the function to call
175      * @param args {Array} the set of arguments to pass to the function.
176      */
177     
178     callSWF: function (func, args)
179     {
180     if (!args) { 
181           args= []; 
182     }   
183         if (this._swf._node[func]) {
184         return(this._swf._node[func].apply(this._swf._node, args));
185         } else {
186         return null;
187         }
188     },
189     
190     /**
191      * Public accessor to the unique name of the SWF instance.
192      *
193      * @method toString
194      * @return {String} Unique name of the SWF instance.
195      */
196     toString: function()
197     {
198         return "SWF " + this._id;
199     }
202 Y.augment(SWF, Y.EventTarget);
204 Y.SWF = SWF;
207 }, '3.5.0' ,{requires:['event-custom','node','swfdetect','escape']});