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"],
31 function normalizeSize(size) {
32 return typeof(size) == "string" ? size.replace(/[^0-9%]/g, '') : size;
35 function toArray(obj) {
38 if (obj && !obj.splice) {
41 for (i = 0; true; i++) {
54 tinymce.create('tinymce.plugins.MediaPlugin', {
55 init : function(ed, url) {
56 var self = this, lookup = {}, i, y, item, name;
58 function isMediaImg(node) {
59 return node && node.nodeName === 'IMG' && ed.dom.hasClass(node, 'mceItemMedia');
65 // Parse media types into a lookup table
67 for (i = 0; i < mediaTypes.length; i++) {
68 name = mediaTypes[i][0];
72 clsids : tinymce.explode(mediaTypes[i][1] || ''),
73 mimes : tinymce.explode(mediaTypes[i][2] || ''),
74 codebase : mediaTypes[i][3]
77 for (y = 0; y < item.clsids.length; y++)
78 lookup['clsid:' + item.clsids[y]] = item;
80 for (y = 0; y < item.mimes.length; y++)
81 lookup[item.mimes[y]] = item;
83 lookup['mceItem' + name] = item;
84 lookup[name.toLowerCase()] = item;
86 scriptRegExp += (scriptRegExp ? '|' : '') + name;
89 // Handle the media_types setting
90 tinymce.each(ed.getParam("media_types",
91 "video=mp4,m4v,ogv,webm;" +
95 "quicktime=mov,qt,mpg,mpeg;" +
97 "windowsmedia=avi,wmv,wm,asf,asx,wmx,wvx;" +
98 "realmedia=rm,ra,ram;" +
101 ).split(';'), function(item) {
102 var i, extensions, type;
104 item = item.split(/=/);
105 extensions = tinymce.explode(item[1].toLowerCase());
106 for (i = 0; i < extensions.length; i++) {
107 type = lookup[item[0].toLowerCase()];
110 lookup[extensions[i]] = type;
114 scriptRegExp = new RegExp('write(' + scriptRegExp + ')\\(([^)]+)\\)');
115 self.lookup = lookup;
117 ed.onPreInit.add(function() {
118 // Allow video elements
119 ed.schema.addValidElements('object[id|style|width|height|classid|codebase|*],param[name|value],embed[id|style|width|height|type|src|*],video[*],audio[*],source[*]');
121 // Convert video elements to image placeholder
122 ed.parser.addNodeFilter('object,embed,video,audio,script,iframe', function(nodes) {
123 var i = nodes.length;
126 self.objectToImg(nodes[i]);
129 // Convert image placeholders to video elements
130 ed.serializer.addNodeFilter('img', function(nodes, name, args) {
131 var i = nodes.length, node;
135 if ((node.attr('class') || '').indexOf('mceItemMedia') !== -1)
136 self.imgToObject(node, args);
141 ed.onInit.add(function() {
142 // Display "media" instead of "img" in element path
143 if (ed.theme && ed.theme.onResolveName) {
144 ed.theme.onResolveName.add(function(theme, path_object) {
145 if (path_object.name === 'img' && ed.dom.hasClass(path_object.node, 'mceItemMedia'))
146 path_object.name = 'media';
150 // Add contect menu if it's loaded
151 if (ed && ed.plugins.contextmenu) {
152 ed.plugins.contextmenu.onContextMenu.add(function(plugin, menu, element) {
153 if (element.nodeName === 'IMG' && element.className.indexOf('mceItemMedia') !== -1)
154 menu.add({title : 'media.edit', icon : 'media', cmd : 'mceMedia'});
160 ed.addCommand('mceMedia', function() {
163 img = ed.selection.getNode();
164 if (isMediaImg(img)) {
165 data = ed.dom.getAttrib(img, 'data-mce-json');
167 data = JSON.parse(data);
169 // Add some extra properties to the data object
170 tinymce.each(rootAttributes, function(name) {
171 var value = ed.dom.getAttrib(img, name);
177 data.type = self.getType(img.className).name.toLowerCase();
189 ed.windowManager.open({
190 file : url + '/media.htm',
191 width : 430 + parseInt(ed.getLang('media.delta_width', 0)),
192 height : 500 + parseInt(ed.getLang('media.delta_height', 0)),
201 ed.addButton('media', {title : 'media.desc', cmd : 'mceMedia'});
203 // Update media selection status
204 ed.onNodeChange.add(function(ed, cm, node) {
205 cm.setActive('media', isMediaImg(node));
209 convertUrl : function(url, force_absolute) {
210 var self = this, editor = self.editor, settings = editor.settings,
211 urlConverter = settings.url_converter,
212 urlConverterScope = settings.url_converter_scope || self;
218 return editor.documentBaseURI.toAbsolute(url);
220 return urlConverter.call(urlConverterScope, url, 'src', 'object');
223 getInfo : function() {
226 author : 'Moxiecode Systems AB',
227 authorurl : 'http://tinymce.moxiecode.com',
228 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/media',
229 version : tinymce.majorVersion + "." + tinymce.minorVersion
234 * Converts the JSON data object to an img node.
236 dataToImg : function(data, force_absolute) {
237 var self = this, editor = self.editor, baseUri = editor.documentBaseURI, sources, attrs, img, i;
239 data.params.src = self.convertUrl(data.params.src, force_absolute);
241 attrs = data.video.attrs;
243 attrs.src = self.convertUrl(attrs.src, force_absolute);
246 attrs.poster = self.convertUrl(attrs.poster, force_absolute);
248 sources = toArray(data.video.sources);
250 for (i = 0; i < sources.length; i++)
251 sources[i].src = self.convertUrl(sources[i].src, force_absolute);
254 img = self.editor.dom.create('img', {
258 hspace : data.hspace,
259 vspace : data.vspace,
260 src : self.editor.theme.url + '/img/trans.gif',
261 'class' : 'mceItemMedia mceItem' + self.getType(data.type).name,
262 'data-mce-json' : JSON.serialize(data, "'")
265 img.width = data.width = normalizeSize(data.width || (data.type == 'audio' ? "300" : "320"));
266 img.height = data.height = normalizeSize(data.height || (data.type == 'audio' ? "32" : "240"));
272 * Converts the JSON data object to a HTML string.
274 dataToHtml : function(data, force_absolute) {
275 return this.editor.serializer.serialize(this.dataToImg(data, force_absolute), {forced_root_block : '', force_absolute : force_absolute});
279 * Converts the JSON data object to a HTML string.
281 htmlToData : function(html) {
282 var fragment, img, data;
290 fragment = this.editor.parser.parse(html);
291 img = fragment.getAll('img')[0];
294 data = JSON.parse(img.attr('data-mce-json'));
295 data.type = this.getType(img.attr('class')).name.toLowerCase();
297 // Add some extra properties to the data object
298 tinymce.each(rootAttributes, function(name) {
299 var value = img.attr(name);
310 * Get type item by extension, class, clsid or mime type.
313 * @param {String} value Value to get type item by.
314 * @return {Object} Type item object or undefined.
316 getType : function(value) {
317 var i, values, typeItem;
319 // Find type by checking the classes
320 values = tinymce.explode(value, ' ');
321 for (i = 0; i < values.length; i++) {
322 typeItem = this.lookup[values[i]];
330 * Converts a tinymce.html.Node image element to video/object/embed.
332 imgToObject : function(node, args) {
333 var self = this, editor = self.editor, video, object, embed, iframe, name, value, data,
334 source, sources, params, param, typeItem, i, item, mp4Source, replacement,
335 posterSrc, style, audio;
337 // Adds the flash player
338 function addPlayer(video_src, poster_src) {
339 var baseUri, flashVars, flashVarsOutput, params, flashPlayer;
341 flashPlayer = editor.getParam('flash_video_player_url', self.convertUrl(self.url + '/moxieplayer.swf'));
343 baseUri = editor.documentBaseURI;
344 data.params.src = flashPlayer;
346 // Convert the movie url to absolute urls
347 if (editor.getParam('flash_video_player_absvideourl', true)) {
348 video_src = baseUri.toAbsolute(video_src || '', true);
349 poster_src = baseUri.toAbsolute(poster_src || '', true);
352 // Generate flash vars
353 flashVarsOutput = '';
354 flashVars = editor.getParam('flash_video_player_flashvars', {url : '$url', poster : '$poster'});
355 tinymce.each(flashVars, function(value, name) {
356 // Replace $url and $poster variables in flashvars value
357 value = value.replace(/\$url/, video_src || '');
358 value = value.replace(/\$poster/, poster_src || '');
360 if (value.length > 0)
361 flashVarsOutput += (flashVarsOutput ? '&' : '') + name + '=' + escape(value);
364 if (flashVarsOutput.length)
365 data.params.flashvars = flashVarsOutput;
367 params = editor.getParam('flash_video_player_params', {
368 allowfullscreen: true,
369 allowscriptaccess: true
372 tinymce.each(params, function(value, name) {
373 data.params[name] = "" + value;
378 data = node.attr('data-mce-json');
382 data = JSON.parse(data);
383 typeItem = this.getType(node.attr('class'));
385 style = node.attr('data-mce-style');
387 style = node.attr('style');
390 style = editor.dom.serializeStyle(editor.dom.parseStyle(style, 'img'));
393 // Use node width/height to override the data width/height when the placeholder is resized
394 data.width = node.attr('width') || data.width;
395 data.height = node.attr('height') || data.height;
398 if (typeItem.name === 'Iframe') {
399 replacement = new Node('iframe', 1);
401 tinymce.each(rootAttributes, function(name) {
402 var value = node.attr(name);
404 if (name == 'class' && value)
405 value = value.replace(/mceItem.+ ?/g, '');
407 if (value && value.length > 0)
408 replacement.attr(name, value);
411 for (name in data.params)
412 replacement.attr(name, data.params[name]);
419 node.replace(replacement);
425 if (this.editor.settings.media_use_script) {
426 replacement = new Node('script', 1).attr('type', 'text/javascript');
428 value = new Node('#text', 3);
429 value.value = 'write' + typeItem.name + '(' + JSON.serialize(tinymce.extend(data.params, {
430 width: node.attr('width'),
431 height: node.attr('height')
434 replacement.append(value);
435 node.replace(replacement);
440 // Add HTML5 video element
441 if (typeItem.name === 'Video' && data.video.sources[0]) {
442 // Create new object element
443 video = new Node('video', 1).attr(tinymce.extend({
444 id : node.attr('id'),
445 width: normalizeSize(node.attr('width')),
446 height: normalizeSize(node.attr('height')),
448 }, data.video.attrs));
450 // Get poster source and use that for flash fallback
451 if (data.video.attrs)
452 posterSrc = data.video.attrs.poster;
454 sources = data.video.sources = toArray(data.video.sources);
455 for (i = 0; i < sources.length; i++) {
456 if (/\.mp4$/.test(sources[i].src))
457 mp4Source = sources[i].src;
460 if (!sources[0].type) {
461 video.attr('src', sources[0].src);
462 sources.splice(0, 1);
465 for (i = 0; i < sources.length; i++) {
466 source = new Node('source', 1).attr(sources[i]);
467 source.shortEnded = true;
468 video.append(source);
471 // Create flash fallback for video if we have a mp4 source
473 addPlayer(mp4Source, posterSrc);
474 typeItem = self.getType('flash');
476 data.params.src = '';
479 // Add HTML5 audio element
480 if (typeItem.name === 'Audio' && data.video.sources[0]) {
481 // Create new object element
482 audio = new Node('audio', 1).attr(tinymce.extend({
483 id : node.attr('id'),
484 width: normalizeSize(node.attr('width')),
485 height: normalizeSize(node.attr('height')),
487 }, data.video.attrs));
489 // Get poster source and use that for flash fallback
490 if (data.video.attrs)
491 posterSrc = data.video.attrs.poster;
493 sources = data.video.sources = toArray(data.video.sources);
494 if (!sources[0].type) {
495 audio.attr('src', sources[0].src);
496 sources.splice(0, 1);
499 for (i = 0; i < sources.length; i++) {
500 source = new Node('source', 1).attr(sources[i]);
501 source.shortEnded = true;
502 audio.append(source);
505 data.params.src = '';
508 if (typeItem.name === 'EmbeddedAudio') {
509 embed = new Node('embed', 1);
510 embed.shortEnded = true;
513 width: normalizeSize(node.attr('width')),
514 height: normalizeSize(node.attr('height')),
516 type: node.attr('type')
519 for (name in data.params)
520 embed.attr(name, data.params[name]);
522 tinymce.each(rootAttributes, function(name) {
523 if (data[name] && name != 'type')
524 embed.attr(name, data[name]);
527 data.params.src = '';
530 // Do we have a params src then we can generate object
531 if (data.params.src) {
532 // Is flv movie add player for it
533 if (/\.flv$/i.test(data.params.src))
534 addPlayer(data.params.src, '');
536 if (args && args.force_absolute)
537 data.params.src = editor.documentBaseURI.toAbsolute(data.params.src);
539 // Create new object element
540 object = new Node('object', 1).attr({
541 id : node.attr('id'),
542 width: normalizeSize(node.attr('width')),
543 height: normalizeSize(node.attr('height')),
547 tinymce.each(rootAttributes, function(name) {
548 var value = data[name];
550 if (name == 'class' && value)
551 value = value.replace(/mceItem.+ ?/g, '');
553 if (value && name != 'type')
554 object.attr(name, value);
558 for (name in data.params) {
559 param = new Node('param', 1);
560 param.shortEnded = true;
561 value = data.params[name];
563 // Windows media needs to use url instead of src for the media URL
564 if (name === 'src' && typeItem.name === 'WindowsMedia')
567 param.attr({name: name, value: value});
568 object.append(param);
571 // Setup add type and classid if strict is disabled
572 if (this.editor.getParam('media_strict', true)) {
574 data: data.params.src,
575 type: typeItem.mimes[0]
579 classid: "clsid:" + typeItem.clsids[0],
580 codebase: typeItem.codebase
583 embed = new Node('embed', 1);
584 embed.shortEnded = true;
587 width: normalizeSize(node.attr('width')),
588 height: normalizeSize(node.attr('height')),
590 type: typeItem.mimes[0]
593 for (name in data.params)
594 embed.attr(name, data.params[name]);
596 tinymce.each(rootAttributes, function(name) {
597 if (data[name] && name != 'type')
598 embed.attr(name, data[name]);
601 object.append(embed);
605 if (data.object_html) {
606 value = new Node('#text', 3);
608 value.value = data.object_html;
609 object.append(value);
612 // Append object to video element if it exists
614 video.append(object);
619 if (data.video_html) {
620 value = new Node('#text', 3);
622 value.value = data.video_html;
629 if (data.video_html) {
630 value = new Node('#text', 3);
632 value.value = data.video_html;
637 var n = video || audio || object || embed;
645 * Converts a tinymce.html.Node video/object/embed to an img element.
647 * The video/object/embed will be converted into an image placeholder with a JSON data attribute like this:
648 * <img class="mceItemMedia mceItemFlash" width="100" height="100" data-mce-json="{..}" />
650 * The JSON structure will be like this:
651 * {'params':{'flashvars':'something','quality':'high','src':'someurl'}, 'video':{'sources':[{src: 'someurl', type: 'video/mp4'}]}}
653 objectToImg : function(node) {
654 var object, embed, video, iframe, img, name, id, width, height, style, i, html,
655 param, params, source, sources, data, type, lookup = this.lookup,
656 matches, attrs, urlConverter = this.editor.settings.url_converter,
657 urlConverterScope = this.editor.settings.url_converter_scope,
658 hspace, vspace, align, bgcolor;
660 function getInnerHTML(node) {
661 return new tinymce.html.Serializer({
667 function lookupAttribute(o, attr) {
668 return lookup[(o.attr(attr) || '').toLowerCase()];
671 function lookupExtension(src) {
672 var ext = src.replace(/^.*\.([^.]+)$/, '$1');
673 return lookup[ext.toLowerCase() || ''];
676 // If node isn't in document
680 // Handle media scripts
681 if (node.name === 'script') {
683 matches = scriptRegExp.exec(node.firstChild.value);
689 data = {video : {}, params : JSON.parse(matches[2])};
690 width = data.params.width;
691 height = data.params.height;
694 // Setup data objects
700 // Setup new image object
701 img = new Node('img', 1);
703 src : this.editor.theme.url + '/img/trans.gif'
708 if (name === 'video' || name == 'audio') {
710 object = node.getAll('object')[0];
711 embed = node.getAll('embed')[0];
712 width = video.attr('width');
713 height = video.attr('height');
714 id = video.attr('id');
715 data.video = {attrs : {}, sources : []};
717 // Get all video attributes
718 attrs = data.video.attrs;
719 for (name in video.attributes.map)
720 attrs[name] = video.attributes.map[name];
722 source = node.attr('src');
724 data.video.sources.push({src : urlConverter.call(urlConverterScope, source, 'src', node.name)});
727 sources = video.getAll("source");
728 for (i = 0; i < sources.length; i++) {
729 source = sources[i].remove();
731 data.video.sources.push({
732 src: urlConverter.call(urlConverterScope, source.attr('src'), 'src', 'source'),
733 type: source.attr('type'),
734 media: source.attr('media')
738 // Convert the poster URL
740 attrs.poster = urlConverter.call(urlConverterScope, attrs.poster, 'poster', node.name);
744 if (node.name === 'object') {
746 embed = node.getAll('embed')[0];
750 if (node.name === 'embed')
754 if (node.name === 'iframe') {
761 width = width || object.attr('width');
762 height = height || object.attr('height');
763 style = style || object.attr('style');
764 id = id || object.attr('id');
765 hspace = hspace || object.attr('hspace');
766 vspace = vspace || object.attr('vspace');
767 align = align || object.attr('align');
768 bgcolor = bgcolor || object.attr('bgcolor');
769 data.name = object.attr('name');
771 // Get all object params
772 params = object.getAll("param");
773 for (i = 0; i < params.length; i++) {
775 name = param.remove().attr('name');
777 if (!excludedAttrs[name])
778 data.params[name] = param.attr('value');
781 data.params.src = data.params.src || object.attr('data');
786 width = width || embed.attr('width');
787 height = height || embed.attr('height');
788 style = style || embed.attr('style');
789 id = id || embed.attr('id');
790 hspace = hspace || embed.attr('hspace');
791 vspace = vspace || embed.attr('vspace');
792 align = align || embed.attr('align');
793 bgcolor = bgcolor || embed.attr('bgcolor');
795 // Get all embed attributes
796 for (name in embed.attributes.map) {
797 if (!excludedAttrs[name] && !data.params[name])
798 data.params[name] = embed.attributes.map[name];
804 width = normalizeSize(iframe.attr('width'));
805 height = normalizeSize(iframe.attr('height'));
806 style = style || iframe.attr('style');
807 id = iframe.attr('id');
808 hspace = iframe.attr('hspace');
809 vspace = iframe.attr('vspace');
810 align = iframe.attr('align');
811 bgcolor = iframe.attr('bgcolor');
813 tinymce.each(rootAttributes, function(name) {
814 img.attr(name, iframe.attr(name));
817 // Get all iframe attributes
818 for (name in iframe.attributes.map) {
819 if (!excludedAttrs[name] && !data.params[name])
820 data.params[name] = iframe.attributes.map[name];
825 if (data.params.movie) {
826 data.params.src = data.params.src || data.params.movie;
827 delete data.params.movie;
830 // Convert the URL to relative/absolute depending on configuration
832 data.params.src = urlConverter.call(urlConverterScope, data.params.src, 'src', 'object');
835 if (node.name === 'video')
836 type = lookup.video.name;
837 else if (node.name === 'audio')
838 type = lookup.audio.name;
842 type = (lookupAttribute(object, 'clsid') || lookupAttribute(object, 'classid') || lookupAttribute(object, 'type') || {}).name;
845 type = (lookupAttribute(embed, 'type') || lookupExtension(data.params.src) || {}).name;
847 // for embedded audio we preserve the original specified type
848 if (embed && type == 'EmbeddedAudio') {
849 data.params.type = embed.attr('type');
852 // Replace the video/object/embed element with a placeholder image containing the data
859 // Serialize the inner HTML of the object element
861 html = getInnerHTML(object.remove());
864 data.object_html = html;
867 // Serialize the inner HTML of the video element
869 html = getInnerHTML(video.remove());
872 data.video_html = html;
875 data.hspace = hspace;
876 data.vspace = vspace;
878 data.bgcolor = bgcolor;
880 // Set width/height of placeholder
883 'class' : 'mceItemMedia mceItem' + (type || 'Flash'),
885 width : width || (node.name == 'audio' ? "300" : "320"),
886 height : height || (node.name == 'audio' ? "32" : "240"),
891 "data-mce-json" : JSON.serialize(data, "'")
897 tinymce.PluginManager.add('media', tinymce.plugins.MediaPlugin);