Debian Codegraph @

dojox/embed/Flash.js

raw symbols SCIP
1 define([
2 "dojo/_base/lang",
3 "dojo/_base/unload",
4 "dojo/_base/array",
5 "dojo/query",
6 "dojo/has",
7 "dojo/dom",
8 "dojo/on",
9 "dojo/window",
10 "dojo/string"
11 ], function(lang,unload,array,query,has,dom,on,win,stringUtil) {
12
13 // module:
14 // dojox/embed/Flash
15 // summary:
16 // Base functionality to insert a flash movie into
17 // a document on the fly.
18 // example:
19 // | var movie=new Flash({ args }, containerNode);
20
21
22 var fMarkup, fVersion;
23 var minimumVersion = 9; // anything below this will throw an error (may overwrite)
24 var keyBase = "dojox-embed-flash-", keyCount=0;
25 var _baseKwArgs = {
26 expressInstall: false,
27 width: 320,
28 height: 240,
29 swLiveConnect: "true",
30 allowScriptAccess: "sameDomain",
31 allowNetworking:"all",
32 style: null,
33 redirect: null
34 };
35
36 function prep(kwArgs){
37 kwArgs = lang.delegate(_baseKwArgs, kwArgs);
38
39 if(!("path" in kwArgs)){
40 console.error("dojox.embed.Flash(ctor):: no path reference to a Flash movie was provided.");
41 return null;
42 }
43
44 if(!("id" in kwArgs)){
45 kwArgs.id = (keyBase + keyCount++);
46 }
47 return kwArgs;
48 }
49
50 if(has('ie')) {
51 fMarkup = function(kwArgs){
52 kwArgs = prep(kwArgs);
53 if(!kwArgs){ return null; }
54
55 var p;
56 var path = kwArgs.path;
57 if(kwArgs.vars){
58 var a = [];
59 for(p in kwArgs.vars){
60 a.push(encodeURIComponent(p) + '=' + encodeURIComponent(kwArgs.vars[p]));
61 }
62 kwArgs.params.FlashVars = a.join("&");
63 delete kwArgs.vars;
64 }
65 var s = '<object id="' + stringUtil.escape(String(kwArgs.id)) + '" '
66 + 'classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '
67 + 'width="' + stringUtil.escape(String(kwArgs.width)) + '" '
68 + 'height="' + stringUtil.escape(String(kwArgs.height)) + '"'
69 + ((kwArgs.style)?' style="' + stringUtil.escape(String(kwArgs.style)) + '"':'')
70 + '>'
71 + '<param name="movie" value="' + stringUtil.escape(String(path)) + '" />';
72 if(kwArgs.params){
73 for(p in kwArgs.params){
74 s += '<param name="' + stringUtil.escape(p) + '" value="' + stringUtil.escape(String(kwArgs.params[p])) + '" />';
75 }
76 }
77 s += '</object>';
78 return { id: kwArgs.id, markup: s };
79 };
80
81 fVersion = (function(){
82 var testVersion = 10, testObj = null;
83 while(!testObj && testVersion > 7){
84 try {
85 testObj = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + testVersion--);
86 }catch(e){ }
87 }
88 if(testObj){
89 var v = testObj.GetVariable("$version").split(" ")[1].split(",");
90 return {
91 major: (v[0]!=null) ? parseInt(v[0]) : 0,
92 minor: (v[1]!=null) ? parseInt(v[1]) : 0,
93 rev: (v[2]!=null) ? parseInt(v[2]) : 0
94 };
95 }
96 return { major: 0, minor: 0, rev: 0 };
97 })();
98
99 // attach some cleanup for IE, thanks to deconcept :)
100 unload.addOnWindowUnload(function(){
101 console.warn('***************UNLOAD');
102 var dummy = function(){};
103 var objs = query("object").
104 reverse().
105 style("display", "none").
106 forEach(function(i){
107 for(var p in i){
108 if((p != "FlashVars") && typeof i[p] == "function"){
109 try{
110 i[p] = dummy;
111 }catch(e){}
112 }
113 }
114 });
115 });
116
117 } else {
118 // *** Sane browsers branch ******************************************************************
119 fMarkup = function(kwArgs){
120 kwArgs = prep(kwArgs);
121 if(!kwArgs){ return null; }
122
123 var p;
124 var path = kwArgs.path;
125 if(kwArgs.vars){
126 var a = [];
127 for(p in kwArgs.vars){
128 a.push(encodeURIComponent(p) + '=' + encodeURIComponent(kwArgs.vars[p]));
129 }
130 kwArgs.params.flashVars = a.join("&");
131 delete kwArgs.vars;
132 }
133 var s = '<embed type="application/x-shockwave-flash" '
134 + 'src="' + stringUtil.escape(String(path)) + '" '
135 + 'id="' + stringUtil.escape(String(kwArgs.id)) + '" '
136 + 'width="' + stringUtil.escape(String(kwArgs.width)) + '" '
137 + 'height="' + stringUtil.escape(String(kwArgs.height)) + '"'
138 + ((kwArgs.style)?' style="' + stringUtil.escape(String(kwArgs.style)) + '" ':'')
139
140 + 'pluginspage="' + window.location.protocol + '//www.adobe.com/go/getflashplayer" ';
141 if(kwArgs.params){
142 for(p in kwArgs.params){
143 s += ' ' + stringUtil.escape(p) + '="' + stringUtil.escape(String(kwArgs.params[p])) + '"';
144 }
145 }
146 s += ' />';
147 return { id: kwArgs.id, markup: s };
148 };
149
150 fVersion=(function(){
151 var plugin = navigator.plugins["Shockwave Flash"];
152 if(plugin && plugin.description){
153 var v = plugin.description.replace(/([a-zA-Z]|\s)+/, "").replace(/(\s+r|\s+b[0-9]+)/, ".").split(".");
154 return {
155 major: (v[0]!=null) ? parseInt(v[0]) : 0,
156 minor: (v[1]!=null) ? parseInt(v[1]) : 0,
157 rev: (v[2]!=null) ? parseInt(v[2]) : 0
158 };
159 }
160 return { major: 0, minor: 0, rev: 0 };
161 })();
162 }
163
164
165 /*=====
166 var __flashArgs = {
167 // path: String
168 // The URL of the movie to embed.
169 // id: String?
170 // A unique key that will be used as the id of the created markup. If you don't
171 // provide this, a unique key will be generated.
172 // width: Number?
173 // The width of the embedded movie; the default value is 320px.
174 // height: Number?
175 // The height of the embedded movie; the default value is 240px
176 // minimumVersion: Number?
177 // The minimum targeted version of the Flash Player (defaults to 9)
178 // style: String?
179 // Any CSS style information (i.e. style="background-color:transparent") you want
180 // to define on the markup.
181 // params: Object?
182 // A set of key/value pairs that you want to define in the resultant markup.
183 // vars: Object?
184 // A set of key/value pairs that the Flash movie will interpret as FlashVars.
185 // expressInstall: Boolean?
186 // Whether or not to include any kind of expressInstall info. Default is false.
187 // redirect: String?
188 // A url to redirect the browser to if the current Flash version is not supported.
189 };
190 =====*/
191
192 // the main entry point
193 var Flash = function(/*__flashArgs*/ kwArgs, /*DOMNode*/ node){
194 // summary:
195 // Create a wrapper object around a Flash movie; this is the DojoX equivilent
196 // to SWFObject.
197 //
198 // description:
199 // Creates a wrapper object around a Flash movie. Wrapper object will
200 // insert the movie reference in node; when the browser first starts
201 // grabbing the movie, onReady will be fired; when the movie has finished
202 // loading, it will fire onLoad.
203 //
204 // If your movie uses ExternalInterface, you should use the onLoad event
205 // to do any kind of proxy setup (see dojox.embed.Flash.proxy); this seems
206 // to be the only consistent time calling EI methods are stable (since the
207 // Flash movie will shoot several methods into the window object before
208 // EI callbacks can be used properly).
209 //
210 // kwArgs: __flashArgs
211 // The various arguments that will be used to help define the Flash movie.
212 // node: DomNode
213 // The node where the embed object will be placed
214 //
215 // example:
216 // Embed a flash movie in a document using the new operator, and get a reference to it.
217 // | var movie = new dojox.embed.Flash({
218 // | path: "path/to/my/movie.swf",
219 // | width: 400,
220 // | height: 300
221 // | }, myWrapperNode, "testLoaded");
222 //
223 // example:
224 // Embed a flash movie in a document without using the new operator.
225 // | var movie = dojox.embed.Flash({
226 // | path: "path/to/my/movie.swf",
227 // | width: 400,
228 // | height: 300,
229 // | style: "position:absolute;top:0;left:0"
230 // | }, myWrapperNode, "testLoaded");
231
232 // File can only be run from a server, due to SWF dependency.
233 if(location.href.toLowerCase().indexOf("file://")>-1){
234 throw new Error("dojox.embed.Flash can't be run directly from a file. To instatiate the required SWF correctly it must be run from a server, like localHost.");
235 }
236
237 // available: Number
238 // If there is a flash player available, and if so what version.
239 this.available = fVersion.major;
240
241 // minimumVersion: Number
242 // The minimum version of Flash required to run this movie.
243 this.minimumVersion = kwArgs.minimumVersion || minimumVersion;
244
245 // id: String
246 // The id of the DOMNode to be used for this movie. Can be used with dojo.byId to get a reference.
247 this.id = null;
248
249 // movie: FlashObject
250 // A reference to the movie itself.
251 this.movie = null;
252
253 // domNode: DOMNode
254 // A reference to the DOMNode that contains this movie.
255 this.domNode = null;
256 if(node){
257 node = dom.byId(node);
258 }
259 // setTimeout Fixes #8743 - creating double SWFs
260 // also allows time for code to attach to onError
261 setTimeout(lang.hitch(this, function(){
262 if(kwArgs.expressInstall || this.available && this.available >= this.minimumVersion){
263 if(kwArgs && node){
264 this.init(kwArgs, node);
265 }else{
266 this.onError("embed.Flash was not provided with the proper arguments.");
267 }
268 }else{
269 if(!this.available){
270 this.onError("Flash is not installed.");
271 }else{
272 this.onError("Flash version detected: "+this.available+" is out of date. Minimum required: "+this.minimumVersion);
273 }
274 }
275 }), 100);
276 };
277
278 lang.extend(Flash, {
279 onReady: function(/*HTMLObject*/ movie){
280 // summary:
281 // Stub function for you to attach to when the movie reference is first
282 // pushed into the document.
283 },
284 onLoad: function(/*HTMLObject*/ movie){
285 // summary:
286 // Stub function for you to attach to when the movie has finished downloading
287 // and is ready to be manipulated.
288 },
289 onError: function(msg){
290
291 },
292 _onload: function(){
293 // summary:
294 // Internal. Cleans up before calling onLoad.
295 clearInterval(this._poller);
296 delete this._poller;
297 delete this._pollCount;
298 delete this._pollMax;
299 this.onLoad(this.movie);
300 },
301 init: function(/*__flashArgs*/ kwArgs, /*DOMNode?*/ node){
302 // summary:
303 // Initialize (i.e. place and load) the movie based on kwArgs.
304 this.destroy(); // ensure we are clean first.
305 node = dom.byId(node || this.domNode);
306 if(!node){ throw new Error("dojox.embed.Flash: no domNode reference has been passed."); }
307
308 // vars to help determine load status
309 var p = 0, testLoaded=false;
310 this._poller = null; this._pollCount = 0; this._pollMax = 15; this.pollTime = 100;
311
312 if(Flash.initialized){
313
314 this.id = Flash.place(kwArgs, node);
315 this.domNode = node;
316
317 setTimeout(lang.hitch(this, function(){
318 this.movie = this.byId(this.id, kwArgs.doc);
319 this.onReady(this.movie);
320
321 this._poller = setInterval(lang.hitch(this, function(){
322
323 // catch errors if not quite ready.
324 try{
325 p = this.movie.PercentLoaded();
326 }catch(e){
327 console.warn("this.movie.PercentLoaded() failed", e, this.movie);
328 }
329
330 if(p == 100){
331 // if percent = 100, movie is fully loaded and we're communicating
332 this._onload();
333
334 }else if(p==0 && this._pollCount++ > this._pollMax){
335 // after several attempts, we're not past zero.
336 clearInterval(this._poller);
337 throw new Error("Building SWF failed.");
338 }
339 }), this.pollTime);
340 }), 1);
341 }
342 },
343 _destroy: function(){
344 // summary:
345 // Kill the movie and reset all the properties of this object.
346 try{
347 this.domNode.removeChild(this.movie);
348 }catch(e){}
349 this.id = this.movie = this.domNode = null;
350 },
351 destroy: function(){
352 // summary:
353 // Public interface for destroying all the properties in this object.
354 // Will also clean all proxied methods.
355 if(!this.movie){ return; }
356
357 // remove any proxy functions
358 var test = lang.delegate({
359 id: true,
360 movie: true,
361 domNode: true,
362 onReady: true,
363 onLoad: true
364 });
365 for(var p in this){
366 if(!test[p]){
367 delete this[p];
368 }
369 }
370
371 // poll the movie
372 if(this._poller){
373 // wait until onLoad to destroy
374 on(this, "Load", this, "_destroy");
375 } else {
376 this._destroy();
377 }
378 },
379 byId: function (movieName, doc){
380 // summary:
381 // Gets Flash movie by id.
382 // description:
383 // Probably includes methods for outdated
384 // browsers, but this should catch all cases.
385 // movieName: String
386 // The name of the SWF
387 // doc: Object
388 // The document, if not current window
389 // (not fully supported)
390 // example:
391 // | var movie = dojox.embed.Flash.byId("myId");
392
393 doc = doc || document;
394 if(doc.embeds[movieName]){
395 return doc.embeds[movieName];
396 }
397 if(doc[movieName]){
398 return doc[movieName];
399 }
400 if(window[movieName]){
401 return window[movieName];
402 }
403 if(document[movieName]){
404 return document[movieName];
405 }
406 return null;
407 }
408 });
409
410 // expose information through the constructor function itself.
411 lang.mixin(Flash, {
412 // summary:
413 // A singleton object used internally to get information
414 // about the Flash player available in a browser, and
415 // as the factory for generating and placing markup in a
416 // document.
417 //
418 // minSupported: Number
419 // The minimum supported version of the Flash Player, defaults to 8.
420 // available: Number
421 // Used as both a detection (i.e. if(dojox.embed.Flash.available){ })
422 // and as a variable holding the major version of the player installed.
423 // supported: Boolean
424 // Whether or not the Flash Player installed is supported by dojox.embed.
425 // version: Object
426 // The version of the installed Flash Player; takes the form of
427 // { major, minor, rev }. To get the major version, you'd do this:
428 // var v=dojox.embed.Flash.version.major;
429 // initialized: Boolean
430 // Whether or not the Flash engine is available for use.
431 // onInitialize: Function
432 // A stub you can connect to if you are looking to fire code when the
433 // engine becomes available. A note: DO NOT use this event to
434 // place a movie in a document; it will usually fire before DOMContentLoaded
435 // is fired, and you will get an error. Use dojo.addOnLoad instead.
436 minSupported : 8,
437 available: fVersion.major,
438 supported: (fVersion.major >= fVersion.required),
439 minimumRequired: fVersion.required,
440 version: fVersion,
441 initialized: false,
442 onInitialize: function(){
443 Flash.initialized = true;
444 },
445 __ie_markup__: function(kwArgs){
446 return fMarkup(kwArgs);
447 },
448 proxy: function(/*Flash*/ obj, /*Array|String*/ methods){
449 // summary:
450 // Create the set of passed methods on the Flash object
451 // so that you can call that object directly, as opposed to having to
452 // delve into the internal movie to do this. Intended to make working
453 // with Flash movies that use ExternalInterface much easier to use.
454 //
455 // example:
456 // Create "setMessage" and "getMessage" methods on foo.
457 // | var foo = new Flash(args, someNode);
458 // | dojo.connect(foo, "onLoad", lang.hitch(foo, function(){
459 // | Flash.proxy(this, [ "setMessage", "getMessage" ]);
460 // | this.setMessage("Flash.proxy is pretty cool...");
461 // | console.log(this.getMessage());
462 // | }));
463 array.forEach((methods instanceof Array ? methods : [ methods ]), function(item){
464 this[item] = lang.hitch(this, function(){
465 return (function(){
466 return eval(this.movie.CallFunction(
467 '<invoke name="' + item + '" returntype="javascript">'
468 + '<arguments>'
469 + array.map(arguments, function(item){
470 // FIXME:
471 // investigate if __flash__toXML will
472 // accept direct application via map()
473 // (e.g., does it ignore args past the
474 // first? or does it blow up?)
475 return __flash__toXML(item);
476 }).join("")
477 + '</arguments>'
478 + '</invoke>'
479 ));
480 }).apply(this, arguments||[]);
481 });
482 }, obj);
483 }
484 });
485
486 Flash.place = function(kwArgs, node){
487 var o = fMarkup(kwArgs);
488 node = dom.byId(node);
489 if(!node){
490 node = win.doc.createElement("div");
491 node.id = o.id+"-container";
492 win.body().appendChild(node);
493 }
494 if(o){
495 node.innerHTML = o.markup;
496 return o.id;
497 }
498 return null;
499 }
500 Flash.onInitialize();
501
502 lang.setObject("dojox.embed.Flash", Flash);
503
504 return Flash;
505 });