-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreact-css-builder.js
287 lines (251 loc) · 7.35 KB
/
react-css-builder.js
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// utility / helper functions
// iterate the attributes of an object
function _each(obj, callback) {
for (var name in obj) {
if (obj.hasOwnProperty(name)) {
callback(obj[name], name);
}
}
}
// extend the attributes of src into target
function _extend(target, src) {
if (src) {
_each(src, function(value, name) {
target[name] = value;
});
}
return target;
}
// return true if obj is a function
function _isFunction(obj) {
return typeof obj === 'function';
}
// return true if obj is a string
function _isString(obj) {
return typeof obj === 'string';
}
// create and return the class
function _createClass(constructor, attributes) {
_extend(constructor.prototype, attributes);
return constructor;
}
/**
* Return a style set function that should be executed as
* function(vars)
*/
var getStyleSet = function(path, builder) {
var paths = path.split('.'),
optionalBuilder = paths.length > 1 && builders[paths[0]],
rtn;
rtn = _getStyleSetFromBase(paths, 0, builder._styles);
if (!rtn && optionalBuilder) {
// try a namespace
paths.splice(0, 1);
rtn = _getStyleSetFromBase(paths, 0, optionalBuilder._styles);
}
if (!rtn) {
throw new Error('Unknown style path "' + path + '"');
}
if (!_isFunction(rtn)) {
throw new Error('style path is not a valid styleset "' + path + '"');
}
return rtn;
};
// recurse function used with getStyleSet
var _getStyleSetFromBase = function(parts, index, base) {
var part = parts[index],
rtn = base[part];
if (!rtn) {
return;
}
var nextIndex = index + 1;
if (nextIndex >= parts.length) {
return rtn;
} else {
return _getStyleSetFromBase(parts, nextIndex, rtn);
}
};
/**
* Normalize all stylesheet definitions
* - styles: the user provided stylesheet
* - builder: the associated Builder
*/
function normalizeStyles(styles, builder) {
_each(styles, function(style, key) {
builder._styles[key] = normalizeStyleAttributes(style, builder);
});
}
/**
* Normalize the styleset attributes when registering stylesets.
* Recurse function for normalizeStyles
* - styleset: the style attributes for a particular style class
* - builder: the associated Builder
*/
function normalizeStyleAttributes(styleset, builder) {
var name;
if (_isFunction(styleset)) {
// user provided function so we need to give them the css context to work with
return function(varRetriever) {
return styleset.call(varRetriever, new StyleContext(varRetriever, builder));
};
}
var attr = styleset.attributes;
if (attr) {
// any nesting parent *must* include the "attributes" value
var rtn = normalizeStyleAttributes(attr);
_each(styleset, function(attr, name) {
if (name !== 'attributes') {
rtn[name] = normalizeStyleAttributes(attr, builder);
}
});
return rtn;
} else {
// simple attributes
return function() { return styleset; };
}
}
/**
* The object returned when calling require('react-css-builder').register('...')
*/
var Builder = _createClass(function(parent) {
this._vars = {};
this._mixins = {};
this._styles = {};
this.parent = parent;
}, {
// return
css: function(paths) {
return new StyleSelector(paths, this).css();
},
get: function(paths) {
return new StyleSelector(paths, this);
},
mixin: function(name, mixin) {
if (!mixin) {
return this._mixins[name] || (this.parent && this.parent.mixin(name));
}
this._mixins[name] = mixin;
return this;
},
vars: function(vars) {
if (_isString(vars)) {
return this._vars[vars] || (this.parent && this.parent.vars(vars));
}
_extend(this._vars, vars);
return this;
}
});
/**
* Class used as the return response from calling "css" on exports.
* Allows for chained commands to be completed by the "val" method
* to return the final styleset values.
*/
var StyleSelector = _createClass(function(path, builder) {
this.paths = normalizePaths(path);
this.builder = builder;
var self = this;
this.varRetriever = {
get: function(key) {
return (self._vars && self._vars[key]) || self.builder.vars(key);
}
};
}, {
attr: function(attrs) {
this._attrs = _attrs;
return this;
},
vars: function(vars) {
this._vars = vars;
return this;
},
css: function() {
if (this.paths.length === 1 && !this.attrs) {
return getStyleSet(this.paths[0], this.builder)(this.varRetriever);
}
var attrs = {};
for (var i=0; i<this.paths.length; i++) {
_.extend(attrs, getStyleSet(this.paths[i], this.builder)(this.varRetriever));
}
_.extend(attrs, this._attrs);
return attrs;
}
});
var pathCache = {},
nestingMatchPattern = /[^\s,]+\s*\[[^\]]+\]/g,
nestingChildPattern = /^([^\[\s]+)\s*\[([^\]]+)/;
/**
* Normalize the css selector path
* multiple classes can be included and separated with whitespace or comma
* multiple nested classes have a shorthand of parent[child1 child2 ...] (children separated with space or comma)
* For example foo, a[b c] d[e,f], bar = ['foo', 'a.b', 'a.c', 'd.e', 'd.f', 'bar']
*/
function normalizePaths(path) {
var rtn = pathCache[path];
if (!rtn) {
var result = path.replace(nestingMatchPattern, function(val) {
var match = val.match(nestingChildPattern),
parts = match[2].split(/[\s,]+/g),
rtn = '';
for (var i=0; i<parts.length; i++) {
rtn += (' ' + match[1] + '.' + parts[i]);
}
return rtn;
});
rtn = pathCache[path] = result.split(/[,\s]+/g);
}
return rtn;
}
/**
* The style context object provided to styleset functions
*/
var StyleContext = _createClass(function(varRetriever, builder) {
this.varRetriever = varRetriever;
this.builder = builder;
this.attrs = {};
}, {
include: function(path) {
_extend(this.attrs, getStyleSet(path, this.builder)(this.varRetriever));
return this;
},
mixin: function(name) {
var args = Array.prototype.slice.call(arguments, 1),
mixin = this.builder.mixin(name);
if (!mixin) {
throw new Error('Unknown mixin "' + name + '"');
}
_extend(this.attrs, mixin.apply(this.varRetriever, args));
return this;
},
css: function(attr) {
return _extend(this.attrs, attr);
}
});
// global cache
var builders = {},
main = new Builder();
// wrapper function to ensure the context is the main Builder
function mainFunc(name) {
return function() {
main[name].apply(main, arguments);
};
}
module.exports = {
create: function(namespace, _styles) {
if (!_styles) {
_styles = namespace;
namespace = undefined;
}
var builder = namespace && builders[namespace];
if (!builder) {
builder = new Builder(main);
if (namespace) {
builder.namespace = namespace;
builders[namespace] = builder;
}
}
normalizeStyles(_styles, builder);
return builder;
},
vars: mainFunc('vars'),
mixin: mainFunc('mixin')
};