yllar
2006-12-14 38bf9d3b71067a51ffc9a915ea288929d1fb08e4
commit | author | age
f0ea59 1 /**
S 2  * $Id: editor_plugin_src.js 126 2006-10-22 16:19:55Z spocke $
3  *
4  * @author Moxiecode
5  * @copyright Copyright © 2004-2006, Moxiecode Systems AB, All rights reserved.
6  */
7
8 /* Import plugin specific language pack */
9 tinyMCE.importPluginLanguagePack('media');
10
11 var TinyMCE_MediaPlugin = {
12     getInfo : function() {
13         return {
14             longname : 'Media',
15             author : 'Moxiecode Systems AB',
16             authorurl : 'http://tinymce.moxiecode.com',
17             infourl : 'http://tinymce.moxiecode.com/tinymce/docs/plugin_media.html',
18             version : tinyMCE.majorVersion + "." + tinyMCE.minorVersion
19         };
20     },
21
22     initInstance : function(inst) {
23         if (!tinyMCE.settings['media_skip_plugin_css'])
24             tinyMCE.importCSS(inst.getDoc(), tinyMCE.baseURL + "/plugins/media/css/content.css");
25     },
26
27     getControlHTML : function(cn) {
28         switch (cn) {
29             case "media":
30                 return tinyMCE.getButtonHTML(cn, 'lang_media_desc', '{$pluginurl}/images/media.gif', 'mceMedia');
31         }
32
33         return "";
34     },
35
36     execCommand : function(editor_id, element, command, user_interface, value) {
37         // Handle commands
38         switch (command) {
39             case "mceMedia":
40                 tinyMCE.openWindow({
41                         file : '../../plugins/media/media.htm',
42                         width : 430 + tinyMCE.getLang('lang_media_delta_width', 0),
43                         height : 470 + tinyMCE.getLang('lang_media_delta_height', 0)
44                     }, {
45                         editor_id : editor_id,
46                         inline : "yes"
47                 });
48
49                 return true;
50        }
51
52        // Pass to next handler in chain
53        return false;
54     },
55
56     cleanup : function(type, content, inst) {
57         var nl, img, i, ne, d, s, ci;
58
59         switch (type) {
60             case "insert_to_editor":
61                 img = tinyMCE.getParam("theme_href") + '/images/spacer.gif';
62                 content = content.replace(/<script[^>]*>\s*write(Flash|ShockWave|WindowsMedia|QuickTime|RealMedia)\(\{([^\)]*)\}\);\s*<\/script>/gi, '<img class="mceItem$1" title="$2" src="' + img + '" />');
63                 content = content.replace(/<object([^>]*)>/gi, '<div class="mceItemObject" $1>');
64                 content = content.replace(/<embed([^>]*)>/gi, '<div class="mceItemObjectEmbed" $1>');
65                 content = content.replace(/<\/(object|embed)([^>]*)>/gi, '</div>');
66                 content = content.replace(/<param([^>]*)>/gi, '<div $1 class="mceItemParam"></div>');
67                 content = content.replace(new RegExp('\\/ class="mceItemParam"><\\/div>', 'gi'), 'class="mceItemParam"></div>');
68                 break;
69
70             case "insert_to_editor_dom":
71                 d = inst.getDoc();
72                 nl = content.getElementsByTagName("img");
73                 for (i=0; i<nl.length; i++) {
74                     if (/mceItem(Flash|ShockWave|WindowsMedia|QuickTime|RealMedia)/.test(nl[i].className)) {
75                         nl[i].width = nl[i].title.replace(/.*width:[^0-9]?([0-9]+)%?.*/g, '$1');
76                         nl[i].height = nl[i].title.replace(/.*height:[^0-9]?([0-9]+)%?.*/g, '$1');
77                         //nl[i].align = nl[i].title.replace(/.*align:([a-z]+).*/gi, '$1');
78                     }
79                 }
80
81                 nl = tinyMCE.selectElements(content, 'DIV', function (n) {return tinyMCE.hasCSSClass(n, 'mceItemObject');});
82                 for (i=0; i<nl.length; i++) {
83                     ci = tinyMCE.getAttrib(nl[i], "classid").toLowerCase().replace(/\s+/g, '');
84
85                     switch (ci) {
86                         case 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000':
87                             nl[i].parentNode.replaceChild(TinyMCE_MediaPlugin._createImg('mceItemFlash', d, nl[i]), nl[i]);
88                             break;
89
90                         case 'clsid:166b1bca-3f9c-11cf-8075-444553540000':
91                             nl[i].parentNode.replaceChild(TinyMCE_MediaPlugin._createImg('mceItemShockWave', d, nl[i]), nl[i]);
92                             break;
93
94                         case 'clsid:6bf52a52-394a-11d3-b153-00c04f79faa6':
95                             nl[i].parentNode.replaceChild(TinyMCE_MediaPlugin._createImg('mceItemWindowsMedia', d, nl[i]), nl[i]);
96                             break;
97
98                         case 'clsid:02bf25d5-8c17-4b23-bc80-d3488abddc6b':
99                             nl[i].parentNode.replaceChild(TinyMCE_MediaPlugin._createImg('mceItemQuickTime', d, nl[i]), nl[i]);
100                             break;
101
102                         case 'clsid:cfcdaa03-8be4-11cf-b84b-0020afbbccfa':
103                         case 'clsid:22d6f312-b0f6-11d0-94ab-0080c74c7e95':
104                         case 'clsid:05589fa1-c356-11ce-bf01-00aa0055595a':
105                             nl[i].parentNode.replaceChild(TinyMCE_MediaPlugin._createImg('mceItemRealMedia', d, nl[i]), nl[i]);
106                             break;
107                     }
108                 }
109
110                 // Handle embed (if any)
111                 nl = tinyMCE.selectNodes(content, function (n) {return n.className == 'mceItemObjectEmbed';});
112                 for (i=0; i<nl.length; i++) {
113                     switch (tinyMCE.getAttrib(nl[i], 'type')) {
114                         case 'application/x-shockwave-flash':
115                             TinyMCE_MediaPlugin._createImgFromEmbed(nl[i], d, 'mceItemFlash');
116                             break;
117
118                         case 'application/x-director':
119                             TinyMCE_MediaPlugin._createImgFromEmbed(nl[i], d, 'mceItemShockWave');
120                             break;
121
122                         case 'application/x-mplayer2':
123                             TinyMCE_MediaPlugin._createImgFromEmbed(nl[i], d, 'mceItemWindowsMedia');
124                             break;
125
126                         case 'video/quicktime':
127                             TinyMCE_MediaPlugin._createImgFromEmbed(nl[i], d, 'mceItemQuickTime');
128                             break;
129
130                         case 'audio/x-pn-realaudio-plugin':
131                             TinyMCE_MediaPlugin._createImgFromEmbed(nl[i], d, 'mceItemRealMedia');
132                             break;
133                     }
134                 }
135                 break;
136
137             case "get_from_editor":
138                 var startPos = -1, endPos, attribs, chunkBefore, chunkAfter, embedHTML, at, pl, cb, mt, ex;
139
140                 while ((startPos = content.indexOf('<img', startPos+1)) != -1) {
141                     endPos = content.indexOf('/>', startPos);
142                     attribs = TinyMCE_MediaPlugin._parseAttributes(content.substring(startPos + 4, endPos));
143
144                     // Is not flash, skip it
145                     if (!/mceItem(Flash|ShockWave|WindowsMedia|QuickTime|RealMedia)/.test(attribs['class']))
146                         continue;
147
148                     endPos += 2;
149
150                     // Parse attributes
151                     at = attribs['title'];
152                     if (at) {
153                         at = at.replace(/&#39;/g, "'");
154                         at = at.replace(/&#quot;/g, '"');
155
156                         try {
157                             pl = eval('x={' + at + '};');
158                         } catch (ex) {
159                             pl = {};
160                         }
161                     }
162
163                     // Use object/embed
164                     if (!tinyMCE.getParam('media_use_script', false)) {
165                         switch (attribs['class']) {
166                             case 'mceItemFlash':
167                                 ci = 'd27cdb6e-ae6d-11cf-96b8-444553540000';
168                                 cb = 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0';
169                                 mt = 'application/x-shockwave-flash';
170                                 break;
171
172                             case 'mceItemShockWave':
173                                 ci = '166B1BCA-3F9C-11CF-8075-444553540000';
174                                 cb = 'http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=8,5,1,0';
175                                 mt = 'application/x-director';
176                                 break;
177
178                             case 'mceItemWindowsMedia':
179                                 ci = tinyMCE.getParam('media_wmp6_compatible') ? '05589FA1-C356-11CE-BF01-00AA0055595A' : '6BF52A52-394A-11D3-B153-00C04F79FAA6';
180                                 cb = 'http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701';
181                                 mt = 'application/x-mplayer2';
182                                 break;
183
184                             case 'mceItemQuickTime':
185                                 ci = '02BF25D5-8C17-4B23-BC80-D3488ABDDC6B';
186                                 cb = 'http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0';
187                                 mt = 'video/quicktime';
188                                 break;
189
190                             case 'mceItemRealMedia':
191                                 ci = 'CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA';
192                                 cb = 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0';
193                                 mt = 'audio/x-pn-realaudio-plugin';
194                                 break;
195                         }
196
197                         // Force absolute URL
198                         if (!tinyMCE.getParam("relative_urls"))
199                             pl.src = tinyMCE.convertRelativeToAbsoluteURL(tinyMCE.settings['base_href'], pl.src);
200
201                         embedHTML = TinyMCE_MediaPlugin._getEmbed(ci, cb, mt, pl, attribs);
202                     } else {
203                         // Use script version
204                         switch (attribs['class']) {
205                             case 'mceItemFlash':
206                                 s = 'writeFlash';
207                                 break;
208
209                             case 'mceItemShockWave':
210                                 s = 'writeShockWave';
211                                 break;
212
213                             case 'mceItemWindowsMedia':
214                                 s = 'writeWindowsMedia';
215                                 break;
216
217                             case 'mceItemQuickTime':
218                                 s = 'writeQuickTime';
219                                 break;
220
221                             case 'mceItemRealMedia':
222                                 s = 'writeRealMedia';
223                                 break;
224                         }
225
226                         if (attribs.width)
227                             at = at.replace(/width:[^0-9]?[0-9]+%?[^0-9]?/g, "width:'" + attribs.width + "'");
228
229                         if (attribs.height)
230                             at = at.replace(/height:[^0-9]?[0-9]+%?[^0-9]?/g, "height:'" + attribs.height + "'");
231
232                         // Force absolute URL
233                         if (!tinyMCE.getParam("relative_urls")) {
234                             pl.src = tinyMCE.convertRelativeToAbsoluteURL(tinyMCE.settings['base_href'], pl.src);
235                             at = at.replace(new RegExp("src:'[^']*'", "g"), "src:'" + pl.src + "'");
236                         }
237
238                         embedHTML = '<script type="text/javascript">' + s + '({' + at + '});</script>';
239                     }
240
241                     // Insert embed/object chunk
242                     chunkBefore = content.substring(0, startPos);
243                     chunkAfter = content.substring(endPos);
244                     content = chunkBefore + embedHTML + chunkAfter;
245                 }
246                 break;
247         }
248
249         return content;
250     },
251
252     handleNodeChange : function(editor_id, node, undo_index, undo_levels, visual_aid, any_selection) {
253         if (node == null)
254             return;
255
256         do {
257             if (node.nodeName == "IMG" && /mceItem(Flash|ShockWave|WindowsMedia|QuickTime|RealMedia)/.test(tinyMCE.getAttrib(node, 'class'))) {
258                 tinyMCE.switchClass(editor_id + '_media', 'mceButtonSelected');
259                 return true;
260             }
261         } while ((node = node.parentNode));
262
263         tinyMCE.switchClass(editor_id + '_media', 'mceButtonNormal');
264
265         return true;
266     },
267
268     _createImgFromEmbed : function(n, d, cl) {
269         var ne, at, i, ti = '', an;
270
271         ne = d.createElement('img');
272         ne.src = tinyMCE.getParam("theme_href") + '/images/spacer.gif';
273         ne.width = tinyMCE.getAttrib(n, 'width');
274         ne.height = tinyMCE.getAttrib(n, 'height');
275         ne.className = cl;
276
277         at = n.attributes;
278         for (i=0; i<at.length; i++) {
279             if (at[i].specified && at[i].nodeValue) {
280                 an = at[i].nodeName.toLowerCase();
281
282                 if (an == 'src')
283                     continue;
284
285                 if (an == 'mce_src')
286                     an = 'src';
287
288                 if (an.indexOf('mce_') == -1 && !new RegExp('^(class|type)$').test(an))
289                     ti += an.toLowerCase() + ':\'' + at[i].nodeValue + "',";
290             }
291         }
292
293         ti = ti.length > 0 ? ti.substring(0, ti.length - 1) : ti;
294         ne.title = ti;
295
296         n.parentNode.replaceChild(ne, n);
297     },
298
299     _createImg : function(cl, d, n) {
300         var i, nl, ti = "", an, av, al = new Array();
301
302         ne = d.createElement('img');
303         ne.src = tinyMCE.getParam("theme_href") + '/images/spacer.gif';
304         ne.width = tinyMCE.getAttrib(n, 'width');
305         ne.height = tinyMCE.getAttrib(n, 'height');
306         ne.className = cl;
307
308         al.id = tinyMCE.getAttrib(n, 'id');
309         al.name = tinyMCE.getAttrib(n, 'name');
310         al.width = tinyMCE.getAttrib(n, 'width');
311         al.height = tinyMCE.getAttrib(n, 'height');
312         al.bgcolor = tinyMCE.getAttrib(n, 'bgcolor');
313         al.align = tinyMCE.getAttrib(n, 'align');
314         al.class_name = tinyMCE.getAttrib(n, 'mce_class');
315
316         nl = n.getElementsByTagName('div');
317         for (i=0; i<nl.length; i++) {
318             av = tinyMCE.getAttrib(nl[i], 'value');
319             av = av.replace(new RegExp('\\\\', 'g'), '\\\\');
320             av = av.replace(new RegExp('"', 'g'), '\\"');
321             av = av.replace(new RegExp("'", 'g'), "\\'");
322             an = tinyMCE.getAttrib(nl[i], 'name');
323             al[an] = av;
324         }
325
326         if (al.movie) {
327             al.src = al.movie;
328             al.movie = null;
329         }
330
331         for (an in al) {
332             if (al[an] != null && typeof(al[an]) != "function" && al[an] != '')
333                 ti += an.toLowerCase() + ':\'' + al[an] + "',";
334         }
335
336         ti = ti.length > 0 ? ti.substring(0, ti.length - 1) : ti;
337         ne.title = ti;
338
339         return ne;
340     },
341
342     _getEmbed : function(cls, cb, mt, p, at) {
343         var h = '', n;
344
345         p.width = at.width ? at.width : p.width;
346         p.height = at.height ? at.height : p.height;
347
348         h += '<object classid="clsid:' + cls + '" codebase="' + cb + '"';
349         h += typeof(p.id) != "undefined" ? ' id="' + p.id + '"' : '';
350         h += typeof(p.name) != "undefined" ? ' name="' + p.name + '"' : '';
351         h += typeof(p.width) != "undefined" ? ' width="' + p.width + '"' : '';
352         h += typeof(p.height) != "undefined" ? ' height="' + p.height + '"' : '';
353         h += typeof(p.align) != "undefined" ? ' align="' + p.align + '"' : '';
354         h += '>';
355
356         for (n in p) {
357             if (p[n] && typeof(p[n]) != "function") {
358                 h += '<param name="' + n + '" value="' + p[n] + '" />';
359
360                 // Add extra url parameter if it's an absolute URL on WMP
361                 if (n == 'src' && p[n].indexOf('://') != -1 && mt == 'application/x-mplayer2')
362                     h += '<param name="url" value="' + p[n] + '" />';
363             }
364         }
365
366         h += '<embed type="' + mt + '"';
367
368         for (n in p) {
369             if (typeof(p[n]) == "function")
370                 continue;
371
372             // Skip url parameter for embed tag on WMP
373             if (!(n == 'url' && mt == 'application/x-mplayer2'))
374                 h += ' ' + n + '="' + p[n] + '"';
375         }
376
377         h += '></embed></object>';
378
379         return h;
380     },
381
382     _parseAttributes : function(attribute_string) {
383         var attributeName = "";
384         var attributeValue = "";
385         var withInName;
386         var withInValue;
387         var attributes = new Array();
388         var whiteSpaceRegExp = new RegExp('^[ \n\r\t]+', 'g');
389
390         if (attribute_string == null || attribute_string.length < 2)
391             return null;
392
393         withInName = withInValue = false;
394
395         for (var i=0; i<attribute_string.length; i++) {
396             var chr = attribute_string.charAt(i);
397
398             if ((chr == '"' || chr == "'") && !withInValue)
399                 withInValue = true;
400             else if ((chr == '"' || chr == "'") && withInValue) {
401                 withInValue = false;
402
403                 var pos = attributeName.lastIndexOf(' ');
404                 if (pos != -1)
405                     attributeName = attributeName.substring(pos+1);
406
407                 attributes[attributeName.toLowerCase()] = attributeValue.substring(1);
408
409                 attributeName = "";
410                 attributeValue = "";
411             } else if (!whiteSpaceRegExp.test(chr) && !withInName && !withInValue)
412                 withInName = true;
413
414             if (chr == '=' && withInName)
415                 withInName = false;
416
417             if (withInName)
418                 attributeName += chr;
419
420             if (withInValue)
421                 attributeValue += chr;
422         }
423
424         return attributes;
425     }
426 };
427
428 tinyMCE.addPlugin("media", TinyMCE_MediaPlugin);