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