PageRenderTime 6ms CodeModel.GetById 38ms app.highlight 63ms RepoModel.GetById 1ms app.codeStats 0ms

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

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