0
|
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 rootAttributes = tinymce.explode('id,name,width,height,style,align,class,hspace,vspace,bgcolor,type'), excludedAttrs = tinymce.makeMap(rootAttributes.join(',')), Node = tinymce.html.Node, |
|
13 |
mediaTypes, scriptRegExp, JSON = tinymce.util.JSON, mimeTypes; |
|
14 |
|
|
15 |
// Media types supported by this plugin |
|
16 |
mediaTypes = [ |
|
17 |
// Type, clsid:s, mime types, codebase |
|
18 |
["Flash", "d27cdb6e-ae6d-11cf-96b8-444553540000", "application/x-shockwave-flash", "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"], |
|
19 |
["ShockWave", "166b1bca-3f9c-11cf-8075-444553540000", "application/x-director", "http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=8,5,1,0"], |
|
20 |
["WindowsMedia", "6bf52a52-394a-11d3-b153-00c04f79faa6,22d6f312-b0f6-11d0-94ab-0080c74c7e95,05589fa1-c356-11ce-bf01-00aa0055595a", "application/x-mplayer2", "http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701"], |
|
21 |
["QuickTime", "02bf25d5-8c17-4b23-bc80-d3488abddc6b", "video/quicktime", "http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0"], |
|
22 |
["RealMedia", "cfcdaa03-8be4-11cf-b84b-0020afbbccfa", "audio/x-pn-realaudio-plugin", "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"], |
|
23 |
["Java", "8ad9c840-044e-11d1-b3e9-00805f499d93", "application/x-java-applet", "http://java.sun.com/products/plugin/autodl/jinstall-1_5_0-windows-i586.cab#Version=1,5,0,0"], |
|
24 |
["Silverlight", "dfeaf541-f3e1-4c24-acac-99c30715084a", "application/x-silverlight-2"], |
|
25 |
["Iframe"], |
|
26 |
["Video"], |
|
27 |
["EmbeddedAudio"], |
|
28 |
["Audio"] |
|
29 |
]; |
|
30 |
|
|
31 |
function normalizeSize(size) { |
|
32 |
return typeof(size) == "string" ? size.replace(/[^0-9%]/g, '') : size; |
|
33 |
} |
|
34 |
|
|
35 |
function toArray(obj) { |
|
36 |
var undef, out, i; |
|
37 |
|
|
38 |
if (obj && !obj.splice) { |
|
39 |
out = []; |
|
40 |
|
|
41 |
for (i = 0; true; i++) { |
|
42 |
if (obj[i]) |
|
43 |
out[i] = obj[i]; |
|
44 |
else |
|
45 |
break; |
|
46 |
} |
|
47 |
|
|
48 |
return out; |
|
49 |
} |
|
50 |
|
|
51 |
return obj; |
|
52 |
}; |
|
53 |
|
|
54 |
tinymce.create('tinymce.plugins.MediaPlugin', { |
|
55 |
init : function(ed, url) { |
|
56 |
var self = this, lookup = {}, i, y, item, name; |
|
57 |
|
|
58 |
function isMediaImg(node) { |
|
59 |
return node && node.nodeName === 'IMG' && ed.dom.hasClass(node, 'mceItemMedia'); |
|
60 |
}; |
|
61 |
|
|
62 |
self.editor = ed; |
|
63 |
self.url = url; |
|
64 |
|
|
65 |
// Parse media types into a lookup table |
|
66 |
scriptRegExp = ''; |
|
67 |
for (i = 0; i < mediaTypes.length; i++) { |
|
68 |
name = mediaTypes[i][0]; |
|
69 |
|
|
70 |
item = { |
|
71 |
name : name, |
|
72 |
clsids : tinymce.explode(mediaTypes[i][1] || ''), |
|
73 |
mimes : tinymce.explode(mediaTypes[i][2] || ''), |
|
74 |
codebase : mediaTypes[i][3] |
|
75 |
}; |
|
76 |
|
|
77 |
for (y = 0; y < item.clsids.length; y++) |
|
78 |
lookup['clsid:' + item.clsids[y]] = item; |
|
79 |
|
|
80 |
for (y = 0; y < item.mimes.length; y++) |
|
81 |
lookup[item.mimes[y]] = item; |
|
82 |
|
|
83 |
lookup['mceItem' + name] = item; |
|
84 |
lookup[name.toLowerCase()] = item; |
|
85 |
|
|
86 |
scriptRegExp += (scriptRegExp ? '|' : '') + name; |
|
87 |
} |
|
88 |
|
|
89 |
// Handle the media_types setting |
|
90 |
tinymce.each(ed.getParam("media_types", |
|
91 |
"video=mp4,m4v,ogv,webm;" + |
|
92 |
"silverlight=xap;" + |
|
93 |
"flash=swf,flv;" + |
|
94 |
"shockwave=dcr;" + |
|
95 |
"quicktime=mov,qt,mpg,mpeg;" + |
|
96 |
"shockwave=dcr;" + |
|
97 |
"windowsmedia=avi,wmv,wm,asf,asx,wmx,wvx;" + |
|
98 |
"realmedia=rm,ra,ram;" + |
|
99 |
"java=jar;" + |
|
100 |
"audio=mp3,ogg" |
|
101 |
).split(';'), function(item) { |
|
102 |
var i, extensions, type; |
|
103 |
|
|
104 |
item = item.split(/=/); |
|
105 |
extensions = tinymce.explode(item[1].toLowerCase()); |
|
106 |
for (i = 0; i < extensions.length; i++) { |
|
107 |
type = lookup[item[0].toLowerCase()]; |
|
108 |
|
|
109 |
if (type) |
|
110 |
lookup[extensions[i]] = type; |
|
111 |
} |
|
112 |
}); |
|
113 |
|
|
114 |
scriptRegExp = new RegExp('write(' + scriptRegExp + ')\\(([^)]+)\\)'); |
|
115 |
self.lookup = lookup; |
|
116 |
|
|
117 |
ed.onPreInit.add(function() { |
|
118 |
// Allow video elements |
|
119 |
ed.schema.addValidElements('object[id|style|width|height|classid|codebase|*],param[name|value],embed[id|style|width|height|type|src|*],video[*],audio[*],source[*]'); |
|
120 |
|
|
121 |
// Convert video elements to image placeholder |
|
122 |
ed.parser.addNodeFilter('object,embed,video,audio,script,iframe', function(nodes) { |
|
123 |
var i = nodes.length; |
|
124 |
|
|
125 |
while (i--) |
|
126 |
self.objectToImg(nodes[i]); |
|
127 |
}); |
|
128 |
|
|
129 |
// Convert image placeholders to video elements |
|
130 |
ed.serializer.addNodeFilter('img', function(nodes, name, args) { |
|
131 |
var i = nodes.length, node; |
|
132 |
|
|
133 |
while (i--) { |
|
134 |
node = nodes[i]; |
|
135 |
if ((node.attr('class') || '').indexOf('mceItemMedia') !== -1) |
|
136 |
self.imgToObject(node, args); |
|
137 |
} |
|
138 |
}); |
|
139 |
}); |
|
140 |
|
|
141 |
ed.onInit.add(function() { |
|
142 |
// Display "media" instead of "img" in element path |
|
143 |
if (ed.theme && ed.theme.onResolveName) { |
|
144 |
ed.theme.onResolveName.add(function(theme, path_object) { |
|
145 |
if (path_object.name === 'img' && ed.dom.hasClass(path_object.node, 'mceItemMedia')) |
|
146 |
path_object.name = 'media'; |
|
147 |
}); |
|
148 |
} |
|
149 |
|
|
150 |
// Add contect menu if it's loaded |
|
151 |
if (ed && ed.plugins.contextmenu) { |
|
152 |
ed.plugins.contextmenu.onContextMenu.add(function(plugin, menu, element) { |
|
153 |
if (element.nodeName === 'IMG' && element.className.indexOf('mceItemMedia') !== -1) |
|
154 |
menu.add({title : 'media.edit', icon : 'media', cmd : 'mceMedia'}); |
|
155 |
}); |
|
156 |
} |
|
157 |
}); |
|
158 |
|
|
159 |
// Register commands |
|
160 |
ed.addCommand('mceMedia', function() { |
|
161 |
var data, img; |
|
162 |
|
|
163 |
img = ed.selection.getNode(); |
|
164 |
if (isMediaImg(img)) { |
|
165 |
data = ed.dom.getAttrib(img, 'data-mce-json'); |
|
166 |
if (data) { |
|
167 |
data = JSON.parse(data); |
|
168 |
|
|
169 |
// Add some extra properties to the data object |
|
170 |
tinymce.each(rootAttributes, function(name) { |
|
171 |
var value = ed.dom.getAttrib(img, name); |
|
172 |
|
|
173 |
if (value) |
|
174 |
data[name] = value; |
|
175 |
}); |
|
176 |
|
|
177 |
data.type = self.getType(img.className).name.toLowerCase(); |
|
178 |
} |
|
179 |
} |
|
180 |
|
|
181 |
if (!data) { |
|
182 |
data = { |
|
183 |
type : 'flash', |
|
184 |
video: {sources:[]}, |
|
185 |
params: {} |
|
186 |
}; |
|
187 |
} |
|
188 |
|
|
189 |
ed.windowManager.open({ |
|
190 |
file : url + '/media.htm', |
|
191 |
width : 430 + parseInt(ed.getLang('media.delta_width', 0)), |
|
192 |
height : 500 + parseInt(ed.getLang('media.delta_height', 0)), |
|
193 |
inline : 1 |
|
194 |
}, { |
|
195 |
plugin_url : url, |
|
196 |
data : data |
|
197 |
}); |
|
198 |
}); |
|
199 |
|
|
200 |
// Register buttons |
|
201 |
ed.addButton('media', {title : 'media.desc', cmd : 'mceMedia'}); |
|
202 |
|
|
203 |
// Update media selection status |
|
204 |
ed.onNodeChange.add(function(ed, cm, node) { |
|
205 |
cm.setActive('media', isMediaImg(node)); |
|
206 |
}); |
|
207 |
}, |
|
208 |
|
|
209 |
convertUrl : function(url, force_absolute) { |
|
210 |
var self = this, editor = self.editor, settings = editor.settings, |
|
211 |
urlConverter = settings.url_converter, |
|
212 |
urlConverterScope = settings.url_converter_scope || self; |
|
213 |
|
|
214 |
if (!url) |
|
215 |
return url; |
|
216 |
|
|
217 |
if (force_absolute) |
|
218 |
return editor.documentBaseURI.toAbsolute(url); |
|
219 |
|
|
220 |
return urlConverter.call(urlConverterScope, url, 'src', 'object'); |
|
221 |
}, |
|
222 |
|
|
223 |
getInfo : function() { |
|
224 |
return { |
|
225 |
longname : 'Media', |
|
226 |
author : 'Moxiecode Systems AB', |
|
227 |
authorurl : 'http://tinymce.moxiecode.com', |
|
228 |
infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/media', |
|
229 |
version : tinymce.majorVersion + "." + tinymce.minorVersion |
|
230 |
}; |
|
231 |
}, |
|
232 |
|
|
233 |
/** |
|
234 |
* Converts the JSON data object to an img node. |
|
235 |
*/ |
|
236 |
dataToImg : function(data, force_absolute) { |
|
237 |
var self = this, editor = self.editor, baseUri = editor.documentBaseURI, sources, attrs, img, i; |
|
238 |
|
|
239 |
data.params.src = self.convertUrl(data.params.src, force_absolute); |
|
240 |
|
|
241 |
attrs = data.video.attrs; |
|
242 |
if (attrs) |
|
243 |
attrs.src = self.convertUrl(attrs.src, force_absolute); |
|
244 |
|
|
245 |
if (attrs) |
|
246 |
attrs.poster = self.convertUrl(attrs.poster, force_absolute); |
|
247 |
|
|
248 |
sources = toArray(data.video.sources); |
|
249 |
if (sources) { |
|
250 |
for (i = 0; i < sources.length; i++) |
|
251 |
sources[i].src = self.convertUrl(sources[i].src, force_absolute); |
|
252 |
} |
|
253 |
|
|
254 |
img = self.editor.dom.create('img', { |
|
255 |
id : data.id, |
|
256 |
style : data.style, |
|
257 |
align : data.align, |
|
258 |
hspace : data.hspace, |
|
259 |
vspace : data.vspace, |
|
260 |
src : self.editor.theme.url + '/img/trans.gif', |
|
261 |
'class' : 'mceItemMedia mceItem' + self.getType(data.type).name, |
|
262 |
'data-mce-json' : JSON.serialize(data, "'") |
|
263 |
}); |
|
264 |
|
|
265 |
img.width = data.width = normalizeSize(data.width || (data.type == 'audio' ? "300" : "320")); |
|
266 |
img.height = data.height = normalizeSize(data.height || (data.type == 'audio' ? "32" : "240")); |
|
267 |
|
|
268 |
return img; |
|
269 |
}, |
|
270 |
|
|
271 |
/** |
|
272 |
* Converts the JSON data object to a HTML string. |
|
273 |
*/ |
|
274 |
dataToHtml : function(data, force_absolute) { |
|
275 |
return this.editor.serializer.serialize(this.dataToImg(data, force_absolute), {forced_root_block : '', force_absolute : force_absolute}); |
|
276 |
}, |
|
277 |
|
|
278 |
/** |
|
279 |
* Converts the JSON data object to a HTML string. |
|
280 |
*/ |
|
281 |
htmlToData : function(html) { |
|
282 |
var fragment, img, data; |
|
283 |
|
|
284 |
data = { |
|
285 |
type : 'flash', |
|
286 |
video: {sources:[]}, |
|
287 |
params: {} |
|
288 |
}; |
|
289 |
|
|
290 |
fragment = this.editor.parser.parse(html); |
|
291 |
img = fragment.getAll('img')[0]; |
|
292 |
|
|
293 |
if (img) { |
|
294 |
data = JSON.parse(img.attr('data-mce-json')); |
|
295 |
data.type = this.getType(img.attr('class')).name.toLowerCase(); |
|
296 |
|
|
297 |
// Add some extra properties to the data object |
|
298 |
tinymce.each(rootAttributes, function(name) { |
|
299 |
var value = img.attr(name); |
|
300 |
|
|
301 |
if (value) |
|
302 |
data[name] = value; |
|
303 |
}); |
|
304 |
} |
|
305 |
|
|
306 |
return data; |
|
307 |
}, |
|
308 |
|
|
309 |
/** |
|
310 |
* Get type item by extension, class, clsid or mime type. |
|
311 |
* |
|
312 |
* @method getType |
|
313 |
* @param {String} value Value to get type item by. |
|
314 |
* @return {Object} Type item object or undefined. |
|
315 |
*/ |
|
316 |
getType : function(value) { |
|
317 |
var i, values, typeItem; |
|
318 |
|
|
319 |
// Find type by checking the classes |
|
320 |
values = tinymce.explode(value, ' '); |
|
321 |
for (i = 0; i < values.length; i++) { |
|
322 |
typeItem = this.lookup[values[i]]; |
|
323 |
|
|
324 |
if (typeItem) |
|
325 |
return typeItem; |
|
326 |
} |
|
327 |
}, |
|
328 |
|
|
329 |
/** |
|
330 |
* Converts a tinymce.html.Node image element to video/object/embed. |
|
331 |
*/ |
|
332 |
imgToObject : function(node, args) { |
|
333 |
var self = this, editor = self.editor, video, object, embed, iframe, name, value, data, |
|
334 |
source, sources, params, param, typeItem, i, item, mp4Source, replacement, |
|
335 |
posterSrc, style, audio; |
|
336 |
|
|
337 |
// Adds the flash player |
|
338 |
function addPlayer(video_src, poster_src) { |
|
339 |
var baseUri, flashVars, flashVarsOutput, params, flashPlayer; |
|
340 |
|
|
341 |
flashPlayer = editor.getParam('flash_video_player_url', self.convertUrl(self.url + '/moxieplayer.swf')); |
|
342 |
if (flashPlayer) { |
|
343 |
baseUri = editor.documentBaseURI; |
|
344 |
data.params.src = flashPlayer; |
|
345 |
|
|
346 |
// Convert the movie url to absolute urls |
|
347 |
if (editor.getParam('flash_video_player_absvideourl', true)) { |
|
348 |
video_src = baseUri.toAbsolute(video_src || '', true); |
|
349 |
poster_src = baseUri.toAbsolute(poster_src || '', true); |
|
350 |
} |
|
351 |
|
|
352 |
// Generate flash vars |
|
353 |
flashVarsOutput = ''; |
|
354 |
flashVars = editor.getParam('flash_video_player_flashvars', {url : '$url', poster : '$poster'}); |
|
355 |
tinymce.each(flashVars, function(value, name) { |
|
356 |
// Replace $url and $poster variables in flashvars value |
|
357 |
value = value.replace(/\$url/, video_src || ''); |
|
358 |
value = value.replace(/\$poster/, poster_src || ''); |
|
359 |
|
|
360 |
if (value.length > 0) |
|
361 |
flashVarsOutput += (flashVarsOutput ? '&' : '') + name + '=' + escape(value); |
|
362 |
}); |
|
363 |
|
|
364 |
if (flashVarsOutput.length) |
|
365 |
data.params.flashvars = flashVarsOutput; |
|
366 |
|
|
367 |
params = editor.getParam('flash_video_player_params', { |
|
368 |
allowfullscreen: true, |
|
369 |
allowscriptaccess: true |
|
370 |
}); |
|
371 |
|
|
372 |
tinymce.each(params, function(value, name) { |
|
373 |
data.params[name] = "" + value; |
|
374 |
}); |
|
375 |
} |
|
376 |
}; |
|
377 |
|
|
378 |
data = node.attr('data-mce-json'); |
|
379 |
if (!data) |
|
380 |
return; |
|
381 |
|
|
382 |
data = JSON.parse(data); |
|
383 |
typeItem = this.getType(node.attr('class')); |
|
384 |
|
|
385 |
style = node.attr('data-mce-style'); |
|
386 |
if (!style) { |
|
387 |
style = node.attr('style'); |
|
388 |
|
|
389 |
if (style) |
|
390 |
style = editor.dom.serializeStyle(editor.dom.parseStyle(style, 'img')); |
|
391 |
} |
|
392 |
|
|
393 |
// Use node width/height to override the data width/height when the placeholder is resized |
|
394 |
data.width = node.attr('width') || data.width; |
|
395 |
data.height = node.attr('height') || data.height; |
|
396 |
|
|
397 |
// Handle iframe |
|
398 |
if (typeItem.name === 'Iframe') { |
|
399 |
replacement = new Node('iframe', 1); |
|
400 |
|
|
401 |
tinymce.each(rootAttributes, function(name) { |
|
402 |
var value = node.attr(name); |
|
403 |
|
|
404 |
if (name == 'class' && value) |
|
405 |
value = value.replace(/mceItem.+ ?/g, ''); |
|
406 |
|
|
407 |
if (value && value.length > 0) |
|
408 |
replacement.attr(name, value); |
|
409 |
}); |
|
410 |
|
|
411 |
for (name in data.params) |
|
412 |
replacement.attr(name, data.params[name]); |
|
413 |
|
|
414 |
replacement.attr({ |
|
415 |
style: style, |
|
416 |
src: data.params.src |
|
417 |
}); |
|
418 |
|
|
419 |
node.replace(replacement); |
|
420 |
|
|
421 |
return; |
|
422 |
} |
|
423 |
|
|
424 |
// Handle scripts |
|
425 |
if (this.editor.settings.media_use_script) { |
|
426 |
replacement = new Node('script', 1).attr('type', 'text/javascript'); |
|
427 |
|
|
428 |
value = new Node('#text', 3); |
|
429 |
value.value = 'write' + typeItem.name + '(' + JSON.serialize(tinymce.extend(data.params, { |
|
430 |
width: node.attr('width'), |
|
431 |
height: node.attr('height') |
|
432 |
})) + ');'; |
|
433 |
|
|
434 |
replacement.append(value); |
|
435 |
node.replace(replacement); |
|
436 |
|
|
437 |
return; |
|
438 |
} |
|
439 |
|
|
440 |
// Add HTML5 video element |
|
441 |
if (typeItem.name === 'Video' && data.video.sources && data.video.sources[0]) { |
|
442 |
// Create new object element |
|
443 |
video = new Node('video', 1).attr(tinymce.extend({ |
|
444 |
id : node.attr('id'), |
|
445 |
width: normalizeSize(node.attr('width')), |
|
446 |
height: normalizeSize(node.attr('height')), |
|
447 |
style : style |
|
448 |
}, data.video.attrs)); |
|
449 |
|
|
450 |
// Get poster source and use that for flash fallback |
|
451 |
if (data.video.attrs) |
|
452 |
posterSrc = data.video.attrs.poster; |
|
453 |
|
|
454 |
sources = data.video.sources = toArray(data.video.sources); |
|
455 |
for (i = 0; i < sources.length; i++) { |
|
456 |
if (/\.mp4$/.test(sources[i].src)) |
|
457 |
mp4Source = sources[i].src; |
|
458 |
} |
|
459 |
|
|
460 |
if (!sources[0].type) { |
|
461 |
video.attr('src', sources[0].src); |
|
462 |
sources.splice(0, 1); |
|
463 |
} |
|
464 |
|
|
465 |
for (i = 0; i < sources.length; i++) { |
|
466 |
source = new Node('source', 1).attr(sources[i]); |
|
467 |
source.shortEnded = true; |
|
468 |
video.append(source); |
|
469 |
} |
|
470 |
|
|
471 |
// Create flash fallback for video if we have a mp4 source |
|
472 |
if (mp4Source) { |
|
473 |
addPlayer(mp4Source, posterSrc); |
|
474 |
typeItem = self.getType('flash'); |
|
475 |
} else |
|
476 |
data.params.src = ''; |
|
477 |
} |
|
478 |
|
|
479 |
// Add HTML5 audio element |
|
480 |
if (typeItem.name === 'Audio' && data.video.sources && data.video.sources[0]) { |
|
481 |
// Create new object element |
|
482 |
audio = new Node('audio', 1).attr(tinymce.extend({ |
|
483 |
id : node.attr('id'), |
|
484 |
width: normalizeSize(node.attr('width')), |
|
485 |
height: normalizeSize(node.attr('height')), |
|
486 |
style : style |
|
487 |
}, data.video.attrs)); |
|
488 |
|
|
489 |
// Get poster source and use that for flash fallback |
|
490 |
if (data.video.attrs) |
|
491 |
posterSrc = data.video.attrs.poster; |
|
492 |
|
|
493 |
sources = data.video.sources = toArray(data.video.sources); |
|
494 |
if (!sources[0].type) { |
|
495 |
audio.attr('src', sources[0].src); |
|
496 |
sources.splice(0, 1); |
|
497 |
} |
|
498 |
|
|
499 |
for (i = 0; i < sources.length; i++) { |
|
500 |
source = new Node('source', 1).attr(sources[i]); |
|
501 |
source.shortEnded = true; |
|
502 |
audio.append(source); |
|
503 |
} |
|
504 |
|
|
505 |
data.params.src = ''; |
|
506 |
} |
|
507 |
|
|
508 |
if (typeItem.name === 'EmbeddedAudio') { |
|
509 |
embed = new Node('embed', 1); |
|
510 |
embed.shortEnded = true; |
|
511 |
embed.attr({ |
|
512 |
id: node.attr('id'), |
|
513 |
width: normalizeSize(node.attr('width')), |
|
514 |
height: normalizeSize(node.attr('height')), |
|
515 |
style : style, |
|
516 |
type: node.attr('type') |
|
517 |
}); |
|
518 |
|
|
519 |
for (name in data.params) |
|
520 |
embed.attr(name, data.params[name]); |
|
521 |
|
|
522 |
tinymce.each(rootAttributes, function(name) { |
|
523 |
if (data[name] && name != 'type') |
|
524 |
embed.attr(name, data[name]); |
|
525 |
}); |
|
526 |
|
|
527 |
data.params.src = ''; |
|
528 |
} |
|
529 |
|
|
530 |
// Do we have a params src then we can generate object |
|
531 |
if (data.params.src) { |
|
532 |
// Is flv movie add player for it |
|
533 |
if (/\.flv$/i.test(data.params.src)) |
|
534 |
addPlayer(data.params.src, ''); |
|
535 |
|
|
536 |
if (args && args.force_absolute) |
|
537 |
data.params.src = editor.documentBaseURI.toAbsolute(data.params.src); |
|
538 |
|
|
539 |
// Create new object element |
|
540 |
object = new Node('object', 1).attr({ |
|
541 |
id : node.attr('id'), |
|
542 |
width: normalizeSize(node.attr('width')), |
|
543 |
height: normalizeSize(node.attr('height')), |
|
544 |
style : style |
|
545 |
}); |
|
546 |
|
|
547 |
tinymce.each(rootAttributes, function(name) { |
|
548 |
var value = data[name]; |
|
549 |
|
|
550 |
if (name == 'class' && value) |
|
551 |
value = value.replace(/mceItem.+ ?/g, ''); |
|
552 |
|
|
553 |
if (value && name != 'type') |
|
554 |
object.attr(name, value); |
|
555 |
}); |
|
556 |
|
|
557 |
// Add params |
|
558 |
for (name in data.params) { |
|
559 |
param = new Node('param', 1); |
|
560 |
param.shortEnded = true; |
|
561 |
value = data.params[name]; |
|
562 |
|
|
563 |
// Windows media needs to use url instead of src for the media URL |
|
564 |
if (name === 'src' && typeItem.name === 'WindowsMedia') |
|
565 |
name = 'url'; |
|
566 |
|
|
567 |
param.attr({name: name, value: value}); |
|
568 |
object.append(param); |
|
569 |
} |
|
570 |
|
|
571 |
// Setup add type and classid if strict is disabled |
|
572 |
if (this.editor.getParam('media_strict', true)) { |
|
573 |
object.attr({ |
|
574 |
data: data.params.src, |
|
575 |
type: typeItem.mimes[0] |
|
576 |
}); |
|
577 |
} else { |
|
578 |
if ( typeItem.clsids ) |
|
579 |
object.attr('clsid', typeItem.clsids[0]); |
|
580 |
object.attr('codebase', typeItem.codebase); |
|
581 |
|
|
582 |
embed = new Node('embed', 1); |
|
583 |
embed.shortEnded = true; |
|
584 |
embed.attr({ |
|
585 |
id: node.attr('id'), |
|
586 |
width: normalizeSize(node.attr('width')), |
|
587 |
height: normalizeSize(node.attr('height')), |
|
588 |
style : style, |
|
589 |
type: typeItem.mimes[0] |
|
590 |
}); |
|
591 |
|
|
592 |
for (name in data.params) |
|
593 |
embed.attr(name, data.params[name]); |
|
594 |
|
|
595 |
tinymce.each(rootAttributes, function(name) { |
|
596 |
if (data[name] && name != 'type') |
|
597 |
embed.attr(name, data[name]); |
|
598 |
}); |
|
599 |
|
|
600 |
object.append(embed); |
|
601 |
} |
|
602 |
|
|
603 |
// Insert raw HTML |
|
604 |
if (data.object_html) { |
|
605 |
value = new Node('#text', 3); |
|
606 |
value.raw = true; |
|
607 |
value.value = data.object_html; |
|
608 |
object.append(value); |
|
609 |
} |
|
610 |
|
|
611 |
// Append object to video element if it exists |
|
612 |
if (video) |
|
613 |
video.append(object); |
|
614 |
} |
|
615 |
|
|
616 |
if (video) { |
|
617 |
// Insert raw HTML |
|
618 |
if (data.video_html) { |
|
619 |
value = new Node('#text', 3); |
|
620 |
value.raw = true; |
|
621 |
value.value = data.video_html; |
|
622 |
video.append(value); |
|
623 |
} |
|
624 |
} |
|
625 |
|
|
626 |
if (audio) { |
|
627 |
// Insert raw HTML |
|
628 |
if (data.video_html) { |
|
629 |
value = new Node('#text', 3); |
|
630 |
value.raw = true; |
|
631 |
value.value = data.video_html; |
|
632 |
audio.append(value); |
|
633 |
} |
|
634 |
} |
|
635 |
|
|
636 |
var n = video || audio || object || embed; |
|
637 |
if (n) |
|
638 |
node.replace(n); |
|
639 |
else |
|
640 |
node.remove(); |
|
641 |
}, |
|
642 |
|
|
643 |
/** |
|
644 |
* Converts a tinymce.html.Node video/object/embed to an img element. |
|
645 |
* |
|
646 |
* The video/object/embed will be converted into an image placeholder with a JSON data attribute like this: |
|
647 |
* <img class="mceItemMedia mceItemFlash" width="100" height="100" data-mce-json="{..}" /> |
|
648 |
* |
|
649 |
* The JSON structure will be like this: |
|
650 |
* {'params':{'flashvars':'something','quality':'high','src':'someurl'}, 'video':{'sources':[{src: 'someurl', type: 'video/mp4'}]}} |
|
651 |
*/ |
|
652 |
objectToImg : function(node) { |
|
653 |
var object, embed, video, iframe, img, name, id, width, height, style, i, html, |
|
654 |
param, params, source, sources, data, type, lookup = this.lookup, |
|
655 |
matches, attrs, urlConverter = this.editor.settings.url_converter, |
|
656 |
urlConverterScope = this.editor.settings.url_converter_scope, |
|
657 |
hspace, vspace, align, bgcolor; |
|
658 |
|
|
659 |
function getInnerHTML(node) { |
|
660 |
return new tinymce.html.Serializer({ |
|
661 |
inner: true, |
|
662 |
validate: false |
|
663 |
}).serialize(node); |
|
664 |
}; |
|
665 |
|
|
666 |
function lookupAttribute(o, attr) { |
|
667 |
return lookup[(o.attr(attr) || '').toLowerCase()]; |
|
668 |
} |
|
669 |
|
|
670 |
function lookupExtension(src) { |
|
671 |
var ext = src.replace(/^.*\.([^.]+)$/, '$1'); |
|
672 |
return lookup[ext.toLowerCase() || '']; |
|
673 |
} |
|
674 |
|
|
675 |
// If node isn't in document |
|
676 |
if (!node.parent) |
|
677 |
return; |
|
678 |
|
|
679 |
// Handle media scripts |
|
680 |
if (node.name === 'script') { |
|
681 |
if (node.firstChild) |
|
682 |
matches = scriptRegExp.exec(node.firstChild.value); |
|
683 |
|
|
684 |
if (!matches) |
|
685 |
return; |
|
686 |
|
|
687 |
type = matches[1]; |
|
688 |
data = {video : {}, params : JSON.parse(matches[2])}; |
|
689 |
width = data.params.width; |
|
690 |
height = data.params.height; |
|
691 |
} |
|
692 |
|
|
693 |
// Setup data objects |
|
694 |
data = data || { |
|
695 |
video : {}, |
|
696 |
params : {} |
|
697 |
}; |
|
698 |
|
|
699 |
// Setup new image object |
|
700 |
img = new Node('img', 1); |
|
701 |
img.attr({ |
|
702 |
src : this.editor.theme.url + '/img/trans.gif' |
|
703 |
}); |
|
704 |
|
|
705 |
// Video element |
|
706 |
name = node.name; |
|
707 |
if (name === 'video' || name == 'audio') { |
|
708 |
video = node; |
|
709 |
object = node.getAll('object')[0]; |
|
710 |
embed = node.getAll('embed')[0]; |
|
711 |
width = video.attr('width'); |
|
712 |
height = video.attr('height'); |
|
713 |
id = video.attr('id'); |
|
714 |
data.video = {attrs : {}, sources : []}; |
|
715 |
|
|
716 |
// Get all video attributes |
|
717 |
attrs = data.video.attrs; |
|
718 |
for (name in video.attributes.map) |
|
719 |
attrs[name] = video.attributes.map[name]; |
|
720 |
|
|
721 |
source = node.attr('src'); |
|
722 |
if (source) |
|
723 |
data.video.sources.push({src : urlConverter.call(urlConverterScope, source, 'src', node.name)}); |
|
724 |
|
|
725 |
// Get all sources |
|
726 |
sources = video.getAll("source"); |
|
727 |
for (i = 0; i < sources.length; i++) { |
|
728 |
source = sources[i].remove(); |
|
729 |
|
|
730 |
data.video.sources.push({ |
|
731 |
src: urlConverter.call(urlConverterScope, source.attr('src'), 'src', 'source'), |
|
732 |
type: source.attr('type'), |
|
733 |
media: source.attr('media') |
|
734 |
}); |
|
735 |
} |
|
736 |
|
|
737 |
// Convert the poster URL |
|
738 |
if (attrs.poster) |
|
739 |
attrs.poster = urlConverter.call(urlConverterScope, attrs.poster, 'poster', node.name); |
|
740 |
} |
|
741 |
|
|
742 |
// Object element |
|
743 |
if (node.name === 'object') { |
|
744 |
object = node; |
|
745 |
embed = node.getAll('embed')[0]; |
|
746 |
} |
|
747 |
|
|
748 |
// Embed element |
|
749 |
if (node.name === 'embed') |
|
750 |
embed = node; |
|
751 |
|
|
752 |
// Iframe element |
|
753 |
if (node.name === 'iframe') { |
|
754 |
iframe = node; |
|
755 |
type = 'Iframe'; |
|
756 |
} |
|
757 |
|
|
758 |
if (object) { |
|
759 |
// Get width/height |
|
760 |
width = width || object.attr('width'); |
|
761 |
height = height || object.attr('height'); |
|
762 |
style = style || object.attr('style'); |
|
763 |
id = id || object.attr('id'); |
|
764 |
hspace = hspace || object.attr('hspace'); |
|
765 |
vspace = vspace || object.attr('vspace'); |
|
766 |
align = align || object.attr('align'); |
|
767 |
bgcolor = bgcolor || object.attr('bgcolor'); |
|
768 |
data.name = object.attr('name'); |
|
769 |
|
|
770 |
// Get all object params |
|
771 |
params = object.getAll("param"); |
|
772 |
for (i = 0; i < params.length; i++) { |
|
773 |
param = params[i]; |
|
774 |
name = param.remove().attr('name'); |
|
775 |
|
|
776 |
if (!excludedAttrs[name]) |
|
777 |
data.params[name] = param.attr('value'); |
|
778 |
} |
|
779 |
|
|
780 |
data.params.src = data.params.src || object.attr('data'); |
|
781 |
} |
|
782 |
|
|
783 |
if (embed) { |
|
784 |
// Get width/height |
|
785 |
width = width || embed.attr('width'); |
|
786 |
height = height || embed.attr('height'); |
|
787 |
style = style || embed.attr('style'); |
|
788 |
id = id || embed.attr('id'); |
|
789 |
hspace = hspace || embed.attr('hspace'); |
|
790 |
vspace = vspace || embed.attr('vspace'); |
|
791 |
align = align || embed.attr('align'); |
|
792 |
bgcolor = bgcolor || embed.attr('bgcolor'); |
|
793 |
|
|
794 |
// Get all embed attributes |
|
795 |
for (name in embed.attributes.map) { |
|
796 |
if (!excludedAttrs[name] && !data.params[name]) |
|
797 |
data.params[name] = embed.attributes.map[name]; |
|
798 |
} |
|
799 |
} |
|
800 |
|
|
801 |
if (iframe) { |
|
802 |
// Get width/height |
|
803 |
width = normalizeSize(iframe.attr('width')); |
|
804 |
height = normalizeSize(iframe.attr('height')); |
|
805 |
style = style || iframe.attr('style'); |
|
806 |
id = iframe.attr('id'); |
|
807 |
hspace = iframe.attr('hspace'); |
|
808 |
vspace = iframe.attr('vspace'); |
|
809 |
align = iframe.attr('align'); |
|
810 |
bgcolor = iframe.attr('bgcolor'); |
|
811 |
|
|
812 |
tinymce.each(rootAttributes, function(name) { |
|
813 |
img.attr(name, iframe.attr(name)); |
|
814 |
}); |
|
815 |
|
|
816 |
// Get all iframe attributes |
|
817 |
for (name in iframe.attributes.map) { |
|
818 |
if (!excludedAttrs[name] && !data.params[name]) |
|
819 |
data.params[name] = iframe.attributes.map[name]; |
|
820 |
} |
|
821 |
} |
|
822 |
|
|
823 |
// Use src not movie |
|
824 |
if (data.params.movie) { |
|
825 |
data.params.src = data.params.src || data.params.movie; |
|
826 |
delete data.params.movie; |
|
827 |
} |
|
828 |
|
|
829 |
// Convert the URL to relative/absolute depending on configuration |
|
830 |
if (data.params.src) |
|
831 |
data.params.src = urlConverter.call(urlConverterScope, data.params.src, 'src', 'object'); |
|
832 |
|
|
833 |
if (video) { |
|
834 |
if (node.name === 'video') |
|
835 |
type = lookup.video.name; |
|
836 |
else if (node.name === 'audio') |
|
837 |
type = lookup.audio.name; |
|
838 |
} |
|
839 |
|
|
840 |
if (object && !type) |
|
841 |
type = (lookupAttribute(object, 'clsid') || lookupAttribute(object, 'classid') || lookupAttribute(object, 'type') || {}).name; |
|
842 |
|
|
843 |
if (embed && !type) |
|
844 |
type = (lookupAttribute(embed, 'type') || lookupExtension(data.params.src) || {}).name; |
|
845 |
|
|
846 |
// for embedded audio we preserve the original specified type |
|
847 |
if (embed && type == 'EmbeddedAudio') { |
|
848 |
data.params.type = embed.attr('type'); |
|
849 |
} |
|
850 |
|
|
851 |
// Replace the video/object/embed element with a placeholder image containing the data |
|
852 |
node.replace(img); |
|
853 |
|
|
854 |
// Remove embed |
|
855 |
if (embed) |
|
856 |
embed.remove(); |
|
857 |
|
|
858 |
// Serialize the inner HTML of the object element |
|
859 |
if (object) { |
|
860 |
html = getInnerHTML(object.remove()); |
|
861 |
|
|
862 |
if (html) |
|
863 |
data.object_html = html; |
|
864 |
} |
|
865 |
|
|
866 |
// Serialize the inner HTML of the video element |
|
867 |
if (video) { |
|
868 |
html = getInnerHTML(video.remove()); |
|
869 |
|
|
870 |
if (html) |
|
871 |
data.video_html = html; |
|
872 |
} |
|
873 |
|
|
874 |
data.hspace = hspace; |
|
875 |
data.vspace = vspace; |
|
876 |
data.align = align; |
|
877 |
data.bgcolor = bgcolor; |
|
878 |
|
|
879 |
// Set width/height of placeholder |
|
880 |
img.attr({ |
|
881 |
id : id, |
|
882 |
'class' : 'mceItemMedia mceItem' + (type || 'Flash'), |
|
883 |
style : style, |
|
884 |
width : width || (node.name == 'audio' ? "300" : "320"), |
|
885 |
height : height || (node.name == 'audio' ? "32" : "240"), |
|
886 |
hspace : hspace, |
|
887 |
vspace : vspace, |
|
888 |
align : align, |
|
889 |
bgcolor : bgcolor, |
|
890 |
"data-mce-json" : JSON.serialize(data, "'") |
|
891 |
}); |
|
892 |
} |
|
893 |
}); |
|
894 |
|
|
895 |
// Register plugin |
|
896 |
tinymce.PluginManager.add('media', tinymce.plugins.MediaPlugin); |
|
897 |
})(); |