Skip to content

Commit cef56b2

Browse files
committed
Make app configurable through OSMCha
1 parent 2499168 commit cef56b2

File tree

8 files changed

+102
-70
lines changed

8 files changed

+102
-70
lines changed

lib/config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ export const config = {
44
overpassBase: 'https://overpass.osmcha.org/api/interpreter',
55
osmchaBase: 'https://osmcha.org/',
66
osmBase: DEFAULT_OSM_URL,
7-
osmApiBase: `${DEFAULT_OSM_URL}/api/0.6/`,
7+
osmApiBase: `${DEFAULT_OSM_URL}/api/0.6`,
88
mapboxAccessToken:
99
'pk.eyJ1Ijoib3BlbnN0cmVldG1hcCIsImEiOiJjam10OXpmc2YwMXI5M3BqeTRiMDBqMHVyIn0.LIcIDe3TZLSDdTWDoojzNg',
1010
S3_URL: 'https://s3.amazonaws.com/mapbox/real-changesets/production/',
11+
enableRealChangesets: false,
1112
isOSMApp: DEFAULT_OSM_URL === 'https://www.openstreetmap.org'
1213
};

lib/featureDiff/MetadataTable.js

+15-8
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ import { propsDiff } from '../propsDiff';
55
import { Dropdown } from '../dropdown';
66
import { DiffTable } from './DiffTable';
77
import { FlagButton } from './FlagButton';
8-
import { config } from '../config';
98

