-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
160 lines (145 loc) · 4.74 KB
/
index.html
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
<head>
<link rel="stylesheet" href="res/css/normalize.css">
<link rel="stylesheet" href="res/css/skeleton.css">
<link rel="stylesheet" href="res/leaflet.css" />
<meta charset="UTF-8">
<style>
#mapid { height: 380px;
width: 380px;
}
body {
margin: 24px;
}
.loader {
border: 4px solid #f3f3f3;
border-radius: 50%;
border-top: 4px solid #3498db;
width: 20px;
height: 20px;
animation: spin 0.8s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
<script src="res/leaflet.js"></script>
</head>
<div id="main" style="margin-right: 70px;"></div>
<script src="main.js"></script>
<script>
var myIcon = L.icon({
iconUrl: 'res/images/marker-icon-orange.png',
iconRetinaUrl: 'res/images/marker-icon-orange-2x.png',
shadowUrl: 'res/images/marker-shadow.png',
iconSize: [25, 41],
iconAnchor: [12, 41],
popupAnchor: [1, -34],
tooltipAnchor: [16, -28],
shadowSize: [41, 41]
});
var blueIcon = L.icon({
iconUrl: 'res/images/marker-icon.png',
iconRetinaUrl: 'res/images/marker-icon-2x.png',
shadowUrl: 'res/images/marker-shadow.png',
iconSize: [25, 41],
iconAnchor: [12, 41],
popupAnchor: [1, -34],
tooltipAnchor: [16, -28],
shadowSize: [41, 41]
});
var node = document.getElementById('main');
var app = Elm.Main.embed(node);
var markers = []
var oldSelected = 0
var mymap;
var bodyObserver = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
setupLeaflet()
this.disconnect()
}.bind(this));
}).observe(document.querySelector("#main"), { attributes: true, childList: true, characterData: true });
function setupLeaflet() {
mymap = L.map('mapid', {
center: [53.483873, -2.237027],
zoom: 13
});
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={token}', {
maxZoom: 17,
attribution: '',
id: 'mapbox.streets',
token: 'pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpandmbXliNDBjZWd2M2x6bDk3c2ZtOTkifQ._QA7i5Mpkd_m30IGElHziw'
}).addTo(mymap);
var target = document.getElementById('mapid');
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
var name = mutation.attributeName
var value = target.getAttribute(name)
if(name=="companyselected") {
highlightMarker(JSON.parse(value))
}
else if(name=="pins") {
addLeafletPins(JSON.parse(value))
}
console.log(name, value);
});
}).observe(target, { attributes: true, childList: true, characterData: true });
}
function highlightMarker(arg) {
oldSelected = arg
for(var i=0;i<markers.length;i++) {
if(markers[i].companyId == arg) {
markers[i].setIcon(myIcon);
mymap.setView(new L.LatLng(markers[i].lat, markers[i].lon), 13, {"animate": true});
} else {
markers[i].setIcon(blueIcon);
}
}
}
function addLeafletPins(arg) {
var companies = arg
for(var i=0;i<markers.length;i++) {
mymap.removeLayer(markers[i])
}
for(var i = 0; i < companies.length; i++) {
var company = companies[i]
var marker = L.marker([company.lat, company.lon]).addTo(mymap);
if(company.id == oldSelected) marker.setIcon(myIcon)
marker.companyId = company.id
marker.lat = company.lat
marker.lon = company.lon
markers.push(marker)
marker.addEventListener("click", function(e) {
for(var i=0;i<markers.length;i++) {
markers[i].setIcon(blueIcon);
}
e.target.setIcon(myIcon)
app.ports.companyClick.send(e.target.companyId);
oldSelected = e.target.companyId
})
}
}
app.ports.focusOnHtml.subscribe(focusOn);
function focusOn(focusId) {
window.requestAnimationFrame(function() {
var ts = document.querySelectorAll(".focus")
for(var i = 0; i < ts.length; i++ ) {
var t = ts[i]
if(t && t.style.display != "none") {
console.debug("request focus on element with focus class", t)
t.focus()
t.classList.remove('focus')
}
}
})
}
app.ports.focusOnHtmlId.subscribe(focusOnId);
function focusOnId(focusId) {
window.requestAnimationFrame(function() {
var t = document.querySelector(focusId)
if(t && t.style.display != "none") {
t.focus()
}
})
}
</script>