-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathmap.js
325 lines (261 loc) · 7.63 KB
/
map.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
import {
merge,
getLineUid,
getElement,
createElement,
removeElement,
} from './util'
import Defaults from './defaults/options'
import SVGCanvasElement from './svg/canvasElement'
import MapPrototypes from './core/index'
import Events from './defaults/events'
import EventHandler from './eventHandler'
import Tooltip from './elements/tooltip'
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
class Map {
static maps = {}
static defaults = Defaults
constructor(options = {}) {
// Merge the given options with the default options
this.params = merge(Map.defaults, options, true)
// Throw an error if the given map name doesn't match
// the map that was set in map file
if (!Map.maps[this.params.map]) {
throw new Error(`Attempt to use map which was not loaded: ${options.map}`)
}
this.mapData = Map.maps[this.params.map]
this.regions = {}
this.markers = {}
this.lines = {}
this.defaultWidth = this.mapData.width
this.defaultHeight = this.mapData.height
this.height = 0
this.width = 0
this.scale = 1
this.baseScale = 1
this.transX = 0
this.transY = 0
this.baseTransX = 0
this.baseTransY = 0
this.isBeingDragged = false
// `document` is already ready, just initialise now
if (document.readyState !== 'loading') {
this.init(options.selector)
} else {
// Wait until `document` is ready
window.addEventListener('DOMContentLoaded', this.init.bind(
this, options.selector
))
}
}
// Initialize the map
init(selector) {
const options = this.params
// @TODO: We can get the selector from params `this.params.selector` but unfortunately
// when passing a DOM element to jsVectorMap constructor, the DOM element doesn't get merged
// with defaults during merging the options so we need to get the selector directly from the options.
this.container = getElement(selector)
this.container.classList.add('jvm-container')
// The map canvas element
this.canvas = new SVGCanvasElement(this.container)
// Set the map's background color
this.setBackgroundColor(options.backgroundColor)
// Create regions
this.createRegions()
// Update size
this.updateSize()
// Create lines
this.createLines(options.lines || {}, options.markers || {})
// Create markers
this.createMarkers(options.markers)
// Position labels
this.repositionLabels()
// Handle the container
this.handleContainerEvents()
// Handle regions/markers events
this.handleElementEvents()
// Create toolip
if (options.showTooltip) {
this.tooltip = new Tooltip(this)
}
// Create zoom buttons if `zoomButtons` is set to true
if (options.zoomButtons) {
this.handleZoomButtons()
}
// Set selected regions if any
if (options.selectedRegions) {
this.setSelected('regions', options.selectedRegions)
}
// Set selected regions if any
if (options.selectedMarkers) {
this.setSelected('markers', options.selectedMarkers)
}
// Set focus on a spcific region
if (options.focusOn) {
this.setFocus(options.focusOn)
}
// Visualize data
if (options.visualizeData) {
this.visualizeData(options.visualizeData)
}
// Bind touch events if true
if (options.bindTouchEvents) {
if (
('ontouchstart' in window) || (window.DocumentTouch && document instanceof DocumentTouch)
) {
this.bindContainerTouchEvents()
}
}
// Create series if any
if (options.series) {
this.container.appendChild(this.legendHorizontal = createElement(
'div', 'jvm-series-container jvm-series-h'
))
this.container.appendChild(this.legendVertical = createElement(
'div', 'jvm-series-container jvm-series-v'
))
this.createSeries()
}
// Fire loaded event
this.emit(Events.onLoaded, [this])
}
// Public
emit(eventName, args) {
for (let event in Events) {
if (Events[event] === eventName && typeof this.params[event] === 'function') {
this.params[event].apply(this, args)
}
}
}
setBackgroundColor(color) {
this.container.style.backgroundColor = color
}
// Markers/Regions
getSelected(type) {
let key, selected = []
for (key in this[type]) {
if (this[type][key].element.isSelected) {
selected.push(key)
}
}
return selected
}
clearSelected(type) {
this.getSelected(type).forEach(i => {
this[type][i].element.select(false)
})
}
setSelected(type, keys) {
keys.forEach(key => {
if (this[type][key]) {
this[type][key].element.select(true)
}
})
}
// Region methods
getSelectedRegions() {
return this.getSelected('regions')
}
clearSelectedRegions() {
this.getSelected('regions').forEach(code => {
this.regions[code].element.select(false)
})
}
// Markers methods
getSelectedMarkers() {
return this.getSelected('markers')
}
clearSelectedMarkers() {
this.getSelected('markers').forEach(index => {
this.markers[index].element.select(false)
})
}
// Deprecated
addMarker(config) {
console.warn('`addMarker` method is depreacted, please use `addMarkers` instead.')
this.createMarkers([config], true)
}
addMarkers(config) {
if (Array.isArray(config)) {
return this.createMarkers(config, true)
}
this.createMarkers([config], true);
}
removeMarkers(markers) {
if (!markers) {
markers = Object.keys(this.markers)
}
markers.forEach(index => {
// Remove the element from the DOM
this.markers[index].element.remove()
// Remove the element from markers object
delete this.markers[index]
})
}
// Create line
addLine(from, to, style = {}) {
this.createLines([{ from, to, style }], this.markers, true)
}
removeLine(from, to) {
const uid = getLineUid(from, to)
if (this.lines.hasOwnProperty(uid)) {
this.lines[uid].element.remove()
delete this.lines[uid]
}
}
// Reset map
reset() {
for (let key in this.series) {
for (let i = 0; i < this.series[key].length; i++) {
this.series[key][i].clear()
}
}
if (this.legendHorizontal) {
removeElement(this.legendHorizontal)
this.legendHorizontal = null
}
if (this.legendVertical) {
removeElement(this.legendVertical)
this.legendVertical = null
}
this.scale = this.baseScale
this.transX = this.baseTransX
this.transY = this.baseTransY
this.applyTransform()
this.clearSelectedMarkers()
this.clearSelectedRegions()
this.removeMarkers()
}
// Destroy the map
destroy(destroyInstance = true) {
const eventRegistry = EventHandler.getEventRegistry()
const keys = Object.keys
// Remove tooltip from the DOM
removeElement(this.tooltip.getElement())
// Remove event registry
keys(eventRegistry).forEach(event => {
EventHandler.off(eventRegistry[event].selector, event, eventRegistry[event].handler)
})
this.emit(Events.onDestroyed)
// For perfomance issues remove all possible properties
if (destroyInstance) {
keys(this).forEach(key => {
try {
delete this[key]
} catch (e) {}
})
}
}
extend(name, callback) {
if (typeof this[name] === 'function') {
throw new Error(`The method [${name}] already exists internally please use another name.`)
}
Map.prototype[name] = callback
}
}
Object.assign(Map.prototype, MapPrototypes)
export default Map