Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
11
dojo.provide("dojo.lfx.rounded");
12
dojo.require("dojo.lang.common");
13
dojo.require("dojo.html.common");
14
dojo.require("dojo.html.style");
15
dojo.require("dojo.html.display");
16
dojo.require("dojo.html.layout");
17
dojo.lfx.rounded = function (settings) {
18
	var options = {validTags:settings.validTags || ["div"], autoPad:settings.autoPad != null ? settings.autoPad : true, antiAlias:settings.antiAlias != null ? settings.antiAlias : true, radii:{tl:(settings.tl && settings.tl.radius != null) ? settings.tl.radius : 5, tr:(settings.tr && settings.tr.radius != null) ? settings.tr.radius : 5, bl:(settings.bl && settings.bl.radius != null) ? settings.bl.radius : 5, br:(settings.br && settings.br.radius != null) ? settings.br.radius : 5}};
19
	var nodes;
20
	if (typeof (arguments[1]) == "string") {
21
		nodes = dojo.html.getElementsByClass(arguments[1]);
22
	} else {
23
		if (dojo.lang.isArrayLike(arguments[1])) {
24
			nodes = arguments[1];
25
			for (var i = 0; i < nodes.length; i++) {
26
				nodes[i] = dojo.byId(nodes[i]);
27
			}
28
		}
29
	}
30
	if (nodes.length == 0) {
31
		return;
32
	}
33
	for (var i = 0; i < nodes.length; i++) {
34
		dojo.lfx.rounded.applyCorners(options, nodes[i]);
35
	}
36
};
37
dojo.lfx.rounded.applyCorners = function (options, node) {
38
	var top = null;
39
	var bottom = null;
40
	var contentNode = null;
41
	var fns = dojo.lfx.rounded._fns;
42
	var width = node.offsetWidth;
43
	var height = node.offsetHeight;
44
	var borderWidth = parseInt(dojo.html.getComputedStyle(node, "border-top-width"));
45
	var borderColor = dojo.html.getComputedStyle(node, "border-top-color");
46
	var color = dojo.html.getComputedStyle(node, "background-color");
47
	var bgImage = dojo.html.getComputedStyle(node, "background-image");
48
	var position = dojo.html.getComputedStyle(node, "position");
49
	var padding = parseInt(dojo.html.getComputedStyle(node, "padding-top"));
50
	var format = {height:height, width:width, borderWidth:borderWidth, color:fns.getRGB(color), padding:padding, borderColor:fns.getRGB(borderColor), borderString:borderWidth + "px" + " solid " + fns.getRGB(borderColor), bgImage:((bgImage != "none") ? bgImage : ""), content:node.innerHTML};
51
	if (!dojo.html.isPositionAbsolute(node)) {
52
		node.style.position = "relative";
53
	}
54
	node.style.padding = "0px";
55
	if (dojo.render.html.ie && width == "auto" && height == "auto") {
56
		node.style.width = "100%";
57
	}
58
	if (options.autoPad && format.padding > 0) {
59
		node.innerHTML = "";
60
	}
61
	var topHeight = Math.max(options.radii.tl, options.radii.tr);
62
	var bottomHeight = Math.max(options.radii.bl, options.radii.br);
63
	if (options.radii.tl || options.radii.tr) {
64
		top = document.createElement("div");
65
		top.style.width = "100%";
66
		top.style.fontSize = "1px";
67
		top.style.overflow = "hidden";
68
		top.style.position = "absolute";
69
		top.style.paddingLeft = format.borderWidth + "px";
70
		top.style.paddingRight = format.borderWidth + "px";
71
		top.style.height = topHeight + "px";
72
		top.style.top = (0 - topHeight) + "px";
73
		top.style.left = (0 - format.borderWidth) + "px";
74
		node.appendChild(top);
75
	}
76
	if (options.radii.bl || options.radii.br) {
77
		bottom = document.createElement("div");
78
		bottom.style.width = "100%";
79
		bottom.style.fontSize = "1px";
80
		bottom.style.overflow = "hidden";
81
		bottom.style.position = "absolute";
82
		bottom.style.paddingLeft = format.borderWidth + "px";
83
		bottom.style.paddingRight = format.borderWidth + "px";
84
		bottom.style.height = bottomHeight + "px";
85
		bottom.style.bottom = (0 - bottomHeight) + "px";
86
		bottom.style.left = (0 - format.borderWidth) + "px";
87
		node.appendChild(bottom);
88
	}
89
	if (top) {
90
		node.style.borderTopWidth = "0px";
91
	}
92
	if (bottom) {
93
		node.style.borderBottomWidth = "0px";
94
	}
95
	var corners = ["tr", "tl", "br", "bl"];
96
	for (var i = 0; i < corners.length; i++) {
97
		var cc = corners[i];
98
		if (options.radii[cc] == 0) {
99
			if ((cc.charAt(0) == "t" && top) || (cc.charAt(0) == "b" && bottom)) {
100
				var corner = document.createElement("div");
101
				corner.style.position = "relative";
102
				corner.style.fontSize = "1px;";
103
				corner.style.overflow = "hidden";
104
				if (format.bgImage == "") {
105
					corner.style.backgroundColor = format.color;
106
				} else {
107
					corner.style.backgroundImage = format.bgImage;
108
				}
109
				switch (cc) {
110
				  case "tl":
111
					corner.style.height = topHeight - format.borderWidth + "px";
112
					corner.style.marginRight = options.radii[cc] - (format.borderWidth * 2) + "px";
113
					corner.style.borderLeft = format.borderString;
114
					corner.style.borderTop = format.borderString;
115
					corner.style.left = -format.borderWidth + "px";
116
					break;
117
				  case "tr":
118
					corner.style.height = topHeight - format.borderWidth + "px";
119
					corner.style.marginLeft = options.radii[cc] - (format.borderWidth * 2) + "px";
120
					corner.style.borderRight = format.borderString;
121
					corner.style.borderTop = format.borderString;
122
					corner.style.backgroundPosition = "-" + (topHeight - format.borderWidth) + "px 0px";
123
					corner.style.left = format.borderWidth + "px";
124
					break;
125
				  case "bl":
126
					corner.style.height = bottomHeight - format.borderWidth + "px";
127
					corner.style.marginRight = options.radii[cc] - (format.borderWidth * 2) + "px";
128
					corner.style.borderLeft = format.borderString;
129
					corner.style.borderBottom = format.borderString;
130
					corner.style.left = format.borderWidth + "px";
131
					corner.style.backgroundPosition = "-" + format.borderWidth + "px -" + (format.height + (bottomHeight + format.borderWidth)) + "px";
132
					break;
133
				  case "br":
134
					corner.style.height = bottomHeight - format.borderWidth + "px";
135
					corner.style.marginLeft = options.radii[cc] - (format.borderWidth * 2) + "px";
136
					corner.style.borderRight = format.borderString;
137
					corner.style.borderBottom = format.borderString;
138
					corner.style.left = format.borderWidth + "px";
139
					corner.style.backgroundPosition = "-" + (bottomHeight + format.borderWidth) + "px -" + (format.height + (bottomHeight + format.borderWidth)) + "px";
140
					break;
141
				}
142
			}
143
		} else {
144
			var corner = document.createElement("div");
145
			corner.style.height = options.radii[cc] + "px";
146
			corner.style.width = options.radii[cc] + "px";
147
			corner.style.position = "absolute";
148
			corner.style.fontSize = "1px";
149
			corner.style.overflow = "hidden";
150
			var borderRadius = Math.floor(options.radii[cc] - format.borderWidth);
151
			for (var x = 0, j = options.radii[cc]; x < j; x++) {
152
				var y1 = Math.floor(Math.sqrt(Math.pow(borderRadius, 2) - Math.pow((x + 1), 2))) - 1;
153
				if ((x + 1) >= borderRadius) {
154
					var y1 = -1;
155
				}
156
				var y2 = Math.ceil(Math.sqrt(Math.pow(borderRadius, 2) - Math.pow(x, 2)));
157
				if (x >= borderRadius) {
158
					y2 = -1;
159
				}
160
				var y3 = Math.floor(Math.sqrt(Math.pow(j, 2) - Math.pow((x + 1), 2))) - 1;
161
				if ((x + 1) >= j) {
162
					y3 = -1;
163
				}
164
				var y4 = Math.ceil(Math.sqrt(Math.pow(j, 2) - Math.pow(x, 2)));
165
				if (x >= j) {
166
					y4 = -1;
167
				}
168
				if (y1 > -1) {
169
					fns.draw(x, 0, format.color, 100, (y1 + 1), corner, -1, j, topHeight, format);
170
				}
171
				for (var y = (y1 + 1); y < y2; y++) {
172
					if (options.antiAlias) {
173
						if (format.bgImage != "") {
174
							var fract = fns.fraction(x, y, borderRadius) * 100;
175
							if (fract < 30) {
176
								fns.draw(x, y, format.borderColor, 100, 1, corner, 0, options.radii[cc], topHeight, format);
177
							} else {
178
								fns.draw(x, y, format.borderColor, 100, 1, corner, -1, options.radii[cc], topHeight, format);
179
							}
180
						} else {
181
							var clr = fns.blend(format.color, format.borderColor, fns.fraction(x, y, borderRadius));
182
							fns.draw(x, y, clr, 100, 1, corner, 0, options.radii[cc], topHeight, format);
183
						}
184
					}
185
				}
186
				if (options.antiAlias) {
187
					if (y3 >= y2) {
188
						if (y2 == -1) {
189
							y2 = 0;
190
						}
191
						fns.draw(x, y2, format.borderColor, 100, (y3 - y2 + 1), corner, 0, 0, topHeight, format);
192
					} else {
193
						if (y3 >= y1) {
194
							fns.draw(x, (y1 + 1), format.borderColor, 100, (y3 - y1), corner, 0, 0, topHeight, format);
195
						}
196
					}
197
					for (var y = (y3 + 1); y < y4; y++) {
198
						fns.draw(x, y, format.borderColor, (fns.fraction(x, y, j) * 100), 1, corner, (format.borderWidth > 0 ? 0 : -1), options.radii[cc], topHeight, format);
199
					}
200
				} else {
201
					y3 = y1;
202
				}
203
			}
204
			if (cc != "br") {
205
				for (var t = 0, k = corner.childNodes.length; t < k; t++) {
206
					var bar = corner.childNodes[t];
207
					var barTop = parseInt(dojo.html.getComputedStyle(bar, "top"));
208
					var barLeft = parseInt(dojo.html.getComputedStyle(bar, "left"));
209
					var barHeight = parseInt(dojo.html.getComputedStyle(bar, "height"));
210
					if (cc.charAt(1) == "l") {
211
						bar.style.left = (options.radii[cc] - barLeft - 1) + "px";
212
					}
213
					if (cc == "tr") {
214
						bar.style.top = (options.radii[cc] - barHeight - barTop) + "px";
215
						bar.style.backgroundPosition = "-" + Math.abs((format.width - options.radii[cc] + format.borderWidth) + barLeft) + "px -" + Math.abs(options.radii[cc] - barHeight - barTop - format.borderWidth) + "px";
216
					} else {
217
						if (cc == "tl") {
218
							bar.style.top = (options.radii[cc] - barHeight - barTop) + "px";
219
							bar.style.backgroundPosition = "-" + Math.abs((options.radii[cc] - barLeft - 1) - format.borderWidth) + "px -" + Math.abs(options.radii[cc] - barHeight - barTop - format.borderWidth) + "px";
220
						} else {
221
							bar.style.backgroundPosition = "-" + Math.abs((options.radii[cc] + barLeft) + format.borderWidth) + "px -" + Math.abs((format.height + options.radii[cc] + barTop) - format.borderWidth) + "px";
222
						}
223
					}
224
				}
225
			}
226
		}
227
		if (corner) {
228
			var psn = [];
229
			if (cc.charAt(0) == "t") {
230
				psn.push("top");
231
			} else {
232
				psn.push("bottom");
233
			}
234
			if (cc.charAt(1) == "l") {
235
				psn.push("left");
236
			} else {
237
				psn.push("right");
238
			}
239
			if (corner.style.position == "absolute") {
240
				for (var z = 0; z < psn.length; z++) {
241
					corner.style[psn[z]] = "0px";
242
				}
243
			}
244
			if (psn[0] == "top") {
245
				if (top) {
246
					top.appendChild(corner);
247
				}
248
			} else {
249
				if (bottom) {
250
					bottom.appendChild(corner);
251
				}
252
			}
253
		}
254
	}
255
	var diff = {t:Math.abs(options.radii.tl - options.radii.tr), b:Math.abs(options.radii.bl - options.radii.br)};
256
	for (var z in diff) {
257
		var smaller = (options.radii[z + "l"] < options.radii[z + "r"] ? z + "l" : z + "r");
258
		var filler = document.createElement("div");
259
		filler.style.height = diff[z] + "px";
260
		filler.style.width = options.radii[smaller] + "px";
261
		filler.style.position = "absolute";
262
		filler.style.fontSize = "1px";
263
		filler.style.overflow = "hidden";
264
		filler.style.backgroundColor = format.color;
265
		switch (smaller) {
266
		  case "tl":
267
			filler.style.bottom = "0px";
268
			filler.style.left = "0px";
269
			filler.style.borderLeft = format.borderString;
270
			top.appendChild(filler);
271
			break;
272
		  case "tr":
273
			filler.style.bottom = "0px";
274
			filler.style.right = "0px";
275
			filler.style.borderRight = format.borderString;
276
			top.appendChild(filler);
277
			break;
278
		  case "bl":
279
			filler.style.top = "0px";
280
			filler.style.left = "0px";
281
			filler.style.borderLeft = format.borderString;
282
			bottom.appendChild(filler);
283
			break;
284
		  case "br":
285
			filler.style.top = "0px";
286
			filler.style.right = "0px";
287
			filler.style.borderRight = format.borderString;
288
			bottom.appendChild(filler);
289
			break;
290
		}
291
		var fillBar = document.createElement("div");
292
		fillBar.style.position = "relative";
293
		fillBar.style.fontSize = "1px";
294
		fillBar.style.overflow = "hidden";
295
		fillBar.style.backgroundColor = format.color;
296
		fillBar.style.backgroundImage = format.bgImage;
297
		if (z == "t") {
298
			if (top) {
299
				if (options.radii.tl && options.radii.tr) {
300
					fillBar.style.height = (topHeight - format.borderWidth) + "px";
301
					fillBar.style.marginLeft = (options.radii.tl - format.borderWidth) + "px";
302
					fillBar.style.marginRight = (options.radii.tr - format.borderWidth) + "px";
303
					fillBar.style.borderTop = format.borderString;
304
					if (format.bgImage != "") {
305
						fillBar.style.backgroundPosition = "-" + (topHeight + format.borderWidth) + "px 0px";
306
					}
307
				}
308
				top.appendChild(fillBar);
309
			}
310
		} else {
311
			if (bottom) {
312
				if (options.radii.bl && options.radii.br) {
313
					fillBar.style.height = (bottomHeight - format.borderWidth) + "px";
314
					fillBar.style.marginLeft = (options.radii.bl - format.borderWidth) + "px";
315
					fillBar.style.marginRight = (options.radii.br - format.borderWidth) + "px";
316
					fillBar.style.borderBottom = format.borderString;
317
					if (format.bgImage != "") {
318
						fillBar.style.backgroundPosition = "-" + (bottomHeight + format.borderWidth) + "px -" + (format.height + (topHeight + format.borderWidth)) + "px";
319
					}
320
				}
321
				bottom.appendChild(fillBar);
322
			}
323
		}
324
	}
325
	if (options.autoPad && format.padding > 0) {
326
		var content = document.createElement("div");
327
		content.style.position = "relative";
328
		content.innerHTML = format.content;
329
		content.className = "autoPadDiv";
330
		if (topHeight < format.padding) {
331
			content.style.paddingTop = Math.abs(topHeight - format.padding) + "px";
332
		}
333
		if (bottomHeight < format.padding) {
334
			content.style.paddingBottom = Math.abs(bottomHeight - format.padding) + "px";
335
		}
336
		content.style.paddingLeft = format.padding + "px";
337
		content.style.paddingRight = format.padding + "px";
338
		node.appendChild(content);
339
	}
340
};
341
var count = 0;
342
dojo.lfx.rounded._fns = {blend:function (clr1, clr2, frac) {
343
	var c1 = {r:parseInt(clr1.substr(1, 2), 16), g:parseInt(clr1.substr(3, 2), 16), b:parseInt(clr1.substr(5, 2), 16)};
344
	var c2 = {r:parseInt(clr2.substr(1, 2), 16), g:parseInt(clr2.substr(3, 2), 16), b:parseInt(clr2.substr(5, 2), 16)};
345
	if (frac > 1 || frac < 0) {
346
		frac = 1;
347
	}
348
	var ret = [Math.min(Math.max(Math.round((c1.r * frac) + (c2.r * (1 - frac))), 0), 255), Math.min(Math.max(Math.round((c1.g * frac) + (c2.g * (1 - frac))), 0), 255), Math.min(Math.max(Math.round((c1.b * frac) + (c2.b * (1 - frac))), 0), 255)];
349
	for (var i = 0; i < ret.length; i++) {
350
		var n = ret[i].toString(16);
351
		if (n.length < 2) {
352
			n = "0" + n;
353
		}
354
		ret[i] = n;
355
	}
356
	return "#" + ret.join("");
357
}, fraction:function (x, y, r) {
358
	var frac = 0;
359
	var xval = [];
360
	var yval = [];
361
	var point = 0;
362
	var whatsides = "";
363
	var intersect = Math.sqrt((Math.pow(r, 2) - Math.pow(x, 2)));
364
	if (intersect >= y && intersect < (y + 1)) {
365
		whatsides = "Left";
366
		xval[point] = 0;
367
		yval[point++] = intersect - y;
368
	}
369
	intersect = Math.sqrt((Math.pow(r, 2) - Math.pow(y + 1, 2)));
370
	if (intersect >= x && intersect < (x + 1)) {
371
		whatsides += "Top";
372
		xval[point] = intersect - x;
373
		yval[point++] = 1;
374
	}
375
	intersect = Math.sqrt((Math.pow(r, 2) - Math.pow(x + 1, 2)));
376
	if (intersect >= y && intersect < (y + 1)) {
377
		whatsides += "Right";
378
		xval[point] = 1;
379
		yval[point++] = intersect - y;
380
	}
381
	intersect = Math.sqrt((Math.pow(r, 2) - Math.pow(y, 2)));
382
	if (intersect >= x && intersect < (x + 1)) {
383
		whatsides += "Bottom";
384
		xval[point] = intersect - x;
385
		yval[point] = 1;
386
	}
387
	switch (whatsides) {
388
	  case "LeftRight":
389
		return Math.min(yval[0], yval[1]) + ((Math.max(yval[0], yval[1]) - Math.min(yval[0], yval[1])) / 2);
390
	  case "TopRight":
391
		return 1 - (((1 - xval[0]) * (1 - yval[1])) / 2);
392
	  case "TopBottom":
393
		return Math.min(xval[0], xval[1]) + ((Math.max(xval[0], xval[1]) - Math.min(xval[0], xval[1])) / 2);
394
	  case "LeftBottom":
395
		return (yval[0] * xval[1]) / 2;
396
	  default:
397
		return 1;
398
	}
399
}, draw:function (x, y, color, opac, height, corner, image, radius, top, format) {
400
	var px = document.createElement("div");
401
	px.style.height = height + "px";
402
	px.style.width = "1px";
403
	px.style.position = "absolute";
404
	px.style.fontSize = "1px";
405
	px.style.overflow = "hidden";
406
	if (image == -1 && format.bgImage != "") {
407
		px.style.backgroundImage = format.bgImage;
408
		px.style.backgroundPosition = "-" + (format.width - (radius - x) + format.borderWidth) + "px -" + ((format.height + top + y) - format.borderWidth) + "px";
409
	} else {
410
		px.style.backgroundColor = color;
411
	}
412
	if (opac != 100) {
413
		dojo.html.setOpacity(px, (opac / 100));
414
	}
415
	px.style.top = y + "px";
416
	px.style.left = x + "px";
417
	corner.appendChild(px);
418
}, getRGB:function (clr) {
419
	var ret = "#ffffff";
420
	if (clr != "" && clr != "transparent") {
421
		if (clr.substr(0, 3) == "rgb") {
422
			var t = clr.substring(4, clr.indexOf(")"));
423
			t = t.split(",");
424
			for (var i = 0; i < t.length; i++) {
425
				var n = parseInt(t[i]).toString(16);
426
				if (n.length < 2) {
427
					n = "0" + n;
428
				}
429
				t[i] = n;
430
			}
431
			ret = "#" + t.join("");
432
		} else {
433
			if (clr.length == 4) {
434
				ret = "#" + clr.substring(1, 2) + clr.substring(1, 2) + clr.substring(2, 3) + clr.substring(2, 3) + clr.substring(3, 4) + clr.substring(3, 4);
435
			} else {
436
				ret = clr;
437
			}
438
		}
439
	}
440
	return ret;
441
}};
442