media.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. // Reworks and improvements by Ivan Tcholakov, JUL-2009.
  2. var dialog = window.parent;
  3. var oEditor = dialog.InnerDialogLoaded();
  4. var FCK = oEditor.FCK;
  5. var FCKLang = oEditor.FCKLang;
  6. var FCKConfig = oEditor.FCKConfig;
  7. var FCKTools = oEditor.FCKTools;
  8. // Security RegExp
  9. var REG_SCRIPT = new RegExp("< *script.*>|< *style.*>|< *link.*>|< *body.*>", "i");
  10. var REG_PROTOCOL = new RegExp("javascript:|vbscript:|about:", "i");
  11. var REG_CALL_SCRIPT = new RegExp("&\{.*\};", "i");
  12. var REG_EVENT = new RegExp("onError|onUnload|onBlur|onFocus|onClick|onMouseOver|onMouseOut|onSubmit|onReset|onChange|onSelect|onAbort", "i");
  13. // Cookie Basic
  14. var REG_AUTH = new RegExp("document\.cookie|Microsoft\.XMLHTTP", "i");
  15. // TEXTAREA
  16. var REG_NEWLINE = new RegExp("\x0d|\x0a", "i");
  17. var MediaSite = 'http://www.Media.com/v/';
  18. var HighQualityString = '%26hl=en%26fs=1%26rel=0%26ap=%2526fmt=18';
  19. var LowQualityString = '%26hl=en%26fs=1%26rel=0';
  20. // Set the language direction.
  21. window.document.dir = FCKLang.Dir;
  22. FCKLang['DlgMediaURLTipContent1'] = FCKLang['DlgMediaURLTipContent1'] ? FCKLang['DlgMediaURLTipContent1'] : '';
  23. FCKLang['DlgMediaURLTipContent3'] = FCKLang['DlgMediaURLTipContent3'] ? FCKLang['DlgMediaURLTipContent3'] : '';
  24. FCKLang['DlgMediaURLTipContent1'] = FCKLang['DlgMediaURLTipContent1'].toString().replace('%s', '<a href="http://www.Media.com/" target="_blank">http://www.Media.com/<\/a>');
  25. FCKLang['DlgMediaURLTipContent3'] = FCKLang['DlgMediaURLTipContent3'].toString().replace('%s', 'http://www.Media.com/watch?v=XXXXXXXXXXX...');
  26. //#### Dialog Tabs
  27. // Set the dialog tabs.
  28. dialog.AddTab('Info', FCKLang.DlgInfoTab);
  29. dialog.AddTab('Preview', FCKLang.DlgImgPreview);
  30. var media = {
  31. type: "macintosh",
  32. color: "red",
  33. is_valid_url: function (url) {
  34. if (url.search(REG_SCRIPT) != -1)
  35. {
  36. return false;
  37. }
  38. if (url.search(REG_PROTOCOL) != -1)
  39. {
  40. return false;
  41. }
  42. if (url.search(REG_CALL_SCRIPT) != -1)
  43. {
  44. return false;
  45. }
  46. if (url.search(REG_EVENT) != -1)
  47. {
  48. return false;
  49. }
  50. if (url.search(REG_AUTH) != -1)
  51. {
  52. return false;
  53. }
  54. if (url.search(REG_NEWLINE) != -1)
  55. {
  56. return false;
  57. }
  58. return true;
  59. },
  60. is_valid_size: function (code) {
  61. if (isNaN(code)){
  62. return false;
  63. }
  64. var value = parseInt(code, 10);
  65. if (value <= 0){
  66. return false;
  67. }
  68. return true;
  69. },
  70. to_size: function (code){
  71. if (isNaN(code)){
  72. return false;
  73. }
  74. var result = parseInt(code, 10);
  75. if (result <= 0){
  76. result = 0;
  77. }
  78. return result;
  79. }
  80. };
  81. var preview = {
  82. clear: function ()
  83. {
  84. if (!ePreview){
  85. return;
  86. }
  87. while (ePreview.firstChild){
  88. ePreview.removeChild(ePreview.firstChild);
  89. }
  90. ePreview.innerHTML = '&nbsp;';
  91. },
  92. update: function ()
  93. {
  94. if (!ePreview){
  95. return;
  96. }
  97. while (ePreview.firstChild){
  98. ePreview.removeChild(ePreview.firstChild);
  99. }
  100. var oDoc = ePreview.ownerDocument || ePreview.document;
  101. var e = oDoc.createElement('EMBED');
  102. UpdateEmbed(e);
  103. if (!IsValidMedia(e))
  104. {
  105. this.clear();
  106. return;
  107. }
  108. var max_width = 515;
  109. var max_height = 275;
  110. var width = GetAttribute(e, 'width', 425);
  111. var height = GetAttribute(e, 'height', 344);
  112. var new_size = FCK.ResizeToFit(width, height, max_width, max_height);
  113. width = new_size[0];
  114. height = new_size[1];
  115. SetAttribute(e, 'width', width);
  116. SetAttribute(e, 'height', height);
  117. ePreview.appendChild(e);
  118. var margin_left = parseInt((max_width - width) / 2, 10);
  119. var margin_top = parseInt((max_height - height) / 2, 10);
  120. if (ePreview.currentStyle)
  121. {
  122. // IE
  123. ePreview.style.marginLeft = margin_left;
  124. ePreview.style.marginTop = margin_top;
  125. }
  126. else
  127. {
  128. // Other browsers
  129. SetAttribute(ePreview, 'style', 'margin-left: ' + margin_left + 'px; margin-top: ' + margin_top + 'px;');
  130. }
  131. }
  132. };
  133. // This function is called when a dialog tab has been selected.
  134. function OnDialogTabChange(tabCode)
  135. {
  136. ShowE('divInfo', (tabCode == 'Info'));
  137. ShowE('divPreview', (tabCode == 'Preview'));
  138. if (tabCode == 'Preview')
  139. {
  140. preview.update();
  141. }
  142. else
  143. {
  144. preview.clear();
  145. }
  146. }
  147. // Get the selected video (if available).
  148. var oFakeImage = FCK.Selection.GetSelectedElement();
  149. var oEmbed;
  150. if (oFakeImage)
  151. {
  152. if (oFakeImage.tagName == 'IMG' && oFakeImage.getAttribute('_fckvideo'))
  153. oEmbed = FCK.GetRealElement(oFakeImage);
  154. else
  155. oFakeImage = null;
  156. }
  157. window.onload = function()
  158. {
  159. // Translate the dialog box texts.
  160. oEditor.FCKLanguageManager.TranslatePage(document);
  161. // Load the selected element information (if any).
  162. LoadSelection();
  163. dialog.SetAutoSize(true);
  164. // Activate the "OK" button.
  165. dialog.SetOkButton(true);
  166. SelectField('txtUrl');
  167. }
  168. function LoadSelection()
  169. {
  170. if (!oEmbed){
  171. return;
  172. }
  173. GetE('txtWidth').value = GetAttribute(oEmbed, 'width', 425);
  174. GetE('txtHeight').value = GetAttribute(oEmbed, 'height', 344);
  175. }
  176. //#### The OK button was hit.
  177. function Ok()
  178. {
  179. GetE('txtUrl').value = GetE('txtUrl').value.Trim();
  180. if (GetE('txtUrl').value.length == 0)
  181. {
  182. dialog.SetSelectedTab('Info');
  183. GetE('txtUrl').focus();
  184. alert(oEditor.FCKLang.DlgMediaCode);
  185. return false;
  186. }
  187. // Check security
  188. var url = GetE('txtUrl').value;
  189. if (!media.is_valid_url(url))
  190. {
  191. alert(oEditor.FCKLang.DlgMediaSecurity);
  192. return false;
  193. }
  194. oEditor.FCKUndo.SaveUndoStep();
  195. if (!oEmbed)
  196. {
  197. oEmbed = FCK.EditorDocument.createElement('EMBED');
  198. oFakeImage = null;
  199. }
  200. UpdateEmbed(oEmbed);
  201. if (!oFakeImage)
  202. {
  203. oFakeImage = oEditor.FCKDocumentProcessor_CreateFakeImage('FCK__Video', oEmbed);
  204. oFakeImage.setAttribute('_fckvideo', 'true', 0);
  205. oFakeImage = FCK.InsertElement(oFakeImage);
  206. }
  207. oEditor.FCKEmbedAndObjectProcessor.RefreshView(oFakeImage, oEmbed);
  208. return true;
  209. }
  210. function UpdateEmbed(e)
  211. {
  212. var MediaUrl = GetE('txtUrl').value;
  213. var MediaId = GetMediaId(MediaUrl);
  214. SetAttribute(e, 'type', 'application/x-shockwave-flash');
  215. SetAttribute(e, 'pluginspage', 'http://www.macromedia.com/go/getflashplayer');
  216. SetAttribute(e, 'allowfullscreen', 'true');
  217. SetAttribute(e, 'wmode', 'transparent');
  218. if (GetE('radioHigh').checked)
  219. {
  220. SetAttribute(e, 'src', MediaSite + MediaId + HighQualityString);
  221. }
  222. else
  223. {
  224. SetAttribute(e, 'src', MediaSite + MediaId + LowQualityString);
  225. }
  226. SetAttribute(e, 'width', GetE('txtWidth').value == '' ? 425 : GetE('txtWidth').value);
  227. SetAttribute(e, 'height', GetE('txtHeight').value == '' ? 344 : GetE('txtHeight').value);
  228. }
  229. function checkCode(code)
  230. {
  231. if (code.search(REG_SCRIPT) != -1)
  232. {
  233. return false;
  234. }
  235. if (code.search(REG_PROTOCOL) != -1)
  236. {
  237. return false;
  238. }
  239. if (code.search(REG_CALL_SCRIPT) != -1)
  240. {
  241. return false;
  242. }
  243. if (code.search(REG_EVENT) != -1)
  244. {
  245. return false;
  246. }
  247. if (code.search(REG_AUTH) != -1)
  248. {
  249. return false;
  250. }
  251. if (code.search(REG_NEWLINE) != -1)
  252. {
  253. return false;
  254. }
  255. return true;
  256. }
  257. function GetMediaId(url)
  258. {
  259. var MediaId = url.toString().slice(url.search(/\?v=/i) + 3);
  260. var end = MediaId.indexOf('%');
  261. if (end > 0)
  262. {
  263. MediaId = MediaId.substring(0, end);
  264. }
  265. return MediaId;
  266. }
  267. function GetQuality(url)
  268. {
  269. var quality = 'low';
  270. var QualityString = url.toString().substr(url.search('%'));
  271. if (QualityString.length > LowQualityString.length)
  272. {
  273. quality = 'high';
  274. }
  275. return quality;
  276. }
  277. var ePreview;
  278. function IsValidMedia(e)
  279. {
  280. if (!e){
  281. return false;
  282. }
  283. var src = GetAttribute(e, 'src', '');
  284. var width = GetAttribute(e, 'width', '');
  285. var height = GetAttribute(e, 'height', '');
  286. if (src.length == 0){
  287. return false;
  288. }
  289. if (isNaN(width)){
  290. return false;
  291. }
  292. if (parseInt(width, 10) <= 0){
  293. return false;
  294. }
  295. if (isNaN(height)){
  296. return false;
  297. }
  298. if (parseInt(height, 10) <= 0){
  299. return false;
  300. }
  301. return true;
  302. }
  303. function SetPreviewElement(previewEl)
  304. {
  305. ePreview = previewEl;
  306. if (IsValidMedia(oEmbed)){
  307. UpdatePreview();
  308. }
  309. }
  310. function UpdatePreview()
  311. {
  312. if (!ePreview){
  313. return;
  314. }
  315. while (ePreview.firstChild)
  316. ePreview.removeChild(ePreview.firstChild);
  317. var oDoc = ePreview.ownerDocument || ePreview.document;
  318. var e = oDoc.createElement('EMBED');
  319. UpdateEmbed(e);
  320. if (!IsValidMedia(e))
  321. {
  322. ClearPreview();
  323. }
  324. else
  325. {
  326. var max_width = 515;
  327. var max_height = 275;
  328. var width = GetAttribute(e, 'width', 425);
  329. var height = GetAttribute(e, 'height', 344);
  330. var new_size = FCK.ResizeToFit(width, height, max_width, max_height);
  331. width = new_size[0];
  332. height = new_size[1];
  333. SetAttribute(e, 'width', width);
  334. SetAttribute(e, 'height', height);
  335. ePreview.appendChild(e);
  336. var margin_left = parseInt((max_width - width) / 2, 10);
  337. var margin_top = parseInt((max_height - height) / 2, 10);
  338. if (ePreview.currentStyle)
  339. {
  340. // IE
  341. ePreview.style.marginLeft = margin_left;
  342. ePreview.style.marginTop = margin_top;
  343. }
  344. else
  345. {
  346. // Other browsers
  347. SetAttribute(ePreview, 'style', 'margin-left: ' + margin_left + 'px; margin-top: ' + margin_top + 'px;');
  348. }
  349. }
  350. }
  351. function ClearPreview()
  352. {
  353. if (!ePreview){
  354. return;
  355. }
  356. while (ePreview.firstChild){
  357. ePreview.removeChild(ePreview.firstChild);
  358. }
  359. ePreview.innerHTML = '&nbsp;';
  360. }