~ubuntu-branches/ubuntu/trusty/ntop/trusty

« back to all changes in this revision

Viewing changes to html/MochiKit/Visual.js

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2008-06-15 14:38:28 UTC
  • mfrom: (2.1.11 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080615143828-oalh84nda2hje4do
Tags: 3:3.3-11
Correction of Polish translation encoding, closes: #479490. Thanks
to Christian Perrier <bubulle@debian.org> for the help.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***
 
2
 
 
3
MochiKit.Visual 1.3.1
 
4
 
 
5
See <http://mochikit.com/> for documentation, downloads, license, etc.
 
6
 
 
7
(c) 2005 Bob Ippolito and others.  All rights Reserved.
 
8
 
 
9
***/
 
10
 
 
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');
 
16
}
 
17
 
 
18
if (typeof(JSAN) != 'undefined') {
 
19
    JSAN.use("MochiKit.Base", []);
 
20
    JSAN.use("MochiKit.DOM", []);
 
21
    JSAN.use("MochiKit.Color", []);
 
22
}
 
23
 
 
24
try {
 
25
    if (typeof(MochiKit.Base) == 'undefined' ||
 
26
        typeof(MochiKit.DOM) == 'undefined' ||
 
27
        typeof(MochiKit.Color) == 'undefined') {
 
28
        throw "";
 
29
    }
 
30
} catch (e) {
 
31
    throw "MochiKit.Visual depends on MochiKit.Base, MochiKit.DOM and MochiKit.Color!";
 
32
}
 
33
 
 
34
if (typeof(MochiKit.Visual) == "undefined") {
 
35
    MochiKit.Visual = {};
 
36
}
 
37
 
 
38
MochiKit.Visual.NAME = "MochiKit.Visual";
 
39
MochiKit.Visual.VERSION = "1.3.1";
 
40
 
 
41
MochiKit.Visual.__repr__ = function () {
 
42
    return "[" + this.NAME + " " + this.VERSION + "]";
 
43
};
 
44
 
 
45
MochiKit.Visual.toString = function () {
 
46
    return this.__repr__();
 
47
};
 
48
 
 
49
 
 
50
MochiKit.Visual._RoundCorners = function (e, options) {
 
51
    e = MochiKit.DOM.getElement(e);
 
52
    this._setOptions(options);
 
53
    if (this.options.__unstable__wrapElement) {
 
54
        e = this._doWrap(e);
 
55
    }
 
56
 
 
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);
 
63
    }
 
64
    this.isTransparent = (color.asRGB().a <= 0);
 
65
 
 
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);
 
71
    }
 
72
 
 
73
    this._roundCornersImpl(e, color, bgColor);
 
74
};
 
75
 
 
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) {
 
82
            return e;
 
83
        }
 
84
        var style = doc.defaultView.getComputedStyle(e, null);
 
85
        if (typeof(style) == "undefined" || style === null) {
 
86
            return e;
 
87
        }
 
88
        var wrapper = MochiKit.DOM.DIV({"style": {
 
89
            display: "block",
 
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
 
96
            padding: "0px"
 
97
            /*
 
98
            paddingRight: "0px",
 
99
            paddingLeft: "0px"
 
100
            */
 
101
        }});
 
102
        wrapper.innerHTML = e.innerHTML;
 
103
        e.innerHTML = "";
 
104
        e.appendChild(wrapper);
 
105
        return e;
 
106
    },
 
107
 
 
108
    _roundCornersImpl: function (e, color, bgColor) {
 
109
        if (this.options.border) {
 
110
            this._renderBorder(e, bgColor);
 
111
        }
 
112
        if (this._isTopRounded()) {
 
113
            this._roundTopCorners(e, color, bgColor);
 
114
        }
 
115
        if (this._isBottomRounded()) {
 
116
            this._roundBottomCorners(e, color, bgColor);
 
117
        }
 
118
    },
 
119
 
 
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>";
 
126
    },
 
