1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-util.CSS'>/**
2 </span> * @class Ext.util.CSS
3 * Utility class for manipulating CSS rules
6 Ext.define('Ext.util.CSS', function() {
10 var camelRe = /(-[a-z])/gi;
11 var camelFn = function(m, a){ return a.charAt(1).toUpperCase(); };
17 constructor: function() {
19 this.initialized = false;
22 <span id='Ext-util.CSS-method-createStyleSheet'> /**
23 </span> * Creates a stylesheet from a text blob of rules.
24 * These rules will be wrapped in a STYLE tag and appended to the HEAD of the document.
25 * @param {String} cssText The text containing the css rules
26 * @param {String} id An id to add to the stylesheet for later removal
27 * @return {StyleSheet}
29 createStyleSheet : function(cssText, id) {
31 head = doc.getElementsByTagName("head")[0],
32 styleEl = doc.createElement("style");
34 styleEl.setAttribute("type", "text/css");
36 styleEl.setAttribute("id", id);
40 head.appendChild(styleEl);
41 ss = styleEl.styleSheet;
45 styleEl.appendChild(doc.createTextNode(cssText));
47 styleEl.cssText = cssText;
49 head.appendChild(styleEl);
50 ss = styleEl.styleSheet ? styleEl.styleSheet : (styleEl.sheet || doc.styleSheets[doc.styleSheets.length-1]);
52 this.cacheStyleSheet(ss);
56 <span id='Ext-util.CSS-method-removeStyleSheet'> /**
57 </span> * Removes a style or link tag by id
58 * @param {String} id The id of the tag
60 removeStyleSheet : function(id) {
61 var existing = document.getElementById(id);
63 existing.parentNode.removeChild(existing);
67 <span id='Ext-util.CSS-method-swapStyleSheet'> /**
68 </span> * Dynamically swaps an existing stylesheet reference for a new one
69 * @param {String} id The id of an existing link tag to remove
70 * @param {String} url The href of the new stylesheet to include
72 swapStyleSheet : function(id, url) {
74 this.removeStyleSheet(id);
75 var ss = doc.createElement("link");
76 ss.setAttribute("rel", "stylesheet");
77 ss.setAttribute("type", "text/css");
78 ss.setAttribute("id", id);
79 ss.setAttribute("href", url);
80 doc.getElementsByTagName("head")[0].appendChild(ss);
83 <span id='Ext-util.CSS-method-refreshCache'> /**
84 </span> * Refresh the rule cache if you have dynamically added stylesheets
85 * @return {Object} An object (hash) of rules indexed by selector
87 refreshCache : function() {
88 return this.getRules(true);
92 cacheStyleSheet : function(ss) {
96 try {// try catch for cross domain access issue
97 var ssRules = ss.cssRules || ss.rules,
99 i = ssRules.length - 1,
103 for (; i >= 0; --i) {
104 selectorText = ssRules[i].selectorText;
107 // Split in case there are multiple, comma-delimited selectors
108 selectorText = selectorText.split(',');
109 selectors = selectorText.length;
110 for (j = 0; j < selectors; j++) {
111 rules[Ext.String.trim(selectorText[j]).toLowerCase()] = ssRules[i];
118 <span id='Ext-util.CSS-method-getRules'> /**
119 </span> * Gets all css rules for the document
120 * @param {Boolean} refreshCache true to refresh the internal cache
121 * @return {Object} An object (hash) of rules indexed by selector
123 getRules : function(refreshCache) {
124 if (rules === null || refreshCache) {
126 var ds = doc.styleSheets,
130 for (; i < len; i++) {
132 if (!ds[i].disabled) {
133 this.cacheStyleSheet(ds[i]);
141 <span id='Ext-util.CSS-method-getRule'> /**
142 </span> * Gets an an individual CSS rule by selector(s)
143 * @param {String/Array} selector The CSS selector or an array of selectors to try. The first selector that is found is returned.
144 * @param {Boolean} refreshCache true to refresh the internal cache if you have recently updated any rules or added styles dynamically
145 * @return {CSSRule} The CSS rule or null if one is not found
147 getRule: function(selector, refreshCache) {
148 var rs = this.getRules(refreshCache);
149 if (!Ext.isArray(selector)) {
150 return rs[selector.toLowerCase()];
152 for (var i = 0; i < selector.length; i++) {
153 if (rs[selector[i]]) {
154 return rs[selector[i].toLowerCase()];
160 <span id='Ext-util.CSS-method-updateRule'> /**
161 </span> * Updates a rule property
162 * @param {String/Array} selector If it's an array it tries each selector until it finds one. Stops immediately once one is found.
163 * @param {String} property The css property
164 * @param {String} value The new value for the property
165 * @return {Boolean} true If a rule was found and updated
167 updateRule : function(selector, property, value){
168 if (!Ext.isArray(selector)) {
169 var rule = this.getRule(selector);
171 rule.style[property.replace(camelRe, camelFn)] = value;
175 for (var i = 0; i < selector.length; i++) {
176 if (this.updateRule(selector[i], property, value)) {
184 }());</pre></pre></body></html>