Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
YUI.add('dom-style', function (Y, NAME) {
2
 
3
/**
4
 * Add style management functionality to DOM.
5
 * @module dom
6
 * @submodule dom-style
7
 * @for DOM
8
 */
9
 
10
var DOCUMENT_ELEMENT = 'documentElement',
11
    DEFAULT_VIEW = 'defaultView',
12
    OWNER_DOCUMENT = 'ownerDocument',
13
    STYLE = 'style',
14
    FLOAT = 'float',
15
    CSS_FLOAT = 'cssFloat',
16
    STYLE_FLOAT = 'styleFloat',
17
    TRANSPARENT = 'transparent',
18
    GET_COMPUTED_STYLE = 'getComputedStyle',
19
    GET_BOUNDING_CLIENT_RECT = 'getBoundingClientRect',
20
 
21
    DOCUMENT = Y.config.doc,
22
 
23
    Y_DOM = Y.DOM,
24
 
25
    TRANSFORM,
26
    TRANSFORMORIGIN,
27
    VENDOR_TRANSFORM = [
28
        'WebkitTransform',
29
        'MozTransform',
30
        'OTransform',
31
        'msTransform',
32
        'transform'
33
    ],
34
 
35
    re_unit = /width|height|top|left|right|bottom|margin|padding/i;
36
 
37
Y.Array.each(VENDOR_TRANSFORM, function(val) {
38
    if (val in DOCUMENT[DOCUMENT_ELEMENT].style) {
39
        TRANSFORM = val;
40
        TRANSFORMORIGIN = val + "Origin";
41
    }
42
});
43
 
44
Y.mix(Y_DOM, {
45
    DEFAULT_UNIT: 'px',
46
 
47
    CUSTOM_STYLES: {
48
    },
49
 
50
 
51
    /**
52
     * Sets a style property for a given element.
53
     * @method setStyle
54
     * @param {HTMLElement} node The HTMLElement to apply the style to.
55
     * @param {String} att The style property to set.
56
     * @param {String|Number} val The value.
57
     * @param {Object} [style] The style node. Defaults to `node.style`.
58
     */
59
    setStyle: function(node, att, val, style) {
60
        style = style || node.style;
61
        var CUSTOM_STYLES = Y_DOM.CUSTOM_STYLES;
62
 
63
        if (style) {
64
            if (val === null || val === '') { // normalize unsetting
65
                val = '';
66
            } else if (!isNaN(Number(val)) && re_unit.test(att)) { // number values may need a unit
67
                val += Y_DOM.DEFAULT_UNIT;
68
            }
69
 
70
            if (att in CUSTOM_STYLES) {
71
                if (CUSTOM_STYLES[att].set) {
72
                    CUSTOM_STYLES[att].set(node, val, style);
73
                    return; // NOTE: return
74
                } else if (typeof CUSTOM_STYLES[att] === 'string') {
75
                    att = CUSTOM_STYLES[att];
76
                }
77
            } else if (att === '') { // unset inline styles
78
                att = 'cssText';
79
                val = '';
80
            }
81
            style[att] = val;
82
        }
83
    },
84
 
85
    /**
86
     * Returns the current style value for the given property.
87
     * @method getStyle
88
     * @param {HTMLElement} node The HTMLElement to get the style from.
89
     * @param {String} att The style property to get.
90
     * @param {Object} [style] The style node. Defaults to `node.style`.
91
     */
92
    getStyle: function(node, att, style) {
93
        style = style || node.style;
94
        var CUSTOM_STYLES = Y_DOM.CUSTOM_STYLES,
95
            val = '';
96
 
97
        if (style) {
98
            if (att in CUSTOM_STYLES) {
99
                if (CUSTOM_STYLES[att].get) {
100
                    return CUSTOM_STYLES[att].get(node, att, style); // NOTE: return
101
                } else if (typeof CUSTOM_STYLES[att] === 'string') {
102
                    att = CUSTOM_STYLES[att];
103
                }
104
            }
105
            val = style[att];
106
            if (val === '') { // TODO: is empty string sufficient?
107
                val = Y_DOM[GET_COMPUTED_STYLE](node, att);
108
            }
109
        }
110
 
111
        return val;
112
    },
113
 
114
    /**
115
     * Sets multiple style properties.
116
     * @method setStyles
117
     * @param {HTMLElement} node The HTMLElement to apply the styles to.
118
     * @param {Object} hash An object literal of property:value pairs.
119
     */
120
    setStyles: function(node, hash) {
121
        var style = node.style;
122
        Y.each(hash, function(v, n) {
123
            Y_DOM.setStyle(node, n, v, style);
124
        }, Y_DOM);
125
    },
126
 
127
    /**
128
     * Returns the computed style for the given node.
129
     * @method getComputedStyle
130
     * @param {HTMLElement} node The HTMLElement to get the style from.
131
     * @param {String} att The style property to get.
132
     * @return {String} The computed value of the style property.
133
     */
134
    getComputedStyle: function(node, att) {
135
        var val = '',
136
            doc = node[OWNER_DOCUMENT],
137
            computed;
138
 
139
        if (node[STYLE] && doc[DEFAULT_VIEW] && doc[DEFAULT_VIEW][GET_COMPUTED_STYLE]) {
140
            computed = doc[DEFAULT_VIEW][GET_COMPUTED_STYLE](node, null);
141
            if (computed) { // FF may be null in some cases (ticket #2530548)
142
                val = computed[att];
143
            }
144
        }
145
        return val;
146
    }
147
});
148
 
149
// normalize reserved word float alternatives ("cssFloat" or "styleFloat")
150
if (DOCUMENT[DOCUMENT_ELEMENT][STYLE][CSS_FLOAT] !== undefined) {
151
    Y_DOM.CUSTOM_STYLES[FLOAT] = CSS_FLOAT;
152
} else if (DOCUMENT[DOCUMENT_ELEMENT][STYLE][STYLE_FLOAT] !== undefined) {
153
    Y_DOM.CUSTOM_STYLES[FLOAT] = STYLE_FLOAT;
154
}
155
 
156
// safari converts transparent to rgba(), others use "transparent"
157
if (Y.UA.webkit) {
158
    Y_DOM[GET_COMPUTED_STYLE] = function(node, att) {
159
        var view = node[OWNER_DOCUMENT][DEFAULT_VIEW],
160
            val = view[GET_COMPUTED_STYLE](node, '')[att];
161
 
162
        if (val === 'rgba(0, 0, 0, 0)') {
163
            val = TRANSPARENT;
164
        }
165
 
166
        return val;
167
    };
168
 
169
}
170
 
171
Y.DOM._getAttrOffset = function(node, attr) {
172
    var val = Y.DOM[GET_COMPUTED_STYLE](node, attr),
173
        offsetParent = node.offsetParent,
174
        position,
175
        parentOffset,
176
        offset;
177
 
178
    if (val === 'auto') {
179
        position = Y.DOM.getStyle(node, 'position');
180
        if (position === 'static' || position === 'relative') {
181
            val = 0;
182
        } else if (offsetParent && offsetParent[GET_BOUNDING_CLIENT_RECT]) {
183
            parentOffset = offsetParent[GET_BOUNDING_CLIENT_RECT]()[attr];
184
            offset = node[GET_BOUNDING_CLIENT_RECT]()[attr];
185
            if (attr === 'left' || attr === 'top') {
186
                val = offset - parentOffset;
187
            } else {
188
                val = parentOffset - node[GET_BOUNDING_CLIENT_RECT]()[attr];
189
            }
190
        }
191
    }
192
 
193
    return val;
194
};
195
 
196
Y.DOM._getOffset = function(node) {
197
    var pos,
198
        xy = null;
199
 
200
    if (node) {
201
        pos = Y_DOM.getStyle(node, 'position');
202
        xy = [
203
            parseInt(Y_DOM[GET_COMPUTED_STYLE](node, 'left'), 10),
204
            parseInt(Y_DOM[GET_COMPUTED_STYLE](node, 'top'), 10)
205
        ];
206
 
207
        if ( isNaN(xy[0]) ) { // in case of 'auto'
208
            xy[0] = parseInt(Y_DOM.getStyle(node, 'left'), 10); // try inline
209
            if ( isNaN(xy[0]) ) { // default to offset value
210
                xy[0] = (pos === 'relative') ? 0 : node.offsetLeft || 0;
211
            }
212
        }
213
 
214
        if ( isNaN(xy[1]) ) { // in case of 'auto'
215
            xy[1] = parseInt(Y_DOM.getStyle(node, 'top'), 10); // try inline
216
            if ( isNaN(xy[1]) ) { // default to offset value
217
                xy[1] = (pos === 'relative') ? 0 : node.offsetTop || 0;
218
            }
219
        }
220
    }
221
 
222
    return xy;
223
 
224
};
225
 
226
if (TRANSFORM) {
227
    Y_DOM.CUSTOM_STYLES.transform = {
228
        set: function(node, val, style) {
229
            style[TRANSFORM] = val;
230
        },
231
 
232
        get: function(node) {
233
            return Y_DOM[GET_COMPUTED_STYLE](node, TRANSFORM);
234
        }
235
    };
236
 
237
    Y_DOM.CUSTOM_STYLES.transformOrigin = {
238
        set: function(node, val, style) {
239
            style[TRANSFORMORIGIN] = val;
240
        },
241
 
242
        get: function(node) {
243
            return Y_DOM[GET_COMPUTED_STYLE](node, TRANSFORMORIGIN);
244
        }
245
    };
246
}
247
 
248
 
249
}, '3.18.1', {"requires": ["dom-base"]});