web/data/jquery.corner.js
changeset 7234 69aa3e68c231
parent 4788 de92a073faa8
equal deleted inserted replaced
7233:614f23606091 7234:69aa3e68c231
     1 /*
     1 /*!
     2  * jQuery corner plugin
     2  * jQuery corner plugin: simple corner rounding
       
     3  * Examples and documentation at: http://jquery.malsup.com/corner/
       
     4  * version 2.11 (15-JUN-2010)
       
     5  * Requires jQuery v1.3.2 or later
       
     6  * Dual licensed under the MIT and GPL licenses:
       
     7  * http://www.opensource.org/licenses/mit-license.php
       
     8  * http://www.gnu.org/licenses/gpl.html
       
     9  * Authors: Dave Methvin and Mike Alsup
       
    10  */
       
    11 
       
    12 /**
       
    13  *  corner() takes a single string argument:  $('#myDiv').corner("effect corners width")
     3  *
    14  *
     4  * version 1.92 (12/18/2007)
    15  *  effect:  name of the effect to apply, such as round, bevel, notch, bite, etc (default is round).
     5  *
    16  *  corners: one or more of: top, bottom, tr, tl, br, or bl.  (default is all corners)
     6  * Dual licensed under the MIT and GPL licenses:
    17  *  width:   width of the effect; in the case of rounded corners this is the radius.
     7  *   http://www.opensource.org/licenses/mit-license.php
    18  *           specify this value using the px suffix such as 10px (yes, it must be pixels).
     8  *   http://www.gnu.org/licenses/gpl.html
       
     9  */
    19  */
    10 
    20 ;(function($) {
    11 /**
    21 
    12  * The corner() method provides a simple way of styling DOM elements.  
    22 var style = document.createElement('div').style,
    13  *
    23     moz = style['MozBorderRadius'] !== undefined,
    14  * corner() takes a single string argument:  $().corner("effect corners width")
    24     webkit = style['WebkitBorderRadius'] !== undefined,
    15  *
    25     radius = style['borderRadius'] !== undefined || style['BorderRadius'] !== undefined,
    16  *   effect:  The name of the effect to apply, such as round or bevel. 
    26     mode = document.documentMode || 0,
    17  *            If you don't specify an effect, rounding is used.
    27     noBottomFold = $.browser.msie && (($.browser.version < 8 && !mode) || mode < 8),
    18  *
    28 
    19  *   corners: The corners can be one or more of top, bottom, tr, tl, br, or bl. 
    29     expr = $.browser.msie && (function() {
    20  *            By default, all four corners are adorned. 
    30         var div = document.createElement('div');
    21  *
    31         try { div.style.setExpression('width','0+0'); div.style.removeExpression('width'); }
    22  *   width:   The width specifies the width of the effect; in the case of rounded corners this 
    32         catch(e) { return false; }
    23  *            will be the radius of the width. 
    33         return true;
    24  *            Specify this value using the px suffix such as 10px, and yes it must be pixels.
    34     })();
    25  *
    35 
    26  * For more details see: http://methvin.com/jquery/jq-corner.html
    36 $.support = $.support || {};
    27  * For a full demo see:  http://malsup.com/jquery/corner/
    37 $.support.borderRadius = moz || webkit || radius; // so you can do:  if (!$.support.borderRadius) $('#myDiv').corner();
    28  *
    38 
    29  *
    39 function sz(el, p) {
    30  * @example $('.adorn').corner();
    40     return parseInt($.css(el,p))||0;
    31  * @desc Create round, 10px corners 
    41 };
    32  *
    42 function hex2(s) {
    33  * @example $('.adorn').corner("25px");
    43     var s = parseInt(s).toString(16);
    34  * @desc Create round, 25px corners 
    44     return ( s.length < 2 ) ? '0'+s : s;
    35  *
    45 };
    36  * @example $('.adorn').corner("notch bottom");
    46 function gpc(node) {
    37  * @desc Create notched, 10px corners on bottom only
    47     while(node) {
    38  *
    48         var v = $.css(node,'backgroundColor'), rgb;
    39  * @example $('.adorn').corner("tr dog 25px");
    49         if (v && v != 'transparent' && v != 'rgba(0, 0, 0, 0)') {
    40  * @desc Create dogeared, 25px corner on the top-right corner only
    50             if (v.indexOf('rgb') >= 0) {
    41  *
    51                 rgb = v.match(/\d+/g);
    42  * @example $('.adorn').corner("round 8px").parent().css('padding', '4px').corner("round 10px");
       
    43  * @desc Create a rounded border effect by styling both the element and its parent
       
    44  * 
       
    45  * @name corner
       
    46  * @type jQuery
       
    47  * @param String options Options which control the corner style
       
    48  * @cat Plugins/Corner
       
    49  * @return jQuery
       
    50  * @author Dave Methvin (dave.methvin@gmail.com)
       
    51  * @author Mike Alsup (malsup@gmail.com)
       
    52  */
       
    53 (function($) { 
       
    54 
       
    55 $.fn.corner = function(o) {
       
    56     var ie6 = $.browser.msie && /MSIE 6.0/.test(navigator.userAgent);
       
    57     function sz(el, p) { return parseInt($.css(el,p))||0; };
       
    58     function hex2(s) {
       
    59         var s = parseInt(s).toString(16);
       
    60         return ( s.length < 2 ) ? '0'+s : s;
       
    61     };
       
    62     function gpc(node) {
       
    63         for ( ; node && node.nodeName.toLowerCase() != 'html'; node = node.parentNode ) {
       
    64             var v = $.css(node,'backgroundColor');
       
    65             if ( v.indexOf('rgb') >= 0 ) { 
       
    66                 if ($.browser.safari && v == 'rgba(0, 0, 0, 0)')
       
    67                     continue;
       
    68                 var rgb = v.match(/\d+/g); 
       
    69                 return '#'+ hex2(rgb[0]) + hex2(rgb[1]) + hex2(rgb[2]);
    52                 return '#'+ hex2(rgb[0]) + hex2(rgb[1]) + hex2(rgb[2]);
    70             }
    53             }
    71             if ( v && v != 'transparent' )
    54             return v;
    72                 return v;
    55         }
    73         }
    56         if (node.nodeName.toLowerCase() == 'html')
    74         return '#ffffff';
    57             break;
    75     };
    58         node = node.parentNode; // keep walking if transparent
    76     function getW(i) {
    59     }
    77         switch(fx) {
    60     return '#ffffff';
    78         case 'round':  return Math.round(width*(1-Math.cos(Math.asin(i/width))));
    61 };
    79         case 'cool':   return Math.round(width*(1+Math.cos(Math.asin(i/width))));
    62 
    80         case 'sharp':  return Math.round(width*(1-Math.cos(Math.acos(i/width))));
    63 function getWidth(fx, i, width) {
    81         case 'bite':   return Math.round(width*(Math.cos(Math.asin((width-i-1)/width))));
    64     switch(fx) {
    82         case 'slide':  return Math.round(width*(Math.atan2(i,width/i)));
    65     case 'round':  return Math.round(width*(1-Math.cos(Math.asin(i/width))));
    83         case 'jut':    return Math.round(width*(Math.atan2(width,(width-i-1))));
    66     case 'cool':   return Math.round(width*(1+Math.cos(Math.asin(i/width))));
    84         case 'curl':   return Math.round(width*(Math.atan(i)));
    67     case 'sharp':  return Math.round(width*(1-Math.cos(Math.acos(i/width))));
    85         case 'tear':   return Math.round(width*(Math.cos(i)));
    68     case 'bite':   return Math.round(width*(Math.cos(Math.asin((width-i-1)/width))));
    86         case 'wicked': return Math.round(width*(Math.tan(i)));
    69     case 'slide':  return Math.round(width*(Math.atan2(i,width/i)));
    87         case 'long':   return Math.round(width*(Math.sqrt(i)));
    70     case 'jut':    return Math.round(width*(Math.atan2(width,(width-i-1))));
    88         case 'sculpt': return Math.round(width*(Math.log((width-i-1),width)));
    71     case 'curl':   return Math.round(width*(Math.atan(i)));
    89         case 'dog':    return (i&1) ? (i+1) : width;
    72     case 'tear':   return Math.round(width*(Math.cos(i)));
    90         case 'dog2':   return (i&2) ? (i+1) : width;
    73     case 'wicked': return Math.round(width*(Math.tan(i)));
    91         case 'dog3':   return (i&3) ? (i+1) : width;
    74     case 'long':   return Math.round(width*(Math.sqrt(i)));
    92         case 'fray':   return (i%2)*width;
    75     case 'sculpt': return Math.round(width*(Math.log((width-i-1),width)));
    93         case 'notch':  return width; 
    76     case 'dogfold':
    94         case 'bevel':  return i+1;
    77     case 'dog':    return (i&1) ? (i+1) : width;
    95         }
    78     case 'dog2':   return (i&2) ? (i+1) : width;
    96     };
    79     case 'dog3':   return (i&3) ? (i+1) : width;
    97     o = (o||"").toLowerCase();
    80     case 'fray':   return (i%2)*width;
    98     var keep = /keep/.test(o);                       // keep borders?
    81     case 'notch':  return width;
    99     var cc = ((o.match(/cc:(#[0-9a-f]+)/)||[])[1]);  // corner color
    82     case 'bevelfold':
   100     var sc = ((o.match(/sc:(#[0-9a-f]+)/)||[])[1]);  // strip color
    83     case 'bevel':  return i+1;
   101     var width = parseInt((o.match(/(\d+)px/)||[])[1]) || 10; // corner width
    84     }
   102     var re = /round|bevel|notch|bite|cool|sharp|slide|jut|curl|tear|fray|wicked|sculpt|long|dog3|dog2|dog/;
    85 };
   103     var fx = ((o.match(re)||['round'])[0]);
    86 
   104     var edges = { T:0, B:1 };
    87 $.fn.corner = function(options) {
   105     var opts = {
    88     // in 1.3+ we can fix mistakes with the ready state
   106         TL:  /top|tl/.test(o),       TR:  /top|tr/.test(o),
    89     if (this.length == 0) {
   107         BL:  /bottom|bl/.test(o),    BR:  /bottom|br/.test(o)
    90         if (!$.isReady && this.selector) {
   108     };
    91             var s = this.selector, c = this.context;
   109     if ( !opts.TL && !opts.TR && !opts.BL && !opts.BR )
    92             $(function() {
   110         opts = { TL:1, TR:1, BL:1, BR:1 };
    93                 $(s,c).corner(options);
   111     var strip = document.createElement('div');
    94             });
   112     strip.style.overflow = 'hidden';
    95         }
   113     strip.style.height = '1px';
    96         return this;
   114     strip.style.backgroundColor = sc || 'transparent';
    97     }
   115     strip.style.borderStyle = 'solid';
    98 
   116     return this.each(function(index){
    99     return this.each(function(index){
   117         var pad = {
   100         var $this = $(this),
       
   101             // meta values override options
       
   102             o = [$this.attr($.fn.corner.defaults.metaAttr) || '', options || ''].join(' ').toLowerCase(),
       
   103             keep = /keep/.test(o),                       // keep borders?
       
   104             cc = ((o.match(/cc:(#[0-9a-f]+)/)||[])[1]),  // corner color
       
   105             sc = ((o.match(/sc:(#[0-9a-f]+)/)||[])[1]),  // strip color
       
   106             width = parseInt((o.match(/(\d+)px/)||[])[1]) || 10, // corner width
       
   107             re = /round|bevelfold|bevel|notch|bite|cool|sharp|slide|jut|curl|tear|fray|wicked|sculpt|long|dog3|dog2|dogfold|dog/,
       
   108             fx = ((o.match(re)||['round'])[0]),
       
   109             fold = /dogfold|bevelfold/.test(o),
       
   110             edges = { T:0, B:1 },
       
   111             opts = {
       
   112                 TL:  /top|tl|left/.test(o),       TR:  /top|tr|right/.test(o),
       
   113                 BL:  /bottom|bl|left/.test(o),    BR:  /bottom|br|right/.test(o)
       
   114             },
       
   115             // vars used in func later
       
   116             strip, pad, cssHeight, j, bot, d, ds, bw, i, w, e, c, common, $horz;
       
   117 
       
   118         if ( !opts.TL && !opts.TR && !opts.BL && !opts.BR )
       
   119             opts = { TL:1, TR:1, BL:1, BR:1 };
       
   120 
       
   121         // support native rounding
       
   122         if ($.fn.corner.defaults.useNative && fx == 'round' && (radius || moz || webkit) && !cc && !sc) {
       
   123             if (opts.TL)
       
   124                 $this.css(radius ? 'border-top-left-radius' : moz ? '-moz-border-radius-topleft' : '-webkit-border-top-left-radius', width + 'px');
       
   125             if (opts.TR)
       
   126                 $this.css(radius ? 'border-top-right-radius' : moz ? '-moz-border-radius-topright' : '-webkit-border-top-right-radius', width + 'px');
       
   127             if (opts.BL)
       
   128                 $this.css(radius ? 'border-bottom-left-radius' : moz ? '-moz-border-radius-bottomleft' : '-webkit-border-bottom-left-radius', width + 'px');
       
   129             if (opts.BR)
       
   130                 $this.css(radius ? 'border-bottom-right-radius' : moz ? '-moz-border-radius-bottomright' : '-webkit-border-bottom-right-radius', width + 'px');
       
   131             return;
       
   132         }
       
   133 
       
   134         strip = document.createElement('div');
       
   135         $(strip).css({
       
   136             overflow: 'hidden',
       
   137             height: '1px',
       
   138             minHeight: '1px',
       
   139             fontSize: '1px',
       
   140             backgroundColor: sc || 'transparent',
       
   141             borderStyle: 'solid'
       
   142         });
       
   143 
       
   144         pad = {
   118             T: parseInt($.css(this,'paddingTop'))||0,     R: parseInt($.css(this,'paddingRight'))||0,
   145             T: parseInt($.css(this,'paddingTop'))||0,     R: parseInt($.css(this,'paddingRight'))||0,
   119             B: parseInt($.css(this,'paddingBottom'))||0,  L: parseInt($.css(this,'paddingLeft'))||0
   146             B: parseInt($.css(this,'paddingBottom'))||0,  L: parseInt($.css(this,'paddingLeft'))||0
   120         };
   147         };
   121 
   148 
   122         if ($.browser.msie) this.style.zoom = 1; // force 'hasLayout' in IE
   149         if (typeof this.style.zoom != undefined) this.style.zoom = 1; // force 'hasLayout' in IE
   123         if (!keep) this.style.border = 'none';
   150         if (!keep) this.style.border = 'none';
   124         strip.style.borderColor = cc || gpc(this.parentNode);
   151         strip.style.borderColor = cc || gpc(this.parentNode);
   125         var cssHeight = $.curCSS(this, 'height');
   152         cssHeight = $(this).outerHeight();
   126 
   153 
   127         for (var j in edges) {
   154         for (j in edges) {
   128             var bot = edges[j];
   155             bot = edges[j];
   129             // only add stips if needed
   156             // only add stips if needed
   130             if ((bot && (opts.BL || opts.BR)) || (!bot && (opts.TL || opts.TR))) {
   157             if ((bot && (opts.BL || opts.BR)) || (!bot && (opts.TL || opts.TR))) {
   131                 strip.style.borderStyle = 'none '+(opts[j+'R']?'solid':'none')+' none '+(opts[j+'L']?'solid':'none');
   158                 strip.style.borderStyle = 'none '+(opts[j+'R']?'solid':'none')+' none '+(opts[j+'L']?'solid':'none');
   132                 var d = document.createElement('div');
   159                 d = document.createElement('div');
   133                 $(d).addClass('jquery-corner');
   160                 $(d).addClass('jquery-corner');
   134                 var ds = d.style;
   161                 ds = d.style;
   135 
   162 
   136                 bot ? this.appendChild(d) : this.insertBefore(d, this.firstChild);
   163                 bot ? this.appendChild(d) : this.insertBefore(d, this.firstChild);
   137 
   164 
   138                 if (bot && cssHeight != 'auto') {
   165                 if (bot && cssHeight != 'auto') {
   139                     if ($.css(this,'position') == 'static')
   166                     if ($.css(this,'position') == 'static')
   140                         this.style.position = 'relative';
   167                         this.style.position = 'relative';
   141                     ds.position = 'absolute';
   168                     ds.position = 'absolute';
   142                     ds.bottom = ds.left = ds.padding = ds.margin = '0';
   169                     ds.bottom = ds.left = ds.padding = ds.margin = '0';
   143                     if (($.browser.msie) && ($.browser.version < 8.0))
   170                     if (expr)
   144                         ds.setExpression('width', 'this.parentNode.offsetWidth');
   171                         ds.setExpression('width', 'this.parentNode.offsetWidth');
   145                     else
   172                     else
   146                         ds.width = '100%';
   173                         ds.width = '100%';
   147                 }
   174                 }
   148                 else if (!bot && $.browser.msie) {
   175                 else if (!bot && $.browser.msie) {
   149                     if ($.css(this,'position') == 'static')
   176                     if ($.css(this,'position') == 'static')
   150                         this.style.position = 'relative';
   177                         this.style.position = 'relative';
   151                     ds.position = 'absolute';
   178                     ds.position = 'absolute';
   152                     ds.top = ds.left = ds.right = ds.padding = ds.margin = '0';
   179                     ds.top = ds.left = ds.right = ds.padding = ds.margin = '0';
   153                     
   180 
   154                     // fix ie6 problem when blocked element has a border width
   181                     // fix ie6 problem when blocked element has a border width
   155                     var bw = 0;
   182                     if (expr) {
   156                     if (ie6 || !$.boxModel)
       
   157                         bw = sz(this,'borderLeftWidth') + sz(this,'borderRightWidth');
   183                         bw = sz(this,'borderLeftWidth') + sz(this,'borderRightWidth');
   158                     ie6 ? ds.setExpression('width', 'this.parentNode.offsetWidth - '+bw+'+ "px"') : ds.width = '100%';
   184                         ds.setExpression('width', 'this.parentNode.offsetWidth - '+bw+'+ "px"');
       
   185                     }
       
   186                     else
       
   187                         ds.width = '100%';
   159                 }
   188                 }
   160                 else {
   189                 else {
   161                     ds.margin = !bot ? '-'+pad.T+'px -'+pad.R+'px '+(pad.T-width)+'px -'+pad.L+'px' : 
   190                     ds.position = 'relative';
   162                                         (pad.B-width)+'px -'+pad.R+'px -'+pad.B+'px -'+pad.L+'px';                
   191                     ds.margin = !bot ? '-'+pad.T+'px -'+pad.R+'px '+(pad.T-width)+'px -'+pad.L+'px' :
   163                 }
   192                                         (pad.B-width)+'px -'+pad.R+'px -'+pad.B+'px -'+pad.L+'px';
   164 
   193                 }
   165                 for (var i=0; i < width; i++) {
   194 
   166                     var w = Math.max(0,getW(i));
   195                 for (i=0; i < width; i++) {
   167                     var e = strip.cloneNode(false);
   196                     w = Math.max(0,getWidth(fx,i, width));
       
   197                     e = strip.cloneNode(false);
   168                     e.style.borderWidth = '0 '+(opts[j+'R']?w:0)+'px 0 '+(opts[j+'L']?w:0)+'px';
   198                     e.style.borderWidth = '0 '+(opts[j+'R']?w:0)+'px 0 '+(opts[j+'L']?w:0)+'px';
   169                     bot ? d.appendChild(e) : d.insertBefore(e, d.firstChild);
   199                     bot ? d.appendChild(e) : d.insertBefore(e, d.firstChild);
   170                 }
   200                 }
       
   201 
       
   202                 if (fold && $.support.boxModel) {
       
   203                     if (bot && noBottomFold) continue;
       
   204                     for (c in opts) {
       
   205                         if (!opts[c]) continue;
       
   206                         if (bot && (c == 'TL' || c == 'TR')) continue;
       
   207                         if (!bot && (c == 'BL' || c == 'BR')) continue;
       
   208 
       
   209                         common = { position: 'absolute', border: 'none', margin: 0, padding: 0, overflow: 'hidden', backgroundColor: strip.style.borderColor };
       
   210                         $horz = $('<div/>').css(common).css({ width: width + 'px', height: '1px' });
       
   211                         switch(c) {
       
   212                         case 'TL': $horz.css({ bottom: 0, left: 0 }); break;
       
   213                         case 'TR': $horz.css({ bottom: 0, right: 0 }); break;
       
   214                         case 'BL': $horz.css({ top: 0, left: 0 }); break;
       
   215                         case 'BR': $horz.css({ top: 0, right: 0 }); break;
       
   216                         }
       
   217                         d.appendChild($horz[0]);
       
   218 
       
   219                         var $vert = $('<div/>').css(common).css({ top: 0, bottom: 0, width: '1px', height: width + 'px' });
       
   220                         switch(c) {
       
   221                         case 'TL': $vert.css({ left: width }); break;
       
   222                         case 'TR': $vert.css({ right: width }); break;
       
   223                         case 'BL': $vert.css({ left: width }); break;
       
   224                         case 'BR': $vert.css({ right: width }); break;
       
   225                         }
       
   226                         d.appendChild($vert[0]);
       
   227                     }
       
   228                 }
   171             }
   229             }
   172         }
   230         }
   173     });
   231     });
   174 };
   232 };
   175 
   233 
   176 $.fn.uncorner = function(o) { return $('.jquery-corner', this).remove(); };
   234 $.fn.uncorner = function() {
   177     
   235     if (radius || moz || webkit)
       
   236         this.css(radius ? 'border-radius' : moz ? '-moz-border-radius' : '-webkit-border-radius', 0);
       
   237     $('div.jquery-corner', this).remove();
       
   238     return this;
       
   239 };
       
   240 
       
   241 // expose options
       
   242 $.fn.corner.defaults = {
       
   243     useNative: true, // true if plugin should attempt to use native browser support for border radius rounding
       
   244     metaAttr:  'data-corner' // name of meta attribute to use for options
       
   245 };
       
   246 
   178 })(jQuery);
   247 })(jQuery);