webpack-bootstrap-ui-kit/src/js_old/_components/_ui.map.api.js

137 lines
3.2 KiB
JavaScript
Raw Normal View History

2021-08-18 20:51:15 +02:00
"use strict";
2019-06-08 17:20:51 +02:00
2021-08-18 20:51:15 +02:00
import $ from "jquery";
import Events from "../_events";
2019-06-08 17:20:51 +02:00
2021-08-18 20:51:15 +02:00
import "../../scss/_components/_ui.map.scss";
2019-06-08 17:20:51 +02:00
2021-08-18 20:51:15 +02:00
import CONSTS from "js/_consts";
2019-06-08 17:20:51 +02:00
2020-01-31 19:54:00 +01:00
const MapAPI = (($) => {
2019-06-08 17:20:51 +02:00
// Constants
2021-08-18 20:51:15 +02:00
const NAME = "jsMapAPI";
2019-06-08 17:20:51 +02:00
const DATA_KEY = NAME;
2021-08-18 20:51:15 +02:00
const $BODY = $("body");
2021-08-18 20:51:15 +02:00
const MAP_DRIVER = CONSTS["MAP_DRIVER"];
const W = window;
2019-06-08 17:20:51 +02:00
class MapAPI {
// Constructor
constructor(el) {
const ui = this;
const Drv = new MAP_DRIVER();
ui.$el = $(el);
const $el = ui.$el;
const config = $el.data();
2019-06-08 17:20:51 +02:00
2021-08-18 20:51:15 +02:00
config["center"] = [
config["lng"] ? config["lng"] : $BODY.data("default-lng"),
config["lat"] ? config["lat"] : $BODY.data("default-lat"),
2019-06-08 17:20:51 +02:00
];
2021-08-18 20:51:15 +02:00
config["style"] = config["style"]
? jQuery.parseJSON(config["style"])
2020-01-31 19:53:29 +01:00
: null;
2021-08-18 20:51:15 +02:00
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>';
2020-10-26 17:07:16 +01:00
}
2020-09-09 17:40:58 +02:00
console.log(`${NAME}: init ${Drv.getName()}...`);
2020-01-29 11:26:07 +01:00
Drv.init($el, config);
ui.drv = Drv;
2020-01-31 19:54:00 +01:00
$el.on(Events.MAPAPILOADED, (e) => {
2020-01-29 11:26:07 +01:00
ui.map = Drv.getMap();
2019-06-08 17:20:51 +02:00
2021-08-18 20:51:15 +02:00
if (config["geojson"]) {
2019-12-17 18:05:46 +01: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"]);
2019-12-17 18:05:46 +01:00
console.log(`${NAME}: setting up address marker`);
2021-08-18 20:51:15 +02:00
Drv.geocode(config["address"], (results) => {
2020-10-26 17:07:16 +01: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}`
2020-10-26 17:07:16 +01:00
);
Drv.addMarker([lng, lat], config);
2020-10-26 17:08:10 +01:00
ui.map.setCenter({ lat, lng });
2019-12-17 18:05:46 +01:00
});
2021-08-18 20:51:15 +02:00
} else if (config["lat"] && config["lng"]) {
const lat = config["lat"];
const lng = config["lng"];
2019-12-17 18:05:46 +01:00
2020-10-26 17:07:16 +01:00
console.log(
2021-08-18 20:51:15 +02:00
`${NAME}: setting up single lat/lng marker lat: ${lat} lng: ${lng}`
2020-10-26 17:07:16 +01:00
);
2019-12-17 18:05:46 +01:00
2020-10-26 17:07:16 +01:00
Drv.addMarker([lng, lat], config);
2019-12-17 18:05:46 +01:00
}
2020-01-29 11:33:32 +01:00
$el.data(DATA_KEY, ui);
$el.addClass(`${NAME}-active`);
$el.trigger(Events.MAPLOADED);
console.log(`${NAME}: Map is loaded`);
2019-06-08 17:20:51 +02:00
});
}
// Public methods
getMap() {
return ui.map;
2019-06-08 17:20:51 +02:00
}
dispose() {
const ui = this;
ui.$el = null;
$.removeData(ui.$el[0], DATA_KEY);
2019-06-08 17:20:51 +02:00
ui.$el.removeClass(`${NAME}-active`);
2019-06-08 17:20:51 +02:00
}
static _jQueryInterface() {
2021-08-18 20:51:15 +02:00
if (typeof W.localStorage !== "undefined") {
2020-12-24 23:42:33 +01:00
return this.each(() => {
// attach functionality to el
const $el = $(this);
let data = $el.data(DATA_KEY);
2019-06-08 17:20:51 +02:00
if (!data) {
data = new MapAPI(this);
$el.data(DATA_KEY, data);
2019-06-08 17:20:51 +02:00
}
});
}
}
}
// jQuery interface
$.fn[NAME] = MapAPI._jQueryInterface;
$.fn[NAME].Constructor = MapAPI;
2020-12-24 23:42:33 +01:00
$.fn[NAME].noConflict = () => {
2019-06-08 17:20:51 +02:00
$.fn[NAME] = JQUERY_NO_CONFLICT;
return MapAPI._jQueryInterface;
};
// auto-apply
$(W).on(`${Events.AJAX} ${Events.LOADED}`, () => {
2021-08-18 20:51:15 +02:00
$(".mapAPI-map-container").jsMapAPI();
2019-06-08 17:20:51 +02:00
});
return MapAPI;
})($);
export default MapAPI;