5
See <http://mochikit.com/> for documentation, downloads, license, etc.
7
(c) 2005 Bob Ippolito and others. All rights Reserved.
11
if (typeof(dojo) != 'undefined') {
12
dojo.provide('MochiKit.Visual');
13
dojo.require('MochiKit.Base');
14
dojo.require('MochiKit.DOM');
15
dojo.require('MochiKit.Color');
18
if (typeof(JSAN) != 'undefined') {
19
JSAN.use("MochiKit.Base", []);
20
JSAN.use("MochiKit.DOM", []);
21
JSAN.use("MochiKit.Color", []);
25
if (typeof(MochiKit.Base) == 'undefined' ||
26
typeof(MochiKit.DOM) == 'undefined' ||
27
typeof(MochiKit.Color) == 'undefined') {
31
throw "MochiKit.Visual depends on MochiKit.Base, MochiKit.DOM and MochiKit.Color!";
34
if (typeof(MochiKit.Visual) == "undefined") {
38
MochiKit.Visual.NAME = "MochiKit.Visual";
39
MochiKit.Visual.VERSION = "1.3.1";
41
MochiKit.Visual.__repr__ = function () {
42
return "[" + this.NAME + " " + this.VERSION + "]";
45
MochiKit.Visual.toString = function () {
46
return this.__repr__();
50
MochiKit.Visual._RoundCorners = function (e, options) {
51
e = MochiKit.DOM.getElement(e);
52
this._setOptions(options);
53
if (this.options.__unstable__wrapElement) {
57
var color = this.options.color;
58
var C = MochiKit.Color.Color;
59
if (this.options.color == "fromElement") {
60
color = C.fromBackground(e);
61
} else if (!(color instanceof C)) {
62
color = C.fromString(color);
64
this.isTransparent = (color.asRGB().a <= 0);
66
var bgColor = this.options.bgColor;
67
if (this.options.bgColor == "fromParent") {
68
bgColor = C.fromBackground(e.offsetParent);
69
} else if (!(bgColor instanceof C)) {
70
bgColor = C.fromString(bgColor);
73
this._roundCornersImpl(e, color, bgColor);
76
MochiKit.Visual._RoundCorners.prototype = {
77
_doWrap: function (e) {
78
var parent = e.parentNode;
79
var doc = MochiKit.DOM.currentDocument();
80
if (typeof(doc.defaultView) == "undefined"
81
|| doc.defaultView === null) {
84
var style = doc.defaultView.getComputedStyle(e, null);
85
if (typeof(style) == "undefined" || style === null) {
88
var wrapper = MochiKit.DOM.DIV({"style": {
90
// convert padding to margin
91
marginTop: style.getPropertyValue("padding-top"),
92
marginRight: style.getPropertyValue("padding-right"),
93
marginBottom: style.getPropertyValue("padding-bottom"),
94
marginLeft: style.getPropertyValue("padding-left"),
95
// remove padding so the rounding looks right
102
wrapper.innerHTML = e.innerHTML;
104
e.appendChild(wrapper);
108
_roundCornersImpl: function (e, color, bgColor) {
109
if (this.options.border) {
110
this._renderBorder(e, bgColor);
112
if (this._isTopRounded()) {
113
this._roundTopCorners(e, color, bgColor);
115
if (this._isBottomRounded()) {
116
this._roundBottomCorners(e, color, bgColor);
120
_renderBorder: function (el, bgColor) {
121
var borderValue = "1px solid " + this._borderColor(bgColor);
122
var borderL = "border-left: " + borderValue;
123
var borderR = "border-right: " + borderValue;
124
var style = "style='" + borderL + ";" + borderR + "'";
125
el.innerHTML = "<div " + style + ">" + el.innerHTML + "</div>";
128
_roundTopCorners: function (el, color, bgColor) {
129
var corner = this._createCorner(bgColor);
130
for (var i = 0; i < this.options.numSlices; i++) {
132
this._createCornerSlice(color, bgColor, i, "top")
135
el.style.paddingTop = 0;
136
el.insertBefore(corner, el.firstChild);
139
_roundBottomCorners: function (el, color, bgColor) {
140
var corner = this._createCorner(bgColor);
141
for (var i = (this.options.numSlices - 1); i >= 0; i--) {
143
this._createCornerSlice(color, bgColor, i, "bottom")
146
el.style.paddingBottom = 0;
147
el.appendChild(corner);
150
_createCorner: function (bgColor) {
151
var dom = MochiKit.DOM;
152
return dom.DIV({style: {backgroundColor: bgColor.toString()}});
155
_createCornerSlice: function (color, bgColor, n, position) {
156
var slice = MochiKit.DOM.SPAN();
158
var inStyle = slice.style;
159
inStyle.backgroundColor = color.toString();
160
inStyle.display = "block";
161
inStyle.height = "1px";
162
inStyle.overflow = "hidden";
163
inStyle.fontSize = "1px";
165
var borderColor = this._borderColor(color, bgColor);
166
if (this.options.border && n === 0) {
167
inStyle.borderTopStyle = "solid";
168
inStyle.borderTopWidth = "1px";
169
inStyle.borderLeftWidth = "0px";
170
inStyle.borderRightWidth = "0px";
171
inStyle.borderBottomWidth = "0px";
172
// assumes css compliant box model
173
inStyle.height = "0px";
174
inStyle.borderColor = borderColor.toString();
175
} else if (borderColor) {
176
inStyle.borderColor = borderColor.toString();
177
inStyle.borderStyle = "solid";
178
inStyle.borderWidth = "0px 1px";
181
if (!this.options.compact && (n == (this.options.numSlices - 1))) {
182
inStyle.height = "2px";
185
this._setMargin(slice, n, position);
186
this._setBorder(slice, n, position);
191
_setOptions: function (options) {
194
color: "fromElement",
195
bgColor: "fromParent",
199
__unstable__wrapElement: false
201
MochiKit.Base.update(this.options, options);
203
this.options.numSlices = (this.options.compact ? 2 : 4);
206
_whichSideTop: function () {
207
var corners = this.options.corners;
208
if (this._hasString(corners, "all", "top")) {
212
var has_tl = (corners.indexOf("tl") != -1);
213
var has_tr = (corners.indexOf("tr") != -1);
214
if (has_tl && has_tr) {
226
_whichSideBottom: function () {
227
var corners = this.options.corners;
228
if (this._hasString(corners, "all", "bottom")) {
232
var has_bl = (corners.indexOf('bl') != -1);
233
var has_br = (corners.indexOf('br') != -1);
234
if (has_bl && has_br) {
246
_borderColor: function (color, bgColor) {
247
if (color == "transparent") {
249
} else if (this.options.border) {
250
return this.options.border;
251
} else if (this.options.blend) {
252
return bgColor.blendedColor(color);
258
_setMargin: function (el, n, corners) {
259
var marginSize = this._marginSize(n) + "px";
261
corners == "top" ? this._whichSideTop() : this._whichSideBottom()
263
var style = el.style;
265
if (whichSide == "left") {
266
style.marginLeft = marginSize;
267
style.marginRight = "0px";
268
} else if (whichSide == "right") {
269
style.marginRight = marginSize;
270
style.marginLeft = "0px";
272
style.marginLeft = marginSize;
273
style.marginRight = marginSize;
277
_setBorder: function (el, n, corners) {
278
var borderSize = this._borderSize(n) + "px";
280
corners == "top" ? this._whichSideTop() : this._whichSideBottom()
283
var style = el.style;
284
if (whichSide == "left") {
285
style.borderLeftWidth = borderSize;
286
style.borderRightWidth = "0px";
287
} else if (whichSide == "right") {
288
style.borderRightWidth = borderSize;
289
style.borderLeftWidth = "0px";
291
style.borderLeftWidth = borderSize;
292
style.borderRightWidth = borderSize;
296
_marginSize: function (n) {
297
if (this.isTransparent) {
301
var o = this.options;
302
if (o.compact && o.blend) {
303
var smBlendedMarginSizes = [1, 0];
304
return smBlendedMarginSizes[n];
305
} else if (o.compact) {
306
var compactMarginSizes = [2, 1];
307
return compactMarginSizes[n];
308
} else if (o.blend) {
309
var blendedMarginSizes = [3, 2, 1, 0];
310
return blendedMarginSizes[n];
312
var marginSizes = [5, 3, 2, 1];
313
return marginSizes[n];
317
_borderSize: function (n) {
318
var o = this.options;
320
if (o.compact && (o.blend || this.isTransparent)) {
322
} else if (o.compact) {
323
borderSizes = [1, 0];
324
} else if (o.blend) {
325
borderSizes = [2, 1, 1, 1];
326
} else if (o.border) {
327
borderSizes = [0, 2, 0, 0];
328
} else if (this.isTransparent) {
329
borderSizes = [5, 3, 2, 1];
333
return borderSizes[n];
336
_hasString: function (str) {
337
for (var i = 1; i< arguments.length; i++) {
338
if (str.indexOf(arguments[i]) != -1) {
345
_isTopRounded: function () {
346
return this._hasString(this.options.corners,
347
"all", "top", "tl", "tr"
351
_isBottomRounded: function () {
352
return this._hasString(this.options.corners,
353
"all", "bottom", "bl", "br"
357
_hasSingleTextChild: function (el) {
358
return (el.childNodes.length == 1 && el.childNodes[0].nodeType == 3);
362
MochiKit.Visual.roundElement = function (e, options) {
363
new MochiKit.Visual._RoundCorners(e, options);
366
MochiKit.Visual.roundClass = function (tagName, className, options) {
367
var elements = MochiKit.DOM.getElementsByTagAndClassName(
370
for (var i = 0; i < elements.length; i++) {
371
MochiKit.Visual.roundElement(elements[i], options);
375
// Compatibility with MochiKit 1.0
376
MochiKit.Visual.Color = MochiKit.Color.Color;
377
MochiKit.Visual.getElementsComputedStyle = MochiKit.DOM.computedStyle;
379
/* end of Rico adaptation */
381
MochiKit.Visual.__new__ = function () {
382
var m = MochiKit.Base;
384
m.nameFunctions(this);
387
":common": this.EXPORT,
388
":all": m.concat(this.EXPORT, this.EXPORT_OK)
393
MochiKit.Visual.EXPORT = [
398
MochiKit.Visual.EXPORT_OK = [];
400
MochiKit.Visual.__new__();
402
MochiKit.Base._exportSymbols(this, MochiKit.Visual);