aboutsummaryrefslogtreecommitdiff
path: root/www/mrPreview.html
blob: eb10dd264b291b1f34959e89d7f87c874f9c0e79 (plain) (blame)
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
<!DOCTYPE html>                                                                                                        
<html>
<head>
    <meta charset='utf-8' />
    <title>MapRoulette Preview</title>
    <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
    <script src='https://api.mapbox.com/mapbox-gl-js/v1.13.0/mapbox-gl.js'></script>
    <link href='https://api.mapbox.com/mapbox-gl-js/v1.13.0/mapbox-gl.css' rel='stylesheet' />
    <style>
        #map { height: 80vh; width: 100%; }

        tr:nth-child(odd) {
            background-color: #eee
        }
        tr:nth-child(even) {
            background-color: #ddd;
        }

        .modify {
            background-color: orange;
        }
        .add {
            background-color: lightgreen;
        }
        .remove {
            background-color: red;
        }
        .row {
            margin: 10px;
            border-width: 1px;
            border-color: black;
            border-style: solid;
        }
    </style>
</head>
<body>
    <div id="map"></div>
    <div id="content"></div>
<script>
    const map = new mapboxgl.Map({
        container: 'map',
        style: {
            version: 8,
            sources: {
                osm: {
                    type: 'raster',
                    tiles: ['https://tile.openstreetmap.org/{z}/{x}/{y}.png'],
                    tileSize: 256,
                    attribution: '© OpenStreetMap contributors'
                }
            },
            layers: [
                {
                    id: 'osm',
                    source: 'osm',
                    type: 'raster'
                }
            ]
        },
        center: [144.597, -37.344],
        zoom: 7
    })

    const q = new URLSearchParams(window.location.search)
    let tagAddCount = 0
    let tagModifyCount = 0
    let tagRemoveCount = 0

    fetch(q.get('changes') || 'changes.json')
        .then(res => res.json())
        .then(changes => {
            map.on('load', () => {
                const changeFeatures = Object.values(changes).map(change => change.before)
                map.addSource('features', {
                    type: 'geojson',
                    data: {
                        type: 'FeatureCollection',
                        features: changeFeatures
                    }
                })
                map.addLayer({
                    id: 'features',
                    source: 'features',
                    type: 'circle',
                    paint: {
                        'circle-color': 'blue',
                        'circle-stroke-color': 'white',
                        'circle-stroke-width': 1,
                        'circle-radius': 5
                    }
                })

                map.on('click', 'features', e => {
                    const feature = e.features[0]
                    const id = `${feature.properties['@type']}/${feature.properties['@id']}`
                    document.getElementById(id).scrollIntoView()
                })
            })

            renderChanges(changes)
        })
    function renderChanges(changes) {
        const content = document.getElementById('content')
        const headerText = document.createElement('p')
        content.appendChild(headerText)

        for (const [id, change] of Object.entries(changes)) {
            const section = document.createElement('section')
            section.setAttribute('id', id)

            const header = document.createElement('h2')
            header.textContent = id
            section.appendChild(header)

            const table = document.createElement('table')

            // table header
            const headerRow = document.createElement('tr')

            const headerTag = document.createElement('th')
            headerTag.textContent = 'Tag'
            headerRow.appendChild(headerTag)

            const headerBefore = document.createElement('th')
            headerBefore.textContent = 'Before'
            headerRow.appendChild(headerBefore)

            const headerAfter = document.createElement('th')
            headerAfter.textContent = 'After'
            headerRow.appendChild(headerAfter)

            table.appendChild(headerRow)

            let tagsModified = false
            let tagsAdded = false
            let tagsRemoved = false

            // properties
            const distinctKeys = [...new Set([...Object.keys(change.before.properties), ...Object.keys(change.after.properties)])]
            for (const key of distinctKeys) {
                const tr = document.createElement('tr')

                const tdKey = document.createElement('td')
                tdKey.textContent = key

                const tdBefore = document.createElement('td')
                const beforeValue = change.before.properties[key]
                tdBefore.textContent = beforeValue

                const tdAfter = document.createElement('td')
                const afterValue = change.after.properties[key]
                tdAfter.textContent = afterValue

                if (beforeValue && afterValue && beforeValue !== afterValue) {
                    tdBefore.classList.add('modify')
                    tdAfter.classList.add('modify')

                    tagsModified = true
                    tagModifyCount++
                }

                if (!beforeValue && afterValue) {
                    tdAfter.classList.add('add')

                    tagsAdded = true
                    tagAddCount++
                }

                if (beforeValue && !afterValue) {
                    tdAfter.classList.add('remove')

                    tagsRemoved = true
                    tagRemoveCount++
                }

                tr.appendChild(tdKey)
                tr.appendChild(tdBefore)
                tr.appendChild(tdAfter)

                table.appendChild(tr)
            }
            header.textContent = `${id} (${tagsAdded ? 'Added' : ''} ${tagsModified ? 'Modified' : ''} ${tagsRemoved ? 'Removed' : ''})`

            section.appendChild(table)
            content.appendChild(section)
        }
        headerText.textContent = `${Object.keys(changes).length} features with changes. ${tagAddCount} new tags, ${tagRemoveCount} removed tags, ${tagModifyCount} changed tags.`
    }
</script>
</body>
</html>