127
 
 
128
    _roundTopCorners: function (el, color, bgColor) {
 
129
        var corner = this._createCorner(bgColor);
 
130
        for (var i = 0; i < this.options.numSlices; i++) {
 
131
            corner.appendChild(
 
132
                this._createCornerSlice(color, bgColor, i, "top")
 
133
            );
 
134
        }
 
135
        el.style.paddingTop = 0;
 
136
        el.insertBefore(corner, el.firstChild);
 
137
    },
 
138
 
 
139
    _roundBottomCorners: function (el, color, bgColor) {
 
140
        var corner = this._createCorner(bgColor);
 
141
        for (var i = (this.options.numSlices - 1); i >= 0; i--) {
 
142
            corner.appendChild(
 
143
                this._createCornerSlice(color, bgColor, i, "bottom")
 
144
            );
 
145
        }
 
146
        el.style.paddingBottom = 0;
 
147
        el.appendChild(corner);
 
148
    },
 
149
 
 
150
    _createCorner: function (bgColor) {
 
151
        var dom = MochiKit.DOM;
 
152
        return dom.DIV({style: {backgroundColor: bgColor.toString()}});
 
153
    },
 
154
 
 
155
    _createCornerSlice: function (color, bgColor, n, position) {
 
156
        var slice = MochiKit.DOM.SPAN();
 
157
 
 
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";
 
164
 
 
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";
 
179
        }
 
180
 
 
181
        if (!this.options.compact && (n == (this.options.numSlices - 1))) {
 
182
            inStyle.height = "2px";
 
183
        }
 
184
 
 
185
        this._setMargin(slice, n, position);
 
186
        this._setBorder(slice, n, position);
 
187
 
 
188
        return slice;
 
189
    },
 
190
 
 
191
    _setOptions: function (options) {
 
192
        this.options = {
 
193
            corners: "all",
 
194
            color: "fromElement",
 
195
            bgColor: "fromParent",
 
196
            blend: true,
 
197
            border: false,
 
198
            compact: false,
 
199
            __unstable__wrapElement: false
 
200
        };
 
201
        MochiKit.Base.update(this.options, options);
 
202
 
 
203
        this.options.numSlices = (this.options.compact ? 2 : 4);
 
204
    },
 
205
 
 
206
    _whichSideTop: function () {
 
207
        var corners = this.options.corners;
 
208
        if (this._hasString(corners, "all", "top")) {
 
209
            return "";
 
210
        }
 
211
 
 
212
        var has_tl = (corners.indexOf("tl") != -1);
 
213
        var has_tr = (corners.indexOf("tr") != -1);
 
214
        if (has_tl && has_tr) {
 
215
            return "";
 
216
        }
 
217
        if (has_tl) {
 
218
            return "left";
 
219
        }
 
220
        if (has_tr) {
 
221
            return "right";
 
222
        }
 
223
        return "";
 
224
    },
 
225
 
 
226
    _whichSideBottom: function () {
 
227
        var corners = this.options.corners;
 
228
        if (this._hasString(corners, "all", "bottom")) {
 
229
            return "";
 
230
        }
 
231
 
 
232
        var has_bl = (corners.indexOf('bl') != -1);
 
233
        var has_br = (corners.indexOf('br') != -1);
 
234
        if (has_bl && has_br) {
 
235
            return "";
 
236
        }
 
237
        if (has_bl) {
 
238
            return "left";
 
239
        }
 
240
        if (has_br) {
 
241
            return "right";
 
242
        }
 
243
        return "";
 
244
    },
 
245
 
 
246
    _borderColor: function (color, bgColor) {
 
247
        if (color == "transparent") {
 
248
            return bgColor;
 
249
        } else if (this.options.border) {
 
250
            return this.options.border;
 
251
        } else if (this.options.blend) {
 
252
            return bgColor.blendedColor(color);
 
253
        }
 
254
        return "";
 
255
    },
 