10-
export const MetadataTable = ({ featuresWithId, id, changesetId }) => {
9+
export const MetadataTable = ({
10+
featuresWithId,
11+
id,
12+
changesetId,
13+
isOSMApp,
14+
osmBase
15+
}) => {
1116
const type = featuresWithId[0].properties.type;
1217
const metadataProps = featuresWithId.map(function(f) {
1318
var filteredProps = Object.assign({}, f.properties);
@@ -22,7 +27,7 @@ export const MetadataTable = ({ featuresWithId, id, changesetId }) => {
2227
const historyTools = [
2328
{
2429
label: 'OSM',
25-
href: `${config.osmBase}/${type}/${id}/history`
30+
href: `${osmBase}/${type}/${id}/history`
2631
},
2732
{
2833
label: 'Deep History',
@@ -37,11 +42,11 @@ export const MetadataTable = ({ featuresWithId, id, changesetId }) => {
3742
const editorLinks = [
3843
{
3944
label: 'OSM',
40-
href: `${config.osmBase}/${type}/${id}`
45+
href: `${osmBase}/${type}/${id}`
4146
},
4247
{
4348
label: 'iD',
44-
href: `${config.osmBase}/edit?editor=id&${type}=${id}`
49+
href: `${osmBase}/edit?editor=id&${type}=${id}`
4550
},
4651
{
4752
label: 'JOSM',
@@ -67,11 +72,11 @@ export const MetadataTable = ({ featuresWithId, id, changesetId }) => {
6772
<div id="cmap-feature-btns">
6873
<Dropdown
6974
display="History"
70-
options={config.isOSMApp ? historyTools : historyTools.slice(0, 1)}
75+
options={isOSMApp ? historyTools : historyTools.slice(0, 1)}
7176
/>
7277
<Dropdown
7378
display="Open feature"
74-
options={config.isOSMApp ? editorLinks : editorLinks.slice(0, 3)}
79+
options={isOSMApp ? editorLinks : editorLinks.slice(0, 3)}
7580
/>
7681
{token && (
7782
<FlagButton
@@ -97,5 +102,7 @@ export const MetadataTable = ({ featuresWithId, id, changesetId }) => {
97102
MetadataTable.propTypes = {
98103
featuresWithId: PropTypes.array.isRequired,
99104
id: PropTypes.string.isRequired,
100-
changesetId: PropTypes.number.isRequired
105+
changesetId: PropTypes.number.isRequired,
106+
isOSMApp: PropTypes.bool,
107+
osmBase: PropTypes.string.isRequired
101108
};

lib/featureDiff/renderFeatureDiff.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ export const renderFeatureDiff = function(
1313
changesetId,
1414
metadataContainer,
1515
tagsContainer,
16-
membersContainer
16+
membersContainer,
17+
isOSMApp,
18+
osmBase
1719
) {
1820
var featuresWithId = featureMap[id];
1921

@@ -22,6 +24,8 @@ export const renderFeatureDiff = function(
2224
featuresWithId={featuresWithId}
2325
id={id}
2426
changesetId={changesetId}
27+
isOSMApp={isOSMApp}
28+
osmBase={osmBase}
2529
/>,
2630
metadataContainer
2731
);

lib/getChangeset.js

+28-21
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,34 @@ import jsonParser from 'real-changesets-parser';
33
import { query } from './query';
44
import { config } from './config';
55

6-
export function getChangeset(changesetID, overpassBase = config.overpassBase) {
7-
return query(changesetID).then(changeset => {
8-
var url = config.S3_URL + changesetID + '.json';
9-
return fetch(url)
10-
.then(r => {
11-
if (r.ok) return r.json();
12-
// Fallback to overpass
13-
return Promise.reject();
14-
})
15-
.then(r => {
16-
if (r.elements.length === 0) return Promise.reject();
17-
var geojson = jsonParser(r);
18-
var featureMap = getFeatureMap(geojson);
19-
var ret = {
20-
geojson: geojson,
21-
featureMap: featureMap,
22-
changeset: changeset
23-
};
24-
return ret;
25-
})
26-
.catch(() => fetchFromOverPass(changesetID, changeset, overpassBase));
6+
export function getChangeset(changesetID, options) {
7+
return query(changesetID, options).then(data => {
8+
const [changeset, options] = data;
9+
if (options.enableRealChangesets) {
10+
const url = `${config.S3_URL}${changesetID}.json`;
11+
return fetch(url)
12+
.then(r => {
13+
if (r.ok) return r.json();
14+
// Fallback to overpass
15+
return Promise.reject();
16+
})
17+
.then(r => {
18+
if (r.elements.length === 0) return Promise.reject();
19+
const geojson = jsonParser(r);
20+
const featureMap = getFeatureMap(geojson);
21+
const ret = {
22+
geojson: geojson,
23+
featureMap: featureMap,
24+
changeset: changeset
25+
};
26+
return ret;
27+
})
28+
.catch(() =>
29+
fetchFromOverPass(changesetID, changeset, options.overpassBase)
30+
);
31+
} else {
32+
return fetchFromOverPass(changesetID, changeset, options.overpassBase);
33+
}
2734
});
2835
}
2936

lib/map.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ const LAYERS_KEY = {
2828
};
2929

3030
export class Map {
31-
constructor() {
31+
constructor(options) {
32+
this.options = options;
3233
this.map = null;
3334
this.queue = [];
3435
this.filterLayers = this.filterLayers.bind(this);
@@ -1261,7 +1262,9 @@ export class Map {
12611262
this.result.changeset.id,
12621263
document.querySelector('.cmap-diff-metadata'),
12631264
document.querySelector('.cmap-diff-tags'),
1264-
document.querySelector('.cmap-diff-members')
1265+
document.querySelector('.cmap-diff-members'),
1266+
this.options.isOSMApp,
1267+
this.options.osmBase
12651268
);
12661269

12671270
document.querySelector('.cmap-diff').style.display = 'block';

lib/query.js

+24-22
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
import { parse, subSeconds } from 'date-fns';
2-
import { config } from './config';
32

4-
export function query(changesetID) {
5-
var url = `${config.osmApiBase}changeset/${changesetID}.json?include_discussion=true`;
6-
var options = {
3+
export function query(changesetID, options) {
4+
const url = `${options.osmApiBase}/changeset/${changesetID}.json?include_discussion=true`;
5+
const meta = {
76
method: 'GET',
87
headers: { 'Content-Type': 'application/json' }
98
};
10-
return fetch(url, options)
9+
return fetch(url, meta)
1110
.then(r => r.json())
1211
.then(r => {
1312
const cs = r.elements[0];
14-
return {
15-
id: changesetID,
16-
uid: cs.uid,
17-
user: cs.user,
18-
from: subSeconds(
19-
parse(cs.created_at, 'yyyy-MM-dd\'T\'HH:mm:ssX', new Date()),
20-
1
21-
).toISOString(),
22-
to: cs.closed_at || null,
23-
comments: r.elements[0].discussion || [],
24-
bbox: {
25-
left: cs.minlon || -180,
26-
bottom: cs.minlat || -90,
27-
right: cs.maxlon || 180,
28-
top: cs.maxlat || 90
29-
}
30-
};
13+
return [
14+
{
15+
id: changesetID,
16+
uid: cs.uid,
17+
user: cs.user,
18+
from: subSeconds(
19+
parse(cs.created_at, 'yyyy-MM-dd\'T\'HH:mm:ssX', new Date()),
20+
1
21+
).toISOString(),
22+
to: cs.closed_at || null,
23+
comments: r.elements[0].discussion || [],
24+
bbox: {
25+
left: cs.minlon || -180,
26+
bottom: cs.minlat || -90,
27+
right: cs.maxlon || 180,
28+
top: cs.maxlat || 90
29+
}
30+
},
31+
options
32+
];
3133
});
3234
}

lib/render.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,21 @@ let map;
1515

1616
window.cmap = cmap;
1717

18-
export function render(container, changesetId, options) {
18+
export function render(container, changesetId, options = config) {
1919
container.style.width = options.width || '1000px';
2020
container.style.height = options.height || '500px';
2121

22-
options = options || {};
23-
options.overpassBase = options.overpassBase || config.overpassBase;
24-
mapboxgl.accessToken = config.mapboxAccessToken;
22+
options = Object.assign(config, options);
23+
mapboxgl.accessToken = options.mapboxAccessToken;
2524
container.classList.add('cmap-loading');
2625
if (!map) {
27-
map = new GlMap();
26+
map = new GlMap(options);
2827
}
2928

3029
if (options.data) {
3130
_render(container, changesetId, options.data, options.disableSidebar);
3231
} else {
33-
getChangeset(changesetId, options.overpassBase)
32+
getChangeset(changesetId, options)
3433
.then(result => _render(container, changesetId, result))
3534
.catch(err => {
3635
errorMessage(err.msg);

www/index.js

+18-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// import { render } from 'react-dom';
33

44
import { render as changesetMap } from '../lib/render';
5+
import { config } from '../lib/config';
56

67
// render(
78
// <div>
@@ -19,10 +20,14 @@ if (location.hash !== '') {
1920
document.getElementById('formContainer').style.display = 'none';
2021
var id = location.hash.split('/')[0].replace('#', '');
2122
var [, geometryType, featureId] = location.hash.split('/');
22-
cMap = changesetMap(document.getElementById('container'), id, {
23-
width: containerWidth,
24-
height: containerHeight
25-
});
23+
cMap = changesetMap(
24+
document.getElementById('container'),
25+
id,
26+
Object.assign(config, {
27+
width: containerWidth,
28+
height: containerHeight
29+
})
30+
);
2631
cMap.on('load', function() {
2732
cMap.emit('selectFeature', geometryType, featureId);
2833
});
@@ -35,11 +40,15 @@ document
3540
document.getElementById('formContainer').style.display = 'none';
3641
var changesetID = document.getElementById('changesetInput').value;
3742
location.hash = changesetID;
38-
cMap = changesetMap(document.getElementById('container'), changesetID, {
39-
hash: location.hash,
40-
width: containerWidth,
41-
height: containerHeight
42-
});
43+
cMap = changesetMap(
44+
document.getElementById('container'),
45+
changesetID,
46+
Object.assign(config, {
47+
hash: location.hash,
48+
width: containerWidth,
49+
height: containerHeight
50+
})
51+
);
4352
});
4453

4554
cMap.on('featureChange', function(geometryType, featureId) {

0 commit comments

Comments
 (0)