PageRenderTime 56ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/ajax/libs/webshim/1.12.5-RC-1/dev/shims/swfmini.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 453 lines | 361 code | 48 blank | 44 comment | 137 complexity | 800384bf877c4511c21f532f8b216a61 MD5 | raw file
  1. /*! SWFMini - a SWFObject 2.2 cut down version for webshims
  2. *
  3. * based on SWFObject v2.2 <http://code.google.com/p/swfobject/>
  4. is released under the MIT License <http://www.opensource.org/licenses/mit-license.php>
  5. */
  6. var swfmini = function() {
  7. var UNDEF = "undefined",
  8. OBJECT = "object",
  9. webshims = window.webshims,
  10. SHOCKWAVE_FLASH = "Shockwave Flash",
  11. SHOCKWAVE_FLASH_AX = "ShockwaveFlash.ShockwaveFlash",
  12. FLASH_MIME_TYPE = "application/x-shockwave-flash",
  13. win = window,
  14. doc = document,
  15. nav = navigator,
  16. plugin = false,
  17. domLoadFnArr = [main],
  18. objIdArr = [],
  19. listenersArr = [],
  20. storedAltContent,
  21. storedAltContentId,
  22. storedCallbackFn,
  23. storedCallbackObj,
  24. isDomLoaded = false,
  25. dynamicStylesheet,
  26. dynamicStylesheetMedia,
  27. autoHideShow = true,
  28. /* Centralized function for browser feature detection
  29. - User agent string detection is only used when no good alternative is possible
  30. - Is executed directly for optimal performance
  31. */
  32. ua = function() {
  33. var w3cdom = typeof doc.getElementById != UNDEF && typeof doc.getElementsByTagName != UNDEF && typeof doc.createElement != UNDEF,
  34. u = nav.userAgent.toLowerCase(),
  35. p = nav.platform.toLowerCase(),
  36. windows = p ? /win/.test(p) : /win/.test(u),
  37. mac = p ? /mac/.test(p) : /mac/.test(u),
  38. webkit = /webkit/.test(u) ? parseFloat(u.replace(/^.*webkit\/(\d+(\.\d+)?).*$/, "$1")) : false, // returns either the webkit version or false if not webkit
  39. ie = !+"\v1", // feature detection based on Andrea Giammarchi's solution: http://webreflection.blogspot.com/2009/01/32-bytes-to-know-if-your-browser-is-ie.html
  40. playerVersion = [0,0,0],
  41. d = null;
  42. if (typeof nav.plugins != UNDEF && typeof nav.plugins[SHOCKWAVE_FLASH] == OBJECT) {
  43. d = nav.plugins[SHOCKWAVE_FLASH].description;
  44. if (d && !(typeof nav.mimeTypes != UNDEF && nav.mimeTypes[FLASH_MIME_TYPE] && !nav.mimeTypes[FLASH_MIME_TYPE].enabledPlugin)) { // navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin indicates whether plug-ins are enabled or disabled in Safari 3+
  45. plugin = true;
  46. ie = false; // cascaded feature detection for Internet Explorer
  47. d = d.replace(/^.*\s+(\S+\s+\S+$)/, "$1");
  48. playerVersion[0] = parseInt(d.replace(/^(.*)\..*$/, "$1"), 10);
  49. playerVersion[1] = parseInt(d.replace(/^.*\.(.*)\s.*$/, "$1"), 10);
  50. playerVersion[2] = /[a-zA-Z]/.test(d) ? parseInt(d.replace(/^.*[a-zA-Z]+(.*)$/, "$1"), 10) : 0;
  51. }
  52. }
  53. else if (typeof win.ActiveXObject != UNDEF) {
  54. try {
  55. var a = new ActiveXObject(SHOCKWAVE_FLASH_AX);
  56. if (a) { // a will return null when ActiveX is disabled
  57. d = a.GetVariable("$version");
  58. if (d) {
  59. ie = true; // cascaded feature detection for Internet Explorer
  60. d = d.split(" ")[1].split(",");
  61. playerVersion = [parseInt(d[0], 10), parseInt(d[1], 10), parseInt(d[2], 10)];
  62. }
  63. }
  64. }
  65. catch(e) {}
  66. }
  67. return { w3:w3cdom, pv:playerVersion, wk:webkit, ie:ie, win:windows, mac:mac };
  68. }();
  69. function callDomLoadFunctions() {
  70. if (isDomLoaded) { return; }
  71. try { // test if we can really add/remove elements to/from the DOM; we don't want to fire it too early
  72. var t = doc.getElementsByTagName("body")[0].appendChild(createElement("span"));
  73. t.parentNode.removeChild(t);
  74. }
  75. catch (e) { return; }
  76. isDomLoaded = true;
  77. var dl = domLoadFnArr.length;
  78. for (var i = 0; i < dl; i++) {
  79. domLoadFnArr[i]();
  80. }
  81. }
  82. function addDomLoadEvent(fn) {
  83. if (isDomLoaded) {
  84. fn();
  85. }
  86. else {
  87. domLoadFnArr[domLoadFnArr.length] = fn; // Array.push() is only available in IE5.5+
  88. }
  89. }
  90. /* Cross-browser onload
  91. - Based on James Edwards' solution: http://brothercake.com/site/resources/scripts/onload/
  92. - Will fire an event as soon as a web page including all of its assets are loaded
  93. */
  94. function addLoadEvent(fn) {
  95. }
  96. /* Main function
  97. - Will preferably execute onDomLoad, otherwise onload (as a fallback)
  98. */
  99. function main() {
  100. if (plugin) {
  101. testPlayerVersion();
  102. }
  103. }
  104. /* Detect the Flash Player version for non-Internet Explorer browsers
  105. - Detecting the plug-in version via the object element is more precise than using the plugins collection item's description:
  106. a. Both release and build numbers can be detected
  107. b. Avoid wrong descriptions by corrupt installers provided by Adobe
  108. c. Avoid wrong descriptions by multiple Flash Player entries in the plugin Array, caused by incorrect browser imports
  109. - Disadvantage of this method is that it depends on the availability of the DOM, while the plugins collection is immediately available
  110. */
  111. function testPlayerVersion() {
  112. var b = doc.getElementsByTagName("body")[0];
  113. var o = createElement(OBJECT);
  114. o.setAttribute("type", FLASH_MIME_TYPE);
  115. var t = b.appendChild(o);
  116. if (t) {
  117. var counter = 0;
  118. (function(){
  119. if (typeof t.GetVariable != UNDEF) {
  120. var d = t.GetVariable("$version");
  121. if (d) {
  122. d = d.split(" ")[1].split(",");
  123. ua.pv = [parseInt(d[0], 10), parseInt(d[1], 10), parseInt(d[2], 10)];
  124. }
  125. }
  126. else if (counter < 10) {
  127. counter++;
  128. setTimeout(arguments.callee, 10);
  129. return;
  130. }
  131. b.removeChild(o);
  132. t = null;
  133. })();
  134. }
  135. }
  136. function getObjectById(objectIdStr) {
  137. var r = null;
  138. var o = getElementById(objectIdStr);
  139. if (o && o.nodeName == "OBJECT") {
  140. if (typeof o.SetVariable != UNDEF) {
  141. r = o;
  142. }
  143. else {
  144. var n = o.getElementsByTagName(OBJECT)[0];
  145. if (n) {
  146. r = n;
  147. }
  148. }
  149. }
  150. return r;
  151. }
  152. /* Cross-browser dynamic SWF creation
  153. */
  154. function createSWF(attObj, parObj, id) {
  155. var r, el = getElementById(id);
  156. if (ua.wk && ua.wk < 312) { return r; }
  157. if (el) {
  158. if (typeof attObj.id == UNDEF) { // if no 'id' is defined for the object element, it will inherit the 'id' from the alternative content
  159. attObj.id = id;
  160. }
  161. if (ua.ie && ua.win) { // Internet Explorer + the HTML object element + W3C DOM methods do not combine: fall back to outerHTML
  162. var att = "";
  163. for (var i in attObj) {
  164. if (attObj[i] != Object.prototype[i]) { // filter out prototype additions from other potential libraries
  165. if (i.toLowerCase() == "data") {
  166. parObj.movie = attObj[i];
  167. }
  168. else if (i.toLowerCase() == "styleclass") { // 'class' is an ECMA4 reserved keyword
  169. att += ' class="' + attObj[i] + '"';
  170. }
  171. else if (i.toLowerCase() != "classid") {
  172. att += ' ' + i + '="' + attObj[i] + '"';
  173. }
  174. }
  175. }
  176. var par = "";
  177. for (var j in parObj) {
  178. if (parObj[j] != Object.prototype[j]) { // filter out prototype additions from other potential libraries
  179. par += '<param name="' + j + '" value="' + parObj[j] + '" />';
  180. }
  181. }
  182. el.outerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"' + att + '>' + par + '</object>';
  183. objIdArr[objIdArr.length] = attObj.id; // stored to fix object 'leaks' on unload (dynamic publishing only)
  184. r = getElementById(attObj.id);
  185. }
  186. else { // well-behaving browsers
  187. var o = createElement(OBJECT);
  188. o.setAttribute("type", FLASH_MIME_TYPE);
  189. for (var m in attObj) {
  190. if (attObj[m] != Object.prototype[m]) { // filter out prototype additions from other potential libraries
  191. if (m.toLowerCase() == "styleclass") { // 'class' is an ECMA4 reserved keyword
  192. o.setAttribute("class", attObj[m]);
  193. }
  194. else if (m.toLowerCase() != "classid") { // filter out IE specific attribute
  195. o.setAttribute(m, attObj[m]);
  196. }
  197. }
  198. }
  199. for (var n in parObj) {
  200. if (parObj[n] != Object.prototype[n] && n.toLowerCase() != "movie") { // filter out prototype additions from other potential libraries and IE specific param element
  201. createObjParam(o, n, parObj[n]);
  202. }
  203. }
  204. el.parentNode.replaceChild(o, el);
  205. r = o;
  206. }
  207. }
  208. return r;
  209. }
  210. function createObjParam(el, pName, pValue) {
  211. var p = createElement("param");
  212. p.setAttribute("name", pName);
  213. p.setAttribute("value", pValue);
  214. el.appendChild(p);
  215. }
  216. /* Cross-browser SWF removal
  217. - Especially needed to safely and completely remove a SWF in Internet Explorer
  218. */
  219. function removeSWF(id) {
  220. var obj = getElementById(id);
  221. if (obj && obj.nodeName == "OBJECT") {
  222. if (ua.ie && ua.win) {
  223. obj.style.display = "none";
  224. (function(){
  225. if (obj.readyState == 4) {
  226. removeObjectInIE(id);
  227. }
  228. else {
  229. setTimeout(arguments.callee, 10);
  230. }
  231. })();
  232. }
  233. else {
  234. obj.parentNode.removeChild(obj);
  235. }
  236. }
  237. }
  238. function removeObjectInIE(id) {
  239. var obj = getElementById(id);
  240. if (obj) {
  241. for (var i in obj) {
  242. if (typeof obj[i] == "function") {
  243. obj[i] = null;
  244. }
  245. }
  246. obj.parentNode.removeChild(obj);
  247. }
  248. }
  249. /* Functions to optimize JavaScript compression
  250. */
  251. function getElementById(id) {
  252. var el = null;
  253. try {
  254. el = doc.getElementById(id);
  255. }
  256. catch (e) {}
  257. return el;
  258. }
  259. function createElement(el) {
  260. return doc.createElement(el);
  261. }
  262. /* Updated attachEvent function for Internet Explorer
  263. - Stores attachEvent information in an Array, so on unload the detachEvent functions can be called to avoid memory leaks
  264. */
  265. function addListener(target, eventType, fn) {
  266. target.attachEvent(eventType, fn);
  267. listenersArr[listenersArr.length] = [target, eventType, fn];
  268. }
  269. /* Flash Player and SWF content version matching
  270. */
  271. function hasPlayerVersion(rv) {
  272. var pv = ua.pv, v = rv.split(".");
  273. v[0] = parseInt(v[0], 10);
  274. v[1] = parseInt(v[1], 10) || 0; // supports short notation, e.g. "9" instead of "9.0.0"
  275. v[2] = parseInt(v[2], 10) || 0;
  276. return (pv[0] > v[0] || (pv[0] == v[0] && pv[1] > v[1]) || (pv[0] == v[0] && pv[1] == v[1] && pv[2] >= v[2])) ? true : false;
  277. }
  278. function setVisibility(id, isVisible) {
  279. if (!autoHideShow) { return; }
  280. var elem;
  281. var v = isVisible ? "visible" : "hidden";
  282. if (isDomLoaded && (elem && getElementById(id))) {
  283. getElementById(id).style.visibility = v;
  284. }
  285. }
  286. /* Release memory to avoid memory leaks caused by closures, fix hanging audio/video threads and force open sockets/NetConnections to disconnect (Internet Explorer only)
  287. */
  288. var cleanup = function() {
  289. if (ua.ie && ua.win && window.attachEvent) {
  290. window.attachEvent("onunload", function() {
  291. // remove listeners to avoid memory leaks
  292. var ll = listenersArr.length;
  293. for (var i = 0; i < ll; i++) {
  294. listenersArr[i][0].detachEvent(listenersArr[i][1], listenersArr[i][2]);
  295. }
  296. // cleanup dynamically embedded objects to fix audio/video threads and force open sockets and NetConnections to disconnect
  297. var il = objIdArr.length;
  298. for (var j = 0; j < il; j++) {
  299. removeSWF(objIdArr[j]);
  300. }
  301. // cleanup library's main closures to avoid memory leaks
  302. for (var k in ua) {
  303. ua[k] = null;
  304. }
  305. ua = null;
  306. for (var l in swfmini) {
  307. swfmini[l] = null;
  308. }
  309. swfmini = null;
  310. });
  311. }
  312. }();
  313. webshims.ready('DOM', callDomLoadFunctions);
  314. return {
  315. /* Public API
  316. - Reference: http://code.google.com/p/swfobject/wiki/documentation
  317. */
  318. registerObject: function() {
  319. },
  320. getObjectById: function(objectIdStr) {
  321. if (ua.w3) {
  322. return getObjectById(objectIdStr);
  323. }
  324. },
  325. embedSWF: function(swfUrlStr, replaceElemIdStr, widthStr, heightStr, swfVersionStr, xiSwfUrlStr, flashvarsObj, parObj, attObj, callbackFn) {
  326. var callbackObj = {success:false, id:replaceElemIdStr};
  327. if (ua.w3 && !(ua.wk && ua.wk < 312) && swfUrlStr && replaceElemIdStr && widthStr && heightStr && swfVersionStr) {
  328. setVisibility(replaceElemIdStr, false);
  329. addDomLoadEvent(function() {
  330. widthStr += ""; // auto-convert to string
  331. heightStr += "";
  332. var att = {};
  333. if (attObj && typeof attObj === OBJECT) {
  334. for (var i in attObj) { // copy object to avoid the use of references, because web authors often reuse attObj for multiple SWFs
  335. att[i] = attObj[i];
  336. }
  337. }
  338. att.data = swfUrlStr;
  339. att.width = widthStr;
  340. att.height = heightStr;
  341. var par = {};
  342. if (parObj && typeof parObj === OBJECT) {
  343. for (var j in parObj) { // copy object to avoid the use of references, because web authors often reuse parObj for multiple SWFs
  344. par[j] = parObj[j];
  345. }
  346. }
  347. if (flashvarsObj && typeof flashvarsObj === OBJECT) {
  348. for (var k in flashvarsObj) { // copy object to avoid the use of references, because web authors often reuse flashvarsObj for multiple SWFs
  349. if (typeof par.flashvars != UNDEF) {
  350. par.flashvars += "&" + k + "=" + flashvarsObj[k];
  351. }
  352. else {
  353. par.flashvars = k + "=" + flashvarsObj[k];
  354. }
  355. }
  356. }
  357. if (hasPlayerVersion(swfVersionStr)) { // create SWF
  358. var obj = createSWF(att, par, replaceElemIdStr);
  359. if (att.id == replaceElemIdStr) {
  360. setVisibility(replaceElemIdStr, true);
  361. }
  362. callbackObj.success = true;
  363. callbackObj.ref = obj;
  364. }
  365. else { // show alternative content
  366. setVisibility(replaceElemIdStr, true);
  367. }
  368. if (callbackFn) { callbackFn(callbackObj); }
  369. });
  370. }
  371. else if (callbackFn) { callbackFn(callbackObj); }
  372. },
  373. switchOffAutoHideShow: function() {
  374. autoHideShow = false;
  375. },
  376. ua: ua,
  377. getFlashPlayerVersion: function() {
  378. return { major:ua.pv[0], minor:ua.pv[1], release:ua.pv[2] };
  379. },
  380. hasFlashPlayerVersion: hasPlayerVersion,
  381. createSWF: function(attObj, parObj, replaceElemIdStr) {
  382. if (ua.w3) {
  383. return createSWF(attObj, parObj, replaceElemIdStr);
  384. }
  385. else {
  386. return undefined;
  387. }
  388. },
  389. showExpressInstall: function() {
  390. },
  391. removeSWF: function(objElemIdStr) {
  392. if (ua.w3) {
  393. removeSWF(objElemIdStr);
  394. }
  395. },
  396. createCSS: function() {
  397. },
  398. addDomLoadEvent: addDomLoadEvent,
  399. addLoadEvent: addLoadEvent,
  400. // For internal usage only
  401. expressInstallCallback: function() {
  402. }
  403. };
  404. }();
  405. webshims.isReady('swfmini', true);