2022-05-03 20:50:57 +02:00
|
|
|
'use strict'
|
2021-08-09 18:04:09 +02:00
|
|
|
|
2022-05-03 20:50:57 +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
|
2022-05-03 20:50:57 +02:00
|
|
|
const NAME = 'js-mapapi'
|
|
|
|
const MAP_DRIVER = Consts.MAP_DRIVER
|
2021-08-09 19:41:14 +02:00
|
|
|
|
|
|
|
class MapAPI {
|
|
|
|
// Constructor
|
2022-05-03 20:50:57 +02:00
|
|
|
constructor (el) {
|
|
|
|
const ui = this
|
|
|
|
const Drv = new MAP_DRIVER()
|
|
|
|
const BODY = document.querySelector('body')
|
|
|
|
const config = el.dataset
|
2022-09-23 17:37:45 +02:00
|
|
|
|
|
|
|
const centerLng = config.lng ? config.lng : BODY.dataset['default-lng'];
|
|
|
|
const centerLat = config.lat ? config.lat : BODY.dataset['default-lat'];
|
|
|
|
|
|
|
|
/*if (centerLat && centerLng) {
|
|
|
|
config.center = [parseFloat(centerLng),parseFloat(centerLat)];
|
|
|
|
}*/
|
2022-05-03 20:50:57 +02:00
|
|
|
|
|
|
|
/* config['style'] = config['style'] ?
|
2021-08-09 19:34:13 +02:00
|
|
|
jQuery.parseJSON(config['style']) :
|
|
|
|
null;
|
|
|
|
|
2022-05-03 20:50:57 +02:00
|
|
|
config['font-family'] = $BODY.css('font-family'); */
|
2021-08-09 19:34:13 +02:00
|
|
|
|
2022-05-03 20:50:57 +02:00
|
|
|
if (!config.icon) {
|
|
|
|
config.icon = '<i class="fas fa-map-marker-alt"></i>'
|
2021-08-09 19:41:14 +02:00
|
|
|
}
|
|
|
|
|
2022-05-03 20:50:57 +02:00
|
|
|
console.log(`${NAME}: init ${Drv.getName()}...`)
|
|
|
|
ui.drv = Drv
|
|
|
|
ui.el = el
|
|
|
|
ui.config = config
|
2021-08-09 19:41:14 +02:00
|
|
|
|
2022-05-03 20:50:57 +02:00
|
|
|
Drv.init(el, config)
|
2021-08-09 19:41:14 +02:00
|
|
|
|
|
|
|
el.addEventListener(Events.MAPAPILOADED, () => {
|
2022-05-03 20:50:57 +02:00
|
|
|
ui.addMarkers()
|
|
|
|
})
|
2021-08-09 18:04:09 +02:00
|
|
|
}
|
|
|
|
|
2021-08-09 19:41:14 +02:00
|
|
|
// Public methods
|
2022-05-03 20:50:57 +02:00
|
|
|
getMap () {
|
2022-05-03 21:44:33 +02:00
|
|
|
const ui = this
|
|
|
|
|
2022-05-03 20:50:57 +02:00
|
|
|
return ui.map
|
2021-08-09 19:41:14 +02:00
|
|
|
}
|
|
|
|
|
2022-05-03 20:50:57 +02:00
|
|
|
dispose () {
|
|
|
|
const ui = this
|
2021-08-09 19:41:14 +02:00
|
|
|
|
2022-05-03 20:50:57 +02:00
|
|
|
ui.el = null
|
|
|
|
ui.el.classList.remove(`${NAME}-active`)
|
2021-08-09 19:41:14 +02:00
|
|
|
}
|
|
|
|
|
2022-05-03 20:50:57 +02:00
|
|
|
addMarkers () {
|
|
|
|
console.log(`${NAME}: addMarkers`)
|
|
|
|
const ui = this
|
|
|
|
const el = ui.el
|
|
|
|
const Drv = ui.drv
|
|
|
|
const config = ui.config
|
2021-08-09 19:41:14 +02:00
|
|
|
|
2022-05-03 20:50:57 +02:00
|
|
|
ui.map = Drv.getMap()
|
2021-08-09 19:41:14 +02:00
|
|
|
|
2022-05-03 20:50:57 +02:00
|
|
|
if (config.geojson) {
|
|
|
|
console.log(`${NAME}: setting up geocode data`)
|
|
|
|
Drv.addGeoJson(config)
|
|
|
|
} else if (config.address) {
|
|
|
|
console.log(config.address)
|
|
|
|
console.log(`${NAME}: setting up address marker`)
|
|
|
|
Drv.geocode(config.address, (results) => {
|
|
|
|
console.log(results)
|
2021-08-09 19:41:14 +02:00
|
|
|
|
2022-05-03 20:50:57 +02:00
|
|
|
const lat = results[0].geometry.location.lat()
|
|
|
|
const lng = results[0].geometry.location.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}`
|
2022-05-03 20:50:57 +02:00
|
|
|
)
|
2021-08-09 19:41:14 +02:00
|
|
|
|
2022-05-03 20:50:57 +02:00
|
|
|
Drv.addMarker([lng, lat], config)
|
2021-08-09 19:41:14 +02:00
|
|
|
ui.map.setCenter({
|
|
|
|
lat,
|
|
|
|
lng,
|
2022-05-03 20:50:57 +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}`
|
2022-05-03 20:50:57 +02:00
|
|
|
)
|
2021-08-09 19:41:14 +02:00
|
|
|
|
2022-05-03 20:50:57 +02:00
|
|
|
Drv.addMarker([lng, lat], config)
|
2021-08-09 19:41:14 +02:00
|
|
|
}
|
|
|
|
|
2022-05-03 20:50:57 +02:00
|
|
|
el.classList.add(`${NAME}-active`)
|
2021-08-09 19:41:14 +02:00
|
|
|
|
2022-05-03 20:50:57 +02:00
|
|
|
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 = () => {
|
2022-05-03 20:50:57 +02:00
|
|
|
console.log(`${NAME}: init`)
|
2021-08-09 19:41:14 +02:00
|
|
|
document.querySelectorAll(`.${NAME}`).forEach((el, i) => {
|
2022-05-03 21:44:33 +02:00
|
|
|
new MapAPI(el)
|
2022-05-03 20:50:57 +02:00
|
|
|
})
|
|
|
|
}
|
2021-08-09 18:04:09 +02:00
|
|
|
|
2021-08-09 19:41:14 +02:00
|
|
|
// auto-apply
|
2022-05-03 20:50:57 +02:00
|
|
|
window.addEventListener(`${Events.LODEDANDREADY}`, init)
|
|
|
|
window.addEventListener(`${Events.AJAX}`, init)
|
2021-08-09 18:04:09 +02:00
|
|
|
|
2022-05-03 20:50:57 +02:00
|
|
|
return MapAPI
|
|
|
|
})(window)
|
2021-08-09 18:04:09 +02:00
|
|
|
|
2022-05-03 20:50:57 +02:00
|
|
|
export default MapAPI
|