2 * $Id: editor_plugin_src.js 296 2007-08-21 10:36:35Z spocke $
5 * @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved.
8 /* Import plugin specific language pack */
9 tinyMCE.importPluginLanguagePack('media');
11 var TinyMCE_MediaPlugin = {
12 getInfo : function() {
15 author : 'Moxiecode Systems AB',
16 authorurl : 'http://tinymce.moxiecode.com',
17 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/media',
18 version : tinyMCE.majorVersion + "." + tinyMCE.minorVersion
22 initInstance : function(inst) {
23 // Warn if user has flash plugin and media plugin at the same time
24 if (inst.hasPlugin('flash') && !tinyMCE.flashWarn) {
25 alert('Flash plugin is deprecated and should not be used together with the media plugin.');
26 tinyMCE.flashWarn = true;
29 if (!tinyMCE.settings['media_skip_plugin_css'])
30 tinyMCE.importCSS(inst.getDoc(), tinyMCE.baseURL + "/plugins/media/css/content.css");
33 getControlHTML : function(cn) {
36 return tinyMCE.getButtonHTML(cn, 'lang_media_desc', '{$pluginurl}/images/media.gif', 'mceMedia');
42 execCommand : function(editor_id, element, command, user_interface, value) {
47 file : '../../plugins/media/media.htm',
48 width : 430 + tinyMCE.getLang('lang_media_delta_width', 0),
49 height : 470 + tinyMCE.getLang('lang_media_delta_height', 0)
51 editor_id : editor_id,
58 // Pass to next handler in chain
62 cleanup : function(type, content, inst) {
63 var nl, img, i, ne, d, s, ci;
66 case "insert_to_editor":
67 img = tinyMCE.getParam("theme_href") + '/images/spacer.gif';
68 content = content.replace(/<script[^>]*>\s*write(Flash|ShockWave|WindowsMedia|QuickTime|RealMedia)\(\{([^\)]*)\}\);\s*<\/script>/gi, '<img class="mceItem$1" title="$2" src="' + img + '" />');
69 content = content.replace(/<object([^>]*)>/gi, '<div class="mceItemObject" $1>');
70 content = content.replace(/<embed([^>]*)>/gi, '<div class="mceItemObjectEmbed" $1>');
71 content = content.replace(/<\/(object|embed)([^>]*)>/gi, '</div>');
72 content = content.replace(/<param([^>]*)>/gi, '<div $1 class="mceItemParam"></div>');
73 content = content.replace(new RegExp('\\/ class="mceItemParam"><\\/div>', 'gi'), 'class="mceItemParam"></div>');
76 case "insert_to_editor_dom":
78 nl = content.getElementsByTagName("img");
79 for (i=0; i<nl.length; i++) {
80 if (/mceItem(Flash|ShockWave|WindowsMedia|QuickTime|RealMedia)/.test(nl[i].className)) {
81 nl[i].width = nl[i].title.replace(/.*width:[^0-9]?([0-9]+)%?.*/g, '$1');
82 nl[i].height = nl[i].title.replace(/.*height:[^0-9]?([0-9]+)%?.*/g, '$1');
83 //nl[i].align = nl[i].title.replace(/.*align:([a-z]+).*/gi, '$1');
87 nl = tinyMCE.selectElements(content, 'DIV', function (n) {return tinyMCE.hasCSSClass(n, 'mceItemObject');});
88 for (i=0; i<nl.length; i++) {
89 ci = tinyMCE.getAttrib(nl[i], "classid").toLowerCase().replace(/\s+/g, '');
92 case 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000':
93 nl[i].parentNode.replaceChild(TinyMCE_MediaPlugin._createImg('mceItemFlash', d, nl[i]), nl[i]);
96 case 'clsid:166b1bca-3f9c-11cf-8075-444553540000':
97 nl[i].parentNode.replaceChild(TinyMCE_MediaPlugin._createImg('mceItemShockWave', d, nl[i]), nl[i]);
100 case 'clsid:6bf52a52-394a-11d3-b153-00c04f79faa6':
101 case 'clsid:22d6f312-b0f6-11d0-94ab-0080c74c7e95':
102 case 'clsid:05589fa1-c356-11ce-bf01-00aa0055595a':
103 nl[i].parentNode.replaceChild(TinyMCE_MediaPlugin._createImg('mceItemWindowsMedia', d, nl[i]), nl[i]);
106 case 'clsid:02bf25d5-8c17-4b23-bc80-d3488abddc6b':
107 nl[i].parentNode.replaceChild(TinyMCE_MediaPlugin._createImg('mceItemQuickTime', d, nl[i]), nl[i]);
110 case 'clsid:cfcdaa03-8be4-11cf-b84b-0020afbbccfa':
111 nl[i].parentNode.replaceChild(TinyMCE_MediaPlugin._createImg('mceItemRealMedia', d, nl[i]), nl[i]);
116 // Handle embed (if any)
117 nl = tinyMCE.selectNodes(content, function (n) {return n.className == 'mceItemObjectEmbed';});
118 for (i=0; i<nl.length; i++) {
119 switch (tinyMCE.getAttrib(nl[i], 'type')) {
120 case 'application/x-shockwave-flash':
121 TinyMCE_MediaPlugin._createImgFromEmbed(nl[i], d, 'mceItemFlash');
124 case 'application/x-director':
125 TinyMCE_MediaPlugin._createImgFromEmbed(nl[i], d, 'mceItemShockWave');
128 case 'application/x-mplayer2':
129 TinyMCE_MediaPlugin._createImgFromEmbed(nl[i], d, 'mceItemWindowsMedia');
132 case 'video/quicktime':
133 TinyMCE_MediaPlugin._createImgFromEmbed(nl[i], d, 'mceItemQuickTime');
136 case 'audio/x-pn-realaudio-plugin':
137 TinyMCE_MediaPlugin._createImgFromEmbed(nl[i], d, 'mceItemRealMedia');
143 case "get_from_editor":
144 var startPos = -1, endPos, attribs, chunkBefore, chunkAfter, embedHTML, at, pl, cb, mt, ex;
146 while ((startPos = content.indexOf('<img', startPos+1)) != -1) {
147 endPos = content.indexOf('/>', startPos);
148 attribs = TinyMCE_MediaPlugin._parseAttributes(content.substring(startPos + 4, endPos));
150 // Is not flash, skip it
151 if (!/mceItem(Flash|ShockWave|WindowsMedia|QuickTime|RealMedia)/.test(attribs['class']))
157 at = attribs['title'];
159 at = at.replace(/&(#39|apos);/g, "'");
160 at = at.replace(/&#quot;/g, '"');
163 pl = eval('x={' + at + '};');
170 if (!tinyMCE.getParam('media_use_script', false)) {
171 switch (attribs['class']) {
173 ci = 'd27cdb6e-ae6d-11cf-96b8-444553540000';
174 cb = 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0';
175 mt = 'application/x-shockwave-flash';
178 case 'mceItemShockWave':
179 ci = '166B1BCA-3F9C-11CF-8075-444553540000';
180 cb = 'http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=8,5,1,0';
181 mt = 'application/x-director';
184 case 'mceItemWindowsMedia':
185 ci = tinyMCE.getParam('media_wmp6_compatible') ? '05589FA1-C356-11CE-BF01-00AA0055595A' : '6BF52A52-394A-11D3-B153-00C04F79FAA6';
186 cb = 'http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701';
187 mt = 'application/x-mplayer2';
190 case 'mceItemQuickTime':
191 ci = '02BF25D5-8C17-4B23-BC80-D3488ABDDC6B';
192 cb = 'http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0';
193 mt = 'video/quicktime';
196 case 'mceItemRealMedia':
197 ci = 'CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA';
198 cb = 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0';
199 mt = 'audio/x-pn-realaudio-plugin';
204 pl.src = tinyMCE.convertURL(pl.src, null, true);
206 embedHTML = TinyMCE_MediaPlugin._getEmbed(ci, cb, mt, pl, attribs);
208 // Use script version
209 switch (attribs['class']) {
214 case 'mceItemShockWave':
215 s = 'writeShockWave';
218 case 'mceItemWindowsMedia':
219 s = 'writeWindowsMedia';
222 case 'mceItemQuickTime':
223 s = 'writeQuickTime';
226 case 'mceItemRealMedia':
227 s = 'writeRealMedia';
232 at = at.replace(/width:[^0-9]?[0-9]+%?[^0-9]?/g, "width:'" + attribs.width + "'");
235 at = at.replace(/height:[^0-9]?[0-9]+%?[^0-9]?/g, "height:'" + attribs.height + "'");
237 // Force absolute URL
238 pl.src = tinyMCE.convertURL(pl.src, null, true);
239 at = at.replace(new RegExp("src:'[^']*'", "g"), "src:'" + pl.src + "'");
241 embedHTML = '<script type="text/javascript">' + s + '({' + at + '});</script>';
244 // Insert embed/object chunk
245 chunkBefore = content.substring(0, startPos);
246 chunkAfter = content.substring(endPos);
247 content = chunkBefore + embedHTML + chunkAfter;
255 handleNodeChange : function(editor_id, node, undo_index, undo_levels, visual_aid, any_selection) {
260 if (node.nodeName == "IMG" && /mceItem(Flash|ShockWave|WindowsMedia|QuickTime|RealMedia)/.test(tinyMCE.getAttrib(node, 'class'))) {
261 tinyMCE.switchClass(editor_id + '_media', 'mceButtonSelected');
264 } while ((node = node.parentNode));
266 tinyMCE.switchClass(editor_id + '_media', 'mceButtonNormal');
271 _createImgFromEmbed : function(n, d, cl) {
272 var ne, at, i, ti = '', an;
274 ne = d.createElement('img');
275 ne.src = tinyMCE.getParam("theme_href") + '/images/spacer.gif';
276 ne.width = tinyMCE.getAttrib(n, 'width');
277 ne.height = tinyMCE.getAttrib(n, 'height');
281 for (i=0; i<at.length; i++) {
282 if (at[i].specified && at[i].nodeValue) {
283 an = at[i].nodeName.toLowerCase();
291 if (an.indexOf('mce_') == -1 && !new RegExp('^(class|type)$').test(an))
292 ti += an.toLowerCase() + ':\'' + at[i].nodeValue + "',";
296 ti = ti.length > 0 ? ti.substring(0, ti.length - 1) : ti;
299 n.parentNode.replaceChild(ne, n);
302 _createImg : function(cl, d, n) {
303 var i, nl, ti = "", an, av, al = new Array();
305 ne = d.createElement('img');
306 ne.src = tinyMCE.getParam("theme_href") + '/images/spacer.gif';
307 ne.width = tinyMCE.getAttrib(n, 'width');
308 ne.height = tinyMCE.getAttrib(n, 'height');
311 al.id = tinyMCE.getAttrib(n, 'id');
312 al.name = tinyMCE.getAttrib(n, 'name');
313 al.width = tinyMCE.getAttrib(n, 'width');
314 al.height = tinyMCE.getAttrib(n, 'height');
315 al.bgcolor = tinyMCE.getAttrib(n, 'bgcolor');
316 al.align = tinyMCE.getAttrib(n, 'align');
317 al.class_name = tinyMCE.getAttrib(n, 'mce_class');
319 nl = n.getElementsByTagName('div');
320 for (i=0; i<nl.length; i++) {
321 av = tinyMCE.getAttrib(nl[i], 'value');
322 av = av.replace(new RegExp('\\\\', 'g'), '\\\\');
323 av = av.replace(new RegExp('"', 'g'), '\\"');
324 av = av.replace(new RegExp("'", 'g'), "\\'");
325 an = tinyMCE.getAttrib(nl[i], 'name');
335 if (al[an] != null && typeof(al[an]) != "function" && al[an] != '')
336 ti += an.toLowerCase() + ':\'' + al[an] + "',";
339 ti = ti.length > 0 ? ti.substring(0, ti.length - 1) : ti;
345 _getEmbed : function(cls, cb, mt, p, at) {
348 p.width = at.width ? at.width : p.width;
349 p.height = at.height ? at.height : p.height;
351 h += '<object classid="clsid:' + cls + '" codebase="' + cb + '"';
352 h += typeof(p.id) != "undefined" ? ' id="' + p.id + '"' : '';
353 h += typeof(p.name) != "undefined" ? ' name="' + p.name + '"' : '';
354 h += typeof(p.width) != "undefined" ? ' width="' + p.width + '"' : '';
355 h += typeof(p.height) != "undefined" ? ' height="' + p.height + '"' : '';
356 h += typeof(p.align) != "undefined" ? ' align="' + p.align + '"' : '';
360 if (typeof(p[n]) != "undefined" && typeof(p[n]) != "function") {
361 h += '<param name="' + n + '" value="' + p[n] + '" />';
363 // Add extra url parameter if it's an absolute URL on WMP
364 if (n == 'src' && p[n].indexOf('://') != -1 && mt == 'application/x-mplayer2')
365 h += '<param name="url" value="' + p[n] + '" />';
369 h += '<embed type="' + mt + '"';
372 if (typeof(p[n]) == "function")
375 // Skip url parameter for embed tag on WMP
376 if (!(n == 'url' && mt == 'application/x-mplayer2'))
377 h += ' ' + n + '="' + p[n] + '"';
380 h += '></embed></object>';
385 _parseAttributes : function(attribute_string) {
386 var attributeName = "", endChr = '"';
387 var attributeValue = "";
390 var attributes = new Array();
391 var whiteSpaceRegExp = new RegExp('^[ \n\r\t]+', 'g');
393 if (attribute_string == null || attribute_string.length < 2)
396 withInName = withInValue = false;
398 for (var i=0; i<attribute_string.length; i++) {
399 var chr = attribute_string.charAt(i);
401 if ((chr == '"' || chr == "'") && !withInValue) {
404 } else if (chr == endChr && withInValue) {
407 var pos = attributeName.lastIndexOf(' ');
409 attributeName = attributeName.substring(pos+1);
411 attributes[attributeName.toLowerCase()] = attributeValue.substring(1);
415 } else if (!whiteSpaceRegExp.test(chr) && !withInName && !withInValue)
418 if (chr == '=' && withInName)
422 attributeName += chr;
425 attributeValue += chr;
432 tinyMCE.addPlugin("media", TinyMCE_MediaPlugin);