/static/scripts/tiny_mce/plugins/media/js/media.js

http://n23.googlecode.com/ · JavaScript · 628 lines · 497 code · 122 blank · 9 comment · 103 complexity · 6bf59f47c0b05d8ae6f9a2686c94e29c MD5 · raw file

  1. tinyMCEPopup.requireLangPack();
  2. var oldWidth, oldHeight, ed, url;
  3. if (url = tinyMCEPopup.getParam("media_external_list_url"))
  4. document.write('<script language="javascript" type="text/javascript" src="' + tinyMCEPopup.editor.documentBaseURI.toAbsolute(url) + '"></script>');
  5. function init() {
  6. var pl = "", f, val;
  7. var type = "flash", fe, i;
  8. ed = tinyMCEPopup.editor;
  9. tinyMCEPopup.resizeToInnerSize();
  10. f = document.forms[0]
  11. fe = ed.selection.getNode();
  12. if (/mceItem(Flash|ShockWave|WindowsMedia|QuickTime|RealMedia)/.test(ed.dom.getAttrib(fe, 'class'))) {
  13. pl = fe.title;
  14. switch (ed.dom.getAttrib(fe, 'class')) {
  15. case 'mceItemFlash':
  16. type = 'flash';
  17. break;
  18. case 'mceItemFlashVideo':
  19. type = 'flv';
  20. break;
  21. case 'mceItemShockWave':
  22. type = 'shockwave';
  23. break;
  24. case 'mceItemWindowsMedia':
  25. type = 'wmp';
  26. break;
  27. case 'mceItemQuickTime':
  28. type = 'qt';
  29. break;
  30. case 'mceItemRealMedia':
  31. type = 'rmp';
  32. break;
  33. }
  34. document.forms[0].insert.value = ed.getLang('update', 'Insert', true);
  35. }
  36. document.getElementById('filebrowsercontainer').innerHTML = getBrowserHTML('filebrowser','src','media','media');
  37. document.getElementById('qtsrcfilebrowsercontainer').innerHTML = getBrowserHTML('qtsrcfilebrowser','qt_qtsrc','media','media');
  38. document.getElementById('bgcolor_pickcontainer').innerHTML = getColorPickerHTML('bgcolor_pick','bgcolor');
  39. var html = getMediaListHTML('medialist','src','media','media');
  40. if (html == "")
  41. document.getElementById("linklistrow").style.display = 'none';
  42. else
  43. document.getElementById("linklistcontainer").innerHTML = html;
  44. // Resize some elements
  45. if (isVisible('filebrowser'))
  46. document.getElementById('src').style.width = '230px';
  47. // Setup form
  48. if (pl != "") {
  49. pl = tinyMCEPopup.editor.plugins.media._parse(pl);
  50. switch (type) {
  51. case "flash":
  52. setBool(pl, 'flash', 'play');
  53. setBool(pl, 'flash', 'loop');
  54. setBool(pl, 'flash', 'menu');
  55. setBool(pl, 'flash', 'swliveconnect');
  56. setStr(pl, 'flash', 'quality');
  57. setStr(pl, 'flash', 'scale');
  58. setStr(pl, 'flash', 'salign');
  59. setStr(pl, 'flash', 'wmode');
  60. setStr(pl, 'flash', 'base');
  61. setStr(pl, 'flash', 'flashvars');
  62. break;
  63. case "qt":
  64. setBool(pl, 'qt', 'loop');
  65. setBool(pl, 'qt', 'autoplay');
  66. setBool(pl, 'qt', 'cache');
  67. setBool(pl, 'qt', 'controller');
  68. setBool(pl, 'qt', 'correction');
  69. setBool(pl, 'qt', 'enablejavascript');
  70. setBool(pl, 'qt', 'kioskmode');
  71. setBool(pl, 'qt', 'autohref');
  72. setBool(pl, 'qt', 'playeveryframe');
  73. setBool(pl, 'qt', 'tarsetcache');
  74. setStr(pl, 'qt', 'scale');
  75. setStr(pl, 'qt', 'starttime');
  76. setStr(pl, 'qt', 'endtime');
  77. setStr(pl, 'qt', 'tarset');
  78. setStr(pl, 'qt', 'qtsrcchokespeed');
  79. setStr(pl, 'qt', 'volume');
  80. setStr(pl, 'qt', 'qtsrc');
  81. break;
  82. case "shockwave":
  83. setBool(pl, 'shockwave', 'sound');
  84. setBool(pl, 'shockwave', 'progress');
  85. setBool(pl, 'shockwave', 'autostart');
  86. setBool(pl, 'shockwave', 'swliveconnect');
  87. setStr(pl, 'shockwave', 'swvolume');
  88. setStr(pl, 'shockwave', 'swstretchstyle');
  89. setStr(pl, 'shockwave', 'swstretchhalign');
  90. setStr(pl, 'shockwave', 'swstretchvalign');
  91. break;
  92. case "wmp":
  93. setBool(pl, 'wmp', 'autostart');
  94. setBool(pl, 'wmp', 'enabled');
  95. setBool(pl, 'wmp', 'enablecontextmenu');
  96. setBool(pl, 'wmp', 'fullscreen');
  97. setBool(pl, 'wmp', 'invokeurls');
  98. setBool(pl, 'wmp', 'mute');
  99. setBool(pl, 'wmp', 'stretchtofit');
  100. setBool(pl, 'wmp', 'windowlessvideo');
  101. setStr(pl, 'wmp', 'balance');
  102. setStr(pl, 'wmp', 'baseurl');
  103. setStr(pl, 'wmp', 'captioningid');
  104. setStr(pl, 'wmp', 'currentmarker');
  105. setStr(pl, 'wmp', 'currentposition');
  106. setStr(pl, 'wmp', 'defaultframe');
  107. setStr(pl, 'wmp', 'playcount');
  108. setStr(pl, 'wmp', 'rate');
  109. setStr(pl, 'wmp', 'uimode');
  110. setStr(pl, 'wmp', 'volume');
  111. break;
  112. case "rmp":
  113. setBool(pl, 'rmp', 'autostart');
  114. setBool(pl, 'rmp', 'loop');
  115. setBool(pl, 'rmp', 'autogotourl');
  116. setBool(pl, 'rmp', 'center');
  117. setBool(pl, 'rmp', 'imagestatus');
  118. setBool(pl, 'rmp', 'maintainaspect');
  119. setBool(pl, 'rmp', 'nojava');
  120. setBool(pl, 'rmp', 'prefetch');
  121. setBool(pl, 'rmp', 'shuffle');
  122. setStr(pl, 'rmp', 'console');
  123. setStr(pl, 'rmp', 'controls');
  124. setStr(pl, 'rmp', 'numloop');
  125. setStr(pl, 'rmp', 'scriptcallbacks');
  126. break;
  127. }
  128. setStr(pl, null, 'src');
  129. setStr(pl, null, 'id');
  130. setStr(pl, null, 'name');
  131. setStr(pl, null, 'vspace');
  132. setStr(pl, null, 'hspace');
  133. setStr(pl, null, 'bgcolor');
  134. setStr(pl, null, 'align');
  135. setStr(pl, null, 'width');
  136. setStr(pl, null, 'height');
  137. if ((val = ed.dom.getAttrib(fe, "width")) != "")
  138. pl.width = f.width.value = val;
  139. if ((val = ed.dom.getAttrib(fe, "height")) != "")
  140. pl.height = f.height.value = val;
  141. oldWidth = pl.width ? parseInt(pl.width) : 0;
  142. oldHeight = pl.height ? parseInt(pl.height) : 0;
  143. } else
  144. oldWidth = oldHeight = 0;
  145. selectByValue(f, 'media_type', type);
  146. changedType(type);
  147. updateColor('bgcolor_pick', 'bgcolor');
  148. TinyMCE_EditableSelects.init();
  149. generatePreview();
  150. }
  151. function insertMedia() {
  152. var fe, f = document.forms[0], h;
  153. tinyMCEPopup.restoreSelection();
  154. if (!AutoValidator.validate(f)) {
  155. alert(ed.getLang('invalid_data'));
  156. return false;
  157. }
  158. f.width.value = f.width.value == "" ? 100 : f.width.value;
  159. f.height.value = f.height.value == "" ? 100 : f.height.value;
  160. fe = ed.selection.getNode();
  161. if (fe != null && /mceItem(Flash|ShockWave|WindowsMedia|QuickTime|RealMedia)/.test(ed.dom.getAttrib(fe, 'class'))) {
  162. switch (f.media_type.options[f.media_type.selectedIndex].value) {
  163. case "flash":
  164. fe.className = "mceItemFlash";
  165. break;
  166. case "flv":
  167. fe.className = "mceItemFlashVideo";
  168. break;
  169. case "shockwave":
  170. fe.className = "mceItemShockWave";
  171. break;
  172. case "qt":
  173. fe.className = "mceItemQuickTime";
  174. break;
  175. case "wmp":
  176. fe.className = "mceItemWindowsMedia";
  177. break;
  178. case "rmp":
  179. fe.className = "mceItemRealMedia";
  180. break;
  181. }
  182. if (fe.width != f.width.value || fe.height != f.height.height)
  183. ed.execCommand('mceRepaint');
  184. fe.title = serializeParameters();
  185. fe.width = f.width.value;
  186. fe.height = f.height.value;
  187. fe.style.width = f.width.value + (f.width.value.indexOf('%') == -1 ? 'px' : '');
  188. fe.style.height = f.height.value + (f.height.value.indexOf('%') == -1 ? 'px' : '');
  189. fe.align = f.align.options[f.align.selectedIndex].value;
  190. } else {
  191. h = '<img src="' + tinyMCEPopup.getWindowArg("plugin_url") + '/img/trans.gif"' ;
  192. switch (f.media_type.options[f.media_type.selectedIndex].value) {
  193. case "flash":
  194. h += ' class="mceItemFlash"';
  195. break;
  196. case "flv":
  197. h += ' class="mceItemFlashVideo"';
  198. break;
  199. case "shockwave":
  200. h += ' class="mceItemShockWave"';
  201. break;
  202. case "qt":
  203. h += ' class="mceItemQuickTime"';
  204. break;
  205. case "wmp":
  206. h += ' class="mceItemWindowsMedia"';
  207. break;
  208. case "rmp":
  209. h += ' class="mceItemRealMedia"';
  210. break;
  211. }
  212. h += ' title="' + serializeParameters() + '"';
  213. h += ' width="' + f.width.value + '"';
  214. h += ' height="' + f.height.value + '"';
  215. h += ' align="' + f.align.options[f.align.selectedIndex].value + '"';
  216. h += ' />';
  217. ed.execCommand('mceInsertContent', false, h);
  218. }
  219. tinyMCEPopup.close();
  220. }
  221. function updatePreview() {
  222. var f = document.forms[0], type;
  223. f.width.value = f.width.value || '320';
  224. f.height.value = f.height.value || '240';
  225. type = getType(f.src.value);
  226. selectByValue(f, 'media_type', type);
  227. changedType(type);
  228. generatePreview();
  229. }
  230. function getMediaListHTML() {
  231. if (typeof(tinyMCEMediaList) != "undefined" && tinyMCEMediaList.length > 0) {
  232. var html = "";
  233. html += '<select id="linklist" name="linklist" style="width: 250px" onchange="this.form.src.value=this.options[this.selectedIndex].value;updatePreview();">';
  234. html += '<option value="">---</option>';
  235. for (var i=0; i<tinyMCEMediaList.length; i++)
  236. html += '<option value="' + tinyMCEMediaList[i][1] + '">' + tinyMCEMediaList[i][0] + '</option>';
  237. html += '</select>';
  238. return html;
  239. }
  240. return "";
  241. }
  242. function getType(v) {
  243. var fo, i, c, el, x, f = document.forms[0];
  244. fo = ed.getParam("media_types", "flash=swf;flv=flv;shockwave=dcr;qt=mov,qt,mpg,mp3,mp4,mpeg;shockwave=dcr;wmp=avi,wmv,wm,asf,asx,wmx,wvx;rmp=rm,ra,ram").split(';');
  245. // YouTube
  246. if (v.match(/watch\?v=(.+)(.*)/)) {
  247. f.width.value = '425';
  248. f.height.value = '350';
  249. f.src.value = 'http://www.youtube.com/v/' + v.match(/v=(.*)(.*)/)[0].split('=')[1];
  250. return 'flash';
  251. }
  252. // Google video
  253. if (v.indexOf('http://video.google.com/videoplay?docid=') == 0) {
  254. f.width.value = '425';
  255. f.height.value = '326';
  256. f.src.value = 'http://video.google.com/googleplayer.swf?docId=' + v.substring('http://video.google.com/videoplay?docid='.length) + '&hl=en';
  257. return 'flash';
  258. }
  259. for (i=0; i<fo.length; i++) {
  260. c = fo[i].split('=');
  261. el = c[1].split(',');
  262. for (x=0; x<el.length; x++)
  263. if (v.indexOf('.' + el[x]) != -1)
  264. return c[0];
  265. }
  266. return null;
  267. }
  268. function switchType(v) {
  269. var t = getType(v), d = document, f = d.forms[0];
  270. if (!t)
  271. return;
  272. selectByValue(d.forms[0], 'media_type', t);
  273. changedType(t);
  274. // Update qtsrc also
  275. if (t == 'qt' && f.src.value.toLowerCase().indexOf('rtsp://') != -1) {
  276. alert(ed.getLang("media_qt_stream_warn"));
  277. if (f.qt_qtsrc.value == '')
  278. f.qt_qtsrc.value = f.src.value;
  279. }
  280. }
  281. function changedType(t) {
  282. var d = document;
  283. d.getElementById('flash_options').style.display = 'none';
  284. d.getElementById('flv_options').style.display = 'none';
  285. d.getElementById('qt_options').style.display = 'none';
  286. d.getElementById('shockwave_options').style.display = 'none';
  287. d.getElementById('wmp_options').style.display = 'none';
  288. d.getElementById('rmp_options').style.display = 'none';
  289. d.getElementById(t + '_options').style.display = 'block';
  290. }
  291. function serializeParameters() {
  292. var d = document, f = d.forms[0], s = '';
  293. switch (f.media_type.options[f.media_type.selectedIndex].value) {
  294. case "flash":
  295. s += getBool('flash', 'play', true);
  296. s += getBool('flash', 'loop', true);
  297. s += getBool('flash', 'menu', true);
  298. s += getBool('flash', 'swliveconnect', false);
  299. s += getStr('flash', 'quality');
  300. s += getStr('flash', 'scale');
  301. s += getStr('flash', 'salign');
  302. s += getStr('flash', 'wmode');
  303. s += getStr('flash', 'base');
  304. s += getStr('flash', 'flashvars');
  305. break;
  306. case "qt":
  307. s += getBool('qt', 'loop', false);
  308. s += getBool('qt', 'autoplay', true);
  309. s += getBool('qt', 'cache', false);
  310. s += getBool('qt', 'controller', true);
  311. s += getBool('qt', 'correction', false, 'none', 'full');
  312. s += getBool('qt', 'enablejavascript', false);
  313. s += getBool('qt', 'kioskmode', false);
  314. s += getBool('qt', 'autohref', false);
  315. s += getBool('qt', 'playeveryframe', false);
  316. s += getBool('qt', 'targetcache', false);
  317. s += getStr('qt', 'scale');
  318. s += getStr('qt', 'starttime');
  319. s += getStr('qt', 'endtime');
  320. s += getStr('qt', 'target');
  321. s += getStr('qt', 'qtsrcchokespeed');
  322. s += getStr('qt', 'volume');
  323. s += getStr('qt', 'qtsrc');
  324. break;
  325. case "shockwave":
  326. s += getBool('shockwave', 'sound');
  327. s += getBool('shockwave', 'progress');
  328. s += getBool('shockwave', 'autostart');
  329. s += getBool('shockwave', 'swliveconnect');
  330. s += getStr('shockwave', 'swvolume');
  331. s += getStr('shockwave', 'swstretchstyle');
  332. s += getStr('shockwave', 'swstretchhalign');
  333. s += getStr('shockwave', 'swstretchvalign');
  334. break;
  335. case "wmp":
  336. s += getBool('wmp', 'autostart', true);
  337. s += getBool('wmp', 'enabled', false);
  338. s += getBool('wmp', 'enablecontextmenu', true);
  339. s += getBool('wmp', 'fullscreen', false);
  340. s += getBool('wmp', 'invokeurls', true);
  341. s += getBool('wmp', 'mute', false);
  342. s += getBool('wmp', 'stretchtofit', false);
  343. s += getBool('wmp', 'windowlessvideo', false);
  344. s += getStr('wmp', 'balance');
  345. s += getStr('wmp', 'baseurl');
  346. s += getStr('wmp', 'captioningid');
  347. s += getStr('wmp', 'currentmarker');
  348. s += getStr('wmp', 'currentposition');
  349. s += getStr('wmp', 'defaultframe');
  350. s += getStr('wmp', 'playcount');
  351. s += getStr('wmp', 'rate');
  352. s += getStr('wmp', 'uimode');
  353. s += getStr('wmp', 'volume');
  354. break;
  355. case "rmp":
  356. s += getBool('rmp', 'autostart', false);
  357. s += getBool('rmp', 'loop', false);
  358. s += getBool('rmp', 'autogotourl', true);
  359. s += getBool('rmp', 'center', false);
  360. s += getBool('rmp', 'imagestatus', true);
  361. s += getBool('rmp', 'maintainaspect', false);
  362. s += getBool('rmp', 'nojava', false);
  363. s += getBool('rmp', 'prefetch', false);
  364. s += getBool('rmp', 'shuffle', false);
  365. s += getStr('rmp', 'console');
  366. s += getStr('rmp', 'controls');
  367. s += getStr('rmp', 'numloop');
  368. s += getStr('rmp', 'scriptcallbacks');
  369. break;
  370. }
  371. s += getStr(null, 'id');
  372. s += getStr(null, 'name');
  373. s += getStr(null, 'src');
  374. s += getStr(null, 'align');
  375. s += getStr(null, 'bgcolor');
  376. s += getInt(null, 'vspace');
  377. s += getInt(null, 'hspace');
  378. s += getStr(null, 'width');
  379. s += getStr(null, 'height');
  380. s = s.length > 0 ? s.substring(0, s.length - 1) : s;
  381. return s;
  382. }
  383. function setBool(pl, p, n) {
  384. if (typeof(pl[n]) == "undefined")
  385. return;
  386. document.forms[0].elements[p + "_" + n].checked = pl[n];
  387. }
  388. function setStr(pl, p, n) {
  389. var f = document.forms[0], e = f.elements[(p != null ? p + "_" : '') + n];
  390. if (typeof(pl[n]) == "undefined")
  391. return;
  392. if (e.type == "text")
  393. e.value = pl[n];
  394. else
  395. selectByValue(f, (p != null ? p + "_" : '') + n, pl[n]);
  396. }
  397. function getBool(p, n, d, tv, fv) {
  398. var v = document.forms[0].elements[p + "_" + n].checked;
  399. tv = typeof(tv) == 'undefined' ? 'true' : "'" + jsEncode(tv) + "'";
  400. fv = typeof(fv) == 'undefined' ? 'false' : "'" + jsEncode(fv) + "'";
  401. return (v == d) ? '' : n + (v ? ':' + tv + ',' : ':' + fv + ',');
  402. }
  403. function getStr(p, n, d) {
  404. var e = document.forms[0].elements[(p != null ? p + "_" : "") + n];
  405. var v = e.type == "text" ? e.value : e.options[e.selectedIndex].value;
  406. if (n == 'src')
  407. v = tinyMCEPopup.editor.convertURL(v, 'src', null);
  408. return ((n == d || v == '') ? '' : n + ":'" + jsEncode(v) + "',");
  409. }
  410. function getInt(p, n, d) {
  411. var e = document.forms[0].elements[(p != null ? p + "_" : "") + n];
  412. var v = e.type == "text" ? e.value : e.options[e.selectedIndex].value;
  413. return ((n == d || v == '') ? '' : n + ":" + v.replace(/[^0-9]+/g, '') + ",");
  414. }
  415. function jsEncode(s) {
  416. s = s.replace(new RegExp('\\\\', 'g'), '\\\\');
  417. s = s.replace(new RegExp('"', 'g'), '\\"');
  418. s = s.replace(new RegExp("'", 'g'), "\\'");
  419. return s;
  420. }
  421. function generatePreview(c) {
  422. var f = document.forms[0], p = document.getElementById('prev'), h = '', cls, pl, n, type, codebase, wp, hp, nw, nh;
  423. p.innerHTML = '<!-- x --->';
  424. nw = parseInt(f.width.value);
  425. nh = parseInt(f.height.value);
  426. if (f.width.value != "" && f.height.value != "") {
  427. if (f.constrain.checked) {
  428. if (c == 'width' && oldWidth != 0) {
  429. wp = nw / oldWidth;
  430. nh = Math.round(wp * nh);
  431. f.height.value = nh;
  432. } else if (c == 'height' && oldHeight != 0) {
  433. hp = nh / oldHeight;
  434. nw = Math.round(hp * nw);
  435. f.width.value = nw;
  436. }
  437. }
  438. }
  439. if (f.width.value != "")
  440. oldWidth = nw;
  441. if (f.height.value != "")
  442. oldHeight = nh;
  443. // After constrain
  444. pl = serializeParameters();
  445. switch (f.media_type.options[f.media_type.selectedIndex].value) {
  446. case "flash":
  447. cls = 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000';
  448. codebase = 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0';
  449. type = 'application/x-shockwave-flash';
  450. break;
  451. case "shockwave":
  452. cls = 'clsid:166B1BCA-3F9C-11CF-8075-444553540000';
  453. codebase = 'http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=8,5,1,0';
  454. type = 'application/x-director';
  455. break;
  456. case "qt":
  457. cls = 'clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B';
  458. codebase = 'http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0';
  459. type = 'video/quicktime';
  460. break;
  461. case "wmp":
  462. cls = ed.getParam('media_wmp6_compatible') ? 'clsid:05589FA1-C356-11CE-BF01-00AA0055595A' : 'clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6';
  463. codebase = 'http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701';
  464. type = 'application/x-mplayer2';
  465. break;
  466. case "rmp":
  467. cls = 'clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA';
  468. codebase = 'http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701';
  469. type = 'audio/x-pn-realaudio-plugin';
  470. break;
  471. }
  472. if (pl == '') {
  473. p.innerHTML = '';
  474. return;
  475. }
  476. pl = tinyMCEPopup.editor.plugins.media._parse(pl);
  477. if (!pl.src) {
  478. p.innerHTML = '';
  479. return;
  480. }
  481. pl.src = tinyMCEPopup.editor.documentBaseURI.toAbsolute(pl.src);
  482. pl.width = !pl.width ? 100 : pl.width;
  483. pl.height = !pl.height ? 100 : pl.height;
  484. pl.id = !pl.id ? 'obj' : pl.id;
  485. pl.name = !pl.name ? 'eobj' : pl.name;
  486. pl.align = !pl.align ? '' : pl.align;
  487. // Avoid annoying warning about insecure items
  488. if (!tinymce.isIE || document.location.protocol != 'https:') {
  489. h += '<object classid="clsid:' + cls + '" codebase="' + codebase + '" width="' + pl.width + '" height="' + pl.height + '" id="' + pl.id + '" name="' + pl.name + '" align="' + pl.align + '">';
  490. for (n in pl) {
  491. h += '<param name="' + n + '" value="' + pl[n] + '">';
  492. // Add extra url parameter if it's an absolute URL
  493. if (n == 'src' && pl[n].indexOf('://') != -1)
  494. h += '<param name="url" value="' + pl[n] + '" />';
  495. }
  496. }
  497. h += '<embed type="' + type + '" ';
  498. for (n in pl)
  499. h += n + '="' + pl[n] + '" ';
  500. h += '></embed>';
  501. // Avoid annoying warning about insecure items
  502. if (!tinymce.isIE || document.location.protocol != 'https:')
  503. h += '</object>';
  504. p.innerHTML = "<!-- x --->" + h;
  505. }
  506. tinyMCEPopup.onInit.add(init);