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