-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathindex.js
148 lines (135 loc) · 3.21 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
137
138
139
140
141
142
143
144
145
146
147
148
import {createElement, Component, render, Children, PureComponent, cloneElement, unmountComponentAtNode} from 'rax';
import View from 'rax-view';
import wrapperGenerator from '../utils/wrapperGenerator';
import log from '../utils/log';
import { toLnglat } from '../utils/common';
/*
* props
* {
* __map__ 父级组件传过来的地图实例
* }
*/
const configurableProps = [
'center',
'radius',
'draggable',
'extData',
/* 非原生的接口,这是本组件的扩展 */
'visible',
'style'
];
const allProps = configurableProps.concat([
'zIndex',
'bubble'
]);
const CircleProps = {
__map__: null,
__ele__: null,
center: null,
onInstanceCreated: null,
radius: 0,
draggable: false,
extData: {},
visible: false,
style: {},
zIndex: 0,
bubble: false,
events: null,
children: null,
};
class Circle extends PureComponent {
static displayName = 'Circle';
props;
map;
element;
mapCircle;
setterMap;
converterMap;
constructor(props) {
super(props);
if (typeof window !== 'undefined') {
if (!props.__map__) {
log.warning('MAP_INSTANCE_REQUIRED');
} else {
const self = this;
this.setterMap = {
visible(val) {
if (self.mapCircle) {
if (val) {
self.mapCircle.show();
} else {
self.mapCircle.hide();
}
}
},
style(val) {
self.mapCircle && self.mapCircle.setOptions(val);
}
};
this.converterMap = {
center: toLnglat
};
this.state = {
loaded: false
};
this.map = props.__map__;
this.element = this.map.getContainer();
this.createInstance(props).then(() => {
this.setState({
loaded: true
});
this.props.onInstanceCreated && this.props.onInstanceCreated();
});
}
}
}
get instance() {
return this.mapCircle;
}
createInstance(props) {
const options = this.buildCreateOptions(props);
options.map = this.map;
this.mapCircle = new window.AMap.Circle(options);
return Promise.resolve(this.mapCircle);
}
buildCreateOptions(props) {
const options = {};
allProps.forEach((key) => {
if (key in props) {
if (key === 'style' && (props.style !== undefined)) {
const styleItem = Object.keys(props.style);
styleItem.forEach((item) => {
// $FlowFixMe
options[item] = props.style[item];
});
} else {
options[key] = this.getSetterValue(key, props);
}
}
});
return options;
}
getSetterValue(key, props) {
if (key in this.converterMap) {
return this.converterMap[key](props[key]);
}
return props[key];
}
renderEditor(children) {
if (!children) {
return null;
}
if (Children.count(children) !== 1) {
return null;
}
return cloneElement(Children.only(children), {
__circle__: this.mapCircle,
__map__: this.map,
__ele__: this.element
});
}
render() {
return this.state.loaded ? (this.renderEditor(this.props.children)) : null;
}
}
export default wrapperGenerator(Circle);