4 * Copyright, Moxiecode Systems AB
5 * Released under LGPL License.
7 * License: http://www.tinymce.com/license
8 * Contributing: http://www.tinymce.com/contributing
11 /*jshint maxlen:255 */
13 /*global tinymce:true */
15 tinymce.PluginManager.add('media', function(editor, url) {
17 {regex: /youtu\.be\/([\w\-.]+)/, type: 'iframe', w: 425, h: 350, url: '//www.youtube.com/embed/$1'},
18 {regex: /youtube\.com(.+)v=([^&]+)/, type: 'iframe', w: 425, h: 350, url: '//www.youtube.com/embed/$2'},
19 {regex: /vimeo\.com\/([0-9]+)/, type: 'iframe', w: 425, h: 350, url: '//player.vimeo.com/video/$1?title=0&byline=0&portrait=0&color=8dc7dc'},
20 {regex: /maps\.google\.([a-z]{2,3})\/maps\/(.+)msid=(.+)/, type: 'iframe', w: 425, h: 350, url: '//maps.google.com/maps/ms?msid=$2&output=embed"'}
23 function guessMime(url) {
24 if (url.indexOf('.mp3') != -1) {
28 if (url.indexOf('.wav') != -1) {
32 if (url.indexOf('.mp4') != -1) {
36 if (url.indexOf('.webm') != -1) {
40 if (url.indexOf('.ogg') != -1) {
44 if (url.indexOf('.swf') != -1) {
45 return 'application/x-shockwave-flash';
51 function getVideoScriptMatch(src) {
52 var prefixes = editor.settings.media_scripts;
55 for (var i = 0; i < prefixes.length; i++) {
56 if (src.indexOf(prefixes[i].filter) !== -1) {
63 function showDialog() {
64 var win, width, height, data;
65 var generalFormItems = [
66 {name: 'source1', type: 'filepicker', filetype: 'media', size: 40, autofocus: true, label: 'Source'}
69 function recalcSize(e) {
70 var widthCtrl, heightCtrl, newWidth, newHeight;
72 widthCtrl = win.find('#width')[0];
73 heightCtrl = win.find('#height')[0];
75 newWidth = widthCtrl.value();
76 newHeight = heightCtrl.value();
78 if (win.find('#constrain')[0].checked() && width && height && newWidth && newHeight) {
79 if (e.control == widthCtrl) {
80 newHeight = Math.round((newWidth / width) * newHeight);
81 heightCtrl.value(newHeight);
83 newWidth = Math.round((newHeight / height) * newWidth);
84 widthCtrl.value(newWidth);
92 if (editor.settings.media_alt_source !== false) {
93 generalFormItems.push({name: 'source2', type: 'filepicker', filetype: 'media', size: 40, label: 'Alternative source'});
96 if (editor.settings.media_poster !== false) {
97 generalFormItems.push({name: 'poster', type: 'filepicker', filetype: 'image', size: 40, label: 'Poster'});
100 if (editor.settings.media_dimensions !== false) {
101 generalFormItems.push({
108 {name: 'width', type: 'textbox', maxLength: 3, size: 3, onchange: recalcSize},
109 {type: 'label', text: 'x'},
110 {name: 'height', type: 'textbox', maxLength: 3, size: 3, onchange: recalcSize},
111 {name: 'constrain', type: 'checkbox', checked: true, text: 'Constrain proportions'}
116 data = getData(editor.selection.getNode());
118 height = data.height;
120 win = editor.windowManager.open({
121 title: 'Insert/edit video',
123 bodyType: 'tabpanel',
128 onShowTab: function() {
129 data = htmlToData(this.next().find('#embed').value());
132 items: generalFormItems
143 onShowTab: function() {
144 this.find('#embed').value(dataToHtml(this.parent().toJSON()));
149 text: 'Paste your embed code below:',
150 forId: 'mcemediasource'
153 id: 'mcemediasource',
164 onSubmit: function() {
165 editor.insertContent(dataToHtml(this.toJSON()));
170 function getSource() {
171 var elm = editor.selection.getNode();
173 if (elm.getAttribute('data-mce-object')) {
174 return editor.selection.getContent();
178 function dataToHtml(data) {
182 tinymce.extend(data, htmlToData(data.embed));
196 data.source1 = editor.convertURL(data.source1, "source");
197 data.source2 = editor.convertURL(data.source2, "source");
198 data.source1mime = guessMime(data.source1);
199 data.source2mime = guessMime(data.source2);
200 data.poster = editor.convertURL(data.poster, "poster");
201 data.flashPlayerUrl = editor.convertURL(url + '/moxieplayer.swf', "movie");
204 html = updateHtml(data.embed, data, true);
206 tinymce.each(urlPatterns, function(pattern) {
209 if ((match = pattern.regex.exec(data.source1))) {
212 for (i = 0; match[i]; i++) {
213 /*jshint loopfunc:true*/
214 /*eslint no-loop-func:0 */
215 url = url.replace('$' + i, function() {
221 data.type = pattern.type;
222 data.width = data.width || pattern.w;
223 data.height = data.height || pattern.h;
227 var videoScript = getVideoScriptMatch(data.source1);
229 data.type = 'script';
230 data.width = videoScript.width;
231 data.height = videoScript.height;
234 data.width = data.width || 300;
235 data.height = data.height || 150;
237 tinymce.each(data, function(value, key) {
238 data[key] = editor.dom.encode(value);
241 if (data.type == "iframe") {
242 html += '<iframe src="' + data.source1 + '" width="' + data.width + '" height="' + data.height + '"></iframe>';
243 } else if (data.source1mime == "application/x-shockwave-flash") {
244 html += '<object data="' + data.source1 + '" width="' + data.width + '" height="' + data.height + '" type="application/x-shockwave-flash">';
247 html += '<img src="' + data.poster + '" width="' + data.width + '" height="' + data.height + '" />';
251 } else if (data.source1mime.indexOf('audio') != -1) {
252 if (editor.settings.audio_template_callback) {
253 html = editor.settings.audio_template_callback(data);
256 '<audio controls="controls" src="' + data.source1 + '">' +
257 (data.source2 ? '\n<source src="' + data.source2 + '"' + (data.source2mime ? ' type="' + data.source2mime + '"' : '') + ' />\n' : '') +
261 } else if (data.type == "script") {
262 html += '<script src="' + data.source1 + '"></script>';
264 if (editor.settings.video_template_callback) {
265 html = editor.settings.video_template_callback(data);
268 '<video width="' + data.width + '" height="' + data.height + '"' + (data.poster ? ' poster="' + data.poster + '"' : '') + ' controls="controls">\n' +
269 '<source src="' + data.source1 + '"' + (data.source1mime ? ' type="' + data.source1mime + '"' : '') + ' />\n' +
270 (data.source2 ? '<source src="' + data.source2 + '"' + (data.source2mime ? ' type="' + data.source2mime + '"' : '') + ' />\n' : '') +
280 function htmlToData(html) {
283 new tinymce.html.SaxParser({
285 allow_conditional_comments: true,
286 special: 'script,noscript',
287 start: function(name, attrs) {
288 if (!data.source1 && name == "param") {
289 data.source1 = attrs.map.movie;
292 if (name == "iframe" || name == "object" || name == "embed" || name == "video" || name == "audio") {
297 data = tinymce.extend(attrs.map, data);
300 if (name == "script") {
301 var videoScript = getVideoScriptMatch(attrs.map.src);
308 source1: attrs.map.src,
309 width: videoScript.width,
310 height: videoScript.height
314 if (name == "source") {
316 data.source1 = attrs.map.src;
317 } else if (!data.source2) {
318 data.source2 = attrs.map.src;
322 if (name == "img" && !data.poster) {
323 data.poster = attrs.map.src;
328 data.source1 = data.source1 || data.src || data.data;
329 data.source2 = data.source2 || '';
330 data.poster = data.poster || '';
335 function getData(element) {
336 if (element.getAttribute('data-mce-object')) {
337 return htmlToData(editor.serializer.serialize(element, {selection: true}));
343 function updateHtml(html, data, updateAll) {
344 var writer = new tinymce.html.Writer();
345 var sourceCount = 0, hasImage;
347 function setAttributes(attrs, updatedAttrs) {
348 var name, i, value, attr;
350 for (name in updatedAttrs) {
351 value = "" + updatedAttrs[name];
353 if (attrs.map[name]) {
358 if (attr.name == name) {
360 attrs.map[name] = value;
363 delete attrs.map[name];
374 attrs.map[name] = value;
379 new tinymce.html.SaxParser({
381 allow_conditional_comments: true,
382 special: 'script,noscript',
384 comment: function(text) {
385 writer.comment(text);
388 cdata: function(text) {
392 text: function(text, raw) {
393 writer.text(text, raw);
396 start: function(name, attrs, empty) {
403 setAttributes(attrs, {
413 setAttributes(attrs, {
419 setAttributes(attrs, {
426 setAttributes(attrs, {
434 if (sourceCount <= 2) {
435 setAttributes(attrs, {
436 src: data["source" + sourceCount],
437 type: data["source" + sourceCount + "mime"]
440 if (!data["source" + sourceCount]) {
456 writer.start(name, attrs, empty);
459 end: function(name) {
460 if (name == "video" && updateAll) {
461 for (var index = 1; index <= 2; index++) {
462 if (data["source" + index]) {
466 if (sourceCount < index) {
467 setAttributes(attrs, {
468 src: data["source" + index],
469 type: data["source" + index + "mime"]
472 writer.start("source", attrs, true);
478 if (data.poster && name == "object" && updateAll && !hasImage) {
482 setAttributes(imgAttrs, {
488 writer.start("img", imgAttrs, true);
493 }, new tinymce.html.Schema({})).parse(html);
495 return writer.getContent();
498 editor.on('ResolveName', function(e) {
501 if (e.target.nodeType == 1 && (name = e.target.getAttribute("data-mce-object"))) {
506 editor.on('preInit', function() {
507 // Make sure that any messy HTML is retained inside these
508 var specialElements = editor.schema.getSpecialElements();
509 tinymce.each('video audio iframe object'.split(' '), function(name) {
510 specialElements[name] = new RegExp('<\/' + name + '[^>]*>','gi');
514 editor.schema.addValidElements('object[id|style|width|height|classid|codebase|*],embed[id|style|width|height|type|src|*],video[*],audio[*]');
516 // Set allowFullscreen attribs as boolean
517 var boolAttrs = editor.schema.getBoolAttrs();
518 tinymce.each('webkitallowfullscreen mozallowfullscreen allowfullscreen'.split(' '), function(name) {
519 boolAttrs[name] = {};
522 // Converts iframe, video etc into placeholder images
523 editor.parser.addNodeFilter('iframe,video,audio,object,embed,script', function(nodes, name) {
524 var i = nodes.length, ai, node, placeHolder, attrName, attrValue, attribs, innerHtml;
530 if (node.name == 'script') {
531 videoScript = getVideoScriptMatch(node.attr('src'));
537 placeHolder = new tinymce.html.Node('img', 1);
538 placeHolder.shortEnded = true;
541 if (videoScript.width) {
542 node.attr('width', videoScript.width.toString());
545 if (videoScript.height) {
546 node.attr('height', videoScript.height.toString());
550 // Prefix all attributes except width, height and style since we
551 // will add these to the placeholder
552 attribs = node.attributes;
555 attrName = attribs[ai].name;
556 attrValue = attribs[ai].value;
558 if (attrName !== "width" && attrName !== "height" && attrName !== "style") {
559 if (attrName == "data" || attrName == "src") {
560 attrValue = editor.convertURL(attrValue, attrName);
563 placeHolder.attr('data-mce-p-' + attrName, attrValue);
567 // Place the inner HTML contents inside an escaped attribute
568 // This enables us to copy/paste the fake object
569 innerHtml = node.firstChild && node.firstChild.value;
571 placeHolder.attr("data-mce-html", escape(innerHtml));
572 placeHolder.firstChild = null;
576 width: node.attr('width') || "300",
577 height: node.attr('height') || (name == "audio" ? "30" : "150"),
578 style: node.attr('style'),
579 src: tinymce.Env.transparentSrc,
580 "data-mce-object": name,
581 "class": "mce-object mce-object-" + name
584 node.replace(placeHolder);
588 // Replaces placeholder images with real elements for video, object, iframe etc
589 editor.serializer.addAttributeFilter('data-mce-object', function(nodes, name) {
590 var i = nodes.length, node, realElm, ai, attribs, innerHtml, innerNode, realElmName;
594 realElmName = node.attr(name);
595 realElm = new tinymce.html.Node(realElmName, 1);
597 // Add width/height to everything but audio
598 if (realElmName != "audio" && realElmName != "script") {
600 width: node.attr('width'),
601 height: node.attr('height')
606 style: node.attr('style')
609 // Unprefix all placeholder attributes
610 attribs = node.attributes;
613 var attrName = attribs[ai].name;
615 if (attrName.indexOf('data-mce-p-') === 0) {
616 realElm.attr(attrName.substr(11), attribs[ai].value);
620 if (realElmName == "script") {
621 realElm.attr('type', 'text/javascript');
625 innerHtml = node.attr('data-mce-html');
627 innerNode = new tinymce.html.Node('#text', 3);
628 innerNode.raw = true;
629 innerNode.value = unescape(innerHtml);
630 realElm.append(innerNode);
633 node.replace(realElm);
638 editor.on('ObjectSelected', function(e) {
639 var objectType = e.target.getAttribute('data-mce-object');
641 if (objectType == "audio" || objectType == "script") {
646 editor.on('objectResized', function(e) {
647 var target = e.target, html;
649 if (target.getAttribute('data-mce-object')) {
650 html = target.getAttribute('data-mce-html');
652 html = unescape(html);
653 target.setAttribute('data-mce-html', escape(
663 editor.addButton('media', {
664 tooltip: 'Insert/edit video',
666 stateSelector: ['img[data-mce-object=video]', 'img[data-mce-object=iframe]']
669 editor.addMenuItem('media', {
671 text: 'Insert video',
674 prependToContext: true