forked from snd/react-kup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
react-kup.js
91 lines (90 loc) · 3.2 KB
/
react-kup.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
// Generated by CoffeeScript 1.10.0
var slice = [].slice;
(function(root, factory) {
if (('function' === typeof define) && (define.amd != null)) {
return define(['react'], factory);
} else if (typeof exports !== "undefined" && exports !== null) {
return module.exports = factory(require('react'));
} else {
if (root.React == null) {
throw new Error('react-kup needs react: make sure react.js is included before react-kup.js');
}
return root.reactKup = factory(root.React);
}
})(this, function(React) {
var ReactKup;
ReactKup = function() {
this.stack = [[]];
return this;
};
ReactKup.prototype = {
normalizeChildren: function(inputs) {
var normalizeChildren, outputs, stack;
stack = this.stack;
normalizeChildren = this.normalizeChildren.bind(this);
outputs = [];
inputs.forEach(function(input) {
if (React.isValidElement(input)) {
return outputs.push(input);
} else if ('function' === typeof input) {
stack.unshift([]);
input();
return outputs = outputs.concat(stack.shift());
} else if (Array.isArray(input)) {
return outputs = outputs.concat(normalizeChildren(input));
} else if (input) {
return outputs.push(input);
}
});
return outputs;
},
build: function() {
var children, config, element, isCurrentLevelEmpty, isToplevel, isValidConfig, normalized, type;
type = arguments[0], config = arguments[1], children = 3 <= arguments.length ? slice.call(arguments, 2) : [];
isToplevel = this.stack.length === 1;
isCurrentLevelEmpty = this.stack[0].length === 0;
if (isToplevel && !isCurrentLevelEmpty) {
throw new Error('only one tag allowed on toplevel but you are trying to add a second one');
}
if (React.isValidElement(type)) {
if ((config != null) || children.length !== 0) {
throw new Error('first argument to .build() is already a react element. in this case additional arguments are not allowed.');
}
this.stack[0].push(type);
return type;
}
isValidConfig = 'object' === typeof config && !React.isValidElement(config) && !Array.isArray(config);
if (!isValidConfig) {
children.unshift(config);
config = {};
}
normalized = this.normalizeChildren(children);
element = React.createElement.apply(React, [type, config].concat(slice.call(normalized)));
this.stack[0].push(element);
return element;
},
element: function() {
return this.stack[0][0] || null;
}
};
Object.keys(React.DOM).forEach(function(tagName) {
if (ReactKup.prototype[tagName] != null) {
throw new Error("React.DOM." + tagName + " is shadowing existing method ReactKup.prototype." + tagName);
}
return ReactKup.prototype[tagName] = function() {
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return this.build.apply(this, [tagName].concat(slice.call(args)));
};
});
return function(callback) {
var kup;
kup = new ReactKup;
if (callback != null) {
callback(kup);
return kup.element();
} else {
return kup;
}
};
});