-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
388 lines (337 loc) · 9.39 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/* @jsx h */
import 'mapbox-gl/dist/mapbox-gl.css'
import './index.css';
import {get} from 'lodash';
import {h, Component, render} from 'preact';
import { Router, route, Link} from 'preact-router';
import classnames from 'classnames';
import Portal from 'preact-portal';
import { createHashHistory } from 'history';
import mapboxgl from 'mapbox-gl';
import {latest} from '@mapbox/mapbox-gl-style-spec';
import 'ol/ol.css'
import olms from 'ol-mapbox-style';
import {Map as OlMap, View as OlView} from 'ol';
class Style extends Component {
constructor () {
super();
this.state = {
showJson: false
}
}
render () {
const {style} = this.props;
const {showJson} = this.state;
return (
<div>
<h2>
<code>{style.path}</code>
</h2>
<p>
Supported: {get(style.style, "metadata.ol.supported") ? "✅ yes" : "❌ no"}
</p>
<div className="style">
<div
className="style__renderer style__renderer--ol"
>
<OpenLayersMapIframe {...style} style={style.id} />
</div>
<div
className="style__renderer style__renderer--mgl"
>
<Map {...style} style={style.style} />
</div>
</div>
<div className="style__toolbox">
<label>
<input
type="checkbox"
onChange={e => this.setState({showJson: e.target.checked})}
/> Show JSON
</label>
</div>
<pre
className={`style__json style__json--${showJson ? 'show' : 'hide'}`}
><code>{JSON.stringify(style.style, null, 2)}</code></pre>
</div>
);
}
}
const propertiesUnsorted = {};
Object.keys(latest).forEach(key => {
if (key.match(/^(layout|paint)_(.*)$/)) {
const type = RegExp.$1;
Object.keys(latest[key]).forEach(id => {
propertiesUnsorted[`${type}-${id}`] = {
type,
id: id,
};
})
}
});
const properties = Object.values(propertiesUnsorted).sort((a, b) => {
if (a.id < b.id) {
return -1;
}
else if (a.id > b.id) {
return 1;
}
else {
return 0;
}
});
const modules = {};
var paths = require.context('./styles', true, /.json$/);
paths.keys().forEach(function(path) {
const rootPath = path.replace(/^\.\//, "./styles/");
if (path.match(/\.\/_/)) {
modules[rootPath] = undefined;
}
else {
modules[rootPath] = paths(path);
}
});
function clone (obj) {
return JSON.parse(JSON.stringify(obj));
}
const styles = properties.map(property => {
const path = `./styles/${property.id}.json`;
return {
id: property.id,
type: property.type,
path: path,
style: modules[path],
}
}).sort((a, b) => {
let n=0;
if (a.id < b.id) { n-=1 }
if (a.id > b.id) { n+=1 }
if (!a.style && b.style) { n+=10 }
if (a.style && !b.style) { n-=10 }
return n;
});
class OpenLayersMapIframe extends Component {
constructor (props) {
super(props);
this.onChange = this.onChange.bind(this);
this.showMap = this.showMap.bind(this);
this.state = {
url: ""
}
}
showMap () {
const loc = window.location;
const olUrl = `${loc.origin}${loc.pathname}#/ol/?style=${this.props.style}`;
this.setState({
url: olUrl,
});
}
destroyMap () {
this.setState({
url: "",
});
}
onChange (entries, observer) {
if (entries[0].intersectionRatio) {
this.showMap();
}
else {
this.destroyMap();
}
}
componentDidMount () {
this._observer = new IntersectionObserver(this.onChange);
this._observer.observe(this._el);
}
componentWillUnmount () {
this._observer.unobserve(this._el);
}
render () {
return (
<iframe ref={el => this._el = el} style={{border: "none"}} src={this.state.url} />
);
}
}
class OpenLayersMap extends Component {
constructor (props) {
super(props);
this.onChange = this.onChange.bind(this);
this.showMap = this.showMap.bind(this);
this._innerEl = null;
}
showMap () {
this.destroyMap();
const style = this.props.style;
this._innerEl = document.createElement("div");
this._innerEl.style = `
width: 100%;
height: 100%;
`;
this._el.appendChild(this._innerEl);
this._map = new OlMap({
target: this._innerEl,
view: new OlView({
zoom: this.props.style.hasOwnProperty("zoom") ? this.props.style.zoom+1 : 2,
rotation: this.props.style.hasOwnProperty("bearing") ? this.props.style.bearing*(Math.PI/180) : 0,
center: this.props.style.hasOwnProperty("center") ? this.props.style.center : [0, 0],
})
});
olms(this._map, clone(style));
}
destroyMap () {
if (this._map) {
this._map.getLayers().clear();
this._map.setTarget(null);
this._map = null;
this._el.removeChild(this._innerEl);
}
}
onChange (entries, observer) {
if (entries[0].intersectionRatio) {
this.showMap();
}
else {
this.destroyMap();
}
}
componentDidMount () {
this._observer = new IntersectionObserver(this.onChange);
this._observer.observe(this._el);
}
componentWillUnmount () {
this._observer.unobserve(this._el);
}
render () {
return (
<div style={{flex: 1}} ref={el => this._el = el} />
);
}
}
class Map extends Component {
constructor (props) {
super(props);
this._visible = false;
this.onChange = this.onChange.bind(this);
this.showMap = this.showMap.bind(this);
}
showMap () {
if (this._visible && !this._map) {
this._map = new mapboxgl.Map({
container: this._el, // container id
style: this.props.style,
zoom: this.props.style.hasOwnProperty("zoom") ? this.props.style.zoom : 1,
bearing: this.props.style.hasOwnProperty("bearing") ? this.props.style.bearing : 0,
center: this.props.style.hasOwnProperty("center") ? this.props.style.center : [0, 0],
});
this._map.addControl(new mapboxgl.NavigationControl());
window.map = this._map;
}
}
onChange (entries, observer) {
if (entries[0].intersectionRatio) {
this._visible = true;
setTimeout(this.showMap, 450);
}
else if (this._map) {
this._visible = false;
this._map.remove();
this._map = undefined;
}
}
componentDidMount () {
this._observer = new IntersectionObserver(this.onChange);
this._observer.observe(this._el);
}
componentWillUnmount () {
this._observer.unobserve(this._el);
}
render () {
return (
<div ref={el => this._el = el} />
);
}
}
class Home extends Component {
render () {
return (
<div className="Home">
<header>
<div className="content">
<h1>mapbox-gl-render-compare</h1>
<p>
Show the ways in which <a href="https://www.npmjs.com/package/ol-mapbox-style">ol-mapbox-style</a> rendering differs from <a href="https://www.npmjs.com/package/mapbox-gl">mapbox-gl</a>. Because <a href="https://www.npmjs.com/package/ol-mapbox-style">ol-mapbox-style</a> only supports a subset of the spec, use this tool to find out what is supported and find out how it differs.
</p>
<p>
We are currently testing for <code>layout</code> and <code>paint</code> properties from the style spec as an initial aim. On from that we'll be testing for
</p>
<ul>
<li><code>source</code> - we currently test <code>geojson</code>/<code>raster</code> as a part of the <code>layout</code>/<code>paint</code> tests, although <code>vector</code> also appears to work in place of <code>geojson</code>.</li>
<li><code>light</code></li>
<li><code>transition</code></li>
</ul>
</div>
</header>
<div>
<div className="content">
{styles.map((style, idx) => {
if (style.style) {
return (
<Style key={idx} style={style} />
);
}
// TODO: Temporarily ignore heatmap, fill-extrusion & hillshade
else if (style.path.match(/heatmap|fill-extrusion|hillshade/)) {
return <div/>
}
else {
return (
<div>
<h2>
<code>{style.path}</code>
</h2>
<p>
Missing
</p>
</div>
);
}
})}
</div>
</div>
<footer>
<div className="content">
Made by a <a href="https://github.com/orangemug">Mug</a>
</div>
</footer>
</div>
);
}
}
class Ol extends Component {
render () {
const url = new URL(`http://example.com/${this.props.url}`);
const styleId = url.searchParams.get("style");
let styleObj = styles.find(s => s.id === styleId) || {};
return (
<div style={{height: "100vh", width: "100%", display: "flex"}}>
<OpenLayersMap style={styleObj.style} />
</div>
);
}
}
class App extends Component {
constructor () {
super();
this.state = {};
}
render() {
return (
<Router history={createHashHistory()}>
<Home path={`/`} {...this.state} />
<Ol path={`/ol`} {...this.state} />
</Router>
);
}
}
// render an instance of Clock into <body>:
render(<App />, document.body);