diff --git a/plugins/media/library.js b/plugins/media/library.js index 1295ad5..2cc4aa6 100644 --- a/plugins/media/library.js +++ b/plugins/media/library.js @@ -51,16 +51,66 @@ // This is pretty hacked for now. // var imgElement = $(this.stripDivs(formattedMedia)); - this.addImageAttributes(imgElement, mediaFile.fid, viewMode, options); + elements = this.correctlyApplyAttributes(imgElement, mediaFile.fid, viewMode, options); - var toInsert = this.outerHTML(imgElement); - // Create an inline tag - var inlineTag = Drupal.settings.ckeditor.plugins['media'].createTag(imgElement); + var toInsert = this.outerHTML(elements['imgElement']); + // Create an inline tag making sure to use the part of the insertion (the + // ) that should actually get tokenized. + var inlineTag = this.createTag(elements['actualImgElement']); // Add it to the tag map in case the user switches input formats Drupal.settings.tagmap[inlineTag] = toInsert; ckeditorInstance.insertHtml(toInsert); }, + correctlyApplyAttributes: function (imgElement, fid, view_mode, additional) { + + if (imgElement.is('img')) { + // Store the actual for use later in the createTag call that + // creates the media token from the element. + var actualImgElement = imgElement; + // Add the correct classes that will cause the element to be tokenized. + this.addImageAttributes(imgElement, fid, view_mode, additional); + // Document tokens need to rerender the associated filename link next + // to the filetype icon. + if (imgElement.hasClass('document')) { + var attributes = this.getAttributesFromClass(actualImgElement.attr('class')); + if (attributes.filename && attributes.href) { + // Construct the link. + link = $('').attr('href', attributes.href.value).text(attributes.filename.value); + // Reconstruct imgElement with the link attached. + imgElement = $('').prepend(actualImgElement).append(link); + } + } + } + else { + // This is for inserting documents who come as a wrapped around + // the document icon and the filename linked to the file. In this + // case we need to apply the correct classes to the child because + // it needs to become the media token, but we need to retain the + // wrapper and linked filename. + var actualImgElement = $('img', imgElement); + // Remove the that does not have the correct classes to make it a + // media token. + imgElement.remove('img'); + // If element contains a filename link, add attributes that will be + // used later to rerender the link when converting the token back into + // html. + var link = $('a', imgElement); + if (link) { + actualImgElement.attr('filename', link.text()).attr('href', link.attr('href')); + actualImgElement.addClass('document'); + } + // Add the correct classes that will cause the element to be tokenized. + this.addImageAttributes(actualImgElement, fid, view_mode, additional); + // Add back the , this time with the correct classes to make it a + // media token. + imgElement = imgElement.prepend(actualImgElement); + } + + return {'imgElement': imgElement, 'actualImgElement': actualImgElement}; + + }, + /** * Gets the HTML content of an element * @@ -92,7 +142,7 @@ stripDivs: function (formattedMedia) { // Check to see if the image tag has divs to strip var stripped = null; - if ($(formattedMedia).is('img')) { + if ($(formattedMedia).is('img') || $(formattedMedia).is('span')) { stripped = this.outerHTML($(formattedMedia)); } else { stripped = this.outerHTML($('img', $(formattedMedia))); @@ -128,8 +178,8 @@ mediaObj = JSON.parse(_tag); var imgElement = $(mediaMarkup); - this.addImageAttributes(imgElement, mediaObj.fid, mediaObj.view_mode); - var toInsert = this.outerHTML(imgElement); + elements = this.correctlyApplyAttributes(imgElement, mediaObj.fid, mediaObj.view_mode, false); + var toInsert = this.outerHTML(elements['imgElement']); content = content.replace(inlineTag, toInsert); } else { @@ -147,7 +197,12 @@ var content = $('
' + content + '
'); $('img.media-image',content).each(function (elem) { var tag = Drupal.settings.ckeditor.plugins['media'].createTag($(this)); - $(this).replaceWith(tag); + if ($(this).hasClass('document') && $(this).parent('span').length > 0) { + $(this).parent('span').replaceWith(tag); + } + else { + $(this).replaceWith(tag); + } var newContent = content.html(); var tagContent = $('
').append($(this)).html(); Drupal.settings.tagmap[tag] = tagContent; @@ -277,6 +332,14 @@ if (typeOf) { imgElement.addClass('attr__typeof__' + typeOf); } + var filename = imgElement.attr('filename'); + if (filename) { + imgElement.addClass('attr__filename__' + filename); + } + var href = imgElement.attr('href'); + if (href) { + imgElement.addClass('attr__href__' + href); + } if (fid) { imgElement.addClass('img__fid__' + fid); }