-
Notifications
You must be signed in to change notification settings - Fork 215
/
index.js
136 lines (118 loc) · 3.79 KB
/
index.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
import React, { Component } from "react";
import PropTypes from "prop-types";
import hoistStatics from "hoist-non-react-statics";
import { newScript, series, noop } from "./utils";
const loadedScript = [];
const pendingScripts = {};
let failedScript = [];
const addCache = (entry) => {
if (loadedScript.indexOf(entry) < 0) {
loadedScript.push(entry);
}
};
const removeFailedScript = () => {
if (failedScript.length > 0) {
failedScript.forEach((script) => {
const node = document.querySelector(`script[src='${script}']`);
if (node != null) {
node.parentNode.removeChild(node);
}
});
failedScript = [];
}
};
export function startLoadingScripts(scripts, onComplete = noop) {
// sequence load
const loadNewScript = (script) => {
const src = typeof script === "object" ? script.src : script;
if (loadedScript.indexOf(src) < 0) {
return (taskComplete) => {
const callbacks = pendingScripts[src] || [];
callbacks.push(taskComplete);
pendingScripts[src] = callbacks;
if (callbacks.length === 1) {
return newScript(script)((err) => {
pendingScripts[src].forEach((cb) => cb(err, src));
delete pendingScripts[src];
});
}
};
}
};
const tasks = scripts.map((src) => {
if (Array.isArray(src)) {
return src.map(loadNewScript);
} else return loadNewScript(src);
});
series(...tasks)((err, src) => {
if (err) {
failedScript.push(src);
} else {
if (Array.isArray(src)) {
src.forEach(addCache);
} else addCache(src);
}
})((err) => {
removeFailedScript();
onComplete(err);
});
}
const uploaderLoader = () => (WrappedComponent) => {
class ScriptLoader extends Component {
static propTypes = {
onScriptLoaded: PropTypes.func,
};
static defaultProps = {
onScriptLoaded: noop,
};
constructor(props, context) {
super(props, context);
this.state = {
isScriptLoaded: false,
isScriptLoadSucceed: false,
};
this._isMounted = false;
}
componentDidMount() {
this._isMounted = true;
const scripts = [
["/static/js/uploader/moxie.js"],
["/static/js/uploader/plupload.dev.js"],
["/static/js/uploader/i18n/zh_CN.js"],
["/static/js/uploader/ui.js"],
["/static/js/uploader/uploader_" + window.policyType + ".js"],
];
startLoadingScripts(scripts, (err) => {
if (this._isMounted) {
this.setState(
{
isScriptLoaded: true,
isScriptLoadSucceed: !err,
},
() => {
if (!err) {
this.props.onScriptLoaded();
}
}
);
}
});
}
componentWillUnmount() {
this._isMounted = false;
}
// getWrappedInstance() {
// return this.refs.wrappedInstance;
// }
render() {
const props = {
...this.props,
...this.state,
ref: "wrappedInstance",
};
return <WrappedComponent {...props} />;
}
}
return hoistStatics(ScriptLoader, WrappedComponent);
};
export default uploaderLoader;