Révision 59ae487e
Ajouté par Assos Assos il y a presque 7 ans
drupal7/sites/all/modules/media_ckeditor/js/plugins/media/library.js | ||
---|---|---|
7 | 7 |
(function ($) { |
8 | 8 |
Drupal.media = Drupal.media || {}; |
9 | 9 |
|
10 |
/** |
|
11 |
* Attaches 'insert' button to media widget. |
|
12 |
*/ |
|
13 |
Drupal.behaviors.mediaWidgetInsert = { |
|
14 |
attach: function (context, settings) { |
|
15 |
if (Drupal.ckeditorInstance && Drupal.settings.media_ckeditor && Drupal.settings.media_ckeditor.wysiwyg_insert) { |
|
16 |
// Only add buttons on fields that have been configured so, by |
|
17 |
// consulting the Drupal.settings.media_ckeditor.wysiwyg_insert var. |
|
18 |
for (var fieldName in Drupal.settings.media_ckeditor.wysiwyg_insert) { |
|
19 |
var fieldId = '#edit-' + fieldName.replace(/_/g, '-'); |
|
20 |
// Within the field markup, look for the table of files. |
|
21 |
$(fieldId, context).find('.media-widget').once('mediaInsertButton', function() { |
|
22 |
// For each file, check to see if there is a file ID. |
|
23 |
if ($(this).find('input.fid').val() != 0) { |
|
24 |
// Now we add the button next to "Remove". |
|
25 |
var insertButton = $('<a class="media-insert button">' + Drupal.t('Insert') + '</a>') |
|
26 |
.click(function(e) { |
|
27 |
e.preventDefault(); |
|
28 |
var fid = $(this).parent().parent().find('.fid').val(); |
|
29 |
var mediaFile = {fid: fid} |
|
30 |
Drupal.ckeditorInstance.mediaInsert = {mediaFiles: [mediaFile]}; |
|
31 |
Drupal.ckeditorInstance.execCommand('media'); |
|
32 |
}); |
|
33 |
// Insert the button, differently for single vs. multi value. |
|
34 |
var multiValue = $(fieldId + ' table', context).length; |
|
35 |
if (multiValue) { |
|
36 |
insertButton.insertBefore($(this).parent().parent().find('input.remove')); |
|
37 |
} |
|
38 |
else { |
|
39 |
insertButton.insertBefore($(this).find('input.remove')); |
|
40 |
} |
|
41 |
} |
|
42 |
}); |
|
43 |
} |
|
44 |
} |
|
45 |
} |
|
46 |
}; |
|
47 |
|
|
10 | 48 |
Drupal.settings.ckeditor.plugins['media'] = { |
11 | 49 |
/** |
12 | 50 |
* Execute the button. |
... | ... | |
26 | 64 |
else { |
27 | 65 |
$alreadyInsertedMedia = jQuery(data.node).find('[data-media-element]'); |
28 | 66 |
} |
29 |
if ($alreadyInsertedMedia.length) { |
|
30 |
// Change the view mode for already-inserted media. |
|
67 |
// First check to see if we are using an Insert button. |
|
68 |
if (typeof Drupal.ckeditorInstance.mediaInsert !== 'undefined') { |
|
69 |
var mediaFile = Drupal.ckeditorInstance.mediaInsert.mediaFiles[0]; |
|
70 |
delete Drupal.ckeditorInstance.mediaInsert; |
|
71 |
Drupal.media.popups.mediaStyleSelector(mediaFile, function (mediaFiles) { |
|
72 |
Drupal.settings.ckeditor.plugins['media'].insertMediaFile(mediaFile, mediaFiles, CKEDITOR.instances[instanceId]); |
|
73 |
}, settings['global']); |
|
74 |
} |
|
75 |
// Next check to see if we are editing already-inserted media. |
|
76 |
else if ($alreadyInsertedMedia.length) { |
|
31 | 77 |
var mediaFile = Drupal.media.filter.extract_file_info($alreadyInsertedMedia); |
32 | 78 |
Drupal.media.popups.mediaStyleSelector(mediaFile, function (mediaFiles) { |
33 | 79 |
Drupal.settings.ckeditor.plugins['media'].insertMediaFile(mediaFile, mediaFiles, CKEDITOR.instances[instanceId]); |
34 | 80 |
}, settings['global']); |
35 | 81 |
} |
82 |
// Otherwise we are embedding new media. |
|
36 | 83 |
else { |
37 | 84 |
Drupal.media.popups.mediaBrowser(function (mediaFiles) { |
38 | 85 |
Drupal.settings.ckeditor.plugins['media'].mediaBrowserOnSelect(mediaFiles, instanceId); |
... | ... | |
54 | 101 |
return; |
55 | 102 |
}, |
56 | 103 |
|
57 |
insertMediaFile: function (mediaFile, formattedMedia, ckeditorInstance) { |
|
104 |
insertMediaFile: function (mediaFile, formattedMedia, ckeditorInstance, fullyRenderedFile) { |
|
105 |
|
|
106 |
// See if we should use ajax to get the fully rendered file. |
|
107 |
if (typeof fullyRenderedFile === 'undefined' && |
|
108 |
Drupal.settings.media_ckeditor.fully_rendered_files) { |
|
109 |
|
|
110 |
$.ajax({ |
|
111 |
url: Drupal.settings.basePath + 'media/rendered-in-wysiwyg', |
|
112 |
type: 'GET', |
|
113 |
data: { |
|
114 |
fid: mediaFile.fid, |
|
115 |
view_mode: formattedMedia.type, |
|
116 |
fields: formattedMedia.options |
|
117 |
}, |
|
118 |
success: function(html) { |
|
119 |
// To work around an IE issue, preload any image. The issue is |
|
120 |
// that IE requests the image 2 times, and one request gets a 503, |
|
121 |
// causing a broken image icon in the WYSIWYG instead of the actual |
|
122 |
// image. |
|
123 |
var $images = $(html).find('img'); |
|
124 |
if (!$images.length || $(html).find('picture').length) { |
|
125 |
// If there are no images, just insert the html immediately, by |
|
126 |
// re-calling this function with the ajax-retrieved HTML. |
|
127 |
Drupal.settings.ckeditor.plugins['media'].insertMediaFile(mediaFile, formattedMedia, ckeditorInstance, html); |
|
128 |
} |
|
129 |
else { |
|
130 |
// Otherwise do the same, but only after the first image preloads. |
|
131 |
// Possible future improvement might be to handle multiple images |
|
132 |
// instead of just the first, but even better would be to remove |
|
133 |
// this workaround entirely, when/if IE's behavior changes. |
|
134 |
var image = new Image(); |
|
135 |
image.onload = function() { |
|
136 |
Drupal.settings.ckeditor.plugins['media'].insertMediaFile(mediaFile, formattedMedia, ckeditorInstance, html); |
|
137 |
} |
|
138 |
image.src = $images.first().attr('src'); |
|
139 |
} |
|
140 |
}, |
|
141 |
error: function(data) { |
|
142 |
// Fallback to whatever the HTML was already going to be. |
|
143 |
Drupal.settings.ckeditor.plugins['media'].insertMediaFile(mediaFile, formattedMedia, ckeditorInstance, formattedMedia.html); |
|
144 |
} |
|
145 |
}); |
|
146 |
|
|
147 |
// Stop for now, because the callback above re-calls this same function. |
|
148 |
return; |
|
149 |
} |
|
150 |
|
|
151 |
// See if we already used ajax to get the fully rendered file. |
|
152 |
if (typeof fullyRenderedFile !== 'undefined') { |
|
153 |
formattedMedia.html = fullyRenderedFile; |
|
154 |
} |
|
155 |
|
|
58 | 156 |
// Customization of Drupal.media.filter.registerNewElement(). |
59 | 157 |
var element = Drupal.media.filter.create_element(formattedMedia.html, { |
60 | 158 |
fid: mediaFile.fid, |
... | ... | |
197 | 295 |
} |
198 | 296 |
}; |
199 | 297 |
|
298 |
// If media_ckeditor is configured to render items in the wysiwyg as full |
|
299 |
// rendered file entities, we need to completely hijack a function from |
|
300 |
// media_wysiwyg.filter.js. |
|
301 |
if (Drupal.settings.media_ckeditor.fully_rendered_files) { |
|
302 |
|
|
303 |
// Replaces function of the same name, from media_wysiwyg.filter.js. |
|
304 |
Drupal.media.filter.replacePlaceholderWithToken = function(content) { |
|
305 |
|
|
306 |
var $placeholder = $(content); |
|
307 |
if ($placeholder.hasClass('media-element')) { |
|
308 |
var macro = Drupal.media.filter.create_macro($placeholder); |
|
309 |
Drupal.media.filter.ensure_tagmap(); |
|
310 |
Drupal.settings.tagmap[macro] = content; |
|
311 |
return macro; |
|
312 |
} |
|
313 |
return content; |
|
314 |
} |
|
315 |
} |
|
200 | 316 |
})(jQuery); |
Formats disponibles : Unified diff
Weekly update of contrib modules