You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
5.9 KiB

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