-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
146 lines (131 loc) · 4.55 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
import { h, app } from 'hyperapp';
import { loadCss, loadModules } from 'esri-loader';
const state = {
zoom: 4,
rotation: 90
};
const actions = {
zoomChange: (value) => (state) => {
return { zoom: value };
},
rotateChange: (value) => (state) => {
return { rotation: value };
}
};
const EsriMapView = ({
style,
basemap,
center,
zoom,
rotation,
zoomChange,
rotationChange
}) => {
return h('div', {
style,
// oncreate is fired after the element is created and attached to the DOM
oncreate: (element) => {
// use esri-loader to load required css
loadCss('https://js.arcgis.com/4.9/esri/css/view.css');
// use esri-loader to load ArcGIS API modules to create an instance of a MapView
loadModules(['esri/Map', 'esri/views/MapView'], {
url: 'https://js.arcgis.com/4.9/'
})
.then(([Map, MapView]) => {
const mapView = new MapView({
container: element,
map: new Map({
basemap: basemap || 'streets'
}),
center: center || [0, 0],
zoom: zoom || 1,
rotation: rotation || 0,
ui: {
components: ['attribution', 'zoom', 'compass']
}
});
mapView.when(() => {
// The mapView is loaded and ready here in the ArcGIS API "world".
// You could potentially communicate the entire mapView instance "out" to Hyperapp's state,
// but that may open the door to coding patterns that go against Hyperapp's paradigms regarding actions and state.
// Why is this a concern? The mapView instance has its own properties, methods, and events.
// What we have here is really a unique ArcGIS API "state" inside of a Hyperapp component.
// This Hyperapp component in turn has its own mechanisms for communicating changes to the Hyperapp state.
// here's an example of communicating a specific mapView zoom property change "out" to Hyperapp
mapView.watch('zoom', (newValue) => zoomChange(newValue));
// and another example with the rotation property
mapView.watch('rotation', (newValue) => rotationChange(newValue));
});
});
}
});
};
const view = (state, actions) => {
return h('div', {
style: {
display: 'flex',
flexDirection: 'column',
alignItems: 'center'
}
}, [
// h1 element with children elements
h('h1', {}, [
h('span', {}, 'made with '),
h('a', {
href: 'https://hyperapp.js.org/',
style: {
fontStyle: 'italic',
color: '#fff',
padding: '0.2rem',
border: '1px solid #fff',
}
}, 'Hyperapp'),
h('span', {}, ' and '),
h('a', {
href: 'https://github.com/Esri/esri-loader',
style: {
fontFamily: 'monospace',
fontSize: '1.25em',
color: '#fff',
padding: '0.2rem',
border: '1px solid #fff',
}
}, 'esri-loader'),
]),
h('p', {}, 'This is an example Hyperapp application that shows how to use esri-loader to make a custom mapping component.'),
h('a', {
style: {
color: '#fff'
},
href: 'https://github.com/jwasilgeo/esri-hyperapp-example'
}, 'Take a look at the source code for more information.'),
// p element that displays some of the Hyperapp's current state
h('p', {
style: {
fontFamily: 'monospace',
fontSize: '1.25em'
}
}, `state: zoom ${state.zoom.toFixed(2)} | rotation ${state.rotation.toFixed(2)}`),
// EsriMapView component:
// - its own HTML markup template begins with a div element
// - this shows some contrived examples of setting initial ArcGIS "mapView" constructor values
h(EsriMapView, {
style: {
height: '50vh',
width: '80%',
border: '3px solid #fff'
},
// these are initial state values to create an ArcGIS API MapView instance
basemap: 'satellite',
center: [15, 65],
zoom: state.zoom,
rotation: state.rotation,
// these are actions to be communicated from the ArcGIS API "out" to Hyperapp's state
// every time the zoom and rotation properties change they will be communicated "out" to Hyperapp's state
zoomChange: actions.zoomChange,
rotationChange: actions.rotateChange
})
]);
};
// Hyperapp entry point
app(state, actions, view, document.body);