PageRenderTime 106ms CodeModel.GetById 40ms app.highlight 40ms RepoModel.GetById 11ms app.codeStats 0ms

/static/scripts/tiny_mce/plugins/media/editor_plugin_src.js

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