Advertisement
Guest User

Untitled

a guest
Apr 14th, 2011
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function($) {
  2.  
  3. Drupal.behaviors.image_caption = {
  4.   attach: function (context, settings) {
  5.     $("img.caption").each(function(i) {
  6.       var imgwidth = $(this).width() ? $(this).width() : false;
  7.       var imgheight = $(this).height() ? $(this).height() : false;
  8.      
  9.       // Get caption from title attribute
  10.       var captiontitletext = $(this).attr('title');
  11.      
  12.        // Get caption from alt attribute
  13.       var captionalttext = $(this).attr('alt');
  14.      
  15.       // Get image alignment and style to apply to container
  16.       if($(this).attr('align')){
  17.         var alignment = $(this).attr('align');
  18.         $(this).css({'float':alignment}); // add to css float
  19.         $(this).removeAttr('align');
  20.       }else if($(this).css('float')){
  21.         var alignment = $(this).css('float');
  22.       }else{
  23.         var alignment = 'normal';
  24.       }
  25.       var style = $(this).attr('style') ? $(this).attr('style') : '';
  26.  
  27.       // Reset img styles as are added to container instead      
  28.       $(this).removeAttr('width');
  29.       $(this).removeAttr('height');
  30.       $(this).css('width', '');
  31.       $(this).css('height', '');    
  32.       $(this).removeAttr('align');
  33.       $(this).removeAttr('style');
  34.      
  35.       //Display inline block so it doesn't break any text aligns on the parent contatiner
  36.       $(this).wrap("<span class=\"image-caption-container\" style=\"display:inline-block;" + style + "\"></span>");
  37.       $(this).parent().addClass('image-caption-container-' + alignment);
  38.      
  39.       // Add dimensions, if available
  40.       if(imgwidth){
  41.         $(this).width(imgwidth);
  42.         $(this).parent().width(imgwidth);
  43.       }
  44.       if(imgheight){
  45.         $(this).height(imgheight);
  46.       }
  47.       // Append caption
  48.       $(this).parent().append(
  49.         "<span style=\"display:block;\" class=\"image-caption\">" +
  50.         "<span style=\"display:block;\" class=\"image-title-caption\">" + captiontitletext + "</span>" +
  51.         "<span style=\"display:block;\" class=\"image-alt-caption\">" + captionalttext + "</span>" +
  52.         "</span>"
  53.         );
  54.     });
  55.   }
  56. };
  57.  
  58. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement