till
2011-11-02 d6284b4d22d1e6912b01228b7d2a63e9fecbc5fb
commit | author | age
d6284b 1 (function() {
T 2     var url;
3
4     if (url = tinyMCEPopup.getParam("media_external_list_url"))
5         document.write('<script language="javascript" type="text/javascript" src="' + tinyMCEPopup.editor.documentBaseURI.toAbsolute(url) + '"></script>');
6
7     function get(id) {
8         return document.getElementById(id);
9     }
10
11     function clone(obj) {
12         var i, len, copy, attr;
13
14         if (null == obj || "object" != typeof obj)
15             return obj;
16
17         // Handle Array
18         if ('length' in obj) {
19             copy = [];
20
21             for (i = 0, len = obj.length; i < len; ++i) {
22                 copy[i] = clone(obj[i]);
23             }
24
25             return copy;
26         }
27
28         // Handle Object
29         copy = {};
30         for (attr in obj) {
31             if (obj.hasOwnProperty(attr))
32                 copy[attr] = clone(obj[attr]);
33         }
34
35         return copy;
36     }
37
38     function getVal(id) {
39         var elm = get(id);
40
41         if (elm.nodeName == "SELECT")
42             return elm.options[elm.selectedIndex].value;
43
44         if (elm.type == "checkbox")
45             return elm.checked;
46
47         return elm.value;
48     }
49
50     function setVal(id, value, name) {
51         if (typeof(value) != 'undefined') {
52             var elm = get(id);
53
54             if (elm.nodeName == "SELECT")
55                 selectByValue(document.forms[0], id, value);
56             else if (elm.type == "checkbox") {
57                 if (typeof(value) == 'string') {
58                     value = value.toLowerCase();
59                     value = (!name && value === 'true') || (name && value === name.toLowerCase());
60                 }
61                 elm.checked = !!value;
62             } else
63                 elm.value = value;
64         }
65     }
66
67     window.Media = {
68         init : function() {
69             var html, editor;
70
71             this.editor = editor = tinyMCEPopup.editor;
72
73             // Setup file browsers and color pickers
74             get('filebrowsercontainer').innerHTML = getBrowserHTML('filebrowser','src','media','media');
75             get('qtsrcfilebrowsercontainer').innerHTML = getBrowserHTML('qtsrcfilebrowser','quicktime_qtsrc','media','media');
76             get('bgcolor_pickcontainer').innerHTML = getColorPickerHTML('bgcolor_pick','bgcolor');
77             get('video_altsource1_filebrowser').innerHTML = getBrowserHTML('video_filebrowser_altsource1','video_altsource1','media','media');
78             get('video_altsource2_filebrowser').innerHTML = getBrowserHTML('video_filebrowser_altsource2','video_altsource2','media','media');
79             get('audio_altsource1_filebrowser').innerHTML = getBrowserHTML('audio_filebrowser_altsource1','audio_altsource1','media','media');
80             get('audio_altsource2_filebrowser').innerHTML = getBrowserHTML('audio_filebrowser_altsource2','audio_altsource2','media','media');
81             get('video_poster_filebrowser').innerHTML = getBrowserHTML('filebrowser_poster','video_poster','media','image');
82
83             html = this.getMediaListHTML('medialist', 'src', 'media', 'media');
84             if (html == "")
85                 get("linklistrow").style.display = 'none';
86             else
87                 get("linklistcontainer").innerHTML = html;
88
89             if (isVisible('filebrowser'))
90                 get('src').style.width = '230px';
91
92             if (isVisible('video_filebrowser_altsource1'))
93                 get('video_altsource1').style.width = '220px';
94
95             if (isVisible('video_filebrowser_altsource2'))
96                 get('video_altsource2').style.width = '220px';
97
98             if (isVisible('audio_filebrowser_altsource1'))
99                 get('audio_altsource1').style.width = '220px';
100
101             if (isVisible('audio_filebrowser_altsource2'))
102                 get('audio_altsource2').style.width = '220px';
103
104             if (isVisible('filebrowser_poster'))
105                 get('video_poster').style.width = '220px';
106
107             editor.dom.setOuterHTML(get('media_type'), this.getMediaTypeHTML(editor));
108
109             this.data = clone(tinyMCEPopup.getWindowArg('data'));
110             this.dataToForm();
111             this.preview();
112
113             updateColor('bgcolor_pick', 'bgcolor');
114         },
115
116         insert : function() {
117             var editor = tinyMCEPopup.editor;
118
119             this.formToData();
120             editor.execCommand('mceRepaint');
121             tinyMCEPopup.restoreSelection();
122             editor.selection.setNode(editor.plugins.media.dataToImg(this.data));
123             tinyMCEPopup.close();
124         },
125
126         preview : function() {
127             get('prev').innerHTML = this.editor.plugins.media.dataToHtml(this.data, true);
128         },
129
130         moveStates : function(to_form, field) {
131             var data = this.data, editor = this.editor,
132                 mediaPlugin = editor.plugins.media, ext, src, typeInfo, defaultStates, src;
133
134             defaultStates = {
135                 // QuickTime
136                 quicktime_autoplay : true,
137                 quicktime_controller : true,
138
139                 // Flash
140                 flash_play : true,
141                 flash_loop : true,
142                 flash_menu : true,
143
144                 // WindowsMedia
145                 windowsmedia_autostart : true,
146                 windowsmedia_enablecontextmenu : true,
147                 windowsmedia_invokeurls : true,
148
149                 // RealMedia
150                 realmedia_autogotourl : true,
151                 realmedia_imagestatus : true
152             };
153
154             function parseQueryParams(str) {
155                 var out = {};
156
157                 if (str) {
158                     tinymce.each(str.split('&'), function(item) {
159                         var parts = item.split('=');
160
161                         out[unescape(parts[0])] = unescape(parts[1]);
162                     });
163                 }
164
165                 return out;
166             };
167
168             function setOptions(type, names) {
169                 var i, name, formItemName, value, list;
170
171                 if (type == data.type || type == 'global') {
172                     names = tinymce.explode(names);
173                     for (i = 0; i < names.length; i++) {
174                         name = names[i];
175                         formItemName = type == 'global' ? name : type + '_' + name;
176
177                         if (type == 'global')
178                             list = data;
179                         else if (type == 'video' || type == 'audio') {
180                             list = data.video.attrs;
181
182                             if (!list && !to_form)
183                                 data.video.attrs = list = {};
184                         } else
185                             list = data.params;
186
187                         if (list) {
188                             if (to_form) {
189                                 setVal(formItemName, list[name], type == 'video' || type == 'audio' ? name : '');
190                             } else {
191                                 delete list[name];
192
193                                 value = getVal(formItemName);
194                                 if ((type == 'video' || type == 'audio') && value === true)
195                                     value = name;
196
197                                 if (defaultStates[formItemName]) {
198                                     if (value !== defaultStates[formItemName]) {
199                                         value = "" + value;
200                                         list[name] = value;
201                                     }
202                                 } else if (value) {
203                                     value = "" + value;
204                                     list[name] = value;
205                                 }
206                             }
207                         }
208                     }
209                 }
210             }
211
212             if (!to_form) {
213                 data.type = get('media_type').options[get('media_type').selectedIndex].value;
214                 data.width = getVal('width');
215                 data.height = getVal('height');
216
217                 // Switch type based on extension
218                 src = getVal('src');
219                 if (field == 'src') {
220                     ext = src.replace(/^.*\.([^.]+)$/, '$1');
221                     if (typeInfo = mediaPlugin.getType(ext))
222                         data.type = typeInfo.name.toLowerCase();
223
224                     setVal('media_type', data.type);
225                 }
226
227                 if (data.type == "video" || data.type == "audio") {
228                     if (!data.video.sources)
229                         data.video.sources = [];
230
231                     data.video.sources[0] = {src: getVal('src')};
232                 }
233             }
234
235             // Hide all fieldsets and show the one active
236             get('video_options').style.display = 'none';
237             get('audio_options').style.display = 'none';
238             get('flash_options').style.display = 'none';
239             get('quicktime_options').style.display = 'none';
240             get('shockwave_options').style.display = 'none';
241             get('windowsmedia_options').style.display = 'none';
242             get('realmedia_options').style.display = 'none';
243             get('embeddedaudio_options').style.display = 'none';
244
245             if (get(data.type + '_options'))
246                 get(data.type + '_options').style.display = 'block';
247
248             setVal('media_type', data.type);
249
250             setOptions('flash', 'play,loop,menu,swliveconnect,quality,scale,salign,wmode,base,flashvars');
251             setOptions('quicktime', 'loop,autoplay,cache,controller,correction,enablejavascript,kioskmode,autohref,playeveryframe,targetcache,scale,starttime,endtime,target,qtsrcchokespeed,volume,qtsrc');
252             setOptions('shockwave', 'sound,progress,autostart,swliveconnect,swvolume,swstretchstyle,swstretchhalign,swstretchvalign');
253             setOptions('windowsmedia', 'autostart,enabled,enablecontextmenu,fullscreen,invokeurls,mute,stretchtofit,windowlessvideo,balance,baseurl,captioningid,currentmarker,currentposition,defaultframe,playcount,rate,uimode,volume');
254             setOptions('realmedia', 'autostart,loop,autogotourl,center,imagestatus,maintainaspect,nojava,prefetch,shuffle,console,controls,numloop,scriptcallbacks');
255             setOptions('video', 'poster,autoplay,loop,muted,preload,controls');
256             setOptions('audio', 'autoplay,loop,preload,controls');
257             setOptions('embeddedaudio', 'autoplay,loop,controls');
258             setOptions('global', 'id,name,vspace,hspace,bgcolor,align,width,height');
259
260             if (to_form) {
261                 if (data.type == 'video') {
262                     if (data.video.sources[0])
263                         setVal('src', data.video.sources[0].src);
264
265                     src = data.video.sources[1];
266                     if (src)
267                         setVal('video_altsource1', src.src);
268
269                     src = data.video.sources[2];
270                     if (src)
271                         setVal('video_altsource2', src.src);
272                 } else if (data.type == 'audio') {
273                     if (data.video.sources[0])
274                         setVal('src', data.video.sources[0].src);
275                     
276                     src = data.video.sources[1];
277                     if (src)
278                         setVal('audio_altsource1', src.src);
279                     
280                     src = data.video.sources[2];
281                     if (src)
282                         setVal('audio_altsource2', src.src);
283                 } else {
284                     // Check flash vars
285                     if (data.type == 'flash') {
286                         tinymce.each(editor.getParam('flash_video_player_flashvars', {url : '$url', poster : '$poster'}), function(value, name) {
287                             if (value == '$url')
288                                 data.params.src = parseQueryParams(data.params.flashvars)[name] || data.params.src || '';
289                         });
290                     }
291
292                     setVal('src', data.params.src);
293                 }
294             } else {
295                 src = getVal("src");
296
297                 // YouTube *NEW*
298                 if (src.match(/youtu.be\/[a-z1-9.-_]+/)) {
299                     data.width = 425;
300                     data.height = 350;
301                     data.params.frameborder = '0';
302                     data.type = 'iframe';
303                     src = 'http://www.youtube.com/embed/' + src.match(/youtu.be\/([a-z1-9.-_]+)/)[1];
304                     setVal('src', src);
305                     setVal('media_type', data.type);
306                 }
307
308                 // YouTube
309                 if (src.match(/youtube.com(.+)v=([^&]+)/)) {
310                     data.width = 425;
311                     data.height = 350;
312                     data.params.frameborder = '0';
313                     data.type = 'iframe';
314                     src = 'http://www.youtube.com/embed/' + src.match(/v=([^&]+)/)[1];
315                     setVal('src', src);
316                     setVal('media_type', data.type);
317                 }
318
319                 // Google video
320                 if (src.match(/video.google.com(.+)docid=([^&]+)/)) {
321                     data.width = 425;
322                     data.height = 326;
323                     data.type = 'flash';
324                     src = 'http://video.google.com/googleplayer.swf?docId=' + src.match(/docid=([^&]+)/)[1] + '&hl=en';
325                     setVal('src', src);
326                     setVal('media_type', data.type);
327                 }
328
329                 if (data.type == 'video') {
330                     if (!data.video.sources)
331                         data.video.sources = [];
332
333                     data.video.sources[0] = {src : src};
334
335                     src = getVal("video_altsource1");
336                     if (src)
337                         data.video.sources[1] = {src : src};
338
339                     src = getVal("video_altsource2");
340                     if (src)
341                         data.video.sources[2] = {src : src};
342                 } else if (data.type == 'audio') {
343                     if (!data.video.sources)
344                         data.video.sources = [];
345                     
346                     data.video.sources[0] = {src : src};
347                     
348                     src = getVal("audio_altsource1");
349                     if (src)
350                         data.video.sources[1] = {src : src};
351                     
352                     src = getVal("audio_altsource2");
353                     if (src)
354                         data.video.sources[2] = {src : src};
355                 } else
356                     data.params.src = src;
357
358                 // Set default size
359                 setVal('width', data.width || (data.type == 'audio' ? 300 : 320));
360                 setVal('height', data.height || (data.type == 'audio' ? 32 : 240));
361             }
362         },
363
364         dataToForm : function() {
365             this.moveStates(true);
366         },
367
368         formToData : function(field) {
369             if (field == "width" || field == "height")
370                 this.changeSize(field);
371
372             if (field == 'source') {
373                 this.moveStates(false, field);
374                 setVal('source', this.editor.plugins.media.dataToHtml(this.data));
375                 this.panel = 'source';
376             } else {
377                 if (this.panel == 'source') {
378                     this.data = clone(this.editor.plugins.media.htmlToData(getVal('source')));
379                     this.dataToForm();
380                     this.panel = '';
381                 }
382
383                 this.moveStates(false, field);
384                 this.preview();
385             }
386         },
387
388         beforeResize : function() {
389             this.width = parseInt(getVal('width') || (this.data.type == 'audio' ? "300" : "320"), 10);
390             this.height = parseInt(getVal('height') || (this.data.type == 'audio' ? "32" : "240"), 10);
391         },
392
393         changeSize : function(type) {
394             var width, height, scale, size;
395
396             if (get('constrain').checked) {
397                 width = parseInt(getVal('width') || (this.data.type == 'audio' ? "300" : "320"), 10);
398                 height = parseInt(getVal('height') || (this.data.type == 'audio' ? "32" : "240"), 10);
399
400                 if (type == 'width') {
401                     this.height = Math.round((width / this.width) * height);
402                     setVal('height', this.height);
403                 } else {
404                     this.width = Math.round((height / this.height) * width);
405                     setVal('width', this.width);
406                 }
407             }
408         },
409
410         getMediaListHTML : function() {
411             if (typeof(tinyMCEMediaList) != "undefined" && tinyMCEMediaList.length > 0) {
412                 var html = "";
413
414                 html += '<select id="linklist" name="linklist" style="width: 250px" onchange="this.form.src.value=this.options[this.selectedIndex].value;Media.formToData(\'src\');">';
415                 html += '<option value="">---</option>';
416
417                 for (var i=0; i<tinyMCEMediaList.length; i++)
418                     html += '<option value="' + tinyMCEMediaList[i][1] + '">' + tinyMCEMediaList[i][0] + '</option>';
419
420                 html += '</select>';
421
422                 return html;
423             }
424
425             return "";
426         },
427
428         getMediaTypeHTML : function(editor) {
429             var html = "";
430             html += '<select id="media_type" name="media_type" onchange="Media.formToData(\'type\');">';
431             html += '<option value="video">HTML5 Video</option>';
432             html += '<option value="audio">HTML5 Audio</option>';
433             html += '<option value="flash">Flash</option>';
434             html += '<option value="quicktime">QuickTime</option>';
435             html += '<option value="shockwave">Shockwave</option>';
436             html += '<option value="windowsmedia">Windows Media</option>';
437             html += '<option value="realmedia">Real Media</option>';
438             html += '<option value="iframe">Iframe</option>';
439
440             if (editor.getParam('media_embedded_audio', false)) {
441                 html += '<option value="embeddedaudio">Embedded Audio</option>';
442             }
443             
444             html += '</select>';
445             return html;
446         }
447     };
448
449     tinyMCEPopup.requireLangPack();
450     tinyMCEPopup.onInit.add(function() {
451         Media.init();
452     });
453 })();