forked from qunarcorp/anu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateClass.js
executable file
·185 lines (180 loc) · 5.44 KB
/
createClass.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
/**
* React.createClass补丁 by 司徒正美 Copyright 2018-06-222
* IE9+
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.createReactClass = factory());
}(this, (function () {
var hasOwnProperty = Object.prototype.hasOwnProperty;
var __type = Object.prototype.toString;
var fakeWindow = {};
function getWindow() {
try {
return window;
} catch (e) {
try {
return global;
} catch (e) {
return fakeWindow;
}
}
}
function toWarnDev(msg, deprecated) {
msg = deprecated ? msg + ' is deprecated' : msg;
var process = getWindow().process;
if (process && process.env.NODE_ENV === 'development') {
throw msg;
}
}
function extend(obj, props) {
for (var i in props) {
if (hasOwnProperty.call(props, i)) {
obj[i] = props[i];
}
}
return obj;
}
function inherit(SubClass, SupClass) {
function Bridge() {}
var orig = SubClass.prototype;
Bridge.prototype = SupClass.prototype;
var fn = SubClass.prototype = new Bridge();
extend(fn, orig);
fn.constructor = SubClass;
return fn;
}
function miniCreateClass(ctor, superClass, methods, statics) {
var className = ctor.name || 'IEComponent';
var Ctor = Function('superClass', 'ctor', 'return function ' + className + ' (props, context) {\n superClass.apply(this, arguments); \n ctor.apply(this, arguments);\n }')(superClass, ctor);
var fn = inherit(Ctor, superClass);
extend(fn, methods);
if (statics) {
extend(Ctor, statics);
}
return Ctor;
}
function isFn(obj) {
return __type.call(obj) === '[object Function]';
}
var NOBIND = {
render: 1,
shouldComponentUpdate: 1,
componentWillReceiveProps: 1,
componentWillUpdate: 1,
componentDidUpdate: 1,
componentWillMount: 1,
componentDidMount: 1,
componentWillUnmount: 1,
componentDidUnmount: 1
};
function collectMixins(mixins) {
var keyed = {};
for (var i = 0; i < mixins.length; i++) {
var mixin = mixins[i];
if (mixin.mixins) {
applyMixins(mixin, collectMixins(mixin.mixins));
}
for (var key in mixin) {
if (mixin.hasOwnProperty(key) && key !== "mixins") {
(keyed[key] || (keyed[key] = [])).push(mixin[key]);
}
}
}
return keyed;
}
var MANY_MERGED = {
getInitialState: 1,
getDefaultProps: 1,
getChildContext: 1
};
function flattenHooks(key, hooks) {
var hookType = __type.call(hooks[0]).slice(8, -1);
if (hookType === "Object") {
var ret = {};
for (var i = 0; i < hooks.length; i++) {
extend(ret, hooks[i]);
}
return ret;
} else if (hookType === "Function" && hooks.length > 1) {
return function () {
var ret = {},
r = void 0,
hasReturn = MANY_MERGED[key];
for (var _i = 0; _i < hooks.length; _i++) {
r = hooks[_i].apply(this, arguments);
if (hasReturn && r) {
extend(ret, r);
}
}
if (hasReturn) {
return ret;
}
return r;
};
} else {
return hooks[0];
}
}
function applyMixins(proto, mixins) {
for (var key in mixins) {
if (mixins.hasOwnProperty(key)) {
proto[key] = flattenHooks(key, mixins[key].concat(proto[key] || []));
}
}
}
var win = getWindow();
if (!win.React || !win.React.Component) {
throw "Please load the React first.";
}
win.React.createClass = createClass;
var Component = win.React.Component;
function createClass(spec) {
if (!isFn(spec.render)) {
throw "createClass(...): Class specification must implement a `render` method.";
}
var statics = spec.statics;
var Constructor = miniCreateClass(function Ctor() {
if (!(this instanceof Component)) {
throw "must new Component(...)";
}
for (var methodName in this) {
var method = this[methodName];
if (typeof method === "function" && !NOBIND[methodName]) {
this[methodName] = method.bind(this);
}
}
if (spec.getInitialState) {
var test = this.state = spec.getInitialState.call(this);
if (!(test === null || {}.toString.call(test) == "[object Object]")) {
throw "Component.getInitialState(): must return an object or null";
}
}
}, Component, spec, statics);
if (spec.mixins) {
applyMixins(spec, collectMixins(spec.mixins));
extend(Constructor.prototype, spec);
}
if (statics && statics.getDefaultProps) {
throw "getDefaultProps is not statics";
}
"propTypes,contextTypes,childContextTypes,displayName".replace(/\w+/g, function (name) {
if (spec[name]) {
var props = Constructor[name] = spec[name];
if (name !== "displayName") {
for (var i in props) {
if (!isFn(props[i])) {
toWarnDev(i + " in " + name + " must be a function");
}
}
}
}
});
if (isFn(spec.getDefaultProps)) {
Constructor.defaultProps = spec.getDefaultProps();
}
return Constructor;
}
return createClass;
})));