/* * Based on the work of * Copyright (C) 2009 Joel Sutherland * Licenced under the MIT license * http://www.newmediacampaigns.com/page/jcaption-a-jquery-plugin-for-simple-image-captions */ Drupal.behaviors.jcaption = { attach: function(context, settings) { (function($) { var captionSelectors = Drupal.settings.jcaption.jcaption_selectors; $.fn.jcaption = function(settings) { settings = $.extend({ wrapperElement: 'span', wrapperClass: 'caption', captionElement: 'p', imageAttr: Drupal.settings.jcaption.jcaption_alt_title, requireText: Drupal.settings.jcaption.jcaption_requireText, copyStyle: Drupal.settings.jcaption.jcaption_copyStyle, removeStyle: Drupal.settings.jcaption.jcaption_removeStyle, removeClass: Drupal.settings.jcaption.jcaption_removeClass, removeAlign: Drupal.settings.jcaption.jcaption_removeAlign, copyAlignmentToClass: Drupal.settings.jcaption.jcaption_copyAlignmentToClass, copyFloatToClass: Drupal.settings.jcaption.jcaption_copyFloatToClass, copyClassToClass: Drupal.settings.jcaption.jcaption_copyClassToClass, autoWidth: Drupal.settings.jcaption.jcaption_autoWidth, keepLink: Drupal.settings.jcaption.jcaption_keepLink, styleMarkup: Drupal.settings.jcaption.jcaption_styleMarkup, animate: Drupal.settings.jcaption.jcaption_animate, show: {opacity: 'show'}, showDuration: Drupal.settings.jcaption.jcaption_showDuration, hide: {opacity: 'hide'}, hideDuration: Drupal.settings.jcaption.jcaption_hideDuration }, settings); return $(this).each(function(){ //Only add the caption after the image has been loaded. This makes sure we can know the width of the caption. $(this).bind('load', function(){ //Make sure the captioning isn't applied twice when the IE fix at the bottom is applied if($(this).data('loaded')) return false; $(this).data('loaded', true); //Shorthand for the image we will be applying the caption to if($(this).parent("a").length > 0 && !settings.keepLink) { var image = $(this).parent("a"); var cleanimage = $(this); } else { var image = $(this); var cleanimage = $(this); } //Only create captions if there is content for the caption if(cleanimage.attr(settings.imageAttr) != undefined && (cleanimage.attr(settings.imageAttr).length > 0 || !settings.requireText)){ //Wrap the image with the caption span image.wrap("<" + settings.wrapperElement + " class='" + settings.wrapperClass + "'>"); //Save Image Float var imageFloat = cleanimage.css('float') //Save Image Class var imageClass = cleanimage.attr('class'); if(settings.removeClass) cleanimage.removeAttr('class'); //Save Image Style var imageStyle = cleanimage.attr('style'); if(settings.removeStyle) cleanimage.removeAttr('style'); //Save Image Align var imageAlign = cleanimage.attr('align'); if(settings.removeAlign) cleanimage.removeAttr('align'); //Put Caption in the Wrapper span var span = image.parent().append('<' + settings.captionElement + '>' + cleanimage.attr(settings.imageAttr) + ''); //Add css if there is style markup for the paragraph in the settings //if(settings.styleMarkup) if(settings.styleMarkup) { $('.caption p').attr('style', function() { return settings.styleMarkup; }); } if(settings.animate){ $(this).next().hide(); $(this).parent().hover( function(){ $(this).find('p').animate(settings.show, settings.showDuration); }, function(){ $(this).find('p').animate(settings.hide, settings.hideDuration); }); } //Copy Image Style to span if(settings.copyStyle) span.attr('style',imageStyle); //If there is an alignment on the image (for example align="left") add "left" as a class on the caption. This helps deal with older Text Editors like TinyMCE if(settings.copyAlignmentToClass) span.addClass(imageAlign); //Transfers the float style from the image to the caption container if(settings.copyFloatToClass) span.addClass(imageFloat); //Transfers the class from the image to the caption container if(settings.copyClassToClass) span.addClass(imageClass); //Properly size the caption span based on the loaded image's size if(settings.autoWidth) span.width(image.width()); } }); // Thanks to Captify for this bit! //if the image has already loaded (due to being cached), force the load function to be called if (this.complete || this.naturalWidth > 0){ $(this).trigger('load'); } }); } $(captionSelectors.join(",")).each(function(index, elem){ $(elem).jcaption(); }); })(jQuery); } }; (function ($) { $(document).ready(function(){ }); })(jQuery);