Debian Codegraph @

debian/missing-sources/jcaption/jcaption.js

raw symbols SCIP
1 /*
2 * Copyright (C) 2009 Joel Sutherland
3 * Licenced under the MIT license
4 */
5 (function($) {
6 $.fn.jcaption = function(settings) {
7 settings = $.extend({
8 wrapperElement: 'div',
9 wrapperClass: 'caption',
10 captionElement: 'p',
11 imageAttr: 'alt',
12 copyStyle: true,
13 removeStyle: true,
14 removeAlign: true,
15 copyAlignmentToClass: true,
16 copyFloatToClass: true,
17 autoWidth: true,
18 animate: false,
19 show: {opacity: 'show'},
20 showDuration: 200,
21 hide: {opacity: 'hide'},
22 hideDuration: 200
23 }, settings);
24
25 $(this).each(function(){
26 //Only add the caption after the image has been loaded. This makes sure we can know the width of the caption.
27
28 $(this).bind('load', function(){
29
30 //Make sure the captioning isn't applied twice when the IE fix at the bottom is applied
31 if($(this).data('loaded')) return false;
32 $(this).data('loaded', true);
33
34 //Shorthand for the image we will be applying the caption to
35 var image = $(this);
36
37 //Only create captions if there is content for the caption
38 if(image.attr(settings.imageAttr).length > 0){
39
40 //Wrap the image with the caption div
41 image.wrap("<" + settings.wrapperElement + " class='" + settings.wrapperClass + "'></" + settings.wrapperElement + ">");
42
43 //Save float if exists
44 var imageStyleFloat = image.css('float');
45
46 //Save Image Style
47 var imageStyle = image.attr('style');
48 if(settings.removeStyle) image.removeAttr('style');
49
50 //Save Image Align
51 var imageAlign = image.attr('align');
52 if(settings.removeAlign) image.removeAttr('align');
53
54 //Put Caption in the Wrapper Div
55 var div = $(this).parent().append('<' + settings.captionElement + '>' + image.attr(settings.imageAttr) + '</' + settings.captionElement + '>');
56
57 if(settings.animate){
58 $(this).next().hide();
59 $(this).parent().hover(
60 function(){
61 $(this).find('p').animate(settings.show, settings.showDuration);
62 },
63 function(){
64 $(this).find('p').animate(settings.hide, settings.hideDuration);
65 });
66 }
67
68 //Copy Image Style to Div
69 if(settings.copyStyle) div.attr('style',imageStyle);
70
71 //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
72 if(settings.copyAlignmentToClass) div.addClass(imageAlign);
73
74 //Set float to class
75 if(settings.copyFloatToClass && imageStyleFloat.length > 0) div.addClass(imageStyleFloat);
76
77 //Properly size the caption div based on the loaded image's size
78 if(settings.autoWidth) div.width(image.width());
79 }
80 });
81
82 // Thanks to Captify for this bit!
83 //if the image has already loaded (due to being cached), force the load function to be called
84 if (this.complete || this.naturalWidth > 0){
85 $(this).trigger('load');
86 }
87 });
88 }
89 })(jQuery);