[SPIP] +2.1.12
[velocampus/web/www.git] / www / extensions / msie_compat / javascript / jquery.ifixpng.js
1 /*
2 * jQuery ifixpng plugin
3 * (previously known as pngfix)
4 * Version 2.1 (23/04/2008)
5 * @requires jQuery v1.1.3 or above
6 *
7 * Examples at: http://jquery.khurshid.com
8 * Copyright (c) 2007 Kush M.
9 * Dual licensed under the MIT and GPL licenses:
10 * http://www.opensource.org/licenses/mit-license.php
11 * http://www.gnu.org/licenses/gpl.html
12 */
13
14 /**
15 *
16 * @example
17 *
18 * optional if location of pixel.gif if different to default which is images/pixel.gif
19 * $.ifixpng('media/pixel.gif');
20 *
21 * $('img[src$=.png], #panel').ifixpng();
22 *
23 * @apply hack to all png images and #panel which icluded png img in its css
24 *
25 * @name ifixpng
26 * @type jQuery
27 * @cat Plugins/Image
28 * @return jQuery
29 * @author jQuery Community
30 */
31
32 (function($) {
33
34 /**
35 * helper variables and function
36 */
37 $.ifixpng = function(customPixel) {
38 $.ifixpng.pixel = customPixel;
39 };
40
41 $.ifixpng.getPixel = function() {
42 return $.ifixpng.pixel || 'images/pixel.gif';
43 };
44
45 var hack = {
46 ltie7 : $.browser.msie && $.browser.version < 7,
47 filter : function(src) {
48 return "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src='"+src+"')";
49 }
50 };
51
52 /**
53 * Applies ie png hack to selected dom elements
54 *
55 * $('img[src$=.png]').ifixpng();
56 * @desc apply hack to all images with png extensions
57 *
58 * $('#panel, img[src$=.png]').ifixpng();
59 * @desc apply hack to element #panel and all images with png extensions
60 *
61 * @name ifixpng
62 */
63
64 $.fn.ifixpng = hack.ltie7 ? function() {
65 return this.each(function() {
66 var $$ = $(this);
67 // in case rewriting urls
68 var base = $('base').attr('href');
69 if (base) {
70 // remove anything after the last '/'
71 base = base.replace(/\/[^\/]+$/,'/');
72 }
73 if ($$.is('img') || $$.is('input')) { // hack image tags present in dom
74 if ($$.attr('src')) {
75 if ($$.attr('src').match(/.*\.png([?].*)?$/i)) { // make sure it is png image
76 // use source tag value if set
77 var source = (base && $$.attr('src').search(/^(\/|http:)/i)) ? base + $$.attr('src') : $$.attr('src');
78 // apply filter
79 $$.css({filter:hack.filter(source), width:$$.width(), height:$$.height()})
80 .attr({src:$.ifixpng.getPixel()})
81 .positionFix();
82 }
83 }
84 } else { // hack png css properties present inside css
85 var image = $$.css('backgroundImage');
86 if (image.match(/^url\(["']?(.*\.png([?].*)?)["']?\)$/i)) {
87 image = RegExp.$1;
88 image = (base && image.substring(0,1)!='/') ? base + image : image;
89 $$.css({backgroundImage:'none', filter:hack.filter(image)})
90 .children().children().positionFix();
91 }
92 }
93 });
94 } : function() { return this; };
95
96 /**
97 * Removes any png hack that may have been applied previously
98 *
99 * $('img[src$=.png]').iunfixpng();
100 * @desc revert hack on all images with png extensions
101 *
102 * $('#panel, img[src$=.png]').iunfixpng();
103 * @desc revert hack on element #panel and all images with png extensions
104 *
105 * @name iunfixpng
106 */
107
108 $.fn.iunfixpng = hack.ltie7 ? function() {
109 return this.each(function() {
110 var $$ = $(this);
111 var src = $$.css('filter');
112 if (src.match(/src=["']?(.*\.png([?].*)?)["']?/i)) { // get img source from filter
113 src = RegExp.$1;
114 if ($$.is('img') || $$.is('input')) {
115 $$.attr({src:src}).css({filter:''});
116 } else {
117 $$.css({filter:'', background:'url('+src+')'});
118 }
119 }
120 });
121 } : function() { return this; };
122
123 /**
124 * positions selected item relatively
125 */
126
127 $.fn.positionFix = function() {
128 return this.each(function() {
129 var $$ = $(this);
130 var position = $$.css('position');
131 if (position != 'absolute' && position != 'relative') {
132 $$.css({position:'relative'});
133 }
134 });
135 };
136
137 })(jQuery);