-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.js
145 lines (129 loc) · 3.54 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
import {createElement, Component, render, Children, PureComponent, cloneElement, unmountComponentAtNode} from 'rax';
import View from 'rax-view';
import log from '../utils/log';
import wrapperGenerator from '../utils/wrapperGenerator';
import {
MarkerAllProps,
renderMarkerComponent
} from '../utils/markerUtils';
import {
toLnglat,
toPixel
} from '../utils/common';
class Marker extends PureComponent {
static displayName = 'Marker';
map;
element;
marker;
contentWrapper;
setterMap;
converterMap;
constructor(props) {
super(props);
if (typeof window !== 'undefined') {
if (!props.__map__) {
log.warning('MAP_INSTANCE_REQUIRED');
} else {
this.setterMap = {
visible: (val) => {
if (val) {
this.marker && this.marker.show();
} else {
this.marker && this.marker.hide();
}
},
zIndex: (val) => {
this.marker && this.marker.setzIndex(val);
}
};
this.converterMap = {
position: toLnglat,
offset: toPixel
};
this.map = props.__map__;
this.element = this.map.getContainer();
setTimeout(() => {
this.createMarker(props);
}, 13);
}
}
}
get instance() {
return this.marker;
}
shouldComponentUpdate() {
return false;
}
componentWillReceiveProps(nextProps) {
if (this.map) {
this.refreshMarkerLayout(nextProps);
}
}
createMarker(props) {
const options = this.buildMarkerOptions(props);
this.marker = new window.AMap.Marker(options);
this.marker.render = (function(marker) {
return function(component) {
renderMarkerComponent(component, marker);
};
})(this.marker);
this.props.onInstanceCreated && this.props.onInstanceCreated();
this.setMarkerLayout(props);
this.setChildComponent(props);
}
// 在创建实例时根据传入配置,设置初始化选项
buildMarkerOptions(props) {
let opts = {};
MarkerAllProps.forEach((key) => {
if (key in props) {
opts[key] = this.getSetterParam(key, props[key]);
}
});
opts.map = this.map;
return opts;
}
// 初始化标记的外观
setMarkerLayout(props) {
if (('render' in props) || ('children' in props && props.children)) {
this.createContentWrapper();
if ('className' in props && props.className) {
// https://github.com/ElemeFE/react-amap/issues/40
this.contentWrapper.className = props.className;
}
}
}
createContentWrapper() {
this.contentWrapper = document.createElement('div');
this.marker.setContent(this.contentWrapper);
}
setChildComponent(props) {
if (this.contentWrapper) {
if ('className' in props && props.className) {
// https://github.com/ElemeFE/react-amap/issues/40
this.contentWrapper.className = props.className;
}
if ('render' in props) {
renderMarkerComponent(props.render, this.marker);
} else if ('children' in props) {
const child = props.children;
const childType = typeof child;
if (childType !== 'undefined' && this.contentWrapper) {
render(<View>{child}</View>, this.contentWrapper);
}
}
}
}
refreshMarkerLayout(nextProps) {
this.setChildComponent(nextProps);
}
getSetterParam(key, val) {
if (key in this.converterMap) {
return this.converterMap[key](val);
}
return val;
}
render() {
return null;
}
}
export default wrapperGenerator(Marker);