root / drupal7 / sites / all / modules / media / modules / media_wysiwyg / js / media_wysiwyg.filter.js @ 1f623f01
1 |
/**
|
---|---|
2 |
* @file
|
3 |
* File with utilities to handle media in html editing.
|
4 |
*/
|
5 |
(function ($) { |
6 |
|
7 |
Drupal.media = Drupal.media || {}; |
8 |
/**
|
9 |
* Utility to deal with media tokens / placeholders.
|
10 |
*/
|
11 |
Drupal.media.filter = { |
12 |
/**
|
13 |
* Replaces media tokens with the placeholders for html editing.
|
14 |
* @param content
|
15 |
*/
|
16 |
replaceTokenWithPlaceholder: function(content) { |
17 |
Drupal.media.filter.ensure_tagmap(); |
18 |
var matches = content.match(/\[\[.*?\]\]/g); |
19 |
|
20 |
if (matches) {
|
21 |
for (var i = 0; i < matches.length; i++) { |
22 |
var match = matches[i];
|
23 |
if (match.indexOf('"type":"media"') == -1) { |
24 |
continue;
|
25 |
} |
26 |
|
27 |
// Check if the macro exists in the tagmap. This ensures backwards
|
28 |
// compatibility with existing media and is moderately more efficient
|
29 |
// than re-building the element.
|
30 |
var media = Drupal.settings.tagmap[match];
|
31 |
var media_json = match.replace('[[', '').replace(']]', ''); |
32 |
|
33 |
// Ensure that the media JSON is valid.
|
34 |
try {
|
35 |
var media_definition = JSON.parse(media_json);
|
36 |
} |
37 |
catch (err) {
|
38 |
// @todo: error logging.
|
39 |
// Content should be returned to prevent an empty editor.
|
40 |
return content;
|
41 |
} |
42 |
|
43 |
// Re-build the media if the macro has changed from the tagmap.
|
44 |
if (!media && media_definition.fid) {
|
45 |
Drupal.media.filter.ensureSourceMap(); |
46 |
var source = Drupal.settings.mediaSourceMap[media_definition.fid];
|
47 |
media = document.createElement(source.tagName); |
48 |
media.src = source.src; |
49 |
media.innerHTML = source.innerHTML; |
50 |
} |
51 |
|
52 |
// Apply attributes.
|
53 |
var element = Drupal.media.filter.create_element(media, media_definition);
|
54 |
var markup = Drupal.media.filter.outerHTML(element);
|
55 |
|
56 |
// Use split and join to replace all instances of macro with markup.
|
57 |
content = content.split(match).join(markup); |
58 |
} |
59 |
} |
60 |
|
61 |
return content;
|
62 |
}, |
63 |
|
64 |
/**
|
65 |
* Returns alt and title field attribute data from the corresponding fields.
|
66 |
*
|
67 |
* Specifically looks for file_entity module's file_image_alt_text and
|
68 |
* file_image_title_text fields as those are by default used to store
|
69 |
* override values for image alt and title attributes.
|
70 |
*
|
71 |
* @param options (array)
|
72 |
* Options passed through a popup form submission.
|
73 |
* @param includeFieldID (bool)
|
74 |
* If set, the returned object will have extra keys with the IDs of the
|
75 |
* found fields.
|
76 |
*
|
77 |
* If the alt or title fields were not found, their keys will be excluded
|
78 |
* from the returned array.
|
79 |
*
|
80 |
* @return
|
81 |
* An object with the following keys:
|
82 |
* - alt: The value of the alt field.
|
83 |
* - altField: The id of the alt field.
|
84 |
* - title: The value of the title field.
|
85 |
* - titleField: The id of the title field.
|
86 |
*/
|
87 |
parseAttributeFields: function(options, includeFieldID) { |
88 |
var attributes = {};
|
89 |
|
90 |
for (var field in options) { |
91 |
// If the field is set to false, use an empty string for output.
|
92 |
options[field] = options[field] === false ? '' : options[field]; |
93 |
//if (field.match(/^field_file_image_alt_text/)) {
|
94 |
if (field.match(new RegExp('^' + Drupal.settings.media.img_alt_field))) { |
95 |
attributes.alt = options[field]; |
96 |
if (includeFieldID) {
|
97 |
attributes.altField = field; |
98 |
} |
99 |
} |
100 |
|
101 |
//if (field.match(/^field_file_image_title_text/)) {
|
102 |
if (field.match(new RegExp('^' + Drupal.settings.media.img_title_field))) { |
103 |
attributes.title = options[field]; |
104 |
if (includeFieldID) {
|
105 |
attributes.titleField = field; |
106 |
} |
107 |
} |
108 |
} |
109 |
|
110 |
return attributes;
|
111 |
}, |
112 |
|
113 |
/**
|
114 |
* Ensures changes made to fielded attributes are done on the fields too.
|
115 |
*
|
116 |
* This should be called when creating a macro tag from a placeholder.
|
117 |
*
|
118 |
* Changed made to attributes represented by fields are synced back to the
|
119 |
* corresponding fields, if they exist. The alt/title attribute
|
120 |
* values encoded in the macro will override the alt/title field values (set
|
121 |
* in the Media dialog) during rendering of both WYSIWYG placeholders and
|
122 |
* the final file entity on the server. Syncing makes changes applied to a
|
123 |
* placeholder's alt/title attribute using native WYSIWYG tools visible in
|
124 |
* the fields shown in the Media dialog.
|
125 |
*
|
126 |
* The reverse should be done when creating a placeholder from a macro tag
|
127 |
* so changes made in the Media dialog are reflected in the placeholder's
|
128 |
* alt and title attributes or the values there become stale and the change
|
129 |
* appears uneffective.
|
130 |
*
|
131 |
* @param file_info (object)
|
132 |
* A JSON decoded object of the file being inserted/updated.
|
133 |
*/
|
134 |
syncAttributesToFields: function(file_info) { |
135 |
if (!file_info) {
|
136 |
file_info = {}; |
137 |
} |
138 |
if (!file_info.attributes) {
|
139 |
file_info.attributes = {}; |
140 |
} |
141 |
if (!file_info.fields) {
|
142 |
file_info.fields = {}; |
143 |
} |
144 |
var fields = Drupal.media.filter.parseAttributeFields(file_info.fields, true); |
145 |
|
146 |
// If the title attribute has changed, ensure the title field is updated.
|
147 |
var titleAttr = file_info.attributes.title || false; |
148 |
if (fields.titleField && (titleAttr !== fields.title)) {
|
149 |
file_info.fields[fields.titleField] = titleAttr; |
150 |
} |
151 |
|
152 |
// If the alt attribute has changed, ensure the alt field is updated.
|
153 |
var altAttr = file_info.attributes.alt || false; |
154 |
if (fields.altField && (altAttr !== fields.alt)) {
|
155 |
file_info.fields[fields.altField] = altAttr; |
156 |
} |
157 |
|
158 |
return file_info;
|
159 |
}, |
160 |
|
161 |
/**
|
162 |
* Replaces media elements with tokens.
|
163 |
*
|
164 |
* @param content (string)
|
165 |
* The markup within the wysiwyg instance.
|
166 |
*/
|
167 |
replacePlaceholderWithToken: function(content) { |
168 |
Drupal.media.filter.ensure_tagmap(); |
169 |
|
170 |
// Replace all media placeholders with their JSON macro representations.
|
171 |
//
|
172 |
// There are issues with using jQuery to parse the WYSIWYG content (see
|
173 |
// http://drupal.org/node/1280758), and parsing HTML with regular
|
174 |
// expressions is a terrible idea (see http://stackoverflow.com/a/1732454/854985)
|
175 |
//
|
176 |
// WYSIWYG editors act wacky with complex placeholder markup anyway, so an
|
177 |
// image is the most reliable and most usable anyway: images can be moved by
|
178 |
// dragging and dropping, and can be resized using interactive handles.
|
179 |
//
|
180 |
// Media requests a WYSIWYG place holder rendering of the file by passing
|
181 |
// the wysiwyg => 1 flag in the settings array when calling
|
182 |
// media_get_file_without_label().
|
183 |
//
|
184 |
// Finds the media-element class.
|
185 |
var classRegex = 'class=([\'"])[^\\1]*?media-element'; |
186 |
// Image tag with the media-element class.
|
187 |
var regex = '<img[^>]+' + classRegex + '[^>]*?>'; |
188 |
// Or a span with the media-element class (used for documents).
|
189 |
// \S\s catches any character, including a linebreak; JavaScript does not
|
190 |
// have a dotall flag.
|
191 |
regex += '|<span[^>]+' + classRegex + '[^>]*?>[\\S\\s]+?</span>'; |
192 |
var matches = content.match(RegExp(regex, 'gi')); |
193 |
if (matches) {
|
194 |
for (i = 0; i < matches.length; i++) { |
195 |
var markup = matches[i];
|
196 |
var macro = Drupal.media.filter.create_macro($(markup)); |
197 |
// If we have a truthy response, store the macro and perform the
|
198 |
// replacement.
|
199 |
if (macro) {
|
200 |
Drupal.settings.tagmap[macro] = markup; |
201 |
content = content.replace(markup, macro); |
202 |
} |
203 |
} |
204 |
} |
205 |
|
206 |
return content;
|
207 |
}, |
208 |
|
209 |
/**
|
210 |
* Serializes file information as a url-encoded JSON object and stores it
|
211 |
* as a data attribute on the html element.
|
212 |
*
|
213 |
* @param html (string)
|
214 |
* A html element to be used to represent the inserted media element.
|
215 |
* @param info (object)
|
216 |
* A object containing the media file information (fid, view_mode, etc).
|
217 |
*/
|
218 |
create_element: function (html, info) { |
219 |
if ($('<div>').append(html).text().length === html.length) { |
220 |
// Element is not an html tag. Surround it in a span element so we can
|
221 |
// pass the file attributes.
|
222 |
html = '<span>' + html + '</span>'; |
223 |
} |
224 |
var element = $(html); |
225 |
|
226 |
// Parse out link wrappers. They will be re-applied when the image is
|
227 |
// rendered on the front-end.
|
228 |
if (element.is('a')) { |
229 |
element = element.children(); |
230 |
} |
231 |
|
232 |
// Extract attributes represented by fields and use those values to keep
|
233 |
// them in sync, usually alt and title.
|
234 |
var attributes = Drupal.media.filter.parseAttributeFields(info.fields);
|
235 |
info.attributes = $.extend(info.attributes, attributes);
|
236 |
|
237 |
// Move attributes from the file info array to the placeholder element.
|
238 |
if (info.attributes) {
|
239 |
$.each(Drupal.settings.media.wysiwyg_allowed_attributes, function(i, a) { |
240 |
if (info.attributes[a]) {
|
241 |
element.attr(a, $('<textarea />').html(info.attributes[a]).text()); |
242 |
} |
243 |
}); |
244 |
delete(info.attributes);
|
245 |
|
246 |
// Store information to rebuild the element later, if necessary.
|
247 |
Drupal.media.filter.ensureSourceMap(); |
248 |
Drupal.settings.mediaSourceMap[info.fid] = { |
249 |
tagName: element[0].tagName, |
250 |
src: element[0].src, |
251 |
innerHTML: element[0].innerHTML |
252 |
} |
253 |
} |
254 |
|
255 |
info.type = info.type || "media";
|
256 |
|
257 |
// Store the data in the data map.
|
258 |
Drupal.media.filter.ensureDataMap(); |
259 |
Drupal.settings.mediaDataMap[info.fid] = info; |
260 |
|
261 |
// Store the fid in the DOM to retrieve the data from the info map.
|
262 |
element.attr('data-fid', info.fid);
|
263 |
|
264 |
// Add data-media-element attribute so we can find the markup element later.
|
265 |
element.attr('data-media-element', '1') |
266 |
|
267 |
var classes = ['media-element']; |
268 |
if (info.view_mode) {
|
269 |
// Remove any existing view mode classes.
|
270 |
element.removeClass (function (index, css) {
|
271 |
return (css.match (/\bfile-\S+/g) || []).join(' '); |
272 |
}); |
273 |
classes.push('file-' + info.view_mode.replace(/_/g, '-')); |
274 |
} |
275 |
element.addClass(classes.join(' '));
|
276 |
|
277 |
// Apply link_text if present.
|
278 |
if (info.link_text) {
|
279 |
$('a', element).html(info.link_text); |
280 |
} |
281 |
|
282 |
return element;
|
283 |
}, |
284 |
|
285 |
/**
|
286 |
* Create a macro representation of the inserted media element.
|
287 |
*
|
288 |
* @param element (jQuery object)
|
289 |
* A media element with attached serialized file info.
|
290 |
*/
|
291 |
create_macro: function (element) { |
292 |
var file_info = Drupal.media.filter.extract_file_info(element);
|
293 |
if (file_info) {
|
294 |
if (typeof file_info.link_text == 'string') { |
295 |
// Make sure the link_text-html-tags are properly escaped.
|
296 |
file_info.link_text = file_info.link_text.replace(/</g, '<').replace(/>/g, '>'); |
297 |
} |
298 |
return '[[' + JSON.stringify(file_info) + ']]'; |
299 |
} |
300 |
return false; |
301 |
}, |
302 |
|
303 |
/**
|
304 |
* Extract the file info from a WYSIWYG placeholder element as JSON.
|
305 |
*
|
306 |
* @param element (jQuery object)
|
307 |
* A media element with associated file info via a file id (fid).
|
308 |
*/
|
309 |
extract_file_info: function (element) { |
310 |
var fid, file_info, value;
|
311 |
|
312 |
if (fid = element.data('fid')) { |
313 |
Drupal.media.filter.ensureDataMap(); |
314 |
|
315 |
if (file_info = Drupal.settings.mediaDataMap[fid]) {
|
316 |
file_info.attributes = {}; |
317 |
|
318 |
$.each(Drupal.settings.media.wysiwyg_allowed_attributes, function(i, a) { |
319 |
if (value = element.attr(a)) {
|
320 |
// Replace " by \" to avoid error with JSON format.
|
321 |
if (typeof value == 'string') { |
322 |
value = value.replace('"', '\\"'); |
323 |
} |
324 |
file_info.attributes[a] = value; |
325 |
} |
326 |
}); |
327 |
|
328 |
// Extract the link text, if there is any.
|
329 |
file_info.link_text = element.find('a').html();
|
330 |
} |
331 |
} |
332 |
|
333 |
return Drupal.media.filter.syncAttributesToFields(file_info);
|
334 |
}, |
335 |
|
336 |
/**
|
337 |
* Gets the HTML content of an element.
|
338 |
*
|
339 |
* @param element (jQuery object)
|
340 |
*/
|
341 |
outerHTML: function (element) { |
342 |
return element[0].outerHTML || $('<div>').append(element.eq(0).clone()).html(); |
343 |
}, |
344 |
|
345 |
/**
|
346 |
* Gets the wrapped HTML content of an element to insert into the wysiwyg.
|
347 |
*
|
348 |
* It also registers the element in the tag map so that the token
|
349 |
* replacement works.
|
350 |
*
|
351 |
* @param element (jQuery object) The element to insert.
|
352 |
*
|
353 |
* @see Drupal.media.filter.replacePlaceholderWithToken()
|
354 |
*/
|
355 |
getWysiwygHTML: function (element) { |
356 |
// Create the markup and the macro.
|
357 |
var markup = Drupal.media.filter.outerHTML(element),
|
358 |
macro = Drupal.media.filter.create_macro(element); |
359 |
|
360 |
// Store macro/markup in the tagmap.
|
361 |
Drupal.media.filter.ensure_tagmap(); |
362 |
Drupal.settings.tagmap[macro] = markup; |
363 |
|
364 |
// Return the html code to insert in an editor and use it with
|
365 |
// replacePlaceholderWithToken()
|
366 |
return markup;
|
367 |
}, |
368 |
|
369 |
/**
|
370 |
* Ensures the src tracking has been initialized and returns it.
|
371 |
*/
|
372 |
ensureSourceMap: function() { |
373 |
Drupal.settings.mediaSourceMap = Drupal.settings.mediaSourceMap || {}; |
374 |
return Drupal.settings.mediaSourceMap;
|
375 |
}, |
376 |
|
377 |
/**
|
378 |
* Ensures the data tracking has been initialized and returns it.
|
379 |
*/
|
380 |
ensureDataMap: function() { |
381 |
Drupal.settings.mediaDataMap = Drupal.settings.mediaDataMap || {}; |
382 |
return Drupal.settings.mediaDataMap;
|
383 |
}, |
384 |
|
385 |
/**
|
386 |
* Ensures the tag map has been initialized and returns it.
|
387 |
*/
|
388 |
ensure_tagmap: function () { |
389 |
Drupal.settings.tagmap = Drupal.settings.tagmap || {}; |
390 |
return Drupal.settings.tagmap;
|
391 |
} |
392 |
} |
393 |
|
394 |
})(jQuery); |