fl-stylesheet.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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() == selector.toLowerCase()) {
  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. rule.style[this._toCamelCase(i)] = property[i];
  71. }
  72. }
  73. else {
  74. rule.style[this._toCamelCase(property)] = value;
  75. }
  76. }
  77. // No rule found. Add a new one.
  78. else {
  79. this.addRule(selector, property, value);
  80. }
  81. },
  82. /**
  83. * Add a new rule to this stylesheet.
  84. *
  85. * @since 1.3.3
  86. * @method addRule
  87. * @param {String} selector The CSS selector to add.
  88. * @param {String} property The CSS property to add. Can also be an object of key/value pairs.
  89. * @param {String} value The value of the property to add. Can be omitted if property is an object.
  90. */
  91. addRule: function(selector, property, value)
  92. {
  93. var styles = '',
  94. i = '';
  95. if(typeof property == 'object') {
  96. for(i in property) {
  97. styles += i + ':' + property[i] + ';';
  98. }
  99. }
  100. else {
  101. styles = property + ':' + value + ';';
  102. }
  103. if(this._sheet.insertRule) {
  104. this._sheet.insertRule(selector + ' { ' + styles + ' }', this._sheet.cssRules.length);
  105. }
  106. else {
  107. this._sheet.addRule(selector, styles);
  108. }
  109. },
  110. /**
  111. * Completely destroys the sheet by removing the
  112. * stylesheet element from the DOM and making the
  113. * stored object reference null.
  114. *
  115. * @since 1.9
  116. * @method destroy
  117. */
  118. destroy: function()
  119. {
  120. if(this._sheetElement) {
  121. this._sheetElement.remove();
  122. this._sheetElement = null;
  123. }
  124. if(this._sheet) {
  125. this._sheet = null;
  126. }
  127. },
  128. /**
  129. * Disables the sheet by removing it from the DOM.
  130. *
  131. * @since 1.9
  132. * @method disable
  133. */
  134. disable: function()
  135. {
  136. this._sheet.disabled = true;
  137. },
  138. /**
  139. * Enables the sheet by adding it to the DOM.
  140. *
  141. * @since 1.9
  142. * @method enable
  143. */
  144. enable: function()
  145. {
  146. this._sheet.disabled = false;
  147. },
  148. /**
  149. * Create the style element, add it to the DOM
  150. * and save references.
  151. *
  152. * @since 1.3.3
  153. * @access private
  154. * @method _createSheet
  155. */
  156. _createSheet: function()
  157. {
  158. var id = this.id ? ' id="' + this.id + '"' : '',
  159. className = this.className ? ' class="' + this.className + '"' : '';
  160. if ( ! this._sheet ) {
  161. this._sheetElement = $( '<style type="text/css"' + id + className + '></style>' );
  162. $( 'body' ).append( this._sheetElement );
  163. this._sheet = this._sheetElement[0].sheet;
  164. }
  165. },
  166. /**
  167. * Convert a string to camel case.
  168. *
  169. * @since 1.3.3
  170. * @access private
  171. * @method _toCamelCase
  172. * @param {String} input The string to convert.
  173. */
  174. _toCamelCase: function(input)
  175. {
  176. return input.toLowerCase().replace(/-(.)/g, function(match, group1) {
  177. return group1.toUpperCase();
  178. });
  179. }
  180. };
  181. })(jQuery);