2 * editor_plugin_src.js
\r
4 * Copyright 2009, Moxiecode Systems AB
\r
5 * Released under LGPL License.
\r
7 * License: http://tinymce.moxiecode.com/license
\r
8 * Contributing: http://tinymce.moxiecode.com/contributing
\r
12 var each = tinymce.each;
\r
14 tinymce.create('tinymce.plugins.MediaPlugin', {
\r
15 init : function(ed, url) {
\r
21 function isMediaElm(n) {
\r
22 return /^(mceItemFlash|mceItemShockWave|mceItemWindowsMedia|mceItemQuickTime|mceItemRealMedia)$/.test(n.className);
\r
25 ed.onPreInit.add(function() {
\r
26 // Force in _value parameter this extra parameter is required for older Opera versions
\r
27 ed.serializer.addRules('param[name|value|_mce_value]');
\r
30 // Register commands
\r
31 ed.addCommand('mceMedia', function() {
\r
32 ed.windowManager.open({
\r
33 file : url + '/media.htm',
\r
34 width : 430 + parseInt(ed.getLang('media.delta_width', 0)),
\r
35 height : 470 + parseInt(ed.getLang('media.delta_height', 0)),
\r
43 ed.addButton('media', {title : 'media.desc', cmd : 'mceMedia'});
\r
45 ed.onNodeChange.add(function(ed, cm, n) {
\r
46 cm.setActive('media', n.nodeName == 'IMG' && isMediaElm(n));
\r
49 ed.onInit.add(function() {
\r
51 mceItemFlash : 'flash',
\r
52 mceItemShockWave : 'shockwave',
\r
53 mceItemWindowsMedia : 'windowsmedia',
\r
54 mceItemQuickTime : 'quicktime',
\r
55 mceItemRealMedia : 'realmedia'
\r
58 ed.selection.onSetContent.add(function() {
\r
59 t._spansToImgs(ed.getBody());
\r
62 ed.selection.onBeforeSetContent.add(t._objectsToSpans, t);
\r
64 if (ed.settings.content_css !== false)
\r
65 ed.dom.loadCSS(url + "/css/content.css");
\r
67 if (ed.theme && ed.theme.onResolveName) {
\r
68 ed.theme.onResolveName.add(function(th, o) {
\r
69 if (o.name == 'img') {
\r
70 each(lo, function(v, k) {
\r
71 if (ed.dom.hasClass(o.node, k)) {
\r
73 o.title = ed.dom.getAttrib(o.node, 'title');
\r
81 if (ed && ed.plugins.contextmenu) {
\r
82 ed.plugins.contextmenu.onContextMenu.add(function(th, m, e) {
\r
83 if (e.nodeName == 'IMG' && /mceItem(Flash|ShockWave|WindowsMedia|QuickTime|RealMedia)/.test(e.className)) {
\r
84 m.add({title : 'media.edit', icon : 'media', cmd : 'mceMedia'});
\r
90 ed.onBeforeSetContent.add(t._objectsToSpans, t);
\r
92 ed.onSetContent.add(function() {
\r
93 t._spansToImgs(ed.getBody());
\r
96 ed.onPreProcess.add(function(ed, o) {
\r
100 t._spansToImgs(o.node);
\r
102 each(dom.select('IMG', o.node), function(n) {
\r
105 if (isMediaElm(n)) {
\r
106 p = t._parse(n.title);
\r
107 dom.setAttrib(n, 'width', dom.getAttrib(n, 'width', p.width || 100));
\r
108 dom.setAttrib(n, 'height', dom.getAttrib(n, 'height', p.height || 100));
\r
114 each(dom.select('IMG', o.node), function(n) {
\r
117 if (ed.getParam('media_use_script')) {
\r
119 n.className = n.className.replace(/mceItem/g, 'mceTemp');
\r
124 switch (n.className) {
\r
125 case 'mceItemFlash':
\r
126 ci = 'd27cdb6e-ae6d-11cf-96b8-444553540000';
\r
127 cb = 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0';
\r
128 mt = 'application/x-shockwave-flash';
\r
131 case 'mceItemShockWave':
\r
132 ci = '166b1bca-3f9c-11cf-8075-444553540000';
\r
133 cb = 'http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=8,5,1,0';
\r
134 mt = 'application/x-director';
\r
137 case 'mceItemWindowsMedia':
\r
138 ci = ed.getParam('media_wmp6_compatible') ? '05589fa1-c356-11ce-bf01-00aa0055595a' : '6bf52a52-394a-11d3-b153-00c04f79faa6';
\r
139 cb = 'http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701';
\r
140 mt = 'application/x-mplayer2';
\r
143 case 'mceItemQuickTime':
\r
144 ci = '02bf25d5-8c17-4b23-bc80-d3488abddc6b';
\r
145 cb = 'http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0';
\r
146 mt = 'video/quicktime';
\r
149 case 'mceItemRealMedia':
\r
150 ci = 'cfcdaa03-8be4-11cf-b84b-0020afbbccfa';
\r
151 cb = 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0';
\r
152 mt = 'audio/x-pn-realaudio-plugin';
\r
157 dom.replace(t._buildObj({
\r
167 ed.onPostProcess.add(function(ed, o) {
\r
168 o.content = o.content.replace(/_mce_value=/g, 'value=');
\r
171 function getAttr(s, n) {
\r
172 n = new RegExp(n + '=\"([^\"]+)\"', 'g').exec(s);
\r
174 return n ? ed.dom.decode(n[1]) : '';
\r
177 ed.onPostProcess.add(function(ed, o) {
\r
178 if (ed.getParam('media_use_script')) {
\r
179 o.content = o.content.replace(/<img[^>]+>/g, function(im) {
\r
180 var cl = getAttr(im, 'class');
\r
182 if (/^(mceTempFlash|mceTempShockWave|mceTempWindowsMedia|mceTempQuickTime|mceTempRealMedia)$/.test(cl)) {
\r
183 at = t._parse(getAttr(im, 'title'));
\r
184 at.width = getAttr(im, 'width');
\r
185 at.height = getAttr(im, 'height');
\r
186 im = '<script type="text/javascript">write' + cl.substring(7) + '({' + t._serialize(at) + '});</script>';
\r
195 getInfo : function() {
\r
197 longname : 'Media',
\r
198 author : 'Moxiecode Systems AB',
\r
199 authorurl : 'http://tinymce.moxiecode.com',
\r
200 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/media',
\r
201 version : tinymce.majorVersion + "." + tinymce.minorVersion
\r
206 _objectsToSpans : function(ed, o) {
\r
207 var t = this, h = o.content;
\r
209 h = h.replace(/<script[^>]*>\s*write(Flash|ShockWave|WindowsMedia|QuickTime|RealMedia)\(\{([^\)]*)\}\);\s*<\/script>/gi, function(a, b, c) {
\r
210 var o = t._parse(c);
\r
212 return '<img class="mceItem' + b + '" title="' + ed.dom.encode(c) + '" src="' + t.url + '/img/trans.gif" width="' + o.width + '" height="' + o.height + '" />'
\r
215 h = h.replace(/<object([^>]*)>/gi, '<span class="mceItemObject" $1>');
\r
216 h = h.replace(/<embed([^>]*)\/?>/gi, '<span class="mceItemEmbed" $1></span>');
\r
217 h = h.replace(/<embed([^>]*)>/gi, '<span class="mceItemEmbed" $1>');
\r
218 h = h.replace(/<\/(object)([^>]*)>/gi, '</span>');
\r
219 h = h.replace(/<\/embed>/gi, '');
\r
220 h = h.replace(/<param([^>]*)>/gi, function(a, b) {return '<span ' + b.replace(/value=/gi, '_mce_value=') + ' class="mceItemParam"></span>'});
\r
221 h = h.replace(/\/ class=\"mceItemParam\"><\/span>/gi, 'class="mceItemParam"></span>');
\r
226 _buildObj : function(o, n) {
\r
227 var ob, ed = this.editor, dom = ed.dom, p = this._parse(n.title), stc;
\r
229 stc = ed.getParam('media_strict', true) && o.type == 'application/x-shockwave-flash';
\r
231 p.width = o.width = dom.getAttrib(n, 'width') || 100;
\r
232 p.height = o.height = dom.getAttrib(n, 'height') || 100;
\r
235 p.src = ed.convertURL(p.src, 'src', n);
\r
238 ob = dom.create('span', {
\r
240 _mce_name : 'object',
\r
241 type : 'application/x-shockwave-flash',
\r
243 style : dom.getAttrib(n, 'style'),
\r
248 ob = dom.create('span', {
\r
250 _mce_name : 'object',
\r
251 classid : "clsid:" + o.classid,
\r
252 style : dom.getAttrib(n, 'style'),
\r
253 codebase : o.codebase,
\r
259 each (p, function(v, k) {
\r
260 if (!/^(width|height|codebase|classid|id|_cx|_cy)$/.test(k)) {
\r
261 // Use url instead of src in IE for Windows media
\r
262 if (o.type == 'application/x-mplayer2' && k == 'src' && !p.url)
\r
266 dom.add(ob, 'span', {_mce_name : 'param', name : k, '_mce_value' : v});
\r
271 dom.add(ob, 'span', tinymce.extend({_mce_name : 'embed', type : o.type, style : dom.getAttrib(n, 'style')}, p));
\r
276 _spansToImgs : function(p) {
\r
277 var t = this, dom = t.editor.dom, im, ci;
\r
279 each(dom.select('span', p), function(n) {
\r
280 // Convert object into image
\r
281 if (dom.getAttrib(n, 'class') == 'mceItemObject') {
\r
282 ci = dom.getAttrib(n, "classid").toLowerCase().replace(/\s+/g, '');
\r
285 case 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000':
\r
286 dom.replace(t._createImg('mceItemFlash', n), n);
\r
289 case 'clsid:166b1bca-3f9c-11cf-8075-444553540000':
\r
290 dom.replace(t._createImg('mceItemShockWave', n), n);
\r
293 case 'clsid:6bf52a52-394a-11d3-b153-00c04f79faa6':
\r
294 case 'clsid:22d6f312-b0f6-11d0-94ab-0080c74c7e95':
\r
295 case 'clsid:05589fa1-c356-11ce-bf01-00aa0055595a':
\r
296 dom.replace(t._createImg('mceItemWindowsMedia', n), n);
\r
299 case 'clsid:02bf25d5-8c17-4b23-bc80-d3488abddc6b':
\r
300 dom.replace(t._createImg('mceItemQuickTime', n), n);
\r
303 case 'clsid:cfcdaa03-8be4-11cf-b84b-0020afbbccfa':
\r
304 dom.replace(t._createImg('mceItemRealMedia', n), n);
\r
308 dom.replace(t._createImg('mceItemFlash', n), n);
\r
314 // Convert embed into image
\r
315 if (dom.getAttrib(n, 'class') == 'mceItemEmbed') {
\r
316 switch (dom.getAttrib(n, 'type')) {
\r
317 case 'application/x-shockwave-flash':
\r
318 dom.replace(t._createImg('mceItemFlash', n), n);
\r
321 case 'application/x-director':
\r
322 dom.replace(t._createImg('mceItemShockWave', n), n);
\r
325 case 'application/x-mplayer2':
\r
326 dom.replace(t._createImg('mceItemWindowsMedia', n), n);
\r
329 case 'video/quicktime':
\r
330 dom.replace(t._createImg('mceItemQuickTime', n), n);
\r
333 case 'audio/x-pn-realaudio-plugin':
\r
334 dom.replace(t._createImg('mceItemRealMedia', n), n);
\r
338 dom.replace(t._createImg('mceItemFlash', n), n);
\r
344 _createImg : function(cl, n) {
\r
345 var im, dom = this.editor.dom, pa = {}, ti = '', args;
\r
347 args = ['id', 'name', 'width', 'height', 'bgcolor', 'align', 'flashvars', 'src', 'wmode', 'allowfullscreen', 'quality', 'data'];
\r
350 im = dom.create('img', {
\r
351 src : this.url + '/img/trans.gif',
\r
352 width : dom.getAttrib(n, 'width') || 100,
\r
353 height : dom.getAttrib(n, 'height') || 100,
\r
354 style : dom.getAttrib(n, 'style'),
\r
358 // Setup base parameters
\r
359 each(args, function(na) {
\r
360 var v = dom.getAttrib(n, na);
\r
366 // Add optional parameters
\r
367 each(dom.select('span', n), function(n) {
\r
368 if (dom.hasClass(n, 'mceItemParam'))
\r
369 pa[dom.getAttrib(n, 'name')] = dom.getAttrib(n, '_mce_value');
\r
372 // Use src not movie
\r
384 // Merge with embed args
\r
385 n = dom.select('.mceItemEmbed', n)[0];
\r
387 each(args, function(na) {
\r
388 var v = dom.getAttrib(n, na);
\r
398 im.title = this._serialize(pa);
\r
403 _parse : function(s) {
\r
404 return tinymce.util.JSON.parse('{' + s + '}');
\r
407 _serialize : function(o) {
\r
408 return tinymce.util.JSON.serialize(o).replace(/[{}]/g, '');
\r
413 tinymce.PluginManager.add('media', tinymce.plugins.MediaPlugin);
\r