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 && 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 && 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]
578
if ( typeItem.clsids )
579
object.attr('clsid', typeItem.clsids[0]);
580
object.attr('codebase', typeItem.codebase);
582
embed = new Node('embed', 1);
583
embed.shortEnded = true;
586
width: normalizeSize(node.attr('width')),
587
height: normalizeSize(node.attr('height')),
589
type: typeItem.mimes[0]
592
for (name in data.params)
593
embed.attr(name, data.params[name]);
595
tinymce.each(rootAttributes, function(name) {
596
if (data[name] && name != 'type')
597
embed.attr(name, data[name]);
600
object.append(embed);
604
if (data.object_html) {
605
value = new Node('#text', 3);
607
value.value = data.object_html;
608
object.append(value);
611
// Append object to video element if it exists
613
video.append(object);
618
if (data.video_html) {
619
value = new Node('#text', 3);
621
value.value = data.video_html;
628
if (data.video_html) {
629
value = new Node('#text', 3);
631
value.value = data.video_html;
636
var n = video || audio || object || embed;
644
* Converts a tinymce.html.Node video/object/embed to an img element.
646
* The video/object/embed will be converted into an image placeholder with a JSON data attribute like this:
647
* <img class="mceItemMedia mceItemFlash" width="100" height="100" data-mce-json="{..}" />
649
* The JSON structure will be like this:
650
* {'params':{'flashvars':'something','quality':'high','src':'someurl'}, 'video':{'sources':[{src: 'someurl', type: 'video/mp4'}]}}
652
objectToImg : function(node) {
653
var object, embed, video, iframe, img, name, id, width, height, style, i, html,
654
param, params, source, sources, data, type, lookup = this.lookup,
655
matches, attrs, urlConverter = this.editor.settings.url_converter,
656
urlConverterScope = this.editor.settings.url_converter_scope,
657
hspace, vspace, align, bgcolor;
659
function getInnerHTML(node) {
660
return new tinymce.html.Serializer({
666
function lookupAttribute(o, attr) {
667
return lookup[(o.attr(attr) || '').toLowerCase()];
670
function lookupExtension(src) {
671
var ext = src.replace(/^.*\.([^.]+)$/, '$1');
672
return lookup[ext.toLowerCase() || ''];
675
// If node isn't in document
679
// Handle media scripts
680
if (node.name === 'script') {
682
matches = scriptRegExp.exec(node.firstChild.value);
688
data = {video : {}, params : JSON.parse(matches[2])};
689
width = data.params.width;
690
height = data.params.height;
693
// Setup data objects
699
// Setup new image object
700
img = new Node('img', 1);
702
src : this.editor.theme.url + '/img/trans.gif'
707
if (name === 'video' || name == 'audio') {
709
object = node.getAll('object')[0];
710
embed = node.getAll('embed')[0];
711
width = video.attr('width');
712
height = video.attr('height');
713
id = video.attr('id');
714
data.video = {attrs : {}, sources : []};
716
// Get all video attributes
717
attrs = data.video.attrs;
718
for (name in video.attributes.map)
719
attrs[name] = video.attributes.map[name];
721
source = node.attr('src');
723
data.video.sources.push({src : urlConverter.call(urlConverterScope, source, 'src', node.name)});
726
sources = video.getAll("source");
727
for (i = 0; i < sources.length; i++) {
728
source = sources[i].remove();
730
data.video.sources.push({
731
src: urlConverter.call(urlConverterScope, source.attr('src'), 'src', 'source'),
732
type: source.attr('type'),
733
media: source.attr('media')
737
// Convert the poster URL
739
attrs.poster = urlConverter.call(urlConverterScope, attrs.poster, 'poster', node.name);
743
if (node.name === 'object') {
745
embed = node.getAll('embed')[0];
749
if (node.name === 'embed')
753
if (node.name === 'iframe') {
760
width = width || object.attr('width');
761
height = height || object.attr('height');
762
style = style || object.attr('style');
763
id = id || object.attr('id');
764
hspace = hspace || object.attr('hspace');
765
vspace = vspace || object.attr('vspace');
766
align = align || object.attr('align');
767
bgcolor = bgcolor || object.attr('bgcolor');
768
data.name = object.attr('name');
770
// Get all object params
771
params = object.getAll("param");
772
for (i = 0; i < params.length; i++) {
774
name = param.remove().attr('name');
776
if (!excludedAttrs[name])
777
data.params[name] = param.attr('value');
780
data.params.src = data.params.src || object.attr('data');
785
width = width || embed.attr('width');
786
height = height || embed.attr('height');
787
style = style || embed.attr('style');
788
id = id || embed.attr('id');
789
hspace = hspace || embed.attr('hspace');
790
vspace = vspace || embed.attr('vspace');
791
align = align || embed.attr('align');
792
bgcolor = bgcolor || embed.attr('bgcolor');
794
// Get all embed attributes
795
for (name in embed.attributes.map) {
796
if (!excludedAttrs[name] && !data.params[name])
797
data.params[name] = embed.attributes.map[name];
803
width = normalizeSize(iframe.attr('width'));
804
height = normalizeSize(iframe.attr('height'));
805
style = style || iframe.attr('style');
806
id = iframe.attr('id');
807
hspace = iframe.attr('hspace');
808
vspace = iframe.attr('vspace');
809
align = iframe.attr('align');
810
bgcolor = iframe.attr('bgcolor');
812
tinymce.each(rootAttributes, function(name) {
813
img.attr(name, iframe.attr(name));
816
// Get all iframe attributes
817
for (name in iframe.attributes.map) {
818
if (!excludedAttrs[name] && !data.params[name])
819
data.params[name] = iframe.attributes.map[name];
824
if (data.params.movie) {
825
data.params.src = data.params.src || data.params.movie;
826
delete data.params.movie;
829
// Convert the URL to relative/absolute depending on configuration
831
data.params.src = urlConverter.call(urlConverterScope, data.params.src, 'src', 'object');
834
if (node.name === 'video')
835
type = lookup.video.name;
836
else if (node.name === 'audio')
837
type = lookup.audio.name;
841
type = (lookupAttribute(object, 'clsid') || lookupAttribute(object, 'classid') || lookupAttribute(object, 'type') || {}).name;
844
type = (lookupAttribute(embed, 'type') || lookupExtension(data.params.src) || {}).name;
846
// for embedded audio we preserve the original specified type
847
if (embed && type == 'EmbeddedAudio') {
848
data.params.type = embed.attr('type');
851
// Replace the video/object/embed element with a placeholder image containing the data
858
// Serialize the inner HTML of the object element
860
html = getInnerHTML(object.remove());
863
data.object_html = html;
866
// Serialize the inner HTML of the video element
868
html = getInnerHTML(video.remove());
871
data.video_html = html;
874
data.hspace = hspace;
875
data.vspace = vspace;
877
data.bgcolor = bgcolor;
879
// Set width/height of placeholder
882
'class' : 'mceItemMedia mceItem' + (type || 'Flash'),
884
width : width || (node.name == 'audio' ? "300" : "320"),
885
height : height || (node.name == 'audio' ? "32" : "240"),
890
"data-mce-json" : JSON.serialize(data, "'")
896
tinymce.PluginManager.add('media', tinymce.plugins.MediaPlugin);