-
Notifications
You must be signed in to change notification settings - Fork 49
/
index.js
181 lines (163 loc) · 5.76 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
import MobileDetect from 'mobile-detect'
import canvas from './source/Canvas'
import ui from './source/Ui'
import metrics from './source/Metrics'
import exporter from './source/file/Exporter'
import importer from './source/file/Importer'
import datasetResource from './source/resources/DatasetResource'
import geographyResource from './source/resources/GeographyResource'
import tilegramResource from './source/resources/TilegramResource'
import gridGeometry from './source/geometry/GridGeometry'
import {startDownload, isDevEnvironment} from './source/utils'
import {updateCanvasSize} from './source/constants'
import logo from './source/images/logo.png' // eslint-disable-line no-unused-vars
require('./source/css/main.scss')
require('font-awesome/scss/font-awesome.scss')
let cartogramComputeRafId
let importing = false
const defaultGeography = 'United States'
if (typeof window !== 'undefined') {
const mobileDetect = new MobileDetect(window.navigator.userAgent)
const isMobile = mobileDetect.mobile()
if (isMobile) {
document.body.className = 'isMobile'
}
}
function selectDataset(geography, index, customCsv) {
const dataset = index !== null ?
datasetResource.getDataset(geography, index) :
datasetResource.buildDatasetFromCustomCsv(geography, customCsv)
importing = false
ui.setSelectedDataset(dataset)
canvas.computeCartogram(dataset)
function iterateLoop() {
const [iterated] = canvas.iterateCartogram(dataset.geography)
if (iterated) {
requestAnimationFrame(iterateLoop);
} else {
canvas.updateTilesFromMetrics()
}
}
cancelAnimationFrame(cartogramComputeRafId)
canvas.progress = 0
cartogramComputeRafId = requestAnimationFrame(iterateLoop)
}
function updateUi() {
ui.setTiles(canvas.getGrid().getTiles())
ui.render()
}
function loadTopoJson(topoJson) {
cancelAnimationFrame(cartogramComputeRafId)
importing = true
const {tiles, dataset, metricPerTile, geography} = importer.fromTopoJson(topoJson)
ui.setGeography(geography)
ui.setSelectedDataset(dataset)
metrics.metricPerTile = metricPerTile
canvas.setGeoCodeToName(geographyResource.getGeoCodeHash(geography))
canvas.importTiles(tiles)
updateUi()
}
function selectGeography(geography) {
/**
* Updates ui with matching geo data (list of tilegrams, list of datasets).
* Update ui and canvas with the matching geoCodeHash for the current geography. This is used
* in the hexMetrics component and to render the labels on canvas.
* Loads the first tilegram associated with the geography if it exists, else loads the first
* dataset.
* NB: ui.selectTilegramGenerateOption is loaded _after_ the dataset is updated to prevent error
* on first load.
*/
importing = false
const datasets = datasetResource.getDatasetsByGeography(geography)
const tilegrams = tilegramResource.getTilegramsByGeography(geography)
const geoCodeToName = geographyResource.getGeoCodeHash(geography)
ui.setGeography(geography)
ui.setDatasetLabels(datasets.map(dataset => dataset.label))
ui.setTilegramLabels(tilegrams.map(tilegram => tilegram.label))
canvas.setGeoCodeToName(geoCodeToName)
if (tilegrams.length) {
loadTopoJson(tilegrams[0].topoJson)
// ui.selectTilegram(0)
ui.selectTilegramGenerateOption('import')
} else {
selectDataset(geography, 0)
ui.selectTilegramGenerateOption('generate')
}
}
function confirmNavigation(e) {
// most browsers won't let you display custom text but have something like this anyway
const message = 'Are you sure you want to leave this page? You will lose any unsaved work.'
e.returnValue = message
return message
}
function init() {
// wire up callbacks
canvas.getGrid().onChange(() => updateUi())
canvas.getGrid().setUiEditingCallback(() => ui.setEditingTrue())
ui.setAddTileCallback(id => canvas.getGrid().onAddTileMouseDown(id))
ui.setDatasetSelectedCallback((geography, index) => selectDataset(geography, index))
ui.setTilegramSelectedCallback((geography, index) => {
const tilegram = (tilegramResource.getTilegram(geography, index))
if (tilegram) {
loadTopoJson(tilegramResource.getTilegram(geography, index))
}
})
ui.setCustomDatasetCallback((geography, csv) => selectDataset(geography, null, csv))
ui.setHightlightCallback(id => canvas.getGrid().onHighlightGeo(id))
ui.setUnhighlightCallback(() => canvas.getGrid().resetHighlightedGeo())
ui.setResolutionChangedCallback((metricPerTile, sumMetrics) => {
if (importing) {
return
}
metrics.metricPerTile = metricPerTile
metrics.sumMetrics = sumMetrics
canvas.updateTilesFromMetrics()
})
ui.setUnsavedChangesCallback(() => canvas.getGrid().checkForEdits())
ui.setResetUnsavedChangesCallback(() => canvas.getGrid().resetEdits())
ui.setExportCallback(geography => {
const json = exporter.toTopoJson(
canvas.getGrid().getTiles(),
ui.getSelectedDataset(),
metrics.metricPerTile,
geography
)
startDownload({
filename: 'tiles.topo.json',
mimeType: 'text/plain',
content: JSON.stringify(json),
})
})
ui.setExportSvgCallback(geography => {
const svg = exporter.toSvg(
canvas.getGrid().getTiles(),
geography
)
startDownload({
filename: 'tiles.svg',
mimeType: 'image/svg+xml',
content: svg,
})
})
ui.setImportCallback(loadTopoJson)
ui.setGeographySelectCallback(selectGeography)
selectGeography(defaultGeography)
if (!isDevEnvironment()) {
window.addEventListener('beforeunload', confirmNavigation)
}
}
function resize() {
updateCanvasSize()
canvas.resize()
gridGeometry.resize()
canvas.getMap().updatePreProjection()
}
window.onresize = resize
resize()
// Ignore ctrl-Z altogether
document.addEventListener('keydown', event => {
if (event.metaKey && event.key === 'z') {
event.preventDefault()
}
})
init()