source: trunk/web/app/plugins/tinymce/jscripts/tiny_mce/plugins/media/editor_plugin_src.js @ 6

Last change on this file since 6 was 6, checked in by dj3c1t, 12 years ago

mise a jour du trunk

File size: 9.3 KB
Line 
1/**
2 * $Id: editor_plugin_src.js 561 2008-01-23 15:18:19Z 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                                ed.dom.loadCSS(url + "/css/content.css");
51
52                                if (ed.theme.onResolveName) {
53                                        ed.theme.onResolveName.add(function(th, o) {
54                                                if (o.name == 'img') {
55                                                        each(lo, function(v, k) {
56                                                                if (ed.dom.hasClass(o.node, k)) {
57                                                                        o.name = v;
58                                                                        o.title = ed.dom.getAttrib(o.node, 'title');
59                                                                        return false;
60                                                                }
61                                                        });
62                                                }
63                                        });
64                                }
65
66                                if (ed && ed.plugins.contextmenu) {
67                                        ed.plugins.contextmenu.onContextMenu.add(function(th, m, e) {
68                                                if (e.nodeName == 'IMG' && /mceItem(Flash|ShockWave|WindowsMedia|QuickTime|RealMedia)/.test(e.className)) {
69                                                        m.add({title : 'media.edit', icon : 'media', cmd : 'mceMedia'});
70                                                }
71                                        });
72                                }
73                        });
74
75                        ed.onBeforeSetContent.add(function(ed, o) {
76                                var h = o.content;
77
78                                h = h.replace(/<script[^>]*>\s*write(Flash|ShockWave|WindowsMedia|QuickTime|RealMedia)\(\{([^\)]*)\}\);\s*<\/script>/gi, function(a, b, c) {
79                                        var o = eval("({" + c + "})");
80
81                                        return '<img class="mceItem' + b + '" title="' + ed.dom.encode(c) + '" src="' + url + '/img/trans.gif" width="' + o.width + '" height="' + o.height + '" />'
82                                });
83
84                                h = h.replace(/<object([^>]*)>/gi, '<span class="mceItemObject" $1>');
85                                h = h.replace(/<embed([^>]*)>/gi, '<span class="mceItemEmbed" $1>');
86                                h = h.replace(/<\/(object|embed)([^>]*)>/gi, '</span>');
87                                h = h.replace(/<param([^>]*)>/gi, function(a, b) {return '<span ' + b.replace(/value=/g, '_value=') + ' class="mceItemParam"></span>'});
88                                h = h.replace(/\/ class=\"mceItemParam\"><\/span>/gi, 'class="mceItemParam"></span>');
89
90                                o.content = h;
91                        });
92
93                        ed.onSetContent.add(function() {
94                                t._spansToImgs(ed.getBody());
95                        });
96
97                        ed.onPreProcess.add(function(ed, o) {
98                                var dom = ed.dom;
99
100                                if (o.set) {
101                                        t._spansToImgs(o.node);
102
103                                        each(dom.select('IMG', o.node), function(n) {
104                                                var p;
105
106                                                if (isMediaElm(n)) {
107                                                        p = t._parse(n.title);
108                                                        dom.setAttrib(n, 'width', dom.getAttrib(n, 'width', p.width || 100));
109                                                        dom.setAttrib(n, 'height', dom.getAttrib(n, 'height', p.height || 100));
110                                                }
111                                        });
112                                }
113
114                                if (o.get) {
115                                        each(dom.select('IMG', o.node), function(n) {
116                                                var ci, cb, mt;
117
118                                                if (ed.getParam('media_use_script')) {
119                                                        if (isMediaElm(n))
120                                                                n.className = n.className.replace(/mceItem/g, 'mceTemp');
121
122                                                        return;
123                                                }
124
125                                                switch (n.className) {
126                                                        case 'mceItemFlash':
127                                                                ci = 'd27cdb6e-ae6d-11cf-96b8-444553540000';
128                                                                cb = 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0';
129                                                                mt = 'application/x-shockwave-flash';
130                                                                break;
131
132                                                        case 'mceItemShockWave':
133                                                                ci = '166b1bca-3f9c-11cf-8075-444553540000';
134                                                                cb = 'http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=8,5,1,0';
135                                                                mt = 'application/x-director';
136                                                                break;
137
138                                                        case 'mceItemWindowsMedia':
139                                                                ci = ed.getParam('media_wmp6_compatible') ? '05589fa1-c356-11ce-bf01-00aa0055595a' : '6bf52a52-394a-11d3-b153-00c04f79faa6';
140                                                                cb = 'http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701';
141                                                                mt = 'application/x-mplayer2';
142                                                                break;
143
144                                                        case 'mceItemQuickTime':
145                                                                ci = '02bf25d5-8c17-4b23-bc80-d3488abddc6b';
146                                                                cb = 'http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0';
147                                                                mt = 'video/quicktime';
148                                                                break;
149
150                                                        case 'mceItemRealMedia':
151                                                                ci = 'cfcdaa03-8be4-11cf-b84b-0020afbbccfa';
152                                                                cb = 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0';
153                                                                mt = 'audio/x-pn-realaudio-plugin';
154                                                                break;
155                                                }
156
157                                                if (ci) {
158                                                        dom.replace(t._buildObj({
159                                                                classid : ci,
160                                                                codebase : cb,
161                                                                type : mt
162                                                        }, n), n);
163                                                }
164                                        });
165                                }
166                        });
167
168                        ed.onPostProcess.add(function(ed, o) {
169                                o.content = o.content.replace(/_value=/g, 'value=');
170                        });
171
172                        if (ed.getParam('media_use_script')) {
173                                function getAttr(s, n) {
174                                        n = new RegExp(n + '=\"([^\"]+)\"', 'g').exec(s);
175
176                                        return n ? ed.dom.decode(n[1]) : '';
177                                };
178
179                                ed.onPostProcess.add(function(ed, o) {
180                                        o.content = o.content.replace(/<img[^>]+>/g, function(im) {
181                                                var cl = getAttr(im, 'class');
182
183                                                if (/^(mceTempFlash|mceTempShockWave|mceTempWindowsMedia|mceTempQuickTime|mceTempRealMedia)$/.test(cl)) {
184                                                        at = t._parse(getAttr(im, 'title'));
185                                                        at.width = getAttr(im, 'width');
186                                                        at.height = getAttr(im, 'height');
187                                                        im = '<script type="text/javascript">write' + cl.substring(7) + '({' + t._serialize(at) + '});</script>';
188                                                }
189
190                                                return im;
191                                        });
192                                });
193                        }
194                },
195
196                getInfo : function() {
197                        return {
198                                longname : 'Media',
199                                author : 'Moxiecode Systems AB',
200                                authorurl : 'http://tinymce.moxiecode.com',
201                                infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/media',
202                                version : tinymce.majorVersion + "." + tinymce.minorVersion
203                        };
204                },
205
206                // Private methods
207
208                _buildObj : function(o, n) {
209                        var ob, ed = this.editor, dom = ed.dom, p = this._parse(n.title);
210
211                        p.width = o.width = dom.getAttrib(n, 'width') || 100;
212                        p.height = o.height = dom.getAttrib(n, 'height') || 100;
213
214                        ob = dom.create('span', {
215                                mce_name : 'object',
216                                classid : "clsid:" + o.classid,
217                                codebase : o.codebase,
218                                width : o.width,
219                                height : o.height
220                        });
221
222                        if (p.src)
223                                p.src = ed.convertURL(p.src, 'src', n);
224
225                        each (p, function(v, k) {
226                                if (v && !/^(width|height|codebase|classid)$/.test(k))
227                                        dom.add(ob, 'span', {mce_name : 'param', name : k, '_value' : v});
228                        });
229
230                        dom.add(ob, 'span', tinymce.extend({mce_name : 'embed', type : o.type}, p));
231
232                        return ob;
233                },
234
235                _spansToImgs : function(p) {
236                        var t = this, dom = t.editor.dom, im, ci;
237
238                        each(dom.select('span', p), function(n) {
239                                // Convert object into image
240                                if (dom.getAttrib(n, 'class') == 'mceItemObject') {
241                                        ci = dom.getAttrib(n, "classid").toLowerCase().replace(/\s+/g, '');
242
243                                        switch (ci) {
244                                                case 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000':
245                                                        dom.replace(t._createImg('mceItemFlash', n), n);
246                                                        break;
247
248                                                case 'clsid:166b1bca-3f9c-11cf-8075-444553540000':
249                                                        dom.replace(t._createImg('mceItemShockWave', n), n);
250                                                        break;
251
252                                                case 'clsid:6bf52a52-394a-11d3-b153-00c04f79faa6':
253                                                case 'clsid:22d6f312-b0f6-11d0-94ab-0080c74c7e95':
254                                                case 'clsid:05589fa1-c356-11ce-bf01-00aa0055595a':
255                                                        dom.replace(t._createImg('mceItemWindowsMedia', n), n);
256                                                        break;
257
258                                                case 'clsid:02bf25d5-8c17-4b23-bc80-d3488abddc6b':
259                                                        dom.replace(t._createImg('mceItemQuickTime', n), n);
260                                                        break;
261
262                                                case 'clsid:cfcdaa03-8be4-11cf-b84b-0020afbbccfa':
263                                                        dom.replace(t._createImg('mceItemRealMedia', n), n);
264                                                        break;
265
266                                                default:
267                                                        dom.replace(t._createImg('mceItemFlash', n), n);
268                                        }
269                                }
270                        });
271                },
272
273                _createImg : function(cl, n) {
274                        var im, dom = this.editor.dom, pa = {}, ti = '';
275
276                        // Create image
277                        im = dom.create('img', {
278                                src : this.url + '/img/trans.gif',
279                                width : dom.getAttrib(n, 'width') || 100,
280                                height : dom.getAttrib(n, 'height') || 100,
281                                'class' : cl
282                        });
283
284                        // Setup base parameters
285                        each(['id', 'name', 'width', 'height', 'bgcolor', 'align'], function(n) {
286                                var v = dom.getAttrib(n, 'align');
287
288                                if (v)
289                                        pa[v] = v;
290                        });
291
292                        // Add optional parameters
293                        each(dom.select('span', n), function(n) {
294                                if (dom.hasClass(n, 'mceItemParam'))
295                                        pa[dom.getAttrib(n, 'name')] = dom.getAttrib(n, '_value');
296                        });
297
298                        // Use src not movie
299                        if (pa.movie) {
300                                pa.src = pa.movie;
301                                delete pa.movie;
302                        }
303
304                        delete pa.width;
305                        delete pa.height;
306
307                        im.title = this._serialize(pa);
308
309                        return im;
310                },
311
312                _parse : function(s) {
313                        return tinymce.util.JSON.parse('{' + s + '}');
314                },
315
316                _serialize : function(o) {
317                        return tinymce.util.JSON.serialize(o).replace(/[{}]/g, '');
318                }
319        });
320
321        // Register plugin
322        tinymce.PluginManager.add('media', tinymce.plugins.MediaPlugin);
323})();
Note: See TracBrowser for help on using the repository browser.