MDL-29624 Media embedding system, part 2: preview in file picker
[moodle.git] / lib / editor / tinymce / tiny_mce / 3.5 / plugins / moodlemedia / js / media.js
CommitLineData
6500eefe
PS
1/**
2 * @author Dongsheng Cai <dongsheng@moodle.com>
3 */
4tinyMCEPopup.requireLangPack();
5
6var oldWidth, oldHeight, ed, url;
7
8if (url = tinyMCEPopup.getParam("media_external_list_url")) {
9 document.write('<script language="javascript" type="text/javascript" src="' + tinyMCEPopup.editor.documentBaseURI.toAbsolute(url) + '"></script>');
10}
11
12function init() {
13 ed = tinyMCEPopup.editor;
14 document.getElementById('filebrowsercontainer').innerHTML = getBrowserHTML('filebrowser','src','media','media');
15}
16
17function insertMedia() {
18 var f = document.forms[0];
19 var url = f.filename.value;
20 var linkname = url.substring(url.lastIndexOf('/')+1);
21 var h = '<a href="'+f.src.value+'">'+linkname+'</a>';
22 ed.execCommand('mceInsertContent', false, h);
23 tinyMCEPopup.close();
24}
25
26function getType(v) {
27 var fo, i, c, el, x, f = document.forms[0];
28
29 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(';');
30
31 // YouTube
32 if (v.match(/watch\?v=(.+)(.*)/)) {
33 f.src.value = 'http://www.youtube.com/v/' + v.match(/v=(.*)(.*)/)[0].split('=')[1];
34 return 'flash';
35 } else if (v.match(/v\/(.+)(.*)/)) {
36 return 'flash';
37 }
38
39 // Google video
40 if (v.indexOf('http://video.google.com/videoplay?docid=') == 0) {
41 f.src.value = 'http://video.google.com/googleplayer.swf?docId=' + v.substring('http://video.google.com/videoplay?docid='.length) + '&hl=en';
42 return 'flash';
43 }
44
45 for (i=0; i<fo.length; i++) {
46 c = fo[i].split('=');
47
48 el = c[1].split(',');
49 for (x=0; x<el.length; x++)
50 if (v.indexOf('.' + el[x]) != -1)
51 return c[0];
52 }
53
54 return null;
55}
56
57
58function serializeParameters() {
59 var d = document, f = d.forms[0], s = '';
60 s += getStr(null, 'src');
61 s += 'width:300,';
62 s += 'height:225,';
63
64 // delete the tail comma
65 s = s.length > 0 ? s.substring(0, s.length - 1) : s;
66
67 return s;
68}
69
70
71function getStr(p, n, d) {
72 var e = document.forms[0].elements[(p != null ? p + "_" : "") + n];
73 var v = e.type == "hidden" ? e.value : e.options[e.selectedIndex].value;
74
75 if (n == 'src')
76 v = tinyMCEPopup.editor.convertURL(v, 'src', null);
77
78 return ((n == d || v == '') ? '' : n + ":'" + jsEncode(v) + "',");
79}
80
81function jsEncode(s) {
82 s = s.replace(new RegExp('\\\\', 'g'), '\\\\');
83 s = s.replace(new RegExp('"', 'g'), '\\"');
84 s = s.replace(new RegExp("'", 'g'), "\\'");
85
86 return s;
87}
88
89function generatePreview(c) {
90 var f = document.forms[0], p = document.getElementById('prev'), h = '', cls, pl, n, type, codebase, wp, hp, nw, nh;
91
92 p.innerHTML = '<!-- x --->';
93 var type = getType(f.src.value);
94 var re = new RegExp("(.+)\#(.+)", "i");
95 var result = f.src.value.match(re);
96 if (result) {
97 f.src.value = result[1];
98 f.filename.value = result[2];
99 } else {
100 f.src.value = f.src.value;
101 f.filename.value = f.src.value;
102 }
103
104 // After constrain
105 pl = serializeParameters();
6500eefe
PS
106 if (pl == '') {
107 p.innerHTML = '';
108 return;
109 }
110
111 pl = tinyMCEPopup.editor.plugins.moodlemedia._parse(pl);
112
113 if (!pl.src) {
114 p.innerHTML = '';
115 return;
116 }
117
118 pl.src = tinyMCEPopup.editor.documentBaseURI.toAbsolute(pl.src);
119 pl.width = !pl.width ? 100 : pl.width;
120 pl.height = !pl.height ? 100 : pl.height;
121 pl.id = !pl.id ? 'moodlemediaid' : pl.id;
122 pl.name = !pl.name ? 'moodlemedianame' : pl.name;
123 pl.align = !pl.align ? '' : pl.align;
124
125 // Avoid annoying warning about insecure items
126 if (!tinymce.isIE || document.location.protocol != 'https:') {
f4e7ba5e 127 // Include all the draftfile params after the ?
128 var draftparams = pl.src.toString().replace(/^.*\/draftfile.php\//, '');
129 h = '<iframe src="' + tinyMCE.baseURL + '/plugins/moodlemedia/preview.php?path=' +
130 draftparams + '" width="100%" height="100%"></iframe>';
6500eefe
PS
131 }
132
f4e7ba5e 133 // I don't know why the HTML comment is there, but leaving it just in case
6500eefe
PS
134 p.innerHTML = "<!-- x --->" + h;
135}
136
137tinyMCEPopup.onInit.add(init);