~myfreeweb/jacg/mainline

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
  JACG: Just Another Corners&Gradients.
  Requires jQuery (tested on 1.4.1, but no new features used).
  Optionally uses base64 ( http://plugins.jquery.com/project/base64 ).

  Copyright 2010 MyFreeWeb
  
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  
  http://www.apache.org/licenses/LICENSE-2.0
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/
function svgradient(start, end, round, horiz) {
  if (typeof(round) != 'undefined') {
    round.replace('px', '');
    var r = 'rx="' + round + '" ry="' + round + '"';
  }
  else { var r = ''; }
  if (horiz == true) {
    var dir = 'x1="0%" y1="0%" x2="100%" y2="0%"';
  }
  else {
    var dir = 'x1="0%" y1="0%" x2="0%" y2="100%"';
  }
  var svg = '<?xml version="1.0" ?><svg xmlns="http://www.w3.org/2000/svg"><defs><linearGradient id="gr" ' + dir + '><stop offset="0%" style="stop-color:' +  start + '"/><stop offset="100%" style="stop-color:' + end + '"/></linearGradient></defs><rect x="0" y="0" ' + r + ' width="100%" height="100%" style="fill:url(#gr)"/></svg>';
  if (jQuery.base64Encode) {
    return 'data:image/svg+xml;base64,' + jQuery.base64Encode(svg);
  }
  else {
    return 'data:image/svg+xml,' + escape(svg);
  }
}
function webgr(obj, start, end, height, padding) { // It doesn't support horizontal gradients for now...
  he = parseInt(height.replace('px', '')) + parseInt(padding.replace('px', ''))*2; // This is strange, really. But I have to.
  return obj.css({'background':
      'url("http://webgradients.appspot.com/make?width=1&height=' + he + '&start=' + start.replace('#', '') +'&end=' + end.replace('#', '') + '") repeat-x'});
}
(function($){  
  $.fn.extend({
    jacg: function(options) {
	var defaults = {
	radius: '15px',
	start: '#dddddd',
	end: '#cccccc',
	gradient: true,
        horizontal: false,
	topleft: true,
	topright: true,
	bottomleft: true,
	bottomright: true,
	opera_prefer: 'image' // image or corners
	}
	var o = $.extend(defaults, options);
	return this.each(function() {
	    if (o.topleft == false) { tl = '0' } else if (o.topleft == true) { tl = o.radius }
	    if (o.topright == false) { tr = '0' } else if (o.topright == true) { tr = o.radius }
	    if (o.bottomleft == false) { bl = '0' } else if (o.bottomleft == true) { bl = o.radius }
	    if (o.bottomright == false) { br = '0' } else if (o.bottomright == true) { br = o.radius }
	    radius = [tl, tr, br, bl].join(' ');
	    //console.log(radius);
	    obj = $(this).css({'border-radius': radius}); // I can haz chains!

	    if ($.browser.webkit == true) { // WebKit: Safari, Chrome, Midori, etc.
		obj.css('-webkit-border-radius', radius);
		if (o.gradient == true) {
		    if (o.horizontal == true) {
			var dir = 'left top, right top';
		    }
		    else {
			var dir = 'left top, left bottom';
		    }
		    obj.css('background', '-webkit-gradient(linear, ' + dir + ', from(' + o.start + '), to(' + o.end + '))');
		}
	    }
	    else if ($.browser.mozilla == true) { // Gecko: Firefox, Flock, Conkeror, etc.
		obj.css('-moz-border-radius', radius);
		if (o.gradient == true) {
		    if (parseInt($.browser.version.replace('.', '').replace('.', '')) < 192) { // Gecko 1.9.2 is in Fx 3.6
			// Firefox <= 3.5 has no support for CSS gradients
			// So we use Web Gradients (that's my app too!)
			webgr(obj, o.start, o.end, obj.css('height'), obj.css('padding-bottom'));
		    }
		    else {
			if (o.horizontal == true) {
			    var dir = 'left';
			}
			else {
			    var dir = 'top';
			}
			obj.css('background', '-moz-linear-gradient(' + dir + ', ' + o.start + ', ' + o.end + ')');
		    }
		}
	    }
	    else if ($.browser.opera == true) { // Presto: Opera
		if (opera.version() >= 10.50) {
		    if (o.gradient == true) {
			obj.css({'background': 'url("' + svgradient(o.start, o.end, undefined, o.horizontal) + '")'});
		    }
		    // The corners are specified at top of the fuction
		}
		else {
		    if (o.gradient == true) {
			obj.css({'background': 'url("' + svgradient(o.start, o.end, o.radius, o.horizontal) + '")'});
		    }
		    else if (o.opera_prefer == 'corners') {
			bgcolor = obj.css('background-color');
			obj.css({'background': 'url("' + svgradient(bgcolor, bgcolor, o.radius) + '")'});	
		    }
		}
	    }
	    else { // Any other browser
		if (o.gradient == true) {
		    webgr(obj, o.start, o.end, obj.height(), obj.css('padding-bottom'));
		}
	    }
	  });
    }
  });
})(jQuery);