|
1 | 1 | // utility / helper functions
|
2 | 2 |
|
| 3 | + // iterate the attributes of an object |
| 4 | + function _each(obj, callback) { |
| 5 | + for (var name in obj) { |
| 6 | + if (obj.hasOwnProperty(name)) { |
| 7 | + callback(obj[name], name); |
| 8 | + } |
| 9 | + } |
| 10 | + } |
| 11 | + |
3 | 12 | // extend the attributes of src into target
|
4 | 13 | function _extend(target, src) {
|
5 | 14 | if (src) {
|
6 |
| - for (var name in src) { |
7 |
| - if (src.hasOwnProperty(name)) { |
8 |
| - target[name] = src[name]; |
9 |
| - } |
10 |
| - } |
| 15 | + _each(src, function(value, name) { |
| 16 | + target[name] = value; |
| 17 | + }); |
11 | 18 | }
|
12 | 19 | return target;
|
13 | 20 | }
|
|
17 | 24 | return typeof obj === 'function';
|
18 | 25 | }
|
19 | 26 |
|
20 |
| - function _isDeep(obj) { |
21 |
| - for (var name in obj) { |
22 |
| - if (obj.hasOwnProperty(name)) { |
23 |
| - if (typeof obj[name] === 'object') { |
24 |
| - return true; |
25 |
| - } |
26 |
| - } |
27 |
| - } |
| 27 | + // return true if obj is a string |
| 28 | + function _isString(obj) { |
| 29 | + return typeof obj === 'string'; |
28 | 30 | }
|
29 | 31 |
|
| 32 | + // create and return the class |
| 33 | + function _createClass(constructor, attributes) { |
| 34 | + _extend(constructor.prototype, attributes); |
| 35 | + return constructor; |
| 36 | + } |
30 | 37 |
|
31 | 38 | /**
|
32 | 39 | * Return a style set function that should be executed as
|
|
72 | 79 |
|
73 | 80 | /**
|
74 | 81 | * Normalize all stylesheet definitions
|
| 82 | + * - styles: the user provided stylesheet |
| 83 | + * - builder: the associated Builder |
75 | 84 | */
|
76 | 85 | function normalizeStyles(styles, builder) {
|
77 |
| - for (var key in styles) { |
78 |
| - if (styles.hasOwnProperty(key)) { |
79 |
| - builder._styles[key] = normalizeStyleAttributes(styles[key], builder); |
80 |
| - } |
81 |
| - } |
| 86 | + _each(styles, function(style, key) { |
| 87 | + builder._styles[key] = normalizeStyleAttributes(style, builder); |
| 88 | + }); |
82 | 89 | }
|
83 | 90 |
|
84 | 91 | /**
|
85 | 92 | * Normalize the styleset attributes when registering stylesets.
|
86 | 93 | * Recurse function for normalizeStyles
|
| 94 | + * - styleset: the style attributes for a particular style class |
| 95 | + * - builder: the associated Builder |
87 | 96 | */
|
88 | 97 | function normalizeStyleAttributes(styleset, builder) {
|
89 | 98 | var name;
|
90 | 99 | if (_isFunction(styleset)) {
|
| 100 | + // user provided function so we need to give them the css context to work with |
91 | 101 | return function(varRetriever) {
|
92 | 102 | return styleset.call(varRetriever, new StyleContext(varRetriever, builder));
|
93 | 103 | };
|
94 | 104 | }
|
95 | 105 |
|
96 |
| - if (styleset.attributes) { |
97 |
| - // has nesting |
98 |
| - var rtn = normalizeStyleAttributes(styleset.attributes); |
99 |
| - for (name in styleset) { |
100 |
| - if (styleset.hasOwnProperty(name) && name !== 'attributes') { |
101 |
| - rtn[name] = normalizeStyleAttributes(styleset[name], builder); |
| 106 | + var attr = styleset.attributes; |
| 107 | + if (attr) { |
| 108 | + // any nesting parent *must* include the "attributes" value |
| 109 | + var rtn = normalizeStyleAttributes(attr); |
| 110 | + _each(styleset, function(attr, name) { |
| 111 | + if (name !== 'attributes') { |
| 112 | + rtn[name] = normalizeStyleAttributes(attr, builder); |
102 | 113 | }
|
103 |
| - } |
| 114 | + }); |
104 | 115 | return rtn;
|
105 | 116 | } else {
|
106 |
| - if (_isDeep(styleset)) { |
107 |
| - // nesting container |
108 |
| - for (name in styleset) { |
109 |
| - if (styleset.hasOwnProperty(name)) { |
110 |
| - styleset[name] = normalizeStyleAttributes(styleset[name], builder); |
111 |
| - } |
112 |
| - } |
113 |
| - return styleset; |
114 |
| - } else { |
115 |
| - // simple attributes |
116 |
| - return function() { return styleset; }; |
117 |
| - } |
| 117 | + // simple attributes |
| 118 | + return function() { return styleset; }; |
118 | 119 | }
|
119 | 120 | }
|
120 | 121 |
|
121 | 122 |
|
122 |
| - var Builder = function(parent) { |
| 123 | + /** |
| 124 | + * The object returned when calling require('react-css-builder').register('...') |
| 125 | + */ |
| 126 | + var Builder = _createClass(function(parent) { |
123 | 127 | this._vars = {};
|
124 | 128 | this._mixins = {};
|
125 | 129 | this._styles = {};
|
126 | 130 | this.parent = parent;
|
127 |
| - }; |
128 |
| - _extend(Builder.prototype, { |
| 131 | + }, { |
| 132 | + |
| 133 | + // return |
129 | 134 | css: function(paths) {
|
130 | 135 | return new StyleSelector(paths, this).css();
|
131 | 136 | },
|
|
144 | 149 | },
|
145 | 150 |
|
146 | 151 | vars: function(vars) {
|
147 |
| - if (typeof vars === 'string') { |
| 152 | + if (_isString(vars)) { |
148 | 153 | return this._vars[vars] || (this.parent && this.parent.vars(vars));
|
149 | 154 | }
|
150 | 155 |
|
|
154 | 159 | });
|
155 | 160 |
|
156 | 161 |
|
157 |
| - // globals |
158 |
| - var builders = {}, |
159 |
| - main = new Builder(); |
160 |
| - function mainFunc(name) { |
161 |
| - return function() { |
162 |
| - main[name].apply(main, arguments); |
163 |
| - }; |
164 |
| - } |
165 |
| - |
166 |
| - var root = { |
167 |
| - register: function(namespace, _styles) { |
168 |
| - if (!_styles) { |
169 |
| - _styles = namespace; |
170 |
| - namespace = undefined; |
171 |
| - } |
172 |
| - |
173 |
| - var builder = namespace && builders[namespace]; |
174 |
| - if (!builder) { |
175 |
| - builder = new Builder(main); |
176 |
| - if (namespace) { |
177 |
| - builder.namespace = namespace; |
178 |
| - builders[namespace] = builder; |
179 |
| - } |
180 |
| - } |
181 |
| - |
182 |
| - if (_isFunction(_styles)) { |
183 |
| - _styles = _styles(rtn); |
184 |
| - } |
185 |
| - normalizeStyles(_styles, builder); |
186 |
| - return builder; |
187 |
| - }, |
188 |
| - |
189 |
| - vars: mainFunc('vars'), |
190 |
| - |
191 |
| - mixin: mainFunc('mixin') |
192 |
| - }; |
193 |
| - |
194 | 162 | /**
|
195 | 163 | * Class used as the return response from calling "css" on exports.
|
196 | 164 | * Allows for chained commands to be completed by the "val" method
|
197 | 165 | * to return the final styleset values.
|
198 | 166 | */
|
199 |
| - var StyleSelector = function(paths, builder) { |
200 |
| - this.paths = normalizePaths(paths); |
| 167 | + var StyleSelector = _createClass(function(path, builder) { |
| 168 | + this.paths = normalizePaths(path); |
201 | 169 | this.builder = builder;
|
202 | 170 | var self = this;
|
203 | 171 | this.varRetriever = {
|
204 | 172 | get: function(key) {
|
205 | 173 | return (self._vars && self._vars[key]) || self.builder.vars(key);
|
206 | 174 | }
|
207 | 175 | };
|
208 |
| - }; |
209 |
| - _extend(StyleSelector.prototype, { |
| 176 | + }, { |
210 | 177 | attr: function(attrs) {
|
211 | 178 | this._attrs = _attrs;
|
212 | 179 | return this;
|
|
222 | 189 |
|
223 | 190 | var attrs = {};
|
224 | 191 | for (var i=0; i<this.paths.length; i++) {
|
225 |
| - return getStyleSet(this.paths[i], this.builder)(this.varRetriever); |
| 192 | + _.extend(attrs, getStyleSet(this.paths[i], this.builder)(this.varRetriever)); |
226 | 193 | }
|
227 | 194 | _.extend(attrs, this._attrs);
|
228 | 195 | return attrs;
|
229 | 196 | }
|
230 | 197 | });
|
231 | 198 |
|
232 |
| - function normalizePaths(paths) { |
233 |
| - return paths && (Array.isArray(paths) ? paths : paths.split(/\s+/)); |
| 199 | + var pathCache = {}, |
| 200 | + nestingMatchPattern = /[^\s,]+\s*\[[^\]]+\]/g, |
| 201 | + nestingChildPattern = /^([^\[\s]+)\s*\[([^\]]+)/; |
| 202 | + |
| 203 | + /** |
| 204 | + * Normalize the css selector path |
| 205 | + * multiple classes can be included and separated with whitespace or comma |
| 206 | + * multiple nested classes have a shorthand of parent[child1 child2 ...] (children separated with space or comma) |
| 207 | + * For example foo, a[b c] d[e,f], bar = ['foo', 'a.b', 'a.c', 'd.e', 'd.f', 'bar'] |
| 208 | + */ |
| 209 | + function normalizePaths(path) { |
| 210 | + var rtn = pathCache[path]; |
| 211 | + if (!rtn) { |
| 212 | + var result = path.replace(nestingMatchPattern, function(val) { |
| 213 | + var match = val.match(nestingChildPattern), |
| 214 | + parts = match[2].split(/[\s,]+/g), |
| 215 | + rtn = ''; |
| 216 | + for (var i=0; i<parts.length; i++) { |
| 217 | + rtn += (' ' + match[1] + '.' + parts[i]); |
| 218 | + } |
| 219 | + return rtn; |
| 220 | + }); |
| 221 | + rtn = pathCache[path] = result.split(/[,\s]+/g); |
| 222 | + } |
| 223 | + return rtn; |
234 | 224 | }
|
235 |
| - |
| 225 | + |
236 | 226 |
|
237 | 227 | /**
|
238 | 228 | * The style context object provided to styleset functions
|
239 | 229 | */
|
240 |
| - var StyleContext = function(varRetriever, builder) { |
| 230 | + var StyleContext = _createClass(function(varRetriever, builder) { |
241 | 231 | this.varRetriever = varRetriever;
|
242 | 232 | this.builder = builder;
|
243 | 233 | this.attrs = {};
|
244 |
| - }; |
245 |
| - _extend(StyleContext.prototype, { |
| 234 | + }, { |
246 | 235 | include: function(path) {
|
247 | 236 | _extend(this.attrs, getStyleSet(path, this.builder)(this.varRetriever));
|
248 | 237 | return this;
|
|
257 | 246 | return this;
|
258 | 247 | },
|
259 | 248 | val: function(attr) {
|
260 |
| - if (attr) { |
261 |
| - return _extend(this.attrs, attr); |
262 |
| - } else { |
263 |
| - return this.attrs; |
264 |
| - } |
265 |
| - |
| 249 | + return _extend(this.attrs, attr); |
266 | 250 | }
|
267 | 251 | });
|
268 | 252 |
|
269 |
| - module.exports = root; |
| 253 | + |
| 254 | + // global cache |
| 255 | + var builders = {}, |
| 256 | + main = new Builder(); |
| 257 | + |
| 258 | + // wrapper function to ensure the context is the main Builder |
| 259 | + function mainFunc(name) { |
| 260 | + return function() { |
| 261 | + main[name].apply(main, arguments); |
| 262 | + }; |
| 263 | + } |
| 264 | + |
| 265 | + module.exports = { |
| 266 | + register: function(namespace, _styles) { |
| 267 | + if (!_styles) { |
| 268 | + _styles = namespace; |
| 269 | + namespace = undefined; |
| 270 | + } |
| 271 | + |
| 272 | + var builder = namespace && builders[namespace]; |
| 273 | + if (!builder) { |
| 274 | + builder = new Builder(main); |
| 275 | + if (namespace) { |
| 276 | + builder.namespace = namespace; |
| 277 | + builders[namespace] = builder; |
| 278 | + } |
| 279 | + } |
| 280 | + |
| 281 | + normalizeStyles(_styles, builder); |
| 282 | + return builder; |
| 283 | + }, |
| 284 | + |
| 285 | + vars: mainFunc('vars'), |
| 286 | + mixin: mainFunc('mixin') |
| 287 | + }; |
0 commit comments