library.js
11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/**
* @file
* Attach Media ckeditor behaviors.
*/
(function ($) {
Drupal.media = Drupal.media || {};
Drupal.settings.ckeditor.plugins['media'] = {
/**
* Initializes the tag map.
*/
initializeTagMap: function () {
if (typeof Drupal.settings.tagmap == 'undefined') {
Drupal.settings.tagmap = { };
}
},
/**
* Execute the button.
*/
invoke: function (data, settings, instanceId) {
if (data.format == 'html') {
Drupal.media.popups.mediaBrowser(function (mediaFiles) {
Drupal.settings.ckeditor.plugins['media'].mediaBrowserOnSelect(mediaFiles, instanceId);
}, settings['global']);
}
},
/**
* Respond to the mediaBrowser's onSelect event.
*/
mediaBrowserOnSelect: function (mediaFiles, instanceId) {
var mediaFile = mediaFiles[0];
var options = {};
Drupal.media.popups.mediaStyleSelector(mediaFile, function (formattedMedia) {
Drupal.settings.ckeditor.plugins['media'].insertMediaFile(mediaFile, formattedMedia.type, formattedMedia.html, formattedMedia.options, CKEDITOR.instances[instanceId]);
}, options);
return;
},
insertMediaFile: function (mediaFile, viewMode, formattedMedia, options, ckeditorInstance) {
this.initializeTagMap();
// @TODO: the folks @ ckeditor have told us that there is no way
// to reliably add wrapper divs via normal HTML.
// There is some method of adding a "fake element"
// But until then, we're just going to embed to img.
// This is pretty hacked for now.
//
var imgElement = $(this.stripDivs(formattedMedia));
this.addImageAttributes(imgElement, mediaFile.fid, viewMode, options);
var toInsert = this.outerHTML(imgElement);
// Create an inline tag
var inlineTag = Drupal.settings.ckeditor.plugins['media'].createTag(imgElement);
// Add it to the tag map in case the user switches input formats
Drupal.settings.tagmap[inlineTag] = toInsert;
ckeditorInstance.insertHtml(toInsert);
},
/**
* Gets the HTML content of an element
*
* @param jQuery element
*/
outerHTML: function (element) {
return $('<div>').append( element.eq(0).clone() ).html();
},
addImageAttributes: function (imgElement, fid, view_mode, additional) {
imgElement.addClass('media-image');
this.forceAttributesIntoClass(imgElement, fid, view_mode, additional);
},
/**
* Due to problems handling wrapping divs in ckeditor, this is needed.
*
* Going forward, if we don't care about supporting other editors
* we can use the fakeobjects plugin to ckeditor to provide cleaner
* transparency between what Drupal will output <div class="field..."><img></div>
* instead of just <img>, for now though, we're going to remove all the stuff surrounding the images.
*
* @param String formattedMedia
* Element containing the image
*
* @return HTML of <img> tag inside formattedMedia
*/
stripDivs: function (formattedMedia) {
// Check to see if the image tag has divs to strip
var stripped = null;
if ($(formattedMedia).is('img')) {
stripped = this.outerHTML($(formattedMedia));
} else {
stripped = this.outerHTML($('img', $(formattedMedia)));
}
// This will fail if we pass the img tag without anything wrapping it, like we do when re-enabling ckeditor
return stripped;
},
/**
* Attach function, called when a rich text editor loads.
* This finds all [[tags]] and replaces them with the html
* that needs to show in the editor.
*
*/
attach: function (content, settings, instanceId) {
var matches = content.match(/\[\[.*?\]\]/g);
this.initializeTagMap();
var tagmap = Drupal.settings.tagmap;
if (matches) {
var inlineTag = "";
for (i = 0; i < matches.length; i++) {
inlineTag = matches[i];
if (tagmap[inlineTag]) {
// This probably needs some work...
// We need to somehow get the fid propogated here.
// We really want to
var tagContent = tagmap[inlineTag];
var mediaMarkup = this.stripDivs(tagContent); // THis is <div>..<img>
var _tag = inlineTag;
_tag = _tag.replace('[[','');
_tag = _tag.replace(']]','');
mediaObj = JSON.parse(_tag);
var imgElement = $(mediaMarkup);
this.addImageAttributes(imgElement, mediaObj.fid, mediaObj.view_mode);
var toInsert = this.outerHTML(imgElement);
content = content.replace(inlineTag, toInsert);
}
else {
debug.debug("Could not find content for " + inlineTag);
}
}
}
return content;
},
/**
* Detach function, called when a rich text editor detaches
*/
detach: function (content, settings, instanceId) {
var content = $('<div>' + content + '</div>');
$('img.media-image',content).each(function (elem) {
var tag = Drupal.settings.ckeditor.plugins['media'].createTag($(this));
$(this).replaceWith(tag);
var newContent = content.html();
var tagContent = $('<div></div>').append($(this)).html();
Drupal.settings.tagmap[tag] = tagContent;
});
return content.html();
},
/**
* @param jQuery imgNode
* Image node to create tag from
*/
createTag: function (imgNode) {
// Currently this is the <img> itself
// Collect all attribs to be stashed into tagContent
var mediaAttributes = {};
var imgElement = imgNode[0];
var sorter = [];
// @todo: this does not work in IE, width and height are always 0.
for (i=0; i< imgElement.attributes.length; i++) {
var attr = imgElement.attributes[i];
if (attr.specified == true) {
if (attr.name !== 'class') {
sorter.push(attr);
}
else {
// Exctract expando properties from the class field.
var attributes = this.getAttributesFromClass(attr.value);
for (var name in attributes) {
if (attributes.hasOwnProperty(name)) {
var value = attributes[name];
if (value.type && value.type === 'attr') {
sorter.push(value);
}
}
}
}
}
}
sorter.sort(this.sortAttributes);
for (var prop in sorter) {
mediaAttributes[sorter[prop].name] = sorter[prop].value;
}
// The following 5 ifs are dedicated to IE7
// If the style is null, it is because IE7 can't read values from itself
if (jQuery.browser.msie && jQuery.browser.version == '7.0') {
if (mediaAttributes.style === "null") {
var imgHeight = imgNode.css('height');
var imgWidth = imgNode.css('width');
mediaAttributes.style = {
height: imgHeight,
width: imgWidth
}
if (!mediaAttributes['width']) {
mediaAttributes['width'] = imgWidth;
}
if (!mediaAttributes['height']) {
mediaAttributes['height'] = imgHeight;
}
}
// If the attribute width is zero, get the CSS width
if (Number(mediaAttributes['width']) === 0) {
mediaAttributes['width'] = imgNode.css('width');
}
// IE7 does support 'auto' as a value of the width attribute. It will not
// display the image if this value is allowed to pass through
if (mediaAttributes['width'] === 'auto') {
delete mediaAttributes['width'];
}
// If the attribute height is zero, get the CSS height
if (Number(mediaAttributes['height']) === 0) {
mediaAttributes['height'] = imgNode.css('height');
}
// IE7 does support 'auto' as a value of the height attribute. It will not
// display the image if this value is allowed to pass through
if (mediaAttributes['height'] === 'auto') {
delete mediaAttributes['height'];
}
}
// Remove elements from attribs using the blacklist
for (var blackList in Drupal.settings.media.blacklist) {
delete mediaAttributes[Drupal.settings.media.blacklist[blackList]];
}
tagContent = {
"type": 'media',
// @todo: This will be selected from the format form
"view_mode": attributes['view_mode'].value,
"fid" : attributes['fid'].value,
"attributes": mediaAttributes
};
return '[[' + JSON.stringify(tagContent) + ']]';
},
/**
* Forces custom attributes into the class field of the specified image.
*
* Due to a bug in some versions of Firefox
* (http://forums.mozillazine.org/viewtopic.php?f=9&t=1991855), the
* custom attributes used to share information about the image are
* being stripped as the image markup is set into the rich text
* editor. Here we encode these attributes into the class field so
* the data survives.
*
* @param imgElement
* The image
* @fid
* The file id.
* @param view_mode
* The view mode.
* @param additional
* Additional attributes to add to the image.
*/
forceAttributesIntoClass: function (imgElement, fid, view_mode, additional) {
var wysiwyg = imgElement.attr('wysiwyg');
if (wysiwyg) {
imgElement.addClass('attr__wysiwyg__' + wysiwyg);
}
var format = imgElement.attr('format');
if (format) {
imgElement.addClass('attr__format__' + format);
}
var typeOf = imgElement.attr('typeof');
if (typeOf) {
imgElement.addClass('attr__typeof__' + typeOf);
}
if (fid) {
imgElement.addClass('img__fid__' + fid);
}
if (view_mode) {
imgElement.addClass('img__view_mode__' + view_mode);
}
if (additional) {
for (var name in additional) {
if (additional.hasOwnProperty(name)) {
if (name !== 'alt') {
imgElement.addClass('attr__' + name + '__' + additional[name]);
}
}
}
}
},
/**
* Retrieves encoded attributes from the specified class string.
*
* @param classString
* A string containing the value of the class attribute.
* @return
* An array containing the attribute names as keys, and an object
* with the name, value, and attribute type (either 'attr' or
* 'img', depending on whether it is an image attribute or should
* be it the attributes section)
*/
getAttributesFromClass: function (classString) {
var actualClasses = [];
var otherAttributes = [];
var classes = classString.split(' ');
var regexp = new RegExp('^(attr|img)__([^\S]*)__([^\S]*)$');
for (var index = 0; index < classes.length; index++) {
var matches = classes[index].match(regexp);
if (matches && matches.length === 4) {
otherAttributes[matches[2]] = {
name: matches[2],
value: matches[3],
type: matches[1]
};
}
else {
actualClasses.push(classes[index]);
}
}
if (actualClasses.length > 0) {
otherAttributes['class'] = {
name: 'class',
value: actualClasses.join(' '),
type: 'attr'
};
}
return otherAttributes;
},
sortAttributes: function (a, b) {
var nameA = a.name.toLowerCase();
var nameB = b.name.toLowerCase();
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
return 0;
}
};
})(jQuery);