fl-stylesheet.js
4.2 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
205
206
207
(function($){
/**
* Helper class for dealing with creating
* and updating stylesheets.
*
* @class FLStyleSheet
* @since 1.3.3
*/
FLStyleSheet = function( o )
{
if ( 'object' == typeof o ) {
$.extend( this, o );
}
this._createSheet();
};
/**
* Prototype for new instances.
*
* @since 1.3.3
* @property {Object} prototype
*/
FLStyleSheet.prototype = {
/**
* An ID for the stylesheet element.
*
* @since 1.9
* @property {String} id
*/
id : null,
/**
* A reference to the stylesheet object.
*
* @since 1.3.3
* @access private
* @property {Object} _sheet
*/
_sheet : null,
/**
* A reference to the HTML style element.
*
* @since 1.3.3
* @access private
* @property {Object} _sheetElement
*/
_sheetElement : null,
/**
* Update a rule for this stylesheet.
*
* @since 1.3.3
* @method updateRule
* @param {String} selector The CSS selector to update.
* @param {String} property The CSS property to update. Can also be an object of key/value pairs.
* @param {String} value The value of the property to update. Can be omitted if property is an object.
*/
updateRule: function(selector, property, value)
{
var rules = this._sheet.cssRules ? this._sheet.cssRules : this._sheet.rules,
rule = null,
i = 0;
// Find the rule to update.
for( ; i < rules.length; i++) {
if(rules[i].selectorText.toLowerCase() == selector.toLowerCase()) {
rule = rules[i];
}
}
// Update the existing rule.
if(rule) {
if(typeof property == 'object') {
for(i in property) {
rule.style[this._toCamelCase(i)] = property[i];
}
}
else {
rule.style[this._toCamelCase(property)] = value;
}
}
// No rule found. Add a new one.
else {
this.addRule(selector, property, value);
}
},
/**
* Add a new rule to this stylesheet.
*
* @since 1.3.3
* @method addRule
* @param {String} selector The CSS selector to add.
* @param {String} property The CSS property to add. Can also be an object of key/value pairs.
* @param {String} value The value of the property to add. Can be omitted if property is an object.
*/
addRule: function(selector, property, value)
{
var styles = '',
i = '';
if(typeof property == 'object') {
for(i in property) {
styles += i + ':' + property[i] + ';';
}
}
else {
styles = property + ':' + value + ';';
}
if(this._sheet.insertRule) {
this._sheet.insertRule(selector + ' { ' + styles + ' }', this._sheet.cssRules.length);
}
else {
this._sheet.addRule(selector, styles);
}
},
/**
* Completely destroys the sheet by removing the
* stylesheet element from the DOM and making the
* stored object reference null.
*
* @since 1.9
* @method destroy
*/
destroy: function()
{
if(this._sheetElement) {
this._sheetElement.remove();
this._sheetElement = null;
}
if(this._sheet) {
this._sheet = null;
}
},
/**
* Disables the sheet by removing it from the DOM.
*
* @since 1.9
* @method disable
*/
disable: function()
{
this._sheet.disabled = true;
},
/**
* Enables the sheet by adding it to the DOM.
*
* @since 1.9
* @method enable
*/
enable: function()
{
this._sheet.disabled = false;
},
/**
* Create the style element, add it to the DOM
* and save references.
*
* @since 1.3.3
* @access private
* @method _createSheet
*/
_createSheet: function()
{
var id = this.id ? ' id="' + this.id + '"' : '';
if ( ! this._sheet ) {
this._sheetElement = $( '<style type="text/css"' + id + '></style>' );
$( 'body' ).append( this._sheetElement );
this._sheet = this._sheetElement[0].sheet;
}
},
/**
* Convert a string to camel case.
*
* @since 1.3.3
* @access private
* @method _toCamelCase
* @param {String} input The string to convert.
*/
_toCamelCase: function(input)
{
return input.toLowerCase().replace(/-(.)/g, function(match, group1) {
return group1.toUpperCase();
});
}
};
})(jQuery);