256
 
 
257
 
 
258
    _setMargin: function (el, n, corners) {
 
259
        var marginSize = this._marginSize(n) + "px";
 
260
        var whichSide = (
 
261
            corners == "top" ? this._whichSideTop() : this._whichSideBottom()
 
262
        );
 
263
        var style = el.style;
 
264
 
 
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";
 
271
        } else {
 
272
            style.marginLeft = marginSize;
 
273
            style.marginRight = marginSize;
 
274
        }
 
275
    },
 
276
 
 
277
    _setBorder: function (el, n, corners) {
 
278
        var borderSize = this._borderSize(n) + "px";
 
279
        var whichSide = (
 
280
            corners == "top" ? this._whichSideTop() : this._whichSideBottom()
 
281
        );
 
282
 
 
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";
 
290
        } else {
 
291
            style.borderLeftWidth = borderSize;
 
292
            style.borderRightWidth = borderSize;
 
293
        }
 
294
    },
 
295
 
 
296
    _marginSize: function (n) {
 
297
        if (this.isTransparent) {
 
298
            return 0;
 
299
        }
 
300
 
 
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];
 
311
        } else {
 
312
            var marginSizes = [5, 3, 2, 1];
 
313
            return marginSizes[n];
 
314
        }
 
315
    },
 
316
 
 
317
    _borderSize: function (n) {
 
318
        var o = this.options;
 
319
        var borderSizes;
 
320
        if (o.compact && (o.blend || this.isTransparent)) {
 
321
            return 1;
 
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];
 
330
        } else {
 
331
            return 0;
 
332
        }
 
333
        return borderSizes[n];
 
334
    },
 
335
 
 
336
    _hasString: function (str) {
 
337
        for (var i = 1; i< arguments.length; i++) {
 
338
            if (str.indexOf(arguments[i]) != -1) {
 
339
                return true;
 
340
            }
 
341
        }
 
342
        return false;
 
343
    },
 
344
 
 
345
    _isTopRounded: function () {
 
346
        return this._hasString(this.options.corners,
 
347
            "all", "top", "tl", "tr"
 
348
        );
 
349
    },
 
350
 
 
351
    _isBottomRounded: function () {
 
352
        return this._hasString(this.options.corners,
 
353
            "all", "bottom", "bl", "br"
 
354
        );
 
355
    },
 
356
 
 
357
    _hasSingleTextChild: function (el) {
 
358
        return (el.childNodes.length == 1 && el.childNodes[0].nodeType == 3);
 
359
    }
 
360
};
 
361
 
 
362
MochiKit.Visual.roundElement = function (e, options) {
 
363
    new MochiKit.Visual._RoundCorners(e, options);
 
364
};
 
365
 
 
366
MochiKit.Visual.roundClass = function (tagName, className, options) {
 
367
    var elements = MochiKit.DOM.getElementsByTagAndClassName(
 
368
        tagName, className
 
369
    );
 
370
    for (var i = 0; i < elements.length; i++) {
 
371
        MochiKit.Visual.roundElement(elements[i], options);
 
372
    }
 
373
};
 
374
 
 
375
// Compatibility with MochiKit 1.0
 
376
MochiKit.Visual.Color = MochiKit.Color.Color;
 
377
MochiKit.Visual.getElementsComputedStyle = MochiKit.DOM.computedStyle;
 
378
 
 
379
/* end of Rico adaptation */
 
380
 
 
381
MochiKit.Visual.__new__  = function () {
 
382
    var m = MochiKit.Base;
 
383
 
 
384
    m.nameFunctions(this);
 
385
 
 
386
    this.EXPORT_TAGS = {
 
387
        ":common": this.EXPORT,
 
388
        ":all": m.concat(this.EXPORT, this.EXPORT_OK)
 
389
    };
 
390
 
 
391
};
 
392
 
 
393
MochiKit.Visual.EXPORT = [
 
394
    "roundElement",
 
395
    "roundClass"
 
396
];
 
397
 
 
398
MochiKit.Visual.EXPORT_OK = [];
 
399
 
 
400
MochiKit.Visual.__new__();
 
401
 
 
402
MochiKit.Base._exportSymbols(this, MochiKit.Visual);