-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinspirations.html
More file actions
174 lines (157 loc) · 5.76 KB
/
Copy pathinspirations.html
File metadata and controls
174 lines (157 loc) · 5.76 KB
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="author" content="Nima Johari" />
<meta name="keywords"
content="programming languages, davis, software engineer, johari, nima, نیما, جوهری, جوهری زاده, دیویس, کالیفرنیا" />
<meta name="robots" content="index, follow">
<meta name="description"
content="Nima's internet corner. Nima Johari (Farsi: نیما جوهری) is a hobbyist, computer programmer and a PhD student currently in Davis, CA." />
<title>Nima Johari</title>
<link rel="stylesheet" href="grid.css">
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
</head>
<body>
<div id="elm-app"></div>
<script src="elm.js"></script>
<script>
var app = Elm.Main.init({ node: document.getElementById("elm-app") });
var map, markers;
var selectionRect = null;
var programmaticMove = false;
var dragStart = null;
var dragRect = null;
// Defer map and focus until Elm has rendered the DOM
requestAnimationFrame(function() {
document.getElementById("search-input").focus();
map = L.map("map", { boxZoom: false }).setView([30, 0], 2);
setTimeout(function() { map.invalidateSize(); }, 100);
L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>',
maxZoom: 18
}).addTo(map);
markers = L.layerGroup().addTo(map);
map.on("moveend", function() {
if (programmaticMove) {
programmaticMove = false;
return;
}
if (selectionRect) return;
var b = map.getBounds();
app.ports.onBoundsSelect.send({
south: b.getSouth(),
west: b.getWest(),
north: b.getNorth(),
east: b.getEast()
});
});
map.on("mousedown", function(e) {
if (!e.originalEvent.shiftKey) {
if (selectionRect && !dragStart) { clearSelection(); }
return;
}
map.dragging.disable();
dragStart = e.latlng;
if (dragRect) { map.removeLayer(dragRect); dragRect = null; }
});
map.on("mousemove", function(e) {
if (!dragStart) return;
var bounds = L.latLngBounds(dragStart, e.latlng);
if (dragRect) {
dragRect.setBounds(bounds);
} else {
dragRect = L.rectangle(bounds, { color: "#3388ff", weight: 2, fillOpacity: 0.15 }).addTo(map);
}
});
map.on("mouseup", function(e) {
if (!dragStart) return;
map.dragging.enable();
var bounds = L.latLngBounds(dragStart, e.latlng);
dragStart = null;
if (dragRect) { map.removeLayer(dragRect); dragRect = null; }
if (selectionRect) { map.removeLayer(selectionRect); }
selectionRect = L.rectangle(bounds, { color: "#3388ff", weight: 2, fillOpacity: 0.1, dashArray: "6" }).addTo(map);
var b = bounds;
app.ports.onBoundsSelect.send({
south: b.getSouth(),
west: b.getWest(),
north: b.getNorth(),
east: b.getEast()
});
});
});
function addMarkerForLoc(loc) {
var icon;
if (loc.thumbnail) {
icon = L.divIcon({
className: "",
html: '<div style="width:48px;height:48px;border:2px solid #333;border-radius:4px;overflow:hidden;background:#fff;box-shadow:0 1px 4px rgba(0,0,0,0.3)"><img src="' + loc.thumbnail + '" style="width:100%;height:100%;object-fit:cover" /></div>',
iconSize: [52, 52],
iconAnchor: [26, 26],
popupAnchor: [0, -26]
});
} else {
icon = L.divIcon({
className: "",
html: '<div style="width:32px;height:32px;border:2px solid #333;border-radius:4px;background:#6cf;box-shadow:0 1px 4px rgba(0,0,0,0.3)"></div>',
iconSize: [36, 36],
iconAnchor: [18, 18],
popupAnchor: [0, -18]
});
}
var marker = L.marker([loc.lat, loc.lng], { icon: icon }).bindPopup(loc.label);
marker.on("click", function() {
map.setView([loc.lat, loc.lng], 15);
});
markers.addLayer(marker);
}
function renderMarkers(locations) {
programmaticMove = true;
markers.clearLayers();
if (locations.length === 0) {
map.setView([30, 0], 2);
return;
}
var bounds = [];
locations.forEach(function(loc) {
addMarkerForLoc(loc);
bounds.push([loc.lat, loc.lng]);
});
if (bounds.length === 1) {
map.setView(bounds[0], 10);
} else {
map.fitBounds(bounds, { padding: [40, 40] });
}
}
function clearSelection() {
if (selectionRect) { map.removeLayer(selectionRect); selectionRect = null; }
app.ports.onBoundsSelect.send(null);
}
app.ports.updateMap.subscribe(function(locations) {
if (!map) return;
if (selectionRect) { map.removeLayer(selectionRect); selectionRect = null; }
renderMarkers(locations);
});
app.ports.setInputValue.subscribe(function(value) {
var el = document.getElementById("search-input");
if (el) { el.value = value; el.focus(); }
});
document.addEventListener("keydown", function(e) {
if ((e.metaKey || e.ctrlKey) && e.key === "z") {
e.preventDefault();
app.ports.onUndo.send(null);
}
if (e.key === "Tab" && document.activeElement && document.activeElement.id === "search-input") {
e.preventDefault();
app.ports.onTab.send(null);
}
if (e.key === "Escape" && selectionRect) {
clearSelection();
}
});
</script>
</body>
</html>