maj version dans config.xml
[mtweb] / mw / libs / tiny_mce / plugins / media / editor_plugin_src.js
1 /**\r
2  * editor_plugin_src.js\r
3  *\r
4  * Copyright 2009, Moxiecode Systems AB\r
5  * Released under LGPL License.\r
6  *\r
7  * License: http://tinymce.moxiecode.com/license\r
8  * Contributing: http://tinymce.moxiecode.com/contributing\r
9  */\r
10 \r
11 (function() {\r
12         var each = tinymce.each;\r
13 \r
14         tinymce.create('tinymce.plugins.MediaPlugin', {\r
15                 init : function(ed, url) {\r
16                         var t = this;\r
17                         \r
18                         t.editor = ed;\r
19                         t.url = url;\r
20 \r
21                         function isMediaElm(n) {\r
22                                 return /^(mceItemFlash|mceItemShockWave|mceItemWindowsMedia|mceItemQuickTime|mceItemRealMedia)$/.test(n.className);\r
23                         };\r
24 \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
28                         });\r
29 \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
36                                         inline : 1\r
37                                 }, {\r
38                                         plugin_url : url\r
39                                 });\r
40                         });\r
41 \r
42                         // Register buttons\r
43                         ed.addButton('media', {title : 'media.desc', cmd : 'mceMedia'});\r
44 \r
45                         ed.onNodeChange.add(function(ed, cm, n) {\r
46                                 cm.setActive('media', n.nodeName == 'IMG' && isMediaElm(n));\r
47                         });\r
48 \r
49                         ed.onInit.add(function() {\r
50                                 var lo = {\r
51                                         mceItemFlash : 'flash',\r
52                                         mceItemShockWave : 'shockwave',\r
53                                         mceItemWindowsMedia : 'windowsmedia',\r
54                                         mceItemQuickTime : 'quicktime',\r
55                                         mceItemRealMedia : 'realmedia'\r
56                                 };\r
57 \r
58                                 ed.selection.onSetContent.add(function() {\r
59                                         t._spansToImgs(ed.getBody());\r
60                                 });\r
61 \r
62                                 ed.selection.onBeforeSetContent.add(t._objectsToSpans, t);\r
63 \r
64                                 if (ed.settings.content_css !== false)\r
65                                         ed.dom.loadCSS(url + "/css/content.css");\r
66 \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
72                                                                         o.name = v;\r
73                                                                         o.title = ed.dom.getAttrib(o.node, 'title');\r
74                                                                         return false;\r
75                                                                 }\r
76                                                         });\r
77                                                 }\r
78                                         });\r
79                                 }\r
80 \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
85                                                 }\r
86                                         });\r
87                                 }\r
88                         });\r
89 \r
90                         ed.onBeforeSetContent.add(t._objectsToSpans, t);\r
91 \r
92                         ed.onSetContent.add(function() {\r
93                                 t._spansToImgs(ed.getBody());\r
94                         });\r
95 \r
96                         ed.onPreProcess.add(function(ed, o) {\r
97                                 var dom = ed.dom;\r
98 \r
99                                 if (o.set) {\r
100                                         t._spansToImgs(o.node);\r
101 \r
102                                         each(dom.select('IMG', o.node), function(n) {\r
103                                                 var p;\r
104 \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
109                                                 }\r
110                                         });\r
111                                 }\r
112 \r
113                                 if (o.get) {\r
114                                         each(dom.select('IMG', o.node), function(n) {\r
115                                                 var ci, cb, mt;\r
116 \r
117                                                 if (ed.getParam('media_use_script')) {\r
118                                                         if (isMediaElm(n))\r
119                                                                 n.className = n.className.replace(/mceItem/g, 'mceTemp');\r
120 \r
121                                                         return;\r
122                                                 }\r
123 \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
129                                                                 break;\r
130 \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
135                                                                 break;\r
136 \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
141                                                                 break;\r
142 \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
147                                                                 break;\r
148 \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
153                                                                 break;\r
154                                                 }\r
155 \r
156                                                 if (ci) {\r
157                                                         dom.replace(t._buildObj({\r
158                                                                 classid : ci,\r
159                                                                 codebase : cb,\r
160                                                                 type : mt\r
161                                                         }, n), n);\r
162                                                 }\r
163                                         });\r
164                                 }\r
165                         });\r
166 \r
167                         ed.onPostProcess.add(function(ed, o) {\r
168                                 o.content = o.content.replace(/_mce_value=/g, 'value=');\r
169                         });\r
170 \r
171                         function getAttr(s, n) {\r
172                                 n = new RegExp(n + '=\"([^\"]+)\"', 'g').exec(s);\r
173 \r
174                                 return n ? ed.dom.decode(n[1]) : '';\r
175                         };\r
176 \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
181 \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
187                                                 }\r
188 \r
189                                                 return im;\r
190                                         });\r
191                                 }\r
192                         });\r
193                 },\r
194 \r
195                 getInfo : function() {\r
196                         return {\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
202                         };\r
203                 },\r
204 \r
205                 // Private methods\r
206                 _objectsToSpans : function(ed, o) {\r
207                         var t = this, h = o.content;\r
208 \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
211 \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
213                         });\r
214 \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
222 \r
223                         o.content = h;\r
224                 },\r
225 \r
226                 _buildObj : function(o, n) {\r
227                         var ob, ed = this.editor, dom = ed.dom, p = this._parse(n.title), stc;\r
228                         \r
229                         stc = ed.getParam('media_strict', true) && o.type == 'application/x-shockwave-flash';\r
230 \r
231                         p.width = o.width = dom.getAttrib(n, 'width') || 100;\r
232                         p.height = o.height = dom.getAttrib(n, 'height') || 100;\r
233 \r
234                         if (p.src)\r
235                                 p.src = ed.convertURL(p.src, 'src', n);\r
236 \r
237                         if (stc) {\r
238                                 ob = dom.create('span', {\r
239                                         id : p.id,\r
240                                         _mce_name : 'object',\r
241                                         type : 'application/x-shockwave-flash',\r
242                                         data : p.src,\r
243                                         style : dom.getAttrib(n, 'style'),\r
244                                         width : o.width,\r
245                                         height : o.height\r
246                                 });\r
247                         } else {\r
248                                 ob = dom.create('span', {\r
249                                         id : p.id,\r
250                                         _mce_name : 'object',\r
251                                         classid : "clsid:" + o.classid,\r
252                                         style : dom.getAttrib(n, 'style'),\r
253                                         codebase : o.codebase,\r
254                                         width : o.width,\r
255                                         height : o.height\r
256                                 });\r
257                         }\r
258 \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
263                                                 k = 'url';\r
264 \r
265                                         if (v)\r
266                                                 dom.add(ob, 'span', {_mce_name : 'param', name : k, '_mce_value' : v});\r
267                                 }\r
268                         });\r
269 \r
270                         if (!stc)\r
271                                 dom.add(ob, 'span', tinymce.extend({_mce_name : 'embed', type : o.type, style : dom.getAttrib(n, 'style')}, p));\r
272 \r
273                         return ob;\r
274                 },\r
275 \r
276                 _spansToImgs : function(p) {\r
277                         var t = this, dom = t.editor.dom, im, ci;\r
278 \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
283 \r
284                                         switch (ci) {\r
285                                                 case 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000':\r
286                                                         dom.replace(t._createImg('mceItemFlash', n), n);\r
287                                                         break;\r
288 \r
289                                                 case 'clsid:166b1bca-3f9c-11cf-8075-444553540000':\r
290                                                         dom.replace(t._createImg('mceItemShockWave', n), n);\r
291                                                         break;\r
292 \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
297                                                         break;\r
298 \r
299                                                 case 'clsid:02bf25d5-8c17-4b23-bc80-d3488abddc6b':\r
300                                                         dom.replace(t._createImg('mceItemQuickTime', n), n);\r
301                                                         break;\r
302 \r
303                                                 case 'clsid:cfcdaa03-8be4-11cf-b84b-0020afbbccfa':\r
304                                                         dom.replace(t._createImg('mceItemRealMedia', n), n);\r
305                                                         break;\r
306 \r
307                                                 default:\r
308                                                         dom.replace(t._createImg('mceItemFlash', n), n);\r
309                                         }\r
310                                         \r
311                                         return;\r
312                                 }\r
313 \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
319                                                         break;\r
320 \r
321                                                 case 'application/x-director':\r
322                                                         dom.replace(t._createImg('mceItemShockWave', n), n);\r
323                                                         break;\r
324 \r
325                                                 case 'application/x-mplayer2':\r
326                                                         dom.replace(t._createImg('mceItemWindowsMedia', n), n);\r
327                                                         break;\r
328 \r
329                                                 case 'video/quicktime':\r
330                                                         dom.replace(t._createImg('mceItemQuickTime', n), n);\r
331                                                         break;\r
332 \r
333                                                 case 'audio/x-pn-realaudio-plugin':\r
334                                                         dom.replace(t._createImg('mceItemRealMedia', n), n);\r
335                                                         break;\r
336 \r
337                                                 default:\r
338                                                         dom.replace(t._createImg('mceItemFlash', n), n);\r
339                                         }\r
340                                 }                       \r
341                         });\r
342                 },\r
343 \r
344                 _createImg : function(cl, n) {\r
345                         var im, dom = this.editor.dom, pa = {}, ti = '', args;\r
346 \r
347                         args = ['id', 'name', 'width', 'height', 'bgcolor', 'align', 'flashvars', 'src', 'wmode', 'allowfullscreen', 'quality', 'data'];        \r
348 \r
349                         // Create image\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
355                                 'class' : cl\r
356                         });\r
357 \r
358                         // Setup base parameters\r
359                         each(args, function(na) {\r
360                                 var v = dom.getAttrib(n, na);\r
361 \r
362                                 if (v)\r
363                                         pa[na] = v;\r
364                         });\r
365 \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
370                         });\r
371 \r
372                         // Use src not movie\r
373                         if (pa.movie) {\r
374                                 pa.src = pa.movie;\r
375                                 delete pa.movie;\r
376                         }\r
377 \r
378                         // No src try data\r
379                         if (!pa.src) {\r
380                                 pa.src = pa.data;\r
381                                 delete pa.data;\r
382                         }\r
383 \r
384                         // Merge with embed args\r
385                         n = dom.select('.mceItemEmbed', n)[0];\r
386                         if (n) {\r
387                                 each(args, function(na) {\r
388                                         var v = dom.getAttrib(n, na);\r
389 \r
390                                         if (v && !pa[na])\r
391                                                 pa[na] = v;\r
392                                 });\r
393                         }\r
394 \r
395                         delete pa.width;\r
396                         delete pa.height;\r
397 \r
398                         im.title = this._serialize(pa);\r
399 \r
400                         return im;\r
401                 },\r
402 \r
403                 _parse : function(s) {\r
404                         return tinymce.util.JSON.parse('{' + s + '}');\r
405                 },\r
406 \r
407                 _serialize : function(o) {\r
408                         return tinymce.util.JSON.serialize(o).replace(/[{}]/g, '');\r
409                 }\r
410         });\r
411 \r
412         // Register plugin\r
413         tinymce.PluginManager.add('media', tinymce.plugins.MediaPlugin);\r
414 })();