4 * Copyright 2009, Moxiecode Systems AB
5 * Released under LGPL License.
7 * License: http://tinymce.moxiecode.com/license
8 * Contributing: http://tinymce.moxiecode.com/contributing
12 var rootAttributes = tinymce.explode('id,name,width,height,style,align,class,hspace,vspace,bgcolor,type'), excludedAttrs = tinymce.makeMap(rootAttributes.join(',')), Node = tinymce.html.Node,
13 mediaTypes, scriptRegExp, JSON = tinymce.util.JSON, mimeTypes;
15 // Media types supported by this plugin
17 // Type, clsid:s, mime types, codebase
18 ["Flash", "d27cdb6e-ae6d-11cf-96b8-444553540000", "application/x-shockwave-flash", "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"],
19 ["ShockWave", "166b1bca-3f9c-11cf-8075-444553540000", "application/x-director", "http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=8,5,1,0"],
20 ["WindowsMedia", "6bf52a52-394a-11d3-b153-00c04f79faa6,22d6f312-b0f6-11d0-94ab-0080c74c7e95,05589fa1-c356-11ce-bf01-00aa0055595a", "application/x-mplayer2", "http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701"],
21 ["QuickTime", "02bf25d5-8c17-4b23-bc80-d3488abddc6b", "video/quicktime", "http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0"],
22 ["RealMedia", "cfcdaa03-8be4-11cf-b84b-0020afbbccfa", "audio/x-pn-realaudio-plugin", "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"],
23 ["Java", "8ad9c840-044e-11d1-b3e9-00805f499d93", "application/x-java-applet", "http://java.sun.com/products/plugin/autodl/jinstall-1_5_0-windows-i586.cab#Version=1,5,0,0"],
24 ["Silverlight", "dfeaf541-f3e1-4c24-acac-99c30715084a", "application/x-silverlight-2"],
30 function toArray(obj) {
33 if (obj && !obj.splice) {
36 for (i = 0; true; i++) {
49 tinymce.create('tinymce.plugins.MediaPlugin', {
50 init : function(ed, url) {
51 var self = this, lookup = {}, i, y, item, name;
53 function isMediaImg(node) {
54 return node && node.nodeName === 'IMG' && ed.dom.hasClass(node, 'mceItemMedia');
60 // Parse media types into a lookup table
62 for (i = 0; i < mediaTypes.length; i++) {
63 name = mediaTypes[i][0];
67 clsids : tinymce.explode(mediaTypes[i][1] || ''),
68 mimes : tinymce.explode(mediaTypes[i][2] || ''),
69 codebase : mediaTypes[i][3]
72 for (y = 0; y < item.clsids.length; y++)
73 lookup['clsid:' + item.clsids[y]] = item;
75 for (y = 0; y < item.mimes.length; y++)
76 lookup[item.mimes[y]] = item;
78 lookup['mceItem' + name] = item;
79 lookup[name.toLowerCase()] = item;
81 scriptRegExp += (scriptRegExp ? '|' : '') + name;
84 // Handle the media_types setting
85 tinymce.each(ed.getParam("media_types",
86 "video=mp4,m4v,ogv,webm;" +
90 "quicktime=mov,qt,mpg,mpeg;" +
92 "windowsmedia=avi,wmv,wm,asf,asx,wmx,wvx;" +
93 "realmedia=rm,ra,ram;" +
96 ).split(';'), function(item) {
97 var i, extensions, type;
99 item = item.split(/=/);
100 extensions = tinymce.explode(item[1].toLowerCase());
101 for (i = 0; i < extensions.length; i++) {
102 type = lookup[item[0].toLowerCase()];
105 lookup[extensions[i]] = type;
109 scriptRegExp = new RegExp('write(' + scriptRegExp + ')\\(([^)]+)\\)');
110 self.lookup = lookup;
112 ed.onPreInit.add(function() {
113 // Allow video elements
114 ed.schema.addValidElements('object[id|style|width|height|classid|codebase|*],param[name|value],embed[id|style|width|height|type|src|*],video[*],audio[*],source[*]');
116 // Convert video elements to image placeholder
117 ed.parser.addNodeFilter('object,embed,video,audio,script,iframe', function(nodes) {
118 var i = nodes.length;
121 self.objectToImg(nodes[i]);
124 // Convert image placeholders to video elements
125 ed.serializer.addNodeFilter('img', function(nodes, name, args) {
126 var i = nodes.length, node;
130 if ((node.attr('class') || '').indexOf('mceItemMedia') !== -1)
131 self.imgToObject(node, args);
136 ed.onInit.add(function() {
137 // Display "media" instead of "img" in element path
138 if (ed.theme && ed.theme.onResolveName) {
139 ed.theme.onResolveName.add(function(theme, path_object) {
140 if (path_object.name === 'img' && ed.dom.hasClass(path_object.node, 'mceItemMedia'))
141 path_object.name = 'media';
145 // Add contect menu if it's loaded
146 if (ed && ed.plugins.contextmenu) {
147 ed.plugins.contextmenu.onContextMenu.add(function(plugin, menu, element) {
148 if (element.nodeName === 'IMG' && element.className.indexOf('mceItemMedia') !== -1)
149 menu.add({title : 'media.edit', icon : 'media', cmd : 'mceMedia'});
155 ed.addCommand('mceMedia', function() {
158 img = ed.selection.getNode();
159 if (isMediaImg(img)) {
160 data = ed.dom.getAttrib(img, 'data-mce-json');
162 data = JSON.parse(data);
164 // Add some extra properties to the data object
165 tinymce.each(rootAttributes, function(name) {
166 var value = ed.dom.getAttrib(img, name);
172 data.type = self.getType(img.className).name.toLowerCase();
184 ed.windowManager.open({
185 file : url + '/media.htm',
186 width : 430 + parseInt(ed.getLang('media.delta_width', 0)),
187 height : 500 + parseInt(ed.getLang('media.delta_height', 0)),
196 ed.addButton('media', {title : 'media.desc', cmd : 'mceMedia'});
198 // Update media selection status
199 ed.onNodeChange.add(function(ed, cm, node) {
200 cm.setActive('media', isMediaImg(node));
204 convertUrl : function(url, force_absolute) {
205 var self = this, editor = self.editor, settings = editor.settings,
206 urlConverter = settings.url_converter,
207 urlConverterScope = settings.url_converter_scope || self;
213 return editor.documentBaseURI.toAbsolute(url);
215 return urlConverter.call(urlConverterScope, url, 'src', 'object');
218 getInfo : function() {
221 author : 'Moxiecode Systems AB',
222 authorurl : 'http://tinymce.moxiecode.com',
223 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/media',
224 version : tinymce.majorVersion + "." + tinymce.minorVersion
229 * Converts the JSON data object to an img node.
231 dataToImg : function(data, force_absolute) {
232 var self = this, editor = self.editor, baseUri = editor.documentBaseURI, sources, attrs, img, i;
234 data.params.src = self.convertUrl(data.params.src, force_absolute);
236 attrs = data.video.attrs;
238 attrs.src = self.convertUrl(attrs.src, force_absolute);
241 attrs.poster = self.convertUrl(attrs.poster, force_absolute);
243 sources = toArray(data.video.sources);
245 for (i = 0; i < sources.length; i++)
246 sources[i].src = self.convertUrl(sources[i].src, force_absolute);
249 img = self.editor.dom.create('img', {
253 src : self.editor.theme.url + '/img/trans.gif',
254 'class' : 'mceItemMedia mceItem' + self.getType(data.type).name,
255 'data-mce-json' : JSON.serialize(data, "'")
258 img.width = data.width || (data.type == 'audio' ? "300" : "320");
259 img.height = data.height || (data.type == 'audio' ? "32" : "240");
265 * Converts the JSON data object to a HTML string.
267 dataToHtml : function(data, force_absolute) {
268 return this.editor.serializer.serialize(this.dataToImg(data, force_absolute), {forced_root_block : '', force_absolute : force_absolute});
272 * Converts the JSON data object to a HTML string.
274 htmlToData : function(html) {
275 var fragment, img, data;
283 fragment = this.editor.parser.parse(html);
284 img = fragment.getAll('img')[0];
287 data = JSON.parse(img.attr('data-mce-json'));
288 data.type = this.getType(img.attr('class')).name.toLowerCase();
290 // Add some extra properties to the data object
291 tinymce.each(rootAttributes, function(name) {
292 var value = img.attr(name);
303 * Get type item by extension, class, clsid or mime type.
306 * @param {String} value Value to get type item by.
307 * @return {Object} Type item object or undefined.
309 getType : function(value) {
310 var i, values, typeItem;
312 // Find type by checking the classes
313 values = tinymce.explode(value, ' ');
314 for (i = 0; i < values.length; i++) {
315 typeItem = this.lookup[values[i]];
323 * Converts a tinymce.html.Node image element to video/object/embed.
325 imgToObject : function(node, args) {
326 var self = this, editor = self.editor, video, object, embed, iframe, name, value, data,
327 source, sources, params, param, typeItem, i, item, mp4Source, replacement,
328 posterSrc, style, audio;
330 // Adds the flash player
331 function addPlayer(video_src, poster_src) {
332 var baseUri, flashVars, flashVarsOutput, params, flashPlayer;
334 flashPlayer = editor.getParam('flash_video_player_url', self.convertUrl(self.url + '/moxieplayer.swf'));
336 baseUri = editor.documentBaseURI;
337 data.params.src = flashPlayer;
339 // Convert the movie url to absolute urls
340 if (editor.getParam('flash_video_player_absvideourl', true)) {
341 video_src = baseUri.toAbsolute(video_src || '', true);
342 poster_src = baseUri.toAbsolute(poster_src || '', true);
345 // Generate flash vars
346 flashVarsOutput = '';
347 flashVars = editor.getParam('flash_video_player_flashvars', {url : '$url', poster : '$poster'});
348 tinymce.each(flashVars, function(value, name) {
349 // Replace $url and $poster variables in flashvars value
350 value = value.replace(/\$url/, video_src || '');
351 value = value.replace(/\$poster/, poster_src || '');
353 if (value.length > 0)
354 flashVarsOutput += (flashVarsOutput ? '&' : '') + name + '=' + escape(value);
357 if (flashVarsOutput.length)
358 data.params.flashvars = flashVarsOutput;
360 params = editor.getParam('flash_video_player_params', {
361 allowfullscreen: true,
362 allowscriptaccess: true
365 tinymce.each(params, function(value, name) {
366 data.params[name] = "" + value;
371 data = node.attr('data-mce-json');
375 data = JSON.parse(data);
376 typeItem = this.getType(node.attr('class'));
378 style = node.attr('data-mce-style')
380 style = node.attr('style');
383 style = editor.dom.serializeStyle(editor.dom.parseStyle(style, 'img'));
387 if (typeItem.name === 'Iframe') {
388 replacement = new Node('iframe', 1);
390 tinymce.each(rootAttributes, function(name) {
391 var value = node.attr(name);
393 if (name == 'class' && value)
394 value = value.replace(/mceItem.+ ?/g, '');
396 if (value && value.length > 0)
397 replacement.attr(name, value);
400 for (name in data.params)
401 replacement.attr(name, data.params[name]);
408 node.replace(replacement);
414 if (this.editor.settings.media_use_script) {
415 replacement = new Node('script', 1).attr('type', 'text/javascript');
417 value = new Node('#text', 3);
418 value.value = 'write' + typeItem.name + '(' + JSON.serialize(tinymce.extend(data.params, {
419 width: node.attr('width'),
420 height: node.attr('height')
423 replacement.append(value);
424 node.replace(replacement);
429 // Add HTML5 video element
430 if (typeItem.name === 'Video' && data.video.sources[0]) {
431 // Create new object element
432 video = new Node('video', 1).attr(tinymce.extend({
433 id : node.attr('id'),
434 width: node.attr('width'),
435 height: node.attr('height'),
437 }, data.video.attrs));
439 // Get poster source and use that for flash fallback
440 if (data.video.attrs)
441 posterSrc = data.video.attrs.poster;
443 sources = data.video.sources = toArray(data.video.sources);
444 for (i = 0; i < sources.length; i++) {
445 if (/\.mp4$/.test(sources[i].src))
446 mp4Source = sources[i].src;
449 if (!sources[0].type) {
450 video.attr('src', sources[0].src);
451 sources.splice(0, 1);
454 for (i = 0; i < sources.length; i++) {
455 source = new Node('source', 1).attr(sources[i]);
456 source.shortEnded = true;
457 video.append(source);
460 // Create flash fallback for video if we have a mp4 source
462 addPlayer(mp4Source, posterSrc);
463 typeItem = self.getType('flash');
465 data.params.src = '';
468 // Add HTML5 audio element
469 if (typeItem.name === 'Audio' && data.video.sources[0]) {
470 // Create new object element
471 audio = new Node('audio', 1).attr(tinymce.extend({
472 id : node.attr('id'),
473 width: node.attr('width'),
474 height: node.attr('height'),
476 }, data.video.attrs));
478 // Get poster source and use that for flash fallback
479 if (data.video.attrs)
480 posterSrc = data.video.attrs.poster;
482 sources = data.video.sources = toArray(data.video.sources);
483 if (!sources[0].type) {
484 audio.attr('src', sources[0].src);
485 sources.splice(0, 1);
488 for (i = 0; i < sources.length; i++) {
489 source = new Node('source', 1).attr(sources[i]);
490 source.shortEnded = true;
491 audio.append(source);
494 data.params.src = '';
497 // Do we have a params src then we can generate object
498 if (data.params.src) {
499 // Is flv movie add player for it
500 if (/\.flv$/i.test(data.params.src))
501 addPlayer(data.params.src, '');
503 if (args && args.force_absolute)
504 data.params.src = editor.documentBaseURI.toAbsolute(data.params.src);
506 // Create new object element
507 object = new Node('object', 1).attr({
508 id : node.attr('id'),
509 width: node.attr('width'),
510 height: node.attr('height'),
514 tinymce.each(rootAttributes, function(name) {
515 if (data[name] && name != 'type')
516 object.attr(name, data[name]);
520 for (name in data.params) {
521 param = new Node('param', 1);
522 param.shortEnded = true;
523 value = data.params[name];
525 // Windows media needs to use url instead of src for the media URL
526 if (name === 'src' && typeItem.name === 'WindowsMedia')
529 param.attr({name: name, value: value});
530 object.append(param);
533 // Setup add type and classid if strict is disabled
534 if (this.editor.getParam('media_strict', true)) {
536 data: data.params.src,
537 type: typeItem.mimes[0]
541 classid: "clsid:" + typeItem.clsids[0],
542 codebase: typeItem.codebase
545 embed = new Node('embed', 1);
546 embed.shortEnded = true;
549 width: node.attr('width'),
550 height: node.attr('height'),
552 type: typeItem.mimes[0]
555 for (name in data.params)
556 embed.attr(name, data.params[name]);
558 tinymce.each(rootAttributes, function(name) {
559 if (data[name] && name != 'type')
560 embed.attr(name, data[name]);
563 object.append(embed);
567 if (data.object_html) {
568 value = new Node('#text', 3);
570 value.value = data.object_html;
571 object.append(value);
574 // Append object to video element if it exists
576 video.append(object);
581 if (data.video_html) {
582 value = new Node('#text', 3);
584 value.value = data.video_html;
591 if (data.video_html) {
592 value = new Node('#text', 3);
594 value.value = data.video_html;
599 if (video || audio || object)
600 node.replace(video || audio || object);
606 * Converts a tinymce.html.Node video/object/embed to an img element.
608 * The video/object/embed will be converted into an image placeholder with a JSON data attribute like this:
609 * <img class="mceItemMedia mceItemFlash" width="100" height="100" data-mce-json="{..}" />
611 * The JSON structure will be like this:
612 * {'params':{'flashvars':'something','quality':'high','src':'someurl'}, 'video':{'sources':[{src: 'someurl', type: 'video/mp4'}]}}
614 objectToImg : function(node) {
615 var object, embed, video, iframe, img, name, id, width, height, style, i, html,
616 param, params, source, sources, data, type, lookup = this.lookup,
617 matches, attrs, urlConverter = this.editor.settings.url_converter,
618 urlConverterScope = this.editor.settings.url_converter_scope;
620 function getInnerHTML(node) {
621 return new tinymce.html.Serializer({
627 // If node isn't in document
631 // Handle media scripts
632 if (node.name === 'script') {
634 matches = scriptRegExp.exec(node.firstChild.value);
640 data = {video : {}, params : JSON.parse(matches[2])};
641 width = data.params.width;
642 height = data.params.height;
645 // Setup data objects
651 // Setup new image object
652 img = new Node('img', 1);
654 src : this.editor.theme.url + '/img/trans.gif'
659 if (name === 'video' || name == 'audio') {
661 object = node.getAll('object')[0];
662 embed = node.getAll('embed')[0];
663 width = video.attr('width');
664 height = video.attr('height');
665 id = video.attr('id');
666 data.video = {attrs : {}, sources : []};
668 // Get all video attributes
669 attrs = data.video.attrs;
670 for (name in video.attributes.map)
671 attrs[name] = video.attributes.map[name];
673 source = node.attr('src');
675 data.video.sources.push({src : urlConverter.call(urlConverterScope, source, 'src', node.name)});
678 sources = video.getAll("source");
679 for (i = 0; i < sources.length; i++) {
680 source = sources[i].remove();
682 data.video.sources.push({
683 src: urlConverter.call(urlConverterScope, source.attr('src'), 'src', 'source'),
684 type: source.attr('type'),
685 media: source.attr('media')
689 // Convert the poster URL
691 attrs.poster = urlConverter.call(urlConverterScope, attrs.poster, 'poster', node.name);
695 if (node.name === 'object') {
697 embed = node.getAll('embed')[0];
701 if (node.name === 'embed')
705 if (node.name === 'iframe') {
712 width = width || object.attr('width');
713 height = height || object.attr('height');
714 style = style || object.attr('style');
715 id = id || object.attr('id');
717 // Get all object params
718 params = object.getAll("param");
719 for (i = 0; i < params.length; i++) {
721 name = param.remove().attr('name');
723 if (!excludedAttrs[name])
724 data.params[name] = param.attr('value');
727 data.params.src = data.params.src || object.attr('data');
732 width = width || embed.attr('width');
733 height = height || embed.attr('height');
734 style = style || embed.attr('style');
735 id = id || embed.attr('id');
737 // Get all embed attributes
738 for (name in embed.attributes.map) {
739 if (!excludedAttrs[name] && !data.params[name])
740 data.params[name] = embed.attributes.map[name];
746 width = iframe.attr('width');
747 height = iframe.attr('height');
748 style = style || iframe.attr('style');
749 id = iframe.attr('id');
751 tinymce.each(rootAttributes, function(name) {
752 img.attr(name, iframe.attr(name));
755 // Get all iframe attributes
756 for (name in iframe.attributes.map) {
757 if (!excludedAttrs[name] && !data.params[name])
758 data.params[name] = iframe.attributes.map[name];
763 if (data.params.movie) {
764 data.params.src = data.params.src || data.params.movie;
765 delete data.params.movie;
768 // Convert the URL to relative/absolute depending on configuration
770 data.params.src = urlConverter.call(urlConverterScope, data.params.src, 'src', 'object');
773 if (node.name === 'video')
774 type = lookup.video.name;
775 else if (node.name === 'audio')
776 type = lookup.audio.name;
780 type = (lookup[(object.attr('clsid') || '').toLowerCase()] || lookup[(object.attr('type') || '').toLowerCase()] || {}).name;
783 type = (lookup[(embed.attr('type') || '').toLowerCase()] || {}).name;
785 // Replace the video/object/embed element with a placeholder image containing the data
792 // Serialize the inner HTML of the object element
794 html = getInnerHTML(object.remove());
797 data.object_html = html;
800 // Serialize the inner HTML of the video element
802 html = getInnerHTML(video.remove());
805 data.video_html = html;
808 // Set width/height of placeholder
811 'class' : 'mceItemMedia mceItem' + (type || 'Flash'),
813 width : width || (node.name == 'audio' ? "300" : "320"),
814 height : height || (node.name == 'audio' ? "32" : "240"),
815 "data-mce-json" : JSON.serialize(data, "'")
821 tinymce.PluginManager.add('media', tinymce.plugins.MediaPlugin);