Skip to content

Commit 164b747

Browse files
committed
support map data override from .osm via URL or Drag&Drop
1 parent ee54756 commit 164b747

File tree

2 files changed

+97
-4
lines changed

2 files changed

+97
-4
lines changed

src/index.js

Lines changed: 96 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,95 @@ var errorBox = false;
2929

3030
var gui;
3131

32+
async function getFileFromForm() {
33+
return new Promise(resolve => {
34+
const overlay = document.createElement('div');
35+
overlay.id = 'overlay';
36+
Object.assign(overlay.style, {
37+
position: 'fixed',
38+
inset: '0',
39+
background: '#222',
40+
display: 'flex',
41+
alignItems: 'center',
42+
justifyContent: 'center',
43+
zIndex: '9999',
44+
});
45+
46+
const container = document.createElement('div');
47+
Object.assign(container.style, {
48+
display: 'flex',
49+
alignItems: 'center',
50+
gap: '1rem',
51+
});
52+
53+
const text = document.createElement('p');
54+
text.textContent = 'Select .osm file:';
55+
Object.assign(text.style, {
56+
color: '#eee',
57+
margin: '0',
58+
fontSize: '1.5rem',
59+
fontFamily: 'Arial',
60+
});
61+
62+
const input = document.createElement('input');
63+
input.type = 'file';
64+
Object.assign(input.style, {
65+
fontSize: '1.5rem',
66+
padding: '0.5rem 1rem',
67+
borderRadius: '0.5rem',
68+
cursor: 'pointer',
69+
backgroundColor: '#333',
70+
color: '#eee',
71+
border: '1px solid #555',
72+
});
73+
74+
container.appendChild(text);
75+
container.appendChild(input);
76+
overlay.appendChild(container);
77+
document.body.appendChild(overlay);
78+
79+
input.addEventListener('change', event => {
80+
const file = event.target.files[0];
81+
if (file) {
82+
const reader = new FileReader();
83+
reader.onload = () => resolve(reader.result);
84+
reader.readAsText(file);
85+
overlay.remove();
86+
}
87+
});
88+
89+
overlay.addEventListener('dragover', (event) => {
90+
event.preventDefault();
91+
overlay.style.cursor = 'copy';
92+
});
93+
94+
overlay.addEventListener('dragleave', () => {
95+
overlay.style.cursor = '';
96+
});
97+
98+
overlay.addEventListener('drop', async(event) => {
99+
event.preventDefault();
100+
overlay.style.cursor = '';
101+
102+
const file = event.dataTransfer.files[0];
103+
if (file) {
104+
const reader = new FileReader();
105+
reader.onload = () => resolve(reader.result);
106+
reader.readAsText(file);
107+
overlay.remove();
108+
}
109+
});
110+
});
111+
}
112+
32113
/**
33114
* Initialize the screen
34115
*/
35116
function init() {
36-
var type = 'way';
37-
var id = 66418809;
117+
let type = 'way';
118+
let id = 66418809;
38119

39-
var displayInfo = false;
120+
let displayInfo = false;
40121

41122
window.printError = printError;
42123

@@ -53,7 +134,18 @@ function init() {
53134
if (params.has('errorBox')) {
54135
errorBox = true;
55136
}
56-
Building.downloadDataAroundBuilding(type, id).then(function(innerData){
137+
const fileUrl = new URLSearchParams(location.search).get('fromFile');
138+
async function downloadInnerData() {
139+
if (fileUrl === '') {
140+
return await getFileFromForm();
141+
} else if (fileUrl !== null) {
142+
printError('Loading map data from URL');
143+
return await (await fetch(new URLSearchParams(location.search).get('fromFile'))).text();
144+
} else {
145+
return await Building.downloadDataAroundBuilding(type, id);
146+
}
147+
}
148+
downloadInnerData().then(function(innerData){
57149
mainBuilding = new Building(id, innerData);
58150
const helperSize = mainBuilding.outerElement.getWidth();
59151
const helper = new GridHelper(helperSize / 0.9, helperSize / 9);

style.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
body {
1414
margin: 0;
15+
background-color: #222;
1516
}
1617

1718
/* Add a background color to the button if it is clicked on (add the .active class with JS), and when you move the mouse over it (hover) */

0 commit comments

Comments
 (0)