/** * @file * Attach Media WYSIWYG behaviors. */ (function ($) { Drupal.media = Drupal.media || {}; // Define the behavior. Drupal.wysiwyg.plugins.media = { /** * Initializes the tag map. */ initializeTagMap: function () { if (typeof Drupal.settings.tagmap == 'undefined') { Drupal.settings.tagmap = { }; } }, /** * Execute the button. * @TODO: Debug calls from this are never called. What's its function? */ invoke: function (data, settings, instanceId) { if (data.format == 'html') { Drupal.media.popups.mediaBrowser(function (mediaFiles) { Drupal.wysiwyg.plugins.media.mediaBrowserOnSelect(mediaFiles, instanceId); }, settings['global']); } }, /** * Respond to the mediaBrowser's onSelect event. * @TODO: Debug calls from this are never called. What's its function? */ mediaBrowserOnSelect: function (mediaFiles, instanceId) { var mediaFile = mediaFiles[0]; var options = {}; Drupal.media.popups.mediaStyleSelector(mediaFile, function (formattedMedia) { Drupal.wysiwyg.plugins.media.insertMediaFile(mediaFile, formattedMedia.type, formattedMedia.html, formattedMedia.options, Drupal.wysiwyg.instances[instanceId]); }, options); return; }, insertMediaFile: function (mediaFile, viewMode, formattedMedia, options, wysiwygInstance) { 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.wysiwyg.plugins.media.createTag(imgElement); // Add it to the tag map in case the user switches input formats Drupal.settings.tagmap[inlineTag] = toInsert; wysiwygInstance.insert(toInsert); }, /** * Gets the HTML content of an element * * @param jQuery element */ outerHTML: function (element) { return $('
').append( element.eq(0).clone() ).html(); }, addImageAttributes: function (imgElement, fid, view_mode, additional) { // imgElement.attr('fid', fid); // imgElement.attr('view_mode', view_mode); // Class so we can find this image later. imgElement.addClass('media-image'); this.forceAttributesIntoClass(imgElement, fid, view_mode, additional); if (additional) { for (k in additional) { if (additional.hasOwnProperty(k)) { if (k === 'attr') { imgElement.attr(k, additional[k]); } } } } }, /** * 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
* instead of just , for now though, we're going to remove all the stuff surrounding the images. * * @param String formattedMedia * Element containing the image * * @return HTML of 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 WYSIWYG 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
.. var _tag = inlineTag; _tag = _tag.replace('[[',''); _tag = _tag.replace(']]',''); try { mediaObj = JSON.parse(_tag); } catch(err) { mediaObj = null; } if(mediaObj) { 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) { // Replace all Media placeholder images with the appropriate inline json // string. Using a regular expression instead of jQuery manipulation to // prevent