-
Notifications
You must be signed in to change notification settings - Fork 5
/
react-inline-style.js
522 lines (442 loc) · 16.2 KB
/
react-inline-style.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
(function(){
var objct = require("objct/e");
///////////////////////////////////////////////////////////////////////////////
// Initial Interface
module = module || {};
module.exports = moduleFactory;
moduleFactory.define = callPreInstance("define");
moduleFactory.global = callPreInstance("global");
if(typeof define === "function" && define.amd) {
define(function(){return moduleFactory});
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// creates module closure
function moduleFactory() {
var moduleStore = {};
var moduleId = (Math.random() + 1).toString(36).substring(2);
var globalStyles = false;
var globalStylesUpdated = false;
var moduleNamespace = ""; //moduleNamespace || "";
// 1) new interface - public methods with access to moduleInstance
var moduleInstance = function(){
var mixin = {
childContextTypes: {
reactInlineStyle: function(){} // allows everything - instead of React.PropTypes.object
},
contextTypes: {
reactInlineStyle: function(){} // this avoids React dependency
},
propTypes : {
styleNamespace : function(){} // these definitions are now purely for documentation
},
componentDidMount : callInstance("_componentDidUpdateOrMount"),
componentDidUpdate : callInstance("_componentDidUpdateOrMount"),
componentWillUnmount : callInstance("_componentWillUnmount"),
getChildContext : callInstance("_getChildContext"),
getInitialState : componentFactory
};
return mixin;
}
moduleInstance.define = preInstanceDefine;
moduleInstance.global = preInstanceGlobal;
// Return instance of factory depending of context
return this.preMixin === true ?
moduleInstance:
moduleInstance();
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// creates component closure. (called as getInitialState)
// Provides access to methods via this.style.
function componentFactory() {
var store = {};
var instanceStore = {};
var ownerStyles = {};
var rootNamespace ="";
var that;
var requireConditionals = [];
var conditionalsLength = 0;
var node;
this.style = style;
this.style.define = define;
this.style.log = logStyles;
this.style._getChildContext = getChildContext;
this.style._componentWillUnmount = componentWillUnmount;
this.style._componentDidUpdateOrMount = componentDidUpdateOrMount;
return null;
///////////////////////////////////////////////////////////////////////////////
function componentDidUpdateOrMount(){
setGlobalStyles();
node = that.getDOMNode();
// defines eventListener for requiredConditional ("hover", "pressed")(only if changed);
if(conditionalsLength < requireConditionals.length) {
for(var i=0; i<requireConditionals.length; i++) {
switch(requireConditionals[i]) {
case "hover":
node.addEventListener("mouseenter", onMouseEnter);
node.addEventListener("mouseleave", onMouseLeave);
break;
case "pressed":
node.addEventListener("mousedown", onMouseDown);
node.addEventListener("mouseup", onMouseUp);
break;
}
}
conditionalsLength = requireConditionals.length;
}
}
///////////////////////////////////////////////////////////////////////////////
function componentWillUnmount(){
node.removeEventListener("mouseenter", onMouseEnter);
node.removeEventListener("mouseleave", onMouseLeave);
node.removeEventListener("mousedown", onMouseDown);
node.removeEventListener("mouseup", onMouseUp);
}
///////////////////////////////////////////////////////////////////////////////
// this.style.define()
function define(namespace, styles){
if(typeof styles === "undefined") {
styles = namespace;
namespace = "";
}
if(typeof namespace === "string") {
namespace = namespace.split(",");
}
for(var i=0; i<namespace.length; i++) {
saveStyles(namespace[i], styles, store);
};
}
///////////////////////////////////////////////////////////////////////////////
// Handles "inheritance" of styles down the component tree
function getChildContext(){
that = this;
// get react-inline-style context
this.context.reactInlineStyle = this.context.reactInlineStyle || {};
var context = this.context.reactInlineStyle;
context[this._reactInternalInstance._mountOrder] = context[this._reactInternalInstance._mountOrder] || {};
var thisContext = context[this._reactInternalInstance._mountOrder];
// get parent styles
var id, parent = this._reactInternalInstance;
var parentStyles = false;
while(parent._currentElement._owner) {
parent = parent._currentElement._owner;
id = parent._mountOrder;
if(typeof context[id] === "object" && typeof context[id].ownerStyles === "function") {
parentStyles=context[id];
break;
}
}
// define componentInstance styles
if(parentStyles) {
ownerStyles = parentStyles.ownerStyles();
rootNamespace = parentStyles.rootNamespace || "";
}
// apply root namespace
var namespace = "";
if(typeof this.props.styleNamespace === "string") {
namespace = getNamespace(this.props.styleNamespace);
ownerStyles = getClassFromString(namespace, ownerStyles);
}
rootNamespace = getNamespace(rootNamespace, namespace);
// copy styles from moduleInstance to componentInstance
new objct.e.extend(store, objct.e.deep(moduleStore), objct.e.deep(instanceStore));
// pass on component styles
context[this._reactInternalInstance._mountOrder]={
ownerStyles : objct.e(store, objct.e.deep(ownerStyles)),
rootNamespace : rootNamespace
}
return {
reactInlineStyle : context
}
}
///////////////////////////////////////////////////////////////////////////////
// this.style.log(): Log current virtual stylesheet
function logStyles(){
if(typeof console !== "object" || typeof console.log !== "function") return;
console.log("react-inline-style", {
"rootNamespace" : rootNamespace,
"stylesheet" : new objct.e(store, objct.e.deep(ownerStyles))
});
}
///////////////////////////////////////////////////////////////////////////////
// Add styles to store
function saveStyles(namespace, styles, store) {
namespace = getNamespace(namespace);
var namespaced = addNamespace(namespace, styles);
new objct.e.extend(store, objct.e.deep(namespaced));
new objct.e.extend(instanceStore, objct.e.deep(namespaced));
}
///////////////////////////////////////////////////////////////////////////////
// this.style(): Merge requested component and return "flattened" styles object
function style(){
var styles = Array.prototype.slice.call(arguments);
var stored = new objct.e(store, objct.e.deep(ownerStyles));
var returnStyles = [], style, extend, k;
styles = flattenArray(styles);
// flatten css "classes" and mix everything together -> output styles {};
for(var i = 0; i<styles.length; i++) {
style = styles[i];
if(typeof style === "string") {
if(!testConditional(style)) continue;
// add namespace
style = getNamespace(moduleNamespace, styles[i]);
// get class
style = getClassFromString(style, stored);
}
if(typeof style === "object") {
// EXTEND STYLE
if(typeof style._extend !== "undefined") {
style._extend = typeof style._extend === "string" ?
[style._extend]:
style._extend;
for(k=0; k<style._extend.length; k++) {
if(!testConditional(style._extend[k])) continue;
// add namespace
extend = getNamespace(moduleNamespace, style._extend[k]);
// get class
extend = getClassFromString(extend, stored);
if(typeof extend === "object"){
returnStyles.push(extend);
}
}
}
returnStyles.push(style);
}
}
styles = new objct(returnStyles);
//cleanup
returnStyles = {};
for(style in styles){
if(style !== "_extend" && (typeof styles[style] === "string" || typeof styles[style] === "number")) {
returnStyles[style]= styles[style];
}
}
return returnStyles;
}
///////////////////////////////////////////////////////////////////////////////
function onMouseEnter(e){
if(node !== e.target) return;
that.setState({
hover:true
});
}
///////////////////////////////////////////////////////////////////////////////
function onMouseLeave(e){
if(node !== e.target) return;
that.setState({
hover:false
});
}
///////////////////////////////////////////////////////////////////////////////
function onMouseDown(e){
if(!nodeContains(node, e.target) && node !== e.target) return;
that.setState({
pressed:true
});
}
///////////////////////////////////////////////////////////////////////////////
function onMouseUp(e){
if(!nodeContains(node, e.target) && node !== e.target) return;
that.setState({
pressed:false
});
}
///////////////////////////////////////////////////////////////////////////////
// transforms "class:hover" into "this.state.hover && "class"" then returns the result
function testConditional(style){
style = style.split(":");
if(style.length < 2) return true;
var condition = style[1];
if(!that.state) {
requireConditionals.push(condition);
return false;
}
if(typeof that.state[condition] === "undefined") requireConditionals.push(condition);
return that.state[condition];
}
}
///////////////////////////////////////////////////////////////////////////////
// Savely try to access "path.to.object.that.might.not.exist" in store
function getClassFromString(string, store){
if(string=== "." || string==="") return store;
var path = string.split(".");
var style = store;
for(var i=0; i< path.length; i++) {
if(typeof style[path[i]] !== "object") return undefined;
style = style[path[i]];
}
return style;
}
///////////////////////////////////////////////////////////////////////////////
// Style.define()
function preInstanceDefine(namespace, styles){
if(typeof styles === "undefined") {
styles = namespace;
namespace = "";
}
if(typeof namespace === "string") {
namespace = namespace.split(",");
}
for(var i=0; i<namespace.length; i++) {
var namespaced = addNamespace(namespace[i], styles);
new objct.e.extend(moduleStore, objct.e.deep(namespaced));
}
return moduleInstance;
};
///////////////////////////////////////////////////////////////////////////////
// Style.global()
function preInstanceGlobal(namespace, styles) {
if(typeof styles === "undefined") {
styles = namespace;
namespace = "";
}
if(typeof namespace === "string") {
namespace = namespace.split(",");
}
for(var i=0; i<namespace.length; i++) {
var namespaced = addNamespace(namespace[i], styles);
globalStyles = new objct.e(globalStyles || {}, objct.e.deep(namespaced));
}
globalStylesUpdated = true;
return moduleInstance;
}
///////////////////////////////////////////////////////////////////////////////
// componentDidMount && componentDidUpdate
function setGlobalStyles(){
if(!globalStyles || !globalStylesUpdated) return;
var styleTag = document.getElementById(moduleId);
if(styleTag) {
styleTag.innerHTML = globalStylesToString(globalStyles);
}
else {
styleTag = document.createElement("style");
styleTag.setAttribute("id", moduleId);
styleTag.setAttribute("class", "react-inline-style global-css");
styleTag.innerHTML = globalStylesToString(globalStyles);
document.head.appendChild(styleTag);
}
globalStylesUpdated = false;
}
///////////////////////////////////////////////////////////////////////////////
}
///////////////////////////////////////////////////////////////////////////////
// Add namespace to object
function addNamespace(namespace, obj){
if(namespace === "") return obj;
var namespaced={};
var parts = namespace.split(".");
var position = namespaced;
var part= parts.shift();
var nextPart;
while(part) {
if(typeof position[part] !== "object"){
position[part]={};
}
nextPart= parts.shift();
if(!nextPart) {
position[part] = obj;
}
else {
position = position[part];
}
part = nextPart;
}
return namespaced;
}
///////////////////////////////////////////////////////////////////////////////
// Call "method" inside componentInstance through reference on this.style
function callInstance(method){
return function(){
return this.style[method].apply(this, arguments);
}
}
///////////////////////////////////////////////////////////////////////////////
// Create moduleInstance with preMixin context then call "method"
function callPreInstance(method){
return function(){
var moduleInstance = moduleFactory.apply({preMixin : true});
moduleInstance[method].apply(this, arguments);
return moduleInstance;
}
}
///////////////////////////////////////////////////////////////////////////////
// returns the correct combined namespace from all passed namespaces
function getNamespace() {
var namespace="", argument;
for(var i=0; i<arguments.length; i++){
if(typeof arguments[i] !== "string") continue;
argument = arguments[i].split(":")[0];
if(argument.charAt(0) === ".") {
namespace = argument.substring(1);
}
else if(namespace !== ""){
namespace += "."+argument;
}
else {
namespace = argument;
}
}
return namespace;
}
///////////////////////////////////////////////////////////////////////////////
// [1,[2,3,[4],5],6,7] => [1,2,3,4,5,6,7]
function flattenArray(array){
for(var i =0; i<array.length; i++){
if(objct.isArray(array[i])){
Array.prototype.splice.apply(array, [i,1].concat(array[i]));
i--;
}
}
return array;
}
///////////////////////////////////////////////////////////////////////////////
// Transform globalStyles Object to string
// { ".classname": { color : "green"}} => ".className { color : 'green'; }"
function globalStylesToString(obj){
var string = " ";
var child, children, definitions, cssClasses, i;
for(var cssClass in obj) {
children = " ";
definitions = "";
for(var style in obj[cssClass]) {
if(typeof obj[cssClass][style] === "object") {
child = {};
// handle media queries
if(cssClass.search("@media")>=0) {
child[style] = obj[cssClass][style];
definitions += globalStylesToString(child);
continue;
}
// handle nested style
else {
child[cssClass+" "+style] = obj[cssClass][style];
children += globalStylesToString(child);
continue;
}
}
// handle normal style
else {
definitions += style.replace(/(ms)?([A-Z])/g, function(match, ms, letter){
ms = ms ? "-"+ms : "";
return ms+"-"+letter.toLowerCase();
});
definitions += ":"+obj[cssClass][style]+"; ";
}
}
if(definitions !== "") {
cssClasses = cssClass.split(",");
for(i=0; i<cssClasses.length; i++) {
string+= cssClasses[i]+" { "+definitions+"} ";
}
}
string += children;
}
return string;
}
///////////////////////////////////////////////////////////////////////////////
function nodeContains(parent, child) {
while((child=child.parentNode)&&child!==parent);
return !!child;
}
///////////////////////////////////////////////////////////////////////////////
})();