forked from baidu/amis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScoped.tsx
277 lines (238 loc) · 7.38 KB
/
Scoped.tsx
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
/**
* @file 用来创建一个域,在这个域里面会把里面的运行时实例注册进来,方便组件之间的通信。
* @author fex
*/
import React from 'react';
import find from 'lodash/find';
import PropTypes from 'prop-types';
import hoistNonReactStatic from 'hoist-non-react-statics';
import qs from 'qs';
import {dataMapping} from './utils/tpl-builtin';
import {RendererEnv, RendererProps} from './factory';
import {noop, autobind, qsstringify} from './utils/helper';
import {RendererData, Action} from './types';
export interface ScopedComponentType extends React.Component<RendererProps> {
focus?: () => void;
doAction?: (
action: Action,
data: RendererData,
throwErrors?: boolean
) => void;
receive?: (values: RendererData, subPath?: string) => void;
reload?: (
subPath?: string,
query?: RendererData | null,
ctx?: RendererData
) => void;
context: any;
}
export interface IScopedContext {
parent?: AliasIScopedContext;
registerComponent: (component: ScopedComponentType) => void;
unRegisterComponent: (component: ScopedComponentType) => void;
getComponentByName: (name: string) => ScopedComponentType;
getComponents: () => Array<ScopedComponentType>;
reload: (target: string, ctx: RendererData) => void;
send: (target: string, ctx: RendererData) => void;
close: (target: string) => void;
}
type AliasIScopedContext = IScopedContext;
export const ScopedContext = React.createContext(createScopedTools(''));
function createScopedTools(
path?: string,
parent?: AliasIScopedContext,
env?: RendererEnv
): IScopedContext {
const components: Array<ScopedComponentType> = [];
return {
parent,
registerComponent(component: ScopedComponentType) {
// 不要把自己注册在自己的 Scoped 上,自己的 Scoped 是给子节点们注册的。
if (component.props.$path === path && parent) {
return parent.registerComponent(component);
}
if (!~components.indexOf(component)) {
components.push(component);
}
},
unRegisterComponent(component: ScopedComponentType) {
// 自己本身实际上注册在父级 Scoped 上。
if (component.props.$path === path && parent) {
return parent.unRegisterComponent(component);
}
const idx = components.indexOf(component);
if (~idx) {
components.splice(idx, 1);
}
},
getComponentByName(name: string) {
if (~name.indexOf('.')) {
const paths = name.split('.');
const len = paths.length;
return paths.reduce((scope, name, idx) => {
if (scope && scope.getComponentByName) {
const result = scope.getComponentByName(name);
return result && idx < len - 1 ? result.context : result;
}
return null;
}, this);
}
const resolved = find(
components,
component =>
component.props.name === name || component.props.id === name
);
return resolved || (parent && parent.getComponentByName(name));
},
getComponents() {
return components.concat();
},
reload(target: string, ctx: any) {
const scoped = this;
let targets =
typeof target === 'string' ? target.split(/\s*,\s*/) : target;
targets.forEach(name => {
const idx2 = name.indexOf('?');
let query = null;
if (~idx2) {
query = dataMapping(qs.parse(name.substring(idx2 + 1)), ctx);
name = name.substring(0, idx2);
}
const idx = name.indexOf('.');
let subPath = '';
if (~idx) {
subPath = name.substring(1 + idx);
name = name.substring(0, idx);
}
if (name === 'window') {
if (query) {
const link = location.pathname + '?' + qsstringify(query);
env ? env.updateLocation(link, true) : location.replace(link);
} else {
location.reload();
}
} else {
const component = scoped.getComponentByName(name);
component &&
component.reload &&
component.reload(subPath, query, ctx);
}
});
},
send(receive: string, values: object) {
const scoped = this;
let receives =
typeof receive === 'string' ? receive.split(/\s*,\s*/) : receive;
// todo 没找到做提示!
receives.forEach(name => {
const idx = name.indexOf('.');
let subPath = '';
if (~idx) {
subPath = name.substring(1 + idx);
name = name.substring(0, idx);
}
const component = scoped.getComponentByName(name);
if (component && component.receive) {
component.receive(values, subPath);
} else if (name === 'window' && env && env.updateLocation) {
const query = {
...(location.search ? qs.parse(location.search.substring(1)) : {}),
...values
};
const link = location.pathname + '?' + qsstringify(query);
env.updateLocation(link, true);
}
});
},
/**
* 主要是用来关闭指定弹框的
*
* @param target 目标 name
*/
close(target: string | boolean) {
const scoped = this;
if (typeof target === 'string') {
// 过滤已经关掉的,当用户 close 配置多个弹框 name 时会出现这种情况
target
.split(/\s*,\s*/)
.map(name => scoped.getComponentByName(name))
.filter(component => component && component.props.show)
.forEach(closeDialog);
}
}
};
}
function closeDialog(component: ScopedComponentType) {
(component.context as IScopedContext)
.getComponents()
.filter(
item =>
item &&
(item.props.type === 'dialog' || item.props.type === 'drawer') &&
item.props.show
)
.forEach(closeDialog);
component.props.onClose && component.props.onClose();
}
export function HocScoped<
T extends {
$path?: string;
env: RendererEnv;
}
>(
ComposedComponent: React.ComponentType<T>
): React.ComponentType<
T & {
scopeRef?: (ref: any) => void;
}
> & {
ComposedComponent: React.ComponentType<T>;
} {
class ScopedComponent extends React.Component<
T & {
scopeRef?: (ref: any) => void;
}
> {
static displayName = `Scoped(${
ComposedComponent.displayName || ComposedComponent.name
})`;
static contextType = ScopedContext;
static ComposedComponent = ComposedComponent;
ref: any;
getWrappedInstance() {
return this.ref;
}
@autobind
childRef(ref: any) {
while (ref && ref.getWrappedInstance) {
ref = ref.getWrappedInstance();
}
this.ref = ref;
}
scoped = createScopedTools(this.props.$path, this.context, this.props.env);
componentWillMount() {
const scopeRef = this.props.scopeRef;
scopeRef && scopeRef(this.scoped);
}
componentWillUnmount() {
const scopeRef = this.props.scopeRef;
scopeRef && scopeRef(null);
}
render() {
const {scopeRef, ...rest} = this.props;
return (
<ScopedContext.Provider value={this.scoped}>
<ComposedComponent
{
...(rest as any) /* todo */
}
ref={this.childRef}
/>
</ScopedContext.Provider>
);
}
}
hoistNonReactStatic(ScopedComponent, ComposedComponent);
return ScopedComponent;
}
export default HocScoped;