fl-stylesheet.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. (function($){
  2. /**
  3. * Helper class for dealing with creating
  4. * and updating stylesheets.
  5. *
  6. * @class FLStyleSheet
  7. * @since 1.3.3
  8. */
  9. FLStyleSheet = function( o )
  10. {
  11. if ( 'object' == typeof o ) {
  12. $.extend( this, o );
  13. }
  14. this._createSheet();
  15. };
  16. /**
  17. * Prototype for new instances.
  18. *
  19. * @since 1.3.3
  20. * @property {Object} prototype
  21. */
  22. FLStyleSheet.prototype = {
  23. /**
  24. * An ID for the stylesheet element.
  25. *
  26. * @since 1.9
  27. * @property {String} id
  28. */
  29. id : null,
  30. /**
  31. * A reference to the stylesheet object.
  32. *
  33. * @since 1.3.3
  34. * @access private
  35. * @property {Object} _sheet
  36. */
  37. _sheet : null,
  38. /**
  39. * A reference to the HTML style element.
  40. *
  41. * @since 1.3.3
  42. * @access private
  43. * @property {Object} _sheetElement
  44. */
  45. _sheetElement : null,
  46. /**
  47. * Update a rule for this stylesheet.
  48. *
  49. * @since 1.3.3
  50. * @method updateRule
  51. * @param {String} selector The CSS selector to update.
  52. * @param {String} property The CSS property to update. Can also be an object of key/value pairs.
  53. * @param {String} value The value of the property to update. Can be omitted if property is an object.
  54. */
  55. updateRule: function(selector, property, value)
  56. {
  57. var rules = this._sheet.cssRules ? this._sheet.cssRules : this._sheet.rules,
  58. rule = null,
  59. i = 0;
  60. // Find the rule to update.
  61. for( ; i < rules.length; i++) {
  62. if(rules[i].selectorText.toLowerCase().replace( /\s/g, '' ) == selector.toLowerCase().replace( /\s/g, '' )) {
  63. rule = rules[i];
  64. }
  65. }
  66. // Update the existing rule.
  67. if(rule) {
  68. if(typeof property == 'object') {
  69. for(i in property) {
  70. this.setProperty( rule, i, property[ i ] );
  71. }
  72. }
  73. else {
  74. this.setProperty( rule, property, value );
  75. }
  76. }
  77. // No rule found. Add a new one.
  78. else {
  79. this.addRule(selector, property, value);
  80. }
  81. },
  82. /**
  83. * Sets a property for a rule.
  84. *
  85. * @since 2.2
  86. * @method setProperty
  87. * @param {Object} rule
  88. * @param {String} selector
  89. * @param {String} value
  90. */
  91. setProperty: function( rule, property, value = '' )
  92. {
  93. var important = '';
  94. if ( rule.style.setProperty ) {
  95. if ( value.indexOf( '!important' ) > -1 ) {
  96. important = 'important';
  97. value = value.replace( '!important', '' ).trim();
  98. }
  99. rule.style.setProperty( property, value, important );
  100. } else {
  101. rule.style[ this._toCamelCase( property ) ] = value;
  102. }
  103. },
  104. /**
  105. * Add a new rule to this stylesheet.
  106. *
  107. * @since 1.3.3
  108. * @method addRule
  109. * @param {String} selector The CSS selector to add.
  110. * @param {String} property The CSS property to add. Can also be an object of key/value pairs.
  111. * @param {String} value The value of the property to add. Can be omitted if property is an object.
  112. */
  113. addRule: function(selector, property, value)
  114. {
  115. var styles = '',
  116. i = '';
  117. if(typeof property == 'object') {
  118. for(i in property) {
  119. styles += i + ':' + property[i] + ';';
  120. }
  121. }
  122. else {
  123. styles = property + ':' + value + ';';
  124. }
  125. if(this._sheet.insertRule) {
  126. this._sheet.insertRule(selector + ' { ' + styles + ' }', this._sheet.cssRules.length);
  127. }
  128. else {
  129. this._sheet.addRule(selector, styles);
  130. }
  131. },
  132. /**
  133. * Completely destroys the sheet by removing the
  134. * stylesheet element from the DOM and making the
  135. * stored object reference null.
  136. *
  137. * @since 1.9
  138. * @method destroy
  139. */
  140. destroy: function()
  141. {
  142. if(this._sheetElement) {
  143. this._sheetElement.remove();
  144. this._sheetElement = null;
  145. }
  146. if(this._sheet) {
  147. this._sheet = null;
  148. }
  149. },
  150. /**
  151. * Disables the sheet by removing it from the DOM.
  152. *
  153. * @since 1.9
  154. * @method disable
  155. */
  156. disable: function()
  157. {
  158. this._sheet.disabled = true;
  159. },
  160. /**
  161. * Enables the sheet by adding it to the DOM.
  162. *
  163. * @since 1.9
  164. * @method enable
  165. */
  166. enable: function()
  167. {
  168. this._sheet.disabled = false;
  169. },
  170. /**
  171. * Create the style element, add it to the DOM
  172. * and save references.
  173. *
  174. * @since 1.3.3
  175. * @access private
  176. * @method _createSheet
  177. */
  178. _createSheet: function()
  179. {
  180. var id = this.id ? ' id="' + this.id + '"' : '',
  181. className = this.className ? ' class="' + this.className + '"' : '';
  182. if ( ! this._sheet ) {
  183. this._sheetElement = $( '<style type="text/css"' + id + className + '></style>' );
  184. $( 'body' ).append( this._sheetElement );
  185. this._sheet = this._sheetElement[0].sheet;
  186. }
  187. },
  188. /**
  189. * Convert a string to camel case.
  190. *
  191. * @since 1.3.3
  192. * @access private
  193. * @method _toCamelCase
  194. * @param {String} input The string to convert.
  195. */
  196. _toCamelCase: function(input)
  197. {
  198. return input.toLowerCase().replace(/-(.)/g, function(match, group1) {
  199. return group1.toUpperCase();
  200. });
  201. }
  202. };
  203. })(jQuery);