webpack-bootstrap-ui-kit/src/js/ui/map.api.js

121 lines
3.0 KiB
JavaScript
Raw Normal View History

2021-08-18 20:51:15 +02:00
"use strict";
2021-08-09 18:04:09 +02:00
2021-08-18 20:51:15 +02:00
import Events from "../_events";
import Consts from "js/_consts";
2021-08-09 18:04:09 +02:00
const MapAPI = ((window) => {
2021-08-09 19:41:14 +02:00
// Constants
2021-08-18 20:51:15 +02:00
const NAME = "js-mapapi";
const MAP_DRIVER = Consts["MAP_DRIVER"];
2021-08-09 19:41:14 +02:00
class MapAPI {
// Constructor
constructor(el) {
const ui = this;
const Drv = new MAP_DRIVER();
2021-08-18 20:51:15 +02:00
const BODY = document.querySelector("body");
2021-08-09 19:41:14 +02:00
const config = el.dataset;
2021-08-18 20:51:15 +02:00
config["center"] = [
config["lng"] ? config["lng"] : BODY.dataset["default-lng"],
config["lat"] ? config["lat"] : BODY.dataset["default-lat"],
2021-08-09 19:41:14 +02:00
];
/*config['style'] = config['style'] ?
2021-08-09 19:34:13 +02:00
jQuery.parseJSON(config['style']) :
null;
config['font-family'] = $BODY.css('font-family');*/
2021-08-18 20:51:15 +02:00
if (!config["icon"]) {
config["icon"] = '<i class="fas fa-map-marker-alt"></i>';
2021-08-09 19:41:14 +02:00
}
console.log(`${NAME}: init ${Drv.getName()}...`);
ui.drv = Drv;
ui.el = el;
ui.config = config;
Drv.init(el, config);
el.addEventListener(Events.MAPAPILOADED, () => {
2021-08-18 20:51:15 +02:00
ui.addMarkers();
2021-08-09 19:41:14 +02:00
});
2021-08-09 18:04:09 +02:00
}
2021-08-09 19:41:14 +02:00
// Public methods
getMap() {
return ui.map;
}
dispose() {
const ui = this;
ui.el = null;
ui.el.classList.remove(`${NAME}-active`);
}
addMarkers() {
console.log(`${NAME}: addMarkers`);
const ui = this;
const el = ui.el;
const Drv = ui.drv;
const config = ui.config;
ui.map = Drv.getMap();
2021-08-18 20:51:15 +02:00
if (config["geojson"]) {
2021-08-09 19:41:14 +02:00
console.log(`${NAME}: setting up geocode data`);
Drv.addGeoJson(config);
2021-08-18 20:51:15 +02:00
} else if (config["address"]) {
console.log(config["address"]);
2021-08-09 19:41:14 +02:00
console.log(`${NAME}: setting up address marker`);
2021-08-18 20:51:15 +02:00
Drv.geocode(config["address"], (results) => {
2021-08-09 19:41:14 +02:00
console.log(results);
const lat = results[0].geometry.location.lat();
const lng = results[0].geometry.location.lng();
console.log(
2021-08-18 20:51:15 +02:00
`${NAME}: setting up single lat/lng marker lat: ${lat} lng: ${lng}`
2021-08-09 19:41:14 +02:00
);
Drv.addMarker([lng, lat], config);
ui.map.setCenter({
lat,
lng,
});
2021-08-09 18:04:09 +02:00
});
2021-08-18 20:51:15 +02:00
} else if (config["lat"] && config["lng"]) {
const lat = config["lat"];
const lng = config["lng"];
2021-08-09 19:41:14 +02:00
console.log(
2021-08-18 20:51:15 +02:00
`${NAME}: setting up single lat/lng marker lat: ${lat} lng: ${lng}`
2021-08-09 19:41:14 +02:00
);
Drv.addMarker([lng, lat], config);
}
el.classList.add(`${NAME}-active`);
el.dispatchEvent(new Event(Events.MAPLOADED));
console.log(`${NAME}: Map is loaded`);
2021-08-09 18:04:09 +02:00
}
2021-08-09 19:41:14 +02:00
}
const init = () => {
console.log(`${NAME}: init`);
document.querySelectorAll(`.${NAME}`).forEach((el, i) => {
const map = new MapAPI(el);
});
2021-08-18 20:51:15 +02:00
};
2021-08-09 18:04:09 +02:00
2021-08-09 19:41:14 +02:00
// auto-apply
window.addEventListener(`${Events.LODEDANDREADY}`, init);
window.addEventListener(`${Events.AJAX}`, init);
2021-08-09 18:04:09 +02:00
2021-08-09 19:41:14 +02:00
return MapAPI;
2021-08-09 18:04:09 +02:00
})(window);
export default MapAPI;