this_algorithm/xpin-wasm/index.js

55 lines
1.5 KiB
JavaScript
Raw Normal View History

2023-03-02 18:55:40 -05:00
// import init, { log_to_address, greet } from "./pkg/xpin_wasm.js";
2023-02-25 14:12:18 -05:00
// init().then(() => {
// console.log(greet());
// });
2023-03-02 18:55:40 -05:00
import init, { address_from_lat_lon, address_to_lat_lon } from "./pkg/xpin_wasm.js";
2023-02-25 14:12:18 -05:00
init().then(() => {
2023-02-25 14:49:09 -05:00
let updateEncode = () => {
if (!encodeAddressLat.value || !encodeAddressLon.value) {
2023-02-25 16:44:22 -05:00
encodeAddressResult.value = "";
encodeAddressError.textContent = "";
2023-02-25 14:12:18 -05:00
return;
}
2023-02-25 14:49:09 -05:00
let lat = parseFloat(encodeAddressLat.value);
let lon = parseFloat(encodeAddressLon.value);
try {
encodeAddressResult.value = address_from_lat_lon(lat, lon);
encodeAddressError.textContent = "";
} catch(e) {
2023-02-25 16:44:22 -05:00
encodeAddressResult.value = "";
2023-02-25 14:49:09 -05:00
encodeAddressError.textContent = e;
}
2023-02-25 14:12:18 -05:00
};
2023-02-25 14:49:09 -05:00
let updateDecode = () => {
let addressString = decodeAddressInput.value.trim();
if (!addressString) {
return;
}
try {
let latLon = address_to_lat_lon(addressString);
decodeAddressError.textContent = "";
decodeAddressLat.value = latLon[0];
decodeAddressLon.value = latLon[1];
} catch(e) {
decodeAddressError.textContent = e;
decodeAddressLat.value = "";
decodeAddressLon.value = "";
}
}
// Bind events
encodeAddressLat.addEventListener("input", updateEncode);
encodeAddressLon.addEventListener("input", updateEncode);
decodeAddressInput.addEventListener("input", updateDecode);
// Run in case either field contains data
updateEncode();
updateDecode();
2023-02-25 14:12:18 -05:00
});