annotate static/js/tiny_mce/plugins/media/editor_plugin_src.js @ 337:b4f0980506e0 rollout

Working around a strange CSS problem when bulletins are active. This should probably be investigated later.
author Brian Neal <bgneal@gmail.com>
date Sat, 26 Feb 2011 06:46:28 +0000
parents 88b2b9cb8c1f
children 6c182ceb7147
rev   line source
bgneal@312 1 /**
bgneal@312 2 * editor_plugin_src.js
bgneal@312 3 *
bgneal@312 4 * Copyright 2009, Moxiecode Systems AB
bgneal@312 5 * Released under LGPL License.
bgneal@312 6 *
bgneal@312 7 * License: http://tinymce.moxiecode.com/license
bgneal@312 8 * Contributing: http://tinymce.moxiecode.com/contributing
bgneal@312 9 */
bgneal@312 10
bgneal@312 11 (function() {
bgneal@312 12 var each = tinymce.each;
bgneal@312 13
bgneal@312 14 tinymce.create('tinymce.plugins.MediaPlugin', {
bgneal@312 15 init : function(ed, url) {
bgneal@312 16 var t = this;
bgneal@312 17
bgneal@312 18 t.editor = ed;
bgneal@312 19 t.url = url;
bgneal@312 20
bgneal@312 21 function isMediaElm(n) {
bgneal@312 22 return /^(mceItemFlash|mceItemShockWave|mceItemWindowsMedia|mceItemQuickTime|mceItemRealMedia)$/.test(n.className);
bgneal@312 23 };
bgneal@312 24
bgneal@312 25 ed.onPreInit.add(function() {
bgneal@312 26 // Force in _value parameter this extra parameter is required for older Opera versions
bgneal@312 27 ed.serializer.addRules('param[name|value|_mce_value]');
bgneal@312 28 });
bgneal@312 29
bgneal@312 30 // Register commands
bgneal@312 31 ed.addCommand('mceMedia', function() {
bgneal@312 32 ed.windowManager.open({
bgneal@312 33 file : url + '/media.htm',
bgneal@312 34 width : 430 + parseInt(ed.getLang('media.delta_width', 0)),
bgneal@312 35 height : 470 + parseInt(ed.getLang('media.delta_height', 0)),
bgneal@312 36 inline : 1
bgneal@312 37 }, {
bgneal@312 38 plugin_url : url
bgneal@312 39 });
bgneal@312 40 });
bgneal@312 41
bgneal@312 42 // Register buttons
bgneal@312 43 ed.addButton('media', {title : 'media.desc', cmd : 'mceMedia'});
bgneal@312 44
bgneal@312 45 ed.onNodeChange.add(function(ed, cm, n) {
bgneal@312 46 cm.setActive('media', n.nodeName == 'IMG' && isMediaElm(n));
bgneal@312 47 });
bgneal@312 48
bgneal@312 49 ed.onInit.add(function() {
bgneal@312 50 var lo = {
bgneal@312 51 mceItemFlash : 'flash',
bgneal@312 52 mceItemShockWave : 'shockwave',
bgneal@312 53 mceItemWindowsMedia : 'windowsmedia',
bgneal@312 54 mceItemQuickTime : 'quicktime',
bgneal@312 55 mceItemRealMedia : 'realmedia'
bgneal@312 56 };
bgneal@312 57
bgneal@312 58 ed.selection.onSetContent.add(function() {
bgneal@312 59 t._spansToImgs(ed.getBody());
bgneal@312 60 });
bgneal@312 61
bgneal@312 62 ed.selection.onBeforeSetContent.add(t._objectsToSpans, t);
bgneal@312 63
bgneal@312 64 if (ed.settings.content_css !== false)
bgneal@312 65 ed.dom.loadCSS(url + "/css/content.css");
bgneal@312 66
bgneal@312 67 if (ed.theme && ed.theme.onResolveName) {
bgneal@312 68 ed.theme.onResolveName.add(function(th, o) {
bgneal@312 69 if (o.name == 'img') {
bgneal@312 70 each(lo, function(v, k) {
bgneal@312 71 if (ed.dom.hasClass(o.node, k)) {
bgneal@312 72 o.name = v;
bgneal@312 73 o.title = ed.dom.getAttrib(o.node, 'title');
bgneal@312 74 return false;
bgneal@312 75 }
bgneal@312 76 });
bgneal@312 77 }
bgneal@312 78 });
bgneal@312 79 }
bgneal@312 80
bgneal@312 81 if (ed && ed.plugins.contextmenu) {
bgneal@312 82 ed.plugins.contextmenu.onContextMenu.add(function(th, m, e) {
bgneal@312 83 if (e.nodeName == 'IMG' && /mceItem(Flash|ShockWave|WindowsMedia|QuickTime|RealMedia)/.test(e.className)) {
bgneal@312 84 m.add({title : 'media.edit', icon : 'media', cmd : 'mceMedia'});
bgneal@312 85 }
bgneal@312 86 });
bgneal@312 87 }
bgneal@312 88 });
bgneal@312 89
bgneal@312 90 ed.onBeforeSetContent.add(t._objectsToSpans, t);
bgneal@312 91
bgneal@312 92 ed.onSetContent.add(function() {
bgneal@312 93 t._spansToImgs(ed.getBody());
bgneal@312 94 });
bgneal@312 95
bgneal@312 96 ed.onPreProcess.add(function(ed, o) {
bgneal@312 97 var dom = ed.dom;
bgneal@312 98
bgneal@312 99 if (o.set) {
bgneal@312 100 t._spansToImgs(o.node);
bgneal@312 101
bgneal@312 102 each(dom.select('IMG', o.node), function(n) {
bgneal@312 103 var p;
bgneal@312 104
bgneal@312 105 if (isMediaElm(n)) {
bgneal@312 106 p = t._parse(n.title);
bgneal@312 107 dom.setAttrib(n, 'width', dom.getAttrib(n, 'width', p.width || 100));
bgneal@312 108 dom.setAttrib(n, 'height', dom.getAttrib(n, 'height', p.height || 100));
bgneal@312 109 }
bgneal@312 110 });
bgneal@312 111 }
bgneal@312 112
bgneal@312 113 if (o.get) {
bgneal@312 114 each(dom.select('IMG', o.node), function(n) {
bgneal@312 115 var ci, cb, mt;
bgneal@312 116
bgneal@312 117 if (ed.getParam('media_use_script')) {
bgneal@312 118 if (isMediaElm(n))
bgneal@312 119 n.className = n.className.replace(/mceItem/g, 'mceTemp');
bgneal@312 120
bgneal@312 121 return;
bgneal@312 122 }
bgneal@312 123
bgneal@312 124 switch (n.className) {
bgneal@312 125 case 'mceItemFlash':
bgneal@312 126 ci = 'd27cdb6e-ae6d-11cf-96b8-444553540000';
bgneal@312 127 cb = 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0';
bgneal@312 128 mt = 'application/x-shockwave-flash';
bgneal@312 129 break;
bgneal@312 130
bgneal@312 131 case 'mceItemShockWave':
bgneal@312 132 ci = '166b1bca-3f9c-11cf-8075-444553540000';
bgneal@312 133 cb = 'http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=8,5,1,0';
bgneal@312 134 mt = 'application/x-director';
bgneal@312 135 break;
bgneal@312 136
bgneal@312 137 case 'mceItemWindowsMedia':
bgneal@312 138 ci = ed.getParam('media_wmp6_compatible') ? '05589fa1-c356-11ce-bf01-00aa0055595a' : '6bf52a52-394a-11d3-b153-00c04f79faa6';
bgneal@312 139 cb = 'http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701';
bgneal@312 140 mt = 'application/x-mplayer2';
bgneal@312 141 break;
bgneal@312 142
bgneal@312 143 case 'mceItemQuickTime':
bgneal@312 144 ci = '02bf25d5-8c17-4b23-bc80-d3488abddc6b';
bgneal@312 145 cb = 'http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0';
bgneal@312 146 mt = 'video/quicktime';
bgneal@312 147 break;
bgneal@312 148
bgneal@312 149 case 'mceItemRealMedia':
bgneal@312 150 ci = 'cfcdaa03-8be4-11cf-b84b-0020afbbccfa';
bgneal@312 151 cb = 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0';
bgneal@312 152 mt = 'audio/x-pn-realaudio-plugin';
bgneal@312 153 break;
bgneal@312 154 }
bgneal@312 155
bgneal@312 156 if (ci) {
bgneal@312 157 dom.replace(t._buildObj({
bgneal@312 158 classid : ci,
bgneal@312 159 codebase : cb,
bgneal@312 160 type : mt
bgneal@312 161 }, n), n);
bgneal@312 162 }
bgneal@312 163 });
bgneal@312 164 }
bgneal@312 165 });
bgneal@312 166
bgneal@312 167 ed.onPostProcess.add(function(ed, o) {
bgneal@312 168 o.content = o.content.replace(/_mce_value=/g, 'value=');
bgneal@312 169 });
bgneal@312 170
bgneal@312 171 function getAttr(s, n) {
bgneal@312 172 n = new RegExp(n + '=\"([^\"]+)\"', 'g').exec(s);
bgneal@312 173
bgneal@312 174 return n ? ed.dom.decode(n[1]) : '';
bgneal@312 175 };
bgneal@312 176
bgneal@312 177 ed.onPostProcess.add(function(ed, o) {
bgneal@312 178 if (ed.getParam('media_use_script')) {
bgneal@312 179 o.content = o.content.replace(/<img[^>]+>/g, function(im) {
bgneal@312 180 var cl = getAttr(im, 'class');
bgneal@312 181
bgneal@312 182 if (/^(mceTempFlash|mceTempShockWave|mceTempWindowsMedia|mceTempQuickTime|mceTempRealMedia)$/.test(cl)) {
bgneal@312 183 at = t._parse(getAttr(im, 'title'));
bgneal@312 184 at.width = getAttr(im, 'width');
bgneal@312 185 at.height = getAttr(im, 'height');
bgneal@312 186 im = '<script type="text/javascript">write' + cl.substring(7) + '({' + t._serialize(at) + '});</script>';
bgneal@312 187 }
bgneal@312 188
bgneal@312 189 return im;
bgneal@312 190 });
bgneal@312 191 }
bgneal@312 192 });
bgneal@312 193 },
bgneal@312 194
bgneal@312 195 getInfo : function() {
bgneal@312 196 return {
bgneal@312 197 longname : 'Media',
bgneal@312 198 author : 'Moxiecode Systems AB',
bgneal@312 199 authorurl : 'http://tinymce.moxiecode.com',
bgneal@312 200 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/media',
bgneal@312 201 version : tinymce.majorVersion + "." + tinymce.minorVersion
bgneal@312 202 };
bgneal@312 203 },
bgneal@312 204
bgneal@312 205 // Private methods
bgneal@312 206 _objectsToSpans : function(ed, o) {
bgneal@312 207 var t = this, h = o.content;
bgneal@312 208
bgneal@312 209 h = h.replace(/<script[^>]*>\s*write(Flash|ShockWave|WindowsMedia|QuickTime|RealMedia)\(\{([^\)]*)\}\);\s*<\/script>/gi, function(a, b, c) {
bgneal@312 210 var o = t._parse(c);
bgneal@312 211
bgneal@312 212 return '<img class="mceItem' + b + '" title="' + ed.dom.encode(c) + '" src="' + t.url + '/img/trans.gif" width="' + o.width + '" height="' + o.height + '" />'
bgneal@312 213 });
bgneal@312 214
bgneal@312 215 h = h.replace(/<object([^>]*)>/gi, '<span class="mceItemObject" $1>');
bgneal@312 216 h = h.replace(/<embed([^>]*)\/?>/gi, '<span class="mceItemEmbed" $1></span>');
bgneal@312 217 h = h.replace(/<embed([^>]*)>/gi, '<span class="mceItemEmbed" $1>');
bgneal@312 218 h = h.replace(/<\/(object)([^>]*)>/gi, '</span>');
bgneal@312 219 h = h.replace(/<\/embed>/gi, '');
bgneal@312 220 h = h.replace(/<param([^>]*)>/gi, function(a, b) {return '<span ' + b.replace(/value=/gi, '_mce_value=') + ' class="mceItemParam"></span>'});
bgneal@312 221 h = h.replace(/\/ class=\"mceItemParam\"><\/span>/gi, 'class="mceItemParam"></span>');
bgneal@312 222
bgneal@312 223 o.content = h;
bgneal@312 224 },
bgneal@312 225
bgneal@312 226 _buildObj : function(o, n) {
bgneal@312 227 var ob, ed = this.editor, dom = ed.dom, p = this._parse(n.title), stc;
bgneal@312 228
bgneal@312 229 stc = ed.getParam('media_strict', true) && o.type == 'application/x-shockwave-flash';
bgneal@312 230
bgneal@312 231 p.width = o.width = dom.getAttrib(n, 'width') || 100;
bgneal@312 232 p.height = o.height = dom.getAttrib(n, 'height') || 100;
bgneal@312 233
bgneal@312 234 if (p.src)
bgneal@312 235 p.src = ed.convertURL(p.src, 'src', n);
bgneal@312 236
bgneal@312 237 if (stc) {
bgneal@312 238 ob = dom.create('span', {
bgneal@312 239 id : p.id,
bgneal@312 240 _mce_name : 'object',
bgneal@312 241 type : 'application/x-shockwave-flash',
bgneal@312 242 data : p.src,
bgneal@312 243 style : dom.getAttrib(n, 'style'),
bgneal@312 244 width : o.width,
bgneal@312 245 height : o.height
bgneal@312 246 });
bgneal@312 247 } else {
bgneal@312 248 ob = dom.create('span', {
bgneal@312 249 id : p.id,
bgneal@312 250 _mce_name : 'object',
bgneal@312 251 classid : "clsid:" + o.classid,
bgneal@312 252 style : dom.getAttrib(n, 'style'),
bgneal@312 253 codebase : o.codebase,
bgneal@312 254 width : o.width,
bgneal@312 255 height : o.height
bgneal@312 256 });
bgneal@312 257 }
bgneal@312 258
bgneal@312 259 each (p, function(v, k) {
bgneal@312 260 if (!/^(width|height|codebase|classid|id|_cx|_cy)$/.test(k)) {
bgneal@312 261 // Use url instead of src in IE for Windows media
bgneal@312 262 if (o.type == 'application/x-mplayer2' && k == 'src' && !p.url)
bgneal@312 263 k = 'url';
bgneal@312 264
bgneal@312 265 if (v)
bgneal@312 266 dom.add(ob, 'span', {_mce_name : 'param', name : k, '_mce_value' : v});
bgneal@312 267 }
bgneal@312 268 });
bgneal@312 269
bgneal@312 270 if (!stc)
bgneal@312 271 dom.add(ob, 'span', tinymce.extend({_mce_name : 'embed', type : o.type, style : dom.getAttrib(n, 'style')}, p));
bgneal@312 272
bgneal@312 273 return ob;
bgneal@312 274 },
bgneal@312 275
bgneal@312 276 _spansToImgs : function(p) {
bgneal@312 277 var t = this, dom = t.editor.dom, im, ci;
bgneal@312 278
bgneal@312 279 each(dom.select('span', p), function(n) {
bgneal@312 280 // Convert object into image
bgneal@312 281 if (dom.getAttrib(n, 'class') == 'mceItemObject') {
bgneal@312 282 ci = dom.getAttrib(n, "classid").toLowerCase().replace(/\s+/g, '');
bgneal@312 283
bgneal@312 284 switch (ci) {
bgneal@312 285 case 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000':
bgneal@312 286 dom.replace(t._createImg('mceItemFlash', n), n);
bgneal@312 287 break;
bgneal@312 288
bgneal@312 289 case 'clsid:166b1bca-3f9c-11cf-8075-444553540000':
bgneal@312 290 dom.replace(t._createImg('mceItemShockWave', n), n);
bgneal@312 291 break;
bgneal@312 292
bgneal@312 293 case 'clsid:6bf52a52-394a-11d3-b153-00c04f79faa6':
bgneal@312 294 case 'clsid:22d6f312-b0f6-11d0-94ab-0080c74c7e95':
bgneal@312 295 case 'clsid:05589fa1-c356-11ce-bf01-00aa0055595a':
bgneal@312 296 dom.replace(t._createImg('mceItemWindowsMedia', n), n);
bgneal@312 297 break;
bgneal@312 298
bgneal@312 299 case 'clsid:02bf25d5-8c17-4b23-bc80-d3488abddc6b':
bgneal@312 300 dom.replace(t._createImg('mceItemQuickTime', n), n);
bgneal@312 301 break;
bgneal@312 302
bgneal@312 303 case 'clsid:cfcdaa03-8be4-11cf-b84b-0020afbbccfa':
bgneal@312 304 dom.replace(t._createImg('mceItemRealMedia', n), n);
bgneal@312 305 break;
bgneal@312 306
bgneal@312 307 default:
bgneal@312 308 dom.replace(t._createImg('mceItemFlash', n), n);
bgneal@312 309 }
bgneal@312 310
bgneal@312 311 return;
bgneal@312 312 }
bgneal@312 313
bgneal@312 314 // Convert embed into image
bgneal@312 315 if (dom.getAttrib(n, 'class') == 'mceItemEmbed') {
bgneal@312 316 switch (dom.getAttrib(n, 'type')) {
bgneal@312 317 case 'application/x-shockwave-flash':
bgneal@312 318 dom.replace(t._createImg('mceItemFlash', n), n);
bgneal@312 319 break;
bgneal@312 320
bgneal@312 321 case 'application/x-director':
bgneal@312 322 dom.replace(t._createImg('mceItemShockWave', n), n);
bgneal@312 323 break;
bgneal@312 324
bgneal@312 325 case 'application/x-mplayer2':
bgneal@312 326 dom.replace(t._createImg('mceItemWindowsMedia', n), n);
bgneal@312 327 break;
bgneal@312 328
bgneal@312 329 case 'video/quicktime':
bgneal@312 330 dom.replace(t._createImg('mceItemQuickTime', n), n);
bgneal@312 331 break;
bgneal@312 332
bgneal@312 333 case 'audio/x-pn-realaudio-plugin':
bgneal@312 334 dom.replace(t._createImg('mceItemRealMedia', n), n);
bgneal@312 335 break;
bgneal@312 336
bgneal@312 337 default:
bgneal@312 338 dom.replace(t._createImg('mceItemFlash', n), n);
bgneal@312 339 }
bgneal@312 340 }
bgneal@312 341 });
bgneal@312 342 },
bgneal@312 343
bgneal@312 344 _createImg : function(cl, n) {
bgneal@312 345 var im, dom = this.editor.dom, pa = {}, ti = '', args;
bgneal@312 346
bgneal@312 347 args = ['id', 'name', 'width', 'height', 'bgcolor', 'align', 'flashvars', 'src', 'wmode', 'allowfullscreen', 'quality', 'data'];
bgneal@312 348
bgneal@312 349 // Create image
bgneal@312 350 im = dom.create('img', {
bgneal@312 351 src : this.url + '/img/trans.gif',
bgneal@312 352 width : dom.getAttrib(n, 'width') || 100,
bgneal@312 353 height : dom.getAttrib(n, 'height') || 100,
bgneal@312 354 style : dom.getAttrib(n, 'style'),
bgneal@312 355 'class' : cl
bgneal@312 356 });
bgneal@312 357
bgneal@312 358 // Setup base parameters
bgneal@312 359 each(args, function(na) {
bgneal@312 360 var v = dom.getAttrib(n, na);
bgneal@312 361
bgneal@312 362 if (v)
bgneal@312 363 pa[na] = v;
bgneal@312 364 });
bgneal@312 365
bgneal@312 366 // Add optional parameters
bgneal@312 367 each(dom.select('span', n), function(n) {
bgneal@312 368 if (dom.hasClass(n, 'mceItemParam'))
bgneal@312 369 pa[dom.getAttrib(n, 'name')] = dom.getAttrib(n, '_mce_value');
bgneal@312 370 });
bgneal@312 371
bgneal@312 372 // Use src not movie
bgneal@312 373 if (pa.movie) {
bgneal@312 374 pa.src = pa.movie;
bgneal@312 375 delete pa.movie;
bgneal@312 376 }
bgneal@312 377
bgneal@312 378 // No src try data
bgneal@312 379 if (!pa.src) {
bgneal@312 380 pa.src = pa.data;
bgneal@312 381 delete pa.data;
bgneal@312 382 }
bgneal@312 383
bgneal@312 384 // Merge with embed args
bgneal@312 385 n = dom.select('.mceItemEmbed', n)[0];
bgneal@312 386 if (n) {
bgneal@312 387 each(args, function(na) {
bgneal@312 388 var v = dom.getAttrib(n, na);
bgneal@312 389
bgneal@312 390 if (v && !pa[na])
bgneal@312 391 pa[na] = v;
bgneal@312 392 });
bgneal@312 393 }
bgneal@312 394
bgneal@312 395 delete pa.width;
bgneal@312 396 delete pa.height;
bgneal@312 397
bgneal@312 398 im.title = this._serialize(pa);
bgneal@312 399
bgneal@312 400 return im;
bgneal@312 401 },
bgneal@312 402
bgneal@312 403 _parse : function(s) {
bgneal@312 404 return tinymce.util.JSON.parse('{' + s + '}');
bgneal@312 405 },
bgneal@312 406
bgneal@312 407 _serialize : function(o) {
bgneal@312 408 return tinymce.util.JSON.serialize(o).replace(/[{}]/g, '');
bgneal@312 409 }
bgneal@312 410 });
bgneal@312 411
bgneal@312 412 // Register plugin
bgneal@312 413 tinymce.PluginManager.add('media', tinymce.plugins.MediaPlugin);
bgneal@312 414 })();