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"],
29 function toArray(obj) {
32 if (obj && !obj.splice) {
35 for (i = 0; true; i++) {
48 tinymce.create('tinymce.plugins.MediaPlugin', {
49 init : function(ed, url) {
50 var self = this, lookup = {}, i, y, item, name;
52 function isMediaImg(node) {
53 return node && node.nodeName === 'IMG' && ed.dom.hasClass(node, 'mceItemMedia');
59 // Parse media types into a lookup table
61 for (i = 0; i < mediaTypes.length; i++) {
62 name = mediaTypes[i][0];
66 clsids : tinymce.explode(mediaTypes[i][1] || ''),
67 mimes : tinymce.explode(mediaTypes[i][2] || ''),
68 codebase : mediaTypes[i][3]
71 for (y = 0; y < item.clsids.length; y++)
72 lookup['clsid:' + item.clsids[y]] = item;
74 for (y = 0; y < item.mimes.length; y++)
75 lookup[item.mimes[y]] = item;
77 lookup['mceItem' + name] = item;
78 lookup[name.toLowerCase()] = item;
80 scriptRegExp += (scriptRegExp ? '|' : '') + name;
83 // Handle the media_types setting
84 tinymce.each(ed.getParam("media_types",
85 "video=mp4,m4v,ogv,webm;" +
89 "quicktime=mov,qt,mpg,mp3,mpeg;" +
91 "windowsmedia=avi,wmv,wm,asf,asx,wmx,wvx;" +
92 "realmedia=rm,ra,ram;" +
94 ).split(';'), function(item) {
95 var i, extensions, type;
97 item = item.split(/=/);
98 extensions = tinymce.explode(item[1].toLowerCase());
99 for (i = 0; i < extensions.length; i++) {
100 type = lookup[item[0].toLowerCase()];
103 lookup[extensions[i]] = type;
107 scriptRegExp = new RegExp('write(' + scriptRegExp + ')\\(([^)]+)\\)');
108 self.lookup = lookup;
110 ed.onPreInit.add(function() {
111 // Allow video elements
112 ed.schema.addValidElements('object[id|style|width|height|classid|codebase|*],param[name|value],embed[id|style|width|height|type|src|*],video[*],audio[*],source[*]');
114 // Convert video elements to image placeholder
115 ed.parser.addNodeFilter('object,embed,video,audio,script,iframe', function(nodes) {
116 var i = nodes.length;
119 self.objectToImg(nodes[i]);
122 // Convert image placeholders to video elements
123 ed.serializer.addNodeFilter('img', function(nodes, name, args) {
124 var i = nodes.length, node;
128 if ((node.attr('class') || '').indexOf('mceItemMedia') !== -1)
129 self.imgToObject(node, args);
134 ed.onInit.add(function() {
135 // Display "media" instead of "img" in element path
136 if (ed.theme && ed.theme.onResolveName) {
137 ed.theme.onResolveName.add(function(theme, path_object) {
138 if (path_object.name === 'img' && ed.dom.hasClass(path_object.node, 'mceItemMedia'))
139 path_object.name = 'media';
143 // Add contect menu if it's loaded
144 if (ed && ed.plugins.contextmenu) {
145 ed.plugins.contextmenu.onContextMenu.add(function(plugin, menu, element) {
146 if (element.nodeName === 'IMG' && element.className.indexOf('mceItemMedia') !== -1)
147 menu.add({title : 'media.edit', icon : 'media', cmd : 'mceMedia'});
153 ed.addCommand('mceMedia', function() {
156 img = ed.selection.getNode();
157 if (isMediaImg(img)) {
158 data = JSON.parse(ed.dom.getAttrib(img, 'data-mce-json'));
160 // Add some extra properties to the data object
161 tinymce.each(rootAttributes, function(name) {
162 var value = ed.dom.getAttrib(img, name);
168 data.type = self.getType(img.className).name.toLowerCase();
179 ed.windowManager.open({
180 file : url + '/media.htm',
181 width : 430 + parseInt(ed.getLang('media.delta_width', 0)),
182 height : 500 + parseInt(ed.getLang('media.delta_height', 0)),
191 ed.addButton('media', {title : 'media.desc', cmd : 'mceMedia'});
193 // Update media selection status
194 ed.onNodeChange.add(function(ed, cm, node) {
195 cm.setActive('media', isMediaImg(node));
199 convertUrl : function(url, force_absolute) {
200 var self = this, editor = self.editor, settings = editor.settings,
201 urlConverter = settings.url_converter,
202 urlConverterScope = settings.url_converter_scope || self;
208 return editor.documentBaseURI.toAbsolute(url);
210 return urlConverter.call(urlConverterScope, url, 'src', 'object');
213 getInfo : function() {
216 author : 'Moxiecode Systems AB',
217 authorurl : 'http://tinymce.moxiecode.com',
218 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/media',
219 version : tinymce.majorVersion + "." + tinymce.minorVersion
224 * Converts the JSON data object to an img node.
226 dataToImg : function(data, force_absolute) {
227 var self = this, editor = self.editor, baseUri = editor.documentBaseURI, sources, attrs, img, i;
229 data.params.src = self.convertUrl(data.params.src, force_absolute);
231 attrs = data.video.attrs;
233 attrs.src = self.convertUrl(attrs.src, force_absolute);
236 attrs.poster = self.convertUrl(attrs.poster, force_absolute);
238 sources = toArray(data.video.sources);
240 for (i = 0; i < sources.length; i++)
241 sources[i].src = self.convertUrl(sources[i].src, force_absolute);
244 img = self.editor.dom.create('img', {
248 src : self.editor.theme.url + '/img/trans.gif',
249 'class' : 'mceItemMedia mceItem' + self.getType(data.type).name,
250 'data-mce-json' : JSON.serialize(data, "'")
253 img.width = data.width || "320";
254 img.height = data.height || "240";
260 * Converts the JSON data object to a HTML string.
262 dataToHtml : function(data, force_absolute) {
263 return this.editor.serializer.serialize(this.dataToImg(data, force_absolute), {force_absolute : force_absolute});
267 * Converts the JSON data object to a HTML string.
269 htmlToData : function(html) {
270 var fragment, img, data;
278 fragment = this.editor.parser.parse(html);
279 img = fragment.getAll('img')[0];
282 data = JSON.parse(img.attr('data-mce-json'));
283 data.type = this.getType(img.attr('class')).name.toLowerCase();
285 // Add some extra properties to the data object
286 tinymce.each(rootAttributes, function(name) {
287 var value = img.attr(name);
298 * Get type item by extension, class, clsid or mime type.
301 * @param {String} value Value to get type item by.
302 * @return {Object} Type item object or undefined.
304 getType : function(value) {
305 var i, values, typeItem;
307 // Find type by checking the classes
308 values = tinymce.explode(value, ' ');
309 for (i = 0; i < values.length; i++) {
310 typeItem = this.lookup[values[i]];
318 * Converts a tinymce.html.Node image element to video/object/embed.
320 imgToObject : function(node, args) {
321 var self = this, editor = self.editor, video, object, embed, iframe, name, value, data,
322 source, sources, params, param, typeItem, i, item, mp4Source, replacement,
325 // Adds the flash player
326 function addPlayer(video_src, poster_src) {
327 var baseUri, flashVars, flashVarsOutput, params, flashPlayer;
329 flashPlayer = editor.getParam('flash_video_player_url', self.convertUrl(self.url + '/moxieplayer.swf'));
331 baseUri = editor.documentBaseURI;
332 data.params.src = flashPlayer;
334 // Convert the movie url to absolute urls
335 if (editor.getParam('flash_video_player_absvideourl', true)) {
336 video_src = baseUri.toAbsolute(video_src || '', true);
337 poster_src = baseUri.toAbsolute(poster_src || '', true);
340 // Generate flash vars
341 flashVarsOutput = '';
342 flashVars = editor.getParam('flash_video_player_flashvars', {url : '$url', poster : '$poster'});
343 tinymce.each(flashVars, function(value, name) {
344 // Replace $url and $poster variables in flashvars value
345 value = value.replace(/\$url/, video_src || '');
346 value = value.replace(/\$poster/, poster_src || '');
348 if (value.length > 0)
349 flashVarsOutput += (flashVarsOutput ? '&' : '') + name + '=' + escape(value);
352 if (flashVarsOutput.length)
353 data.params.flashvars = flashVarsOutput;
355 params = editor.getParam('flash_video_player_params', {
356 allowfullscreen: true,
357 allowscriptaccess: true
360 tinymce.each(params, function(value, name) {
361 data.params[name] = "" + value;
366 data = JSON.parse(node.attr('data-mce-json'));
367 typeItem = this.getType(node.attr('class'));
369 style = node.attr('data-mce-style')
371 style = node.attr('style');
374 style = editor.dom.serializeStyle(editor.dom.parseStyle(style, 'img'));
378 if (typeItem.name === 'Iframe') {
379 replacement = new Node('iframe', 1);
381 tinymce.each(rootAttributes, function(name) {
382 var value = node.attr(name);
384 if (name == 'class' && value)
385 value = value.replace(/mceItem.+ ?/g, '');
387 if (value && value.length > 0)
388 replacement.attr(name, value);
391 for (name in data.params)
392 replacement.attr(name, data.params[name]);
399 node.replace(replacement);
405 if (this.editor.settings.media_use_script) {
406 replacement = new Node('script', 1).attr('type', 'text/javascript');
408 value = new Node('#text', 3);
409 value.value = 'write' + typeItem.name + '(' + JSON.serialize(tinymce.extend(data.params, {
410 width: node.attr('width'),
411 height: node.attr('height')
414 replacement.append(value);
415 node.replace(replacement);
420 // Add HTML5 video element
421 if (typeItem.name === 'Video' && data.video.sources[0]) {
422 // Create new object element
423 video = new Node('video', 1).attr(tinymce.extend({
424 id : node.attr('id'),
425 width: node.attr('width'),
426 height: node.attr('height'),
428 }, data.video.attrs));
430 // Get poster source and use that for flash fallback
431 if (data.video.attrs)
432 posterSrc = data.video.attrs.poster;
434 sources = data.video.sources = toArray(data.video.sources);
435 for (i = 0; i < sources.length; i++) {
436 if (/\.mp4$/.test(sources[i].src))
437 mp4Source = sources[i].src;
440 if (!sources[0].type) {
441 video.attr('src', sources[0].src);
442 sources.splice(0, 1);
445 for (i = 0; i < sources.length; i++) {
446 source = new Node('source', 1).attr(sources[i]);
447 source.shortEnded = true;
448 video.append(source);
451 // Create flash fallback for video if we have a mp4 source
453 addPlayer(mp4Source, posterSrc);
454 typeItem = self.getType('flash');
456 data.params.src = '';
459 // Do we have a params src then we can generate object
460 if (data.params.src) {
461 // Is flv movie add player for it
462 if (/\.flv$/i.test(data.params.src))
463 addPlayer(data.params.src, '');
465 if (args && args.force_absolute)
466 data.params.src = editor.documentBaseURI.toAbsolute(data.params.src);
468 // Create new object element
469 object = new Node('object', 1).attr({
470 id : node.attr('id'),
471 width: node.attr('width'),
472 height: node.attr('height'),
476 tinymce.each(rootAttributes, function(name) {
477 if (data[name] && name != 'type')
478 object.attr(name, data[name]);
482 for (name in data.params) {
483 param = new Node('param', 1);
484 param.shortEnded = true;
485 value = data.params[name];
487 // Windows media needs to use url instead of src for the media URL
488 if (name === 'src' && typeItem.name === 'WindowsMedia')
491 param.attr({name: name, value: value});
492 object.append(param);
495 // Setup add type and classid if strict is disabled
496 if (this.editor.getParam('media_strict', true)) {
498 data: data.params.src,
499 type: typeItem.mimes[0]
503 classid: "clsid:" + typeItem.clsids[0],
504 codebase: typeItem.codebase
507 embed = new Node('embed', 1);
508 embed.shortEnded = true;
511 width: node.attr('width'),
512 height: node.attr('height'),
514 type: typeItem.mimes[0]
517 for (name in data.params)
518 embed.attr(name, data.params[name]);
520 tinymce.each(rootAttributes, function(name) {
521 if (data[name] && name != 'type')
522 embed.attr(name, data[name]);
525 object.append(embed);
529 if (data.object_html) {
530 value = new Node('#text', 3);
532 value.value = data.object_html;
533 object.append(value);
536 // Append object to video element if it exists
538 video.append(object);
543 if (data.video_html) {
544 value = new Node('#text', 3);
546 value.value = data.video_html;
552 node.replace(video || object);
558 * Converts a tinymce.html.Node video/object/embed to an img element.
560 * The video/object/embed will be converted into an image placeholder with a JSON data attribute like this:
561 * <img class="mceItemMedia mceItemFlash" width="100" height="100" data-mce-json="{..}" />
563 * The JSON structure will be like this:
564 * {'params':{'flashvars':'something','quality':'high','src':'someurl'}, 'video':{'sources':[{src: 'someurl', type: 'video/mp4'}]}}
566 objectToImg : function(node) {
567 var object, embed, video, iframe, img, name, id, width, height, style, i, html,
568 param, params, source, sources, data, type, lookup = this.lookup,
569 matches, attrs, urlConverter = this.editor.settings.url_converter,
570 urlConverterScope = this.editor.settings.url_converter_scope;
572 function getInnerHTML(node) {
573 return new tinymce.html.Serializer({
579 // If node isn't in document
583 // Handle media scripts
584 if (node.name === 'script') {
586 matches = scriptRegExp.exec(node.firstChild.value);
592 data = {video : {}, params : JSON.parse(matches[2])};
593 width = data.params.width;
594 height = data.params.height;
597 // Setup data objects
603 // Setup new image object
604 img = new Node('img', 1);
606 src : this.editor.theme.url + '/img/trans.gif'
611 if (name === 'video') {
613 object = node.getAll('object')[0];
614 embed = node.getAll('embed')[0];
615 width = video.attr('width');
616 height = video.attr('height');
617 id = video.attr('id');
618 data.video = {attrs : {}, sources : []};
620 // Get all video attributes
621 attrs = data.video.attrs;
622 for (name in video.attributes.map)
623 attrs[name] = video.attributes.map[name];
625 source = node.attr('src');
627 data.video.sources.push({src : urlConverter.call(urlConverterScope, source, 'src', 'video')});
630 sources = video.getAll("source");
631 for (i = 0; i < sources.length; i++) {
632 source = sources[i].remove();
634 data.video.sources.push({
635 src: urlConverter.call(urlConverterScope, source.attr('src'), 'src', 'source'),
636 type: source.attr('type'),
637 media: source.attr('media')
641 // Convert the poster URL
643 attrs.poster = urlConverter.call(urlConverterScope, attrs.poster, 'poster', 'video');
647 if (node.name === 'object') {
649 embed = node.getAll('embed')[0];
653 if (node.name === 'embed')
657 if (node.name === 'iframe') {
664 width = width || object.attr('width');
665 height = height || object.attr('height');
666 style = style || object.attr('style');
667 id = id || object.attr('id');
669 // Get all object params
670 params = object.getAll("param");
671 for (i = 0; i < params.length; i++) {
673 name = param.remove().attr('name');
675 if (!excludedAttrs[name])
676 data.params[name] = param.attr('value');
679 data.params.src = data.params.src || object.attr('data');
684 width = width || embed.attr('width');
685 height = height || embed.attr('height');
686 style = style || embed.attr('style');
687 id = id || embed.attr('id');
689 // Get all embed attributes
690 for (name in embed.attributes.map) {
691 if (!excludedAttrs[name] && !data.params[name])
692 data.params[name] = embed.attributes.map[name];
698 width = iframe.attr('width');
699 height = iframe.attr('height');
700 style = style || iframe.attr('style');
701 id = iframe.attr('id');
703 tinymce.each(rootAttributes, function(name) {
704 img.attr(name, iframe.attr(name));
707 // Get all iframe attributes
708 for (name in iframe.attributes.map) {
709 if (!excludedAttrs[name] && !data.params[name])
710 data.params[name] = iframe.attributes.map[name];
715 if (data.params.movie) {
716 data.params.src = data.params.src || data.params.movie;
717 delete data.params.movie;
720 // Convert the URL to relative/absolute depending on configuration
722 data.params.src = urlConverter.call(urlConverterScope, data.params.src, 'src', 'object');
725 type = lookup.video.name;
728 type = (lookup[(object.attr('clsid') || '').toLowerCase()] || lookup[(object.attr('type') || '').toLowerCase()] || {}).name;
731 type = (lookup[(embed.attr('type') || '').toLowerCase()] || {}).name;
733 // Replace the video/object/embed element with a placeholder image containing the data
740 // Serialize the inner HTML of the object element
742 html = getInnerHTML(object.remove());
745 data.object_html = html;
748 // Serialize the inner HTML of the video element
750 html = getInnerHTML(video.remove());
753 data.video_html = html;
756 // Set width/height of placeholder
759 'class' : 'mceItemMedia mceItem' + (type || 'Flash'),
761 width : width || "320",
762 height : height || "240",
763 "data-mce-json" : JSON.serialize(data, "'")
769 tinymce.PluginManager.add('media', tinymce.plugins.MediaPlugin);