84b7681a547ee3ffc3dee273de782e04d883fef5
[mevemon] / www / js / jquery.corner.js
1 /*!
2  * jQuery corner plugin: simple corner rounding
3  * Examples and documentation at: http://jquery.malsup.com/corner/
4  * version 2.01 (08-SEP-2009)
5  * Dual licensed under the MIT and GPL licenses:
6  * http://www.opensource.org/licenses/mit-license.php
7  * http://www.gnu.org/licenses/gpl.html
8  */
9
10 /**
11  *  corner() takes a single string argument:  $('#myDiv').corner("effect corners width")
12  *
13  *  effect:  name of the effect to apply, such as round, bevel, notch, bite, etc (default is round). 
14  *  corners: one or more of: top, bottom, tr, tl, br, or bl. 
15  *           by default, all four corners are adorned. 
16  *  width:   width of the effect; in the case of rounded corners this is the radius. 
17  *           specify this value using the px suffix such as 10px (and yes, it must be pixels).
18  *
19  * @author Dave Methvin (http://methvin.com/jquery/jq-corner.html)
20  * @author Mike Alsup   (http://jquery.malsup.com/corner/)
21  */
22 ;(function($) { 
23
24 var moz = $.browser.mozilla && /gecko/i.test(navigator.userAgent);
25 var webkit = $.browser.safari && $.browser.version >= 3;
26
27 var expr = $.browser.msie && (function() {
28     var div = document.createElement('div');
29     try { div.style.setExpression('width','0+0'); }
30     catch(e) { return false; }
31     return true;
32 })();
33     
34 function sz(el, p) { 
35     return parseInt($.css(el,p))||0; 
36 };
37 function hex2(s) {
38     var s = parseInt(s).toString(16);
39     return ( s.length < 2 ) ? '0'+s : s;
40 };
41 function gpc(node) {
42     for ( ; node && node.nodeName.toLowerCase() != 'html'; node = node.parentNode ) {
43         var v = $.css(node,'backgroundColor');
44         if (v == 'rgba(0, 0, 0, 0)')
45             continue; // webkit
46         if (v.indexOf('rgb') >= 0) { 
47             var rgb = v.match(/\d+/g); 
48             return '#'+ hex2(rgb[0]) + hex2(rgb[1]) + hex2(rgb[2]);
49         }
50         if ( v && v != 'transparent' )
51             return v;
52     }
53     return '#ffffff';
54 };
55
56 function getWidth(fx, i, width) {
57     switch(fx) {
58     case 'round':  return Math.round(width*(1-Math.cos(Math.asin(i/width))));
59     case 'cool':   return Math.round(width*(1+Math.cos(Math.asin(i/width))));
60     case 'sharp':  return Math.round(width*(1-Math.cos(Math.acos(i/width))));
61     case 'bite':   return Math.round(width*(Math.cos(Math.asin((width-i-1)/width))));
62     case 'slide':  return Math.round(width*(Math.atan2(i,width/i)));
63     case 'jut':    return Math.round(width*(Math.atan2(width,(width-i-1))));
64     case 'curl':   return Math.round(width*(Math.atan(i)));
65     case 'tear':   return Math.round(width*(Math.cos(i)));
66     case 'wicked': return Math.round(width*(Math.tan(i)));
67     case 'long':   return Math.round(width*(Math.sqrt(i)));
68     case 'sculpt': return Math.round(width*(Math.log((width-i-1),width)));
69     case 'dog':    return (i&1) ? (i+1) : width;
70     case 'dog2':   return (i&2) ? (i+1) : width;
71     case 'dog3':   return (i&3) ? (i+1) : width;
72     case 'fray':   return (i%2)*width;
73     case 'notch':  return width; 
74     case 'bevel':  return i+1;
75     }
76 };
77
78 $.fn.corner = function(options) {
79     // in 1.3+ we can fix mistakes with the ready state
80         if (this.length == 0) {
81         if (!$.isReady && this.selector) {
82             var s = this.selector, c = this.context;
83             $(function() {
84                 $(s,c).corner(options);
85             });
86         }
87         return this;
88         }
89
90     return this.each(function(index){
91                 var $this = $(this);
92                 var o = (options || $this.attr($.fn.corner.defaults.metaAttr) || '').toLowerCase();
93                 var keep = /keep/.test(o);                       // keep borders?
94                 var cc = ((o.match(/cc:(#[0-9a-f]+)/)||[])[1]);  // corner color
95                 var sc = ((o.match(/sc:(#[0-9a-f]+)/)||[])[1]);  // strip color
96                 var width = parseInt((o.match(/(\d+)px/)||[])[1]) || 10; // corner width
97                 var re = /round|bevel|notch|bite|cool|sharp|slide|jut|curl|tear|fray|wicked|sculpt|long|dog3|dog2|dog/;
98                 var fx = ((o.match(re)||['round'])[0]);
99                 var edges = { T:0, B:1 };
100                 var opts = {
101                         TL:  /top|tl|left/.test(o),       TR:  /top|tr|right/.test(o),
102                         BL:  /bottom|bl|left/.test(o),    BR:  /bottom|br|right/.test(o)
103                 };
104                 if ( !opts.TL && !opts.TR && !opts.BL && !opts.BR )
105                         opts = { TL:1, TR:1, BL:1, BR:1 };
106                         
107                 // support native rounding
108                 if ($.fn.corner.defaults.useNative && fx == 'round' && (moz || webkit) && !cc && !sc) {
109                         if (opts.TL)
110                                 $this.css(moz ? '-moz-border-radius-topleft' : '-webkit-border-top-left-radius', width + 'px');
111                         if (opts.TR)
112                                 $this.css(moz ? '-moz-border-radius-topright' : '-webkit-border-top-right-radius', width + 'px');
113                         if (opts.BL)
114                                 $this.css(moz ? '-moz-border-radius-bottomleft' : '-webkit-border-bottom-left-radius', width + 'px');
115                         if (opts.BR)
116                                 $this.css(moz ? '-moz-border-radius-bottomright' : '-webkit-border-bottom-right-radius', width + 'px');
117                         return;
118                 }
119                         
120                 var strip = document.createElement('div');
121                 strip.style.overflow = 'hidden';
122                 strip.style.height = '1px';
123                 strip.style.backgroundColor = sc || 'transparent';
124                 strip.style.borderStyle = 'solid';
125         
126         var pad = {
127             T: parseInt($.css(this,'paddingTop'))||0,     R: parseInt($.css(this,'paddingRight'))||0,
128             B: parseInt($.css(this,'paddingBottom'))||0,  L: parseInt($.css(this,'paddingLeft'))||0
129         };
130
131         if (typeof this.style.zoom != undefined) this.style.zoom = 1; // force 'hasLayout' in IE
132         if (!keep) this.style.border = 'none';
133         strip.style.borderColor = cc || gpc(this.parentNode);
134         var cssHeight = $.curCSS(this, 'height');
135
136         for (var j in edges) {
137             var bot = edges[j];
138             // only add stips if needed
139             if ((bot && (opts.BL || opts.BR)) || (!bot && (opts.TL || opts.TR))) {
140                 strip.style.borderStyle = 'none '+(opts[j+'R']?'solid':'none')+' none '+(opts[j+'L']?'solid':'none');
141                 var d = document.createElement('div');
142                 $(d).addClass('jquery-corner');
143                 var ds = d.style;
144
145                 bot ? this.appendChild(d) : this.insertBefore(d, this.firstChild);
146
147                 if (bot && cssHeight != 'auto') {
148                     if ($.css(this,'position') == 'static')
149                         this.style.position = 'relative';
150                     ds.position = 'absolute';
151                     ds.bottom = ds.left = ds.padding = ds.margin = '0';
152                     if (expr)
153                         ds.setExpression('width', 'this.parentNode.offsetWidth');
154                     else
155                         ds.width = '100%';
156                 }
157                 else if (!bot && $.browser.msie) {
158                     if ($.css(this,'position') == 'static')
159                         this.style.position = 'relative';
160                     ds.position = 'absolute';
161                     ds.top = ds.left = ds.right = ds.padding = ds.margin = '0';
162                     
163                     // fix ie6 problem when blocked element has a border width
164                     if (expr) {
165                         var bw = sz(this,'borderLeftWidth') + sz(this,'borderRightWidth');
166                         ds.setExpression('width', 'this.parentNode.offsetWidth - '+bw+'+ "px"');
167                     }
168                     else
169                         ds.width = '100%';
170                 }
171                 else {
172                         ds.position = 'relative';
173                     ds.margin = !bot ? '-'+pad.T+'px -'+pad.R+'px '+(pad.T-width)+'px -'+pad.L+'px' : 
174                                         (pad.B-width)+'px -'+pad.R+'px -'+pad.B+'px -'+pad.L+'px';                
175                 }
176
177                 for (var i=0; i < width; i++) {
178                     var w = Math.max(0,getWidth(fx,i, width));
179                     var e = strip.cloneNode(false);
180                     e.style.borderWidth = '0 '+(opts[j+'R']?w:0)+'px 0 '+(opts[j+'L']?w:0)+'px';
181                     bot ? d.appendChild(e) : d.insertBefore(e, d.firstChild);
182                 }
183             }
184         }
185     });
186 };
187
188 $.fn.uncorner = function() { 
189         if (moz || webkit)
190                 this.css(moz ? '-moz-border-radius' : '-webkit-border-radius', 0);
191         $('div.jquery-corner', this).remove();
192         return this;
193 };
194
195 // expose options
196 $.fn.corner.defaults = {
197         useNative: true, // true if plugin should attempt to use native browser support for border radius rounding
198         metaAttr:  'data-corner' // name of meta attribute to use for options
199 };
200     
201 })(jQuery);