bgneal@312: /** bgneal@312: * editor_plugin_src.js bgneal@312: * bgneal@312: * Copyright 2009, Moxiecode Systems AB bgneal@312: * Released under LGPL License. bgneal@312: * bgneal@312: * License: http://tinymce.moxiecode.com/license bgneal@312: * Contributing: http://tinymce.moxiecode.com/contributing bgneal@312: */ bgneal@312: bgneal@312: (function() { bgneal@312: var each = tinymce.each; bgneal@312: bgneal@312: tinymce.create('tinymce.plugins.MediaPlugin', { bgneal@312: init : function(ed, url) { bgneal@312: var t = this; bgneal@312: bgneal@312: t.editor = ed; bgneal@312: t.url = url; bgneal@312: bgneal@312: function isMediaElm(n) { bgneal@312: return /^(mceItemFlash|mceItemShockWave|mceItemWindowsMedia|mceItemQuickTime|mceItemRealMedia)$/.test(n.className); bgneal@312: }; bgneal@312: bgneal@312: ed.onPreInit.add(function() { bgneal@312: // Force in _value parameter this extra parameter is required for older Opera versions bgneal@312: ed.serializer.addRules('param[name|value|_mce_value]'); bgneal@312: }); bgneal@312: bgneal@312: // Register commands bgneal@312: ed.addCommand('mceMedia', function() { bgneal@312: ed.windowManager.open({ bgneal@312: file : url + '/media.htm', bgneal@312: width : 430 + parseInt(ed.getLang('media.delta_width', 0)), bgneal@312: height : 470 + parseInt(ed.getLang('media.delta_height', 0)), bgneal@312: inline : 1 bgneal@312: }, { bgneal@312: plugin_url : url bgneal@312: }); bgneal@312: }); bgneal@312: bgneal@312: // Register buttons bgneal@312: ed.addButton('media', {title : 'media.desc', cmd : 'mceMedia'}); bgneal@312: bgneal@312: ed.onNodeChange.add(function(ed, cm, n) { bgneal@312: cm.setActive('media', n.nodeName == 'IMG' && isMediaElm(n)); bgneal@312: }); bgneal@312: bgneal@312: ed.onInit.add(function() { bgneal@312: var lo = { bgneal@312: mceItemFlash : 'flash', bgneal@312: mceItemShockWave : 'shockwave', bgneal@312: mceItemWindowsMedia : 'windowsmedia', bgneal@312: mceItemQuickTime : 'quicktime', bgneal@312: mceItemRealMedia : 'realmedia' bgneal@312: }; bgneal@312: bgneal@312: ed.selection.onSetContent.add(function() { bgneal@312: t._spansToImgs(ed.getBody()); bgneal@312: }); bgneal@312: bgneal@312: ed.selection.onBeforeSetContent.add(t._objectsToSpans, t); bgneal@312: bgneal@312: if (ed.settings.content_css !== false) bgneal@312: ed.dom.loadCSS(url + "/css/content.css"); bgneal@312: bgneal@312: if (ed.theme && ed.theme.onResolveName) { bgneal@312: ed.theme.onResolveName.add(function(th, o) { bgneal@312: if (o.name == 'img') { bgneal@312: each(lo, function(v, k) { bgneal@312: if (ed.dom.hasClass(o.node, k)) { bgneal@312: o.name = v; bgneal@312: o.title = ed.dom.getAttrib(o.node, 'title'); bgneal@312: return false; bgneal@312: } bgneal@312: }); bgneal@312: } bgneal@312: }); bgneal@312: } bgneal@312: bgneal@312: if (ed && ed.plugins.contextmenu) { bgneal@312: ed.plugins.contextmenu.onContextMenu.add(function(th, m, e) { bgneal@312: if (e.nodeName == 'IMG' && /mceItem(Flash|ShockWave|WindowsMedia|QuickTime|RealMedia)/.test(e.className)) { bgneal@312: m.add({title : 'media.edit', icon : 'media', cmd : 'mceMedia'}); bgneal@312: } bgneal@312: }); bgneal@312: } bgneal@312: }); bgneal@312: bgneal@312: ed.onBeforeSetContent.add(t._objectsToSpans, t); bgneal@312: bgneal@312: ed.onSetContent.add(function() { bgneal@312: t._spansToImgs(ed.getBody()); bgneal@312: }); bgneal@312: bgneal@312: ed.onPreProcess.add(function(ed, o) { bgneal@312: var dom = ed.dom; bgneal@312: bgneal@312: if (o.set) { bgneal@312: t._spansToImgs(o.node); bgneal@312: bgneal@312: each(dom.select('IMG', o.node), function(n) { bgneal@312: var p; bgneal@312: bgneal@312: if (isMediaElm(n)) { bgneal@312: p = t._parse(n.title); bgneal@312: dom.setAttrib(n, 'width', dom.getAttrib(n, 'width', p.width || 100)); bgneal@312: dom.setAttrib(n, 'height', dom.getAttrib(n, 'height', p.height || 100)); bgneal@312: } bgneal@312: }); bgneal@312: } bgneal@312: bgneal@312: if (o.get) { bgneal@312: each(dom.select('IMG', o.node), function(n) { bgneal@312: var ci, cb, mt; bgneal@312: bgneal@312: if (ed.getParam('media_use_script')) { bgneal@312: if (isMediaElm(n)) bgneal@312: n.className = n.className.replace(/mceItem/g, 'mceTemp'); bgneal@312: bgneal@312: return; bgneal@312: } bgneal@312: bgneal@312: switch (n.className) { bgneal@312: case 'mceItemFlash': bgneal@312: ci = 'd27cdb6e-ae6d-11cf-96b8-444553540000'; bgneal@312: cb = 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0'; bgneal@312: mt = 'application/x-shockwave-flash'; bgneal@312: break; bgneal@312: bgneal@312: case 'mceItemShockWave': bgneal@312: ci = '166b1bca-3f9c-11cf-8075-444553540000'; bgneal@312: cb = 'http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=8,5,1,0'; bgneal@312: mt = 'application/x-director'; bgneal@312: break; bgneal@312: bgneal@312: case 'mceItemWindowsMedia': bgneal@312: ci = ed.getParam('media_wmp6_compatible') ? '05589fa1-c356-11ce-bf01-00aa0055595a' : '6bf52a52-394a-11d3-b153-00c04f79faa6'; bgneal@312: cb = 'http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701'; bgneal@312: mt = 'application/x-mplayer2'; bgneal@312: break; bgneal@312: bgneal@312: case 'mceItemQuickTime': bgneal@312: ci = '02bf25d5-8c17-4b23-bc80-d3488abddc6b'; bgneal@312: cb = 'http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0'; bgneal@312: mt = 'video/quicktime'; bgneal@312: break; bgneal@312: bgneal@312: case 'mceItemRealMedia': bgneal@312: ci = 'cfcdaa03-8be4-11cf-b84b-0020afbbccfa'; bgneal@312: cb = 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0'; bgneal@312: mt = 'audio/x-pn-realaudio-plugin'; bgneal@312: break; bgneal@312: } bgneal@312: bgneal@312: if (ci) { bgneal@312: dom.replace(t._buildObj({ bgneal@312: classid : ci, bgneal@312: codebase : cb, bgneal@312: type : mt bgneal@312: }, n), n); bgneal@312: } bgneal@312: }); bgneal@312: } bgneal@312: }); bgneal@312: bgneal@312: ed.onPostProcess.add(function(ed, o) { bgneal@312: o.content = o.content.replace(/_mce_value=/g, 'value='); bgneal@312: }); bgneal@312: bgneal@312: function getAttr(s, n) { bgneal@312: n = new RegExp(n + '=\"([^\"]+)\"', 'g').exec(s); bgneal@312: bgneal@312: return n ? ed.dom.decode(n[1]) : ''; bgneal@312: }; bgneal@312: bgneal@312: ed.onPostProcess.add(function(ed, o) { bgneal@312: if (ed.getParam('media_use_script')) { bgneal@312: o.content = o.content.replace(/]+>/g, function(im) { bgneal@312: var cl = getAttr(im, 'class'); bgneal@312: bgneal@312: if (/^(mceTempFlash|mceTempShockWave|mceTempWindowsMedia|mceTempQuickTime|mceTempRealMedia)$/.test(cl)) { bgneal@312: at = t._parse(getAttr(im, 'title')); bgneal@312: at.width = getAttr(im, 'width'); bgneal@312: at.height = getAttr(im, 'height'); bgneal@312: im = ''; bgneal@312: } bgneal@312: bgneal@312: return im; bgneal@312: }); bgneal@312: } bgneal@312: }); bgneal@312: }, bgneal@312: bgneal@312: getInfo : function() { bgneal@312: return { bgneal@312: longname : 'Media', bgneal@312: author : 'Moxiecode Systems AB', bgneal@312: authorurl : 'http://tinymce.moxiecode.com', bgneal@312: infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/media', bgneal@312: version : tinymce.majorVersion + "." + tinymce.minorVersion bgneal@312: }; bgneal@312: }, bgneal@312: bgneal@312: // Private methods bgneal@312: _objectsToSpans : function(ed, o) { bgneal@312: var t = this, h = o.content; bgneal@312: bgneal@312: h = h.replace(/]*>\s*write(Flash|ShockWave|WindowsMedia|QuickTime|RealMedia)\(\{([^\)]*)\}\);\s*<\/script>/gi, function(a, b, c) { bgneal@312: var o = t._parse(c); bgneal@312: bgneal@312: return '' bgneal@312: }); bgneal@312: bgneal@312: h = h.replace(/]*)>/gi, ''); bgneal@312: h = h.replace(/]*)\/?>/gi, ''); bgneal@312: h = h.replace(/]*)>/gi, ''); bgneal@312: h = h.replace(/<\/(object)([^>]*)>/gi, ''); bgneal@312: h = h.replace(/<\/embed>/gi, ''); bgneal@312: h = h.replace(/]*)>/gi, function(a, b) {return ''}); bgneal@312: h = h.replace(/\/ class=\"mceItemParam\"><\/span>/gi, 'class="mceItemParam">'); bgneal@312: bgneal@312: o.content = h; bgneal@312: }, bgneal@312: bgneal@312: _buildObj : function(o, n) { bgneal@312: var ob, ed = this.editor, dom = ed.dom, p = this._parse(n.title), stc; bgneal@312: bgneal@312: stc = ed.getParam('media_strict', true) && o.type == 'application/x-shockwave-flash'; bgneal@312: bgneal@312: p.width = o.width = dom.getAttrib(n, 'width') || 100; bgneal@312: p.height = o.height = dom.getAttrib(n, 'height') || 100; bgneal@312: bgneal@312: if (p.src) bgneal@312: p.src = ed.convertURL(p.src, 'src', n); bgneal@312: bgneal@312: if (stc) { bgneal@312: ob = dom.create('span', { bgneal@312: id : p.id, bgneal@312: _mce_name : 'object', bgneal@312: type : 'application/x-shockwave-flash', bgneal@312: data : p.src, bgneal@312: style : dom.getAttrib(n, 'style'), bgneal@312: width : o.width, bgneal@312: height : o.height bgneal@312: }); bgneal@312: } else { bgneal@312: ob = dom.create('span', { bgneal@312: id : p.id, bgneal@312: _mce_name : 'object', bgneal@312: classid : "clsid:" + o.classid, bgneal@312: style : dom.getAttrib(n, 'style'), bgneal@312: codebase : o.codebase, bgneal@312: width : o.width, bgneal@312: height : o.height bgneal@312: }); bgneal@312: } bgneal@312: bgneal@312: each (p, function(v, k) { bgneal@312: if (!/^(width|height|codebase|classid|id|_cx|_cy)$/.test(k)) { bgneal@312: // Use url instead of src in IE for Windows media bgneal@312: if (o.type == 'application/x-mplayer2' && k == 'src' && !p.url) bgneal@312: k = 'url'; bgneal@312: bgneal@312: if (v) bgneal@312: dom.add(ob, 'span', {_mce_name : 'param', name : k, '_mce_value' : v}); bgneal@312: } bgneal@312: }); bgneal@312: bgneal@312: if (!stc) bgneal@312: dom.add(ob, 'span', tinymce.extend({_mce_name : 'embed', type : o.type, style : dom.getAttrib(n, 'style')}, p)); bgneal@312: bgneal@312: return ob; bgneal@312: }, bgneal@312: bgneal@312: _spansToImgs : function(p) { bgneal@312: var t = this, dom = t.editor.dom, im, ci; bgneal@312: bgneal@312: each(dom.select('span', p), function(n) { bgneal@312: // Convert object into image bgneal@312: if (dom.getAttrib(n, 'class') == 'mceItemObject') { bgneal@312: ci = dom.getAttrib(n, "classid").toLowerCase().replace(/\s+/g, ''); bgneal@312: bgneal@312: switch (ci) { bgneal@312: case 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000': bgneal@312: dom.replace(t._createImg('mceItemFlash', n), n); bgneal@312: break; bgneal@312: bgneal@312: case 'clsid:166b1bca-3f9c-11cf-8075-444553540000': bgneal@312: dom.replace(t._createImg('mceItemShockWave', n), n); bgneal@312: break; bgneal@312: bgneal@312: case 'clsid:6bf52a52-394a-11d3-b153-00c04f79faa6': bgneal@312: case 'clsid:22d6f312-b0f6-11d0-94ab-0080c74c7e95': bgneal@312: case 'clsid:05589fa1-c356-11ce-bf01-00aa0055595a': bgneal@312: dom.replace(t._createImg('mceItemWindowsMedia', n), n); bgneal@312: break; bgneal@312: bgneal@312: case 'clsid:02bf25d5-8c17-4b23-bc80-d3488abddc6b': bgneal@312: dom.replace(t._createImg('mceItemQuickTime', n), n); bgneal@312: break; bgneal@312: bgneal@312: case 'clsid:cfcdaa03-8be4-11cf-b84b-0020afbbccfa': bgneal@312: dom.replace(t._createImg('mceItemRealMedia', n), n); bgneal@312: break; bgneal@312: bgneal@312: default: bgneal@312: dom.replace(t._createImg('mceItemFlash', n), n); bgneal@312: } bgneal@312: bgneal@312: return; bgneal@312: } bgneal@312: bgneal@312: // Convert embed into image bgneal@312: if (dom.getAttrib(n, 'class') == 'mceItemEmbed') { bgneal@312: switch (dom.getAttrib(n, 'type')) { bgneal@312: case 'application/x-shockwave-flash': bgneal@312: dom.replace(t._createImg('mceItemFlash', n), n); bgneal@312: break; bgneal@312: bgneal@312: case 'application/x-director': bgneal@312: dom.replace(t._createImg('mceItemShockWave', n), n); bgneal@312: break; bgneal@312: bgneal@312: case 'application/x-mplayer2': bgneal@312: dom.replace(t._createImg('mceItemWindowsMedia', n), n); bgneal@312: break; bgneal@312: bgneal@312: case 'video/quicktime': bgneal@312: dom.replace(t._createImg('mceItemQuickTime', n), n); bgneal@312: break; bgneal@312: bgneal@312: case 'audio/x-pn-realaudio-plugin': bgneal@312: dom.replace(t._createImg('mceItemRealMedia', n), n); bgneal@312: break; bgneal@312: bgneal@312: default: bgneal@312: dom.replace(t._createImg('mceItemFlash', n), n); bgneal@312: } bgneal@312: } bgneal@312: }); bgneal@312: }, bgneal@312: bgneal@312: _createImg : function(cl, n) { bgneal@312: var im, dom = this.editor.dom, pa = {}, ti = '', args; bgneal@312: bgneal@312: args = ['id', 'name', 'width', 'height', 'bgcolor', 'align', 'flashvars', 'src', 'wmode', 'allowfullscreen', 'quality', 'data']; bgneal@312: bgneal@312: // Create image bgneal@312: im = dom.create('img', { bgneal@312: src : this.url + '/img/trans.gif', bgneal@312: width : dom.getAttrib(n, 'width') || 100, bgneal@312: height : dom.getAttrib(n, 'height') || 100, bgneal@312: style : dom.getAttrib(n, 'style'), bgneal@312: 'class' : cl bgneal@312: }); bgneal@312: bgneal@312: // Setup base parameters bgneal@312: each(args, function(na) { bgneal@312: var v = dom.getAttrib(n, na); bgneal@312: bgneal@312: if (v) bgneal@312: pa[na] = v; bgneal@312: }); bgneal@312: bgneal@312: // Add optional parameters bgneal@312: each(dom.select('span', n), function(n) { bgneal@312: if (dom.hasClass(n, 'mceItemParam')) bgneal@312: pa[dom.getAttrib(n, 'name')] = dom.getAttrib(n, '_mce_value'); bgneal@312: }); bgneal@312: bgneal@312: // Use src not movie bgneal@312: if (pa.movie) { bgneal@312: pa.src = pa.movie; bgneal@312: delete pa.movie; bgneal@312: } bgneal@312: bgneal@312: // No src try data bgneal@312: if (!pa.src) { bgneal@312: pa.src = pa.data; bgneal@312: delete pa.data; bgneal@312: } bgneal@312: bgneal@312: // Merge with embed args bgneal@312: n = dom.select('.mceItemEmbed', n)[0]; bgneal@312: if (n) { bgneal@312: each(args, function(na) { bgneal@312: var v = dom.getAttrib(n, na); bgneal@312: bgneal@312: if (v && !pa[na]) bgneal@312: pa[na] = v; bgneal@312: }); bgneal@312: } bgneal@312: bgneal@312: delete pa.width; bgneal@312: delete pa.height; bgneal@312: bgneal@312: im.title = this._serialize(pa); bgneal@312: bgneal@312: return im; bgneal@312: }, bgneal@312: bgneal@312: _parse : function(s) { bgneal@312: return tinymce.util.JSON.parse('{' + s + '}'); bgneal@312: }, bgneal@312: bgneal@312: _serialize : function(o) { bgneal@312: return tinymce.util.JSON.serialize(o).replace(/[{}]/g, ''); bgneal@312: } bgneal@312: }); bgneal@312: bgneal@312: // Register plugin bgneal@312: tinymce.PluginManager.add('media', tinymce.plugins.MediaPlugin); bgneal@312: })();