-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathindex.js
164 lines (144 loc) · 3.82 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
var ol = require('openlayers');
var React = require('react');
var Redux = require('redux');
var ReactRedux = require('react-redux');
require("openlayers/css/ol.css");
require("./popup.css");
var createStore = Redux.createStore;
var Provider = ReactRedux.Provider;
var connect = ReactRedux.connect;
// OL map
var placeLayer = new ol.layer.Vector({
source: new ol.source.Vector({
format: new ol.format.GeoJSON(),
//url: "http://www.geoforall.org/locations/OSGEoLabs.json" raises
//Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://www.geoforall.org/locations/OSGEoLabs.json. (Reason: CORS header 'Access-Control-Allow-Origin' missing).
url: "OSGEoLabs.json"
})
});
map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
}),
placeLayer
],
view: new ol.View({
center: [949282, 6002552],
zoom: 4
})
});
popupElement = document.getElementById('popup');
var popup = new ol.Overlay({
element: popupElement,
autoPan: true,
autoPanAnimation: {
duration: 250
}
});
map.addOverlay(popup);
function placeName(place) {
// extract text from link
return place.name.replace(/<(?:.|\n)*?>/g, '');
}
// OL callbacks
function updateVisiblePlaces() {
var extent = map.getView().calculateExtent(map.getSize());
var places = placeLayer.getSource().getFeaturesInExtent(extent).map(function(feature) {
return feature.getProperties();
});
// Update state in Redux store
store.dispatch(visiblePlacesAction(places))
}
placeLayer.on('change', updateVisiblePlaces);
map.on('moveend', updateVisiblePlaces);
function updateSelection(name) {
var extent = map.getView().calculateExtent(map.getSize());
var selected = placeLayer.getSource().getFeaturesInExtent(extent).filter(function(feature) {
return name == placeName(feature.getProperties());
});
if (selected.length > 0) {
feature = selected[0];
popupElement.innerHTML = feature.getProperties().name;
popup.setPosition(feature.getGeometry().getFirstCoordinate());
}
}
// React component
var PlaceList = React.createClass( {
render: function() {
var onSelectClick = this.props.onSelectClick;
var selected = this.props.selected;
var createItem = function(place) {
var name = placeName(place);
var selClass = (name == selected) ? 'selected' : '';
return <li key={name} className={selClass} onClick={onSelectClick}>{name}</li>;
};
return (
<ul>
{this.props.places.map(createItem)}
</ul>
);
}
});
// Actions:
function visiblePlacesAction(places) {
return {
type: 'visible',
places: places
};
}
function selectAction(placeName) {
return {
type: 'select',
placeName: placeName
};
}
// Reducer:
function placeSelector(state, action) {
if (typeof state === 'undefined') {
state = {places: [], selected: null};
}
switch(action.type){
case 'visible':
return {places: action.places, selected: state.selected};
case 'select':
return {places: state.places, selected: action.placeName};
default:
return state;
}
}
// Store:
var store = createStore(placeSelector);
// Map Redux state to component props
function mapStateToProps(state) {
return {
places: state.places,
selected: state.selected
};
}
// Map Redux actions to component props
function mapDispatchToProps(dispatch) {
return {
onSelectClick: function(e) {
name = e.dispatchMarker.split('$')[1];
dispatch(selectAction(name));
// Update map
updateSelection(name)
}
};
}
// Connected Component:
var App = connect(
mapStateToProps,
mapDispatchToProps
)(PlaceList);
React.render(
React.createElement(Provider, {store: store},
function(){
return (<App/>)
}
),
document.getElementById('root')
);
module.exports = PlaceList;