CSS.html
7.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>The source code</title>
<link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../resources/prettify/prettify.js"></script>
<style type="text/css">
.highlight { display: block; background-color: #ddd; }
</style>
<script type="text/javascript">
function highlight() {
document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
}
</script>
</head>
<body onload="prettyPrint(); highlight();">
<pre class="prettyprint lang-js"><span id='Ext-util-CSS'>/**
</span> * Utility class for manipulating CSS rules
* @singleton
*/
Ext.define('Ext.util.CSS', (function() {
var rules = null,
doc = document,
camelRe = /(-[a-z])/gi,
camelFn = function(m, a){ return a.charAt(1).toUpperCase(); };
return {
singleton: true,
constructor: function() {
this.rules = {};
this.initialized = false;
},
<span id='Ext-util-CSS-method-createStyleSheet'> /**
</span> * Creates a stylesheet from a text blob of rules.
* These rules will be wrapped in a STYLE tag and appended to the HEAD of the document.
* @param {String} cssText The text containing the css rules
* @param {String} id An id to add to the stylesheet for later removal
* @return {CSSStyleSheet}
*/
createStyleSheet : function(cssText, id) {
var ss,
head = doc.getElementsByTagName("head")[0],
styleEl = doc.createElement("style");
styleEl.setAttribute("type", "text/css");
if (id) {
styleEl.setAttribute("id", id);
}
if (Ext.isIE) {
head.appendChild(styleEl);
ss = styleEl.styleSheet;
ss.cssText = cssText;
} else {
try{
styleEl.appendChild(doc.createTextNode(cssText));
} catch(e) {
styleEl.cssText = cssText;
}
head.appendChild(styleEl);
ss = styleEl.styleSheet ? styleEl.styleSheet : (styleEl.sheet || doc.styleSheets[doc.styleSheets.length-1]);
}
this.cacheStyleSheet(ss);
return ss;
},
<span id='Ext-util-CSS-method-removeStyleSheet'> /**
</span> * Removes a style or link tag by id
* @param {String} id The id of the tag
*/
removeStyleSheet : function(id) {
var existing = document.getElementById(id);
if (existing) {
existing.parentNode.removeChild(existing);
}
},
<span id='Ext-util-CSS-method-swapStyleSheet'> /**
</span> * Dynamically swaps an existing stylesheet reference for a new one
* @param {String} id The id of an existing link tag to remove
* @param {String} url The href of the new stylesheet to include
*/
swapStyleSheet : function(id, url) {
var doc = document,
ss;
this.removeStyleSheet(id);
ss = doc.createElement("link");
ss.setAttribute("rel", "stylesheet");
ss.setAttribute("type", "text/css");
ss.setAttribute("id", id);
ss.setAttribute("href", url);
doc.getElementsByTagName("head")[0].appendChild(ss);
},
<span id='Ext-util-CSS-method-refreshCache'> /**
</span> * Refresh the rule cache if you have dynamically added stylesheets
* @return {Object} An object (hash) of rules indexed by selector
*/
refreshCache : function() {
return this.getRules(true);
},
// private
cacheStyleSheet : function(ss) {
if(!rules){
rules = {};
}
try {// try catch for cross domain access issue
var ssRules = ss.cssRules || ss.rules,
selectorText,
i = ssRules.length - 1,
j,
selectors;
for (; i >= 0; --i) {
selectorText = ssRules[i].selectorText;
if (selectorText) {
// Split in case there are multiple, comma-delimited selectors
selectorText = selectorText.split(',');
selectors = selectorText.length;
for (j = 0; j < selectors; j++) {
rules[Ext.String.trim(selectorText[j]).toLowerCase()] = ssRules[i];
}
}
}
} catch(e) {}
},
<span id='Ext-util-CSS-method-getRules'> /**
</span> * Gets all css rules for the document
* @param {Boolean} refreshCache true to refresh the internal cache
* @return {Object} An object (hash) of rules indexed by selector
*/
getRules : function(refreshCache) {
if (rules === null || refreshCache) {
rules = {};
var ds = doc.styleSheets,
i = 0,
len = ds.length;
for (; i < len; i++) {
try {
if (!ds[i].disabled) {
this.cacheStyleSheet(ds[i]);
}
} catch(e) {}
}
}
return rules;
},
<span id='Ext-util-CSS-method-getRule'> /**
</span> * Gets an an individual CSS rule by selector(s)
* @param {String/String[]} selector The CSS selector or an array of selectors to try. The first selector that is found is returned.
* @param {Boolean} refreshCache true to refresh the internal cache if you have recently updated any rules or added styles dynamically
* @return {CSSStyleRule} The CSS rule or null if one is not found
*/
getRule: function(selector, refreshCache) {
var rs = this.getRules(refreshCache),
i;
if (!Ext.isArray(selector)) {
return rs[selector.toLowerCase()];
}
for (i = 0; i < selector.length; i++) {
if (rs[selector[i]]) {
return rs[selector[i].toLowerCase()];
}
}
return null;
},
<span id='Ext-util-CSS-method-updateRule'> /**
</span> * Updates a rule property
* @param {String/String[]} selector If it's an array it tries each selector until it finds one. Stops immediately once one is found.
* @param {String} property The css property
* @param {String} value The new value for the property
* @return {Boolean} true If a rule was found and updated
*/
updateRule : function(selector, property, value){
var rule, i;
if (!Ext.isArray(selector)) {
rule = this.getRule(selector);
if (rule) {
rule.style[property.replace(camelRe, camelFn)] = value;
return true;
}
} else {
for (i = 0; i < selector.length; i++) {
if (this.updateRule(selector[i], property, value)) {
return true;
}
}
}
return false;
}
};
}()));</pre>
</body>
</html>