/source/app/NOS.Wiki/Themes/NOS/includes/flash.js

http://github.com/agross/netopenspace · JavaScript · 288 lines · 122 code · 10 blank · 156 comment · 31 complexity · f70688116016dd88e6e852de5f404f2f MD5 · raw file

  1. /**
  2. * Flash (http://jquery.lukelutman.com/plugins/flash)
  3. * A jQuery plugin for embedding Flash movies.
  4. *
  5. * Version 1.0
  6. * November 9th, 2006
  7. *
  8. * Copyright (c) 2006 Luke Lutman (http://www.lukelutman.com)
  9. * Dual licensed under the MIT and GPL licenses.
  10. * http://www.opensource.org/licenses/mit-license.php
  11. * http://www.opensource.org/licenses/gpl-license.php
  12. *
  13. * Inspired by:
  14. * SWFObject (http://blog.deconcept.com/swfobject/)
  15. * UFO (http://www.bobbyvandersluis.com/ufo/)
  16. * sIFR (http://www.mikeindustries.com/sifr/)
  17. *
  18. * IMPORTANT:
  19. * The packed version of jQuery breaks ActiveX control
  20. * activation in Internet Explorer. Use JSMin to minifiy
  21. * jQuery (see: http://jquery.lukelutman.com/plugins/flash#activex).
  22. *
  23. **/
  24. ;(function(){
  25. var $$;
  26. /**
  27. *
  28. * @desc Replace matching elements with a flash movie.
  29. * @author Luke Lutman
  30. * @version 1.0.1
  31. *
  32. * @name flash
  33. * @param Hash htmlOptions Options for the embed/object tag.
  34. * @param Hash pluginOptions Options for detecting/updating the Flash plugin (optional).
  35. * @param Function replace Custom block called for each matched element if flash is installed (optional).
  36. * @param Function update Custom block called for each matched if flash isn't installed (optional).
  37. * @type jQuery
  38. *
  39. * @cat plugins/flash
  40. *
  41. * @example $('#hello').flash({ src: 'hello.swf' });
  42. * @desc Embed a Flash movie.
  43. *
  44. * @example $('#hello').flash({ src: 'hello.swf' }, { version: 8 });
  45. * @desc Embed a Flash 8 movie.
  46. *
  47. * @example $('#hello').flash({ src: 'hello.swf' }, { expressInstall: true });
  48. * @desc Embed a Flash movie using Express Install if flash isn't installed.
  49. *
  50. * @example $('#hello').flash({ src: 'hello.swf' }, { update: false });
  51. * @desc Embed a Flash movie, don't show an update message if Flash isn't installed.
  52. *
  53. **/
  54. $$ = jQuery.fn.flash = function(htmlOptions, pluginOptions, replace, update) {
  55. // Set the default block.
  56. var block = replace || $$.replace;
  57. // Merge the default and passed plugin options.
  58. pluginOptions = $$.copy($$.pluginOptions, pluginOptions);
  59. // Detect Flash.
  60. if(!$$.hasFlash(pluginOptions.version)) {
  61. // Use Express Install (if specified and Flash plugin 6,0,65 or higher is installed).
  62. if(pluginOptions.expressInstall && $$.hasFlash(6,0,65)) {
  63. // Add the necessary flashvars (merged later).
  64. var expressInstallOptions = {
  65. flashvars: {
  66. MMredirectURL: location,
  67. MMplayerType: 'PlugIn',
  68. MMdoctitle: jQuery('title').text()
  69. }
  70. };
  71. // Ask the user to update (if specified).
  72. } else if (pluginOptions.update) {
  73. // Change the block to insert the update message instead of the flash movie.
  74. block = update || $$.update;
  75. // Fail
  76. } else {
  77. // The required version of flash isn't installed.
  78. // Express Install is turned off, or flash 6,0,65 isn't installed.
  79. // Update is turned off.
  80. // Return without doing anything.
  81. return this;
  82. }
  83. }
  84. // Merge the default, express install and passed html options.
  85. htmlOptions = $$.copy($$.htmlOptions, expressInstallOptions, htmlOptions);
  86. // Invoke $block (with a copy of the merged html options) for each element.
  87. return this.each(function(){
  88. block.call(this, $$.copy(htmlOptions));
  89. });
  90. };
  91. /**
  92. *
  93. * @name flash.copy
  94. * @desc Copy an arbitrary number of objects into a new object.
  95. * @type Object
  96. *
  97. * @example $$.copy({ foo: 1 }, { bar: 2 });
  98. * @result { foo: 1, bar: 2 };
  99. *
  100. **/
  101. $$.copy = function() {
  102. var options = {}, flashvars = {};
  103. for(var i = 0; i < arguments.length; i++) {
  104. var arg = arguments[i];
  105. if(arg == undefined) continue;
  106. jQuery.extend(options, arg);
  107. // don't clobber one flash vars object with another
  108. // merge them instead
  109. if(arg.flashvars == undefined) continue;
  110. jQuery.extend(flashvars, arg.flashvars);
  111. }
  112. options.flashvars = flashvars;
  113. return options;
  114. };
  115. /*
  116. * @name flash.hasFlash
  117. * @desc Check if a specific version of the Flash plugin is installed
  118. * @type Boolean
  119. *
  120. **/
  121. $$.hasFlash = function() {
  122. // look for a flag in the query string to bypass flash detection
  123. if(/hasFlash\=true/.test(location)) return true;
  124. if(/hasFlash\=false/.test(location)) return false;
  125. var pv = $$.hasFlash.playerVersion().match(/\d+/g);
  126. var rv = String([arguments[0], arguments[1], arguments[2]]).match(/\d+/g) || String($$.pluginOptions.version).match(/\d+/g);
  127. for(var i = 0; i < 3; i++) {
  128. pv[i] = parseInt(pv[i] || 0);
  129. rv[i] = parseInt(rv[i] || 0);
  130. // player is less than required
  131. if(pv[i] < rv[i]) return false;
  132. // player is greater than required
  133. if(pv[i] > rv[i]) return true;
  134. }
  135. // major version, minor version and revision match exactly
  136. return true;
  137. };
  138. /**
  139. *
  140. * @name flash.hasFlash.playerVersion
  141. * @desc Get the version of the installed Flash plugin.
  142. * @type String
  143. *
  144. **/
  145. $$.hasFlash.playerVersion = function() {
  146. // ie
  147. try {
  148. try {
  149. // avoid fp6 minor version lookup issues
  150. // see: http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/
  151. var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');
  152. try { axo.AllowScriptAccess = 'always'; }
  153. catch(e) { return '6,0,0'; }
  154. } catch(e) {}
  155. return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1];
  156. // other browsers
  157. } catch(e) {
  158. try {
  159. if(navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin){
  160. return (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description.replace(/\D+/g, ",").match(/^,?(.+),?$/)[1];
  161. }
  162. } catch(e) {}
  163. }
  164. return '0,0,0';
  165. };
  166. /**
  167. *
  168. * @name flash.htmlOptions
  169. * @desc The default set of options for the object or embed tag.
  170. *
  171. **/
  172. $$.htmlOptions = {
  173. height: 240,
  174. flashvars: {},
  175. pluginspage: 'http://www.adobe.com/go/getflashplayer',
  176. src: '#',
  177. type: 'application/x-shockwave-flash',
  178. width: 320
  179. };
  180. /**
  181. *
  182. * @name flash.pluginOptions
  183. * @desc The default set of options for checking/updating the flash Plugin.
  184. *
  185. **/
  186. $$.pluginOptions = {
  187. expressInstall: false,
  188. update: true,
  189. version: '6.0.65'
  190. };
  191. /**
  192. *
  193. * @name flash.replace
  194. * @desc The default method for replacing an element with a Flash movie.
  195. *
  196. **/
  197. $$.replace = function(htmlOptions) {
  198. this.innerHTML = '<div class="alt">'+this.innerHTML+'</div>';
  199. jQuery(this)
  200. .addClass('flash-replaced')
  201. .prepend($$.transform(htmlOptions));
  202. };
  203. /**
  204. *
  205. * @name flash.update
  206. * @desc The default method for replacing an element with an update message.
  207. *
  208. **/
  209. $$.update = function(htmlOptions) {
  210. var url = String(location).split('?');
  211. url.splice(1,0,'?hasFlash=true&');
  212. url = url.join('');
  213. var msg = '<p>This content requires the Flash Player. <a href="http://www.adobe.com/go/getflashplayer">Download Flash Player</a>. Already have Flash Player? <a href="'+url+'">Click here.</a></p>';
  214. this.innerHTML = '<span class="alt">'+this.innerHTML+'</span>';
  215. jQuery(this)
  216. .addClass('flash-update')
  217. .prepend(msg);
  218. };
  219. /**
  220. *
  221. * @desc Convert a hash of html options to a string of attributes, using Function.apply().
  222. * @example toAttributeString.apply(htmlOptions)
  223. * @result foo="bar" foo="bar"
  224. *
  225. **/
  226. function toAttributeString() {
  227. var s = '';
  228. for(var key in this)
  229. if(typeof this[key] != 'function')
  230. s += key+'="'+this[key]+'" ';
  231. return s;
  232. };
  233. /**
  234. *
  235. * @desc Convert a hash of flashvars to a url-encoded string, using Function.apply().
  236. * @example toFlashvarsString.apply(flashvarsObject)
  237. * @result foo=bar&foo=bar
  238. *
  239. **/
  240. function toFlashvarsString() {
  241. var s = '';
  242. for(var key in this)
  243. if(typeof this[key] != 'function')
  244. s += key+'='+encodeURIComponent(this[key])+'&';
  245. return s.replace(/&$/, '');
  246. };
  247. /**
  248. *
  249. * @name flash.transform
  250. * @desc Transform a set of html options into an embed tag.
  251. * @type String
  252. *
  253. * @example $$.transform(htmlOptions)
  254. * @result <embed src="foo.swf" ... />
  255. *
  256. * Note: The embed tag is NOT standards-compliant, but it
  257. * works in all current browsers. flash.transform can be
  258. * overwritten with a custom function to generate more
  259. * standards-compliant markup.
  260. *
  261. **/
  262. $$.transform = function(htmlOptions) {
  263. htmlOptions.toString = toAttributeString;
  264. if(htmlOptions.flashvars) htmlOptions.flashvars.toString = toFlashvarsString;
  265. return '<embed ' + String(htmlOptions) + '/>';
  266. };
  267. /**
  268. *
  269. * Flash Player 9 Fix (http://blog.deconcept.com/2006/07/28/swfobject-143-released/)
  270. *
  271. **/
  272. if (window.attachEvent) {
  273. window.attachEvent("onbeforeunload", function(){
  274. __flash_unloadHandler = function() {};
  275. __flash_savedUnloadHandler = function() {};
  276. });
  277. }
  278. })();