Advertisement
Guest User

Untitled

a guest
Nov 26th, 2011
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.19 KB | None | 0 0
  1. (function($) {
  2.  
  3. Drupal.behaviors.image_caption = {
  4.   attach: function (context, settings) {
  5.  
  6.     // This function correct for conflicting and missing captions classes.  
  7.     $(".caption-these-images img").each(function(i) {  
  8.         if ($(this).hasClass("no-caption")){
  9.             $(this).removeClass("caption no-caption").addClass("caption-processed");
  10.         }else{
  11.             $(this).addClass("caption");
  12.         }
  13.     });    
  14.    
  15.     // This function adds the image captions.
  16.     $(".caption-these-images img.caption:not(.caption-processed)").each(function(i) {
  17.       var imgwidth = $(this).width() ? $(this).width() : false;
  18.       var imgheight = $(this).height() ? $(this).height() : false;
  19.      
  20.       // Get caption from title attribute
  21.       var captiontitletext = $(this).attr('title');
  22.      
  23.        // Get caption from alt attribute
  24.       var captionalttext = $(this).attr('alt');
  25.      
  26.       // Get image alignment and style to apply to container
  27.       if($(this).attr('align')){
  28.         var alignment = $(this).attr('align');
  29.         $(this).css({'float':alignment}); // add to css float
  30.         $(this).removeAttr('align');
  31.       }else if($(this).css('float')){
  32.         var alignment = $(this).css('float');
  33.       }else{
  34.         var alignment = 'normal';
  35.       }
  36.       var style = $(this).attr('style') ? $(this).attr('style') : '';
  37.  
  38.       // Reset img styles as are added to container instead      
  39.       $(this).removeAttr('width');
  40.       $(this).removeAttr('height');
  41.       $(this).css('width', '');
  42.       $(this).css('height', '');    
  43.       $(this).removeAttr('align');
  44.       $(this).removeAttr('style');
  45.      
  46.       //Display inline block so it doesn't break any text aligns on the parent contatiner
  47.       $(this).wrap("<span class=\"image-caption-container\" style=\"display:inline-block;" + style + "\"></span>");
  48.       $(this).parent().addClass('image-caption-container-' + alignment);
  49.      
  50.       // Add dimensions, if available
  51.       if(imgwidth){
  52.         $(this).width(imgwidth);
  53.         $(this).parent().width(imgwidth);
  54.       }
  55.       if(imgheight){
  56.         $(this).height(imgheight);
  57.       }
  58.       // Append caption
  59.       $(this).parent().append(
  60.         "<span style=\"display:block;\" class=\"image-caption\">" +
  61.         "<span style=\"display:block;\" class=\"image-title-caption\">" + captiontitletext + "</span>" +
  62.         "<span style=\"display:block;\" class=\"image-alt-caption\">" + captionalttext + "</span>" +
  63.         "</span>"
  64.         );
  65.        
  66.       // Add class to prevent duplicate caption adding
  67.       $(this).addClass('caption-processed');
  68.     });
  69.    
  70.     // This function cleans up the output of the captioned images.
  71.     $(".caption-these-images").each(function(i) {
  72.         // Adds a class to the image caption's <a> link
  73.         $('a .image-caption-container').each(function() {  
  74.             $(this).parent().addClass('image-caption-link');
  75.         });
  76.        
  77.         // Removes empty alt and title caption span's
  78.         $('.image-caption').each(function() {  
  79.             $(this).find('span:empty').remove();
  80.         });
  81.        
  82.         //If both the alt and title caption spans were removed, now their wrapper is empty so we should remove that too
  83.         $('.image-caption-container').each(function() {
  84.             $(this).find('span:empty').remove();
  85.         });
  86.     });
  87.    
  88.   }
  89. };
  90.  
  91. })(jQuery);
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement