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

137 lines
3.2 KiB
JavaScript
Raw Normal View History

2019-06-08 17:20:51 +02:00
'use strict';
import $ from 'jquery';
import Events from '../_events';
2019-06-08 17:20:51 +02:00
import '../../scss/_components/_ui.map.scss';
2019-06-08 17:20:51 +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
const NAME = 'jsMapAPI';
const DATA_KEY = NAME;
const $BODY = $('body');
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
config['center'] = [
2020-01-29 11:26:07 +01:00
config['lng'] ? config['lng'] : $BODY.data('default-lng'),
config['lat'] ? config['lat'] : $BODY.data('default-lat'),
2019-06-08 17:20:51 +02:00
];
2020-01-31 19:53:29 +01:00
config['style'] = config['style']
? jQuery.parseJSON(config['style'])
: null;
config['font-family'] = $BODY.css('font-family');
2020-10-26 17:07:16 +01:00
if (!config['icon']) {
config['icon'] = '<i class="fas fa-map-marker-alt"></i>';
}
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
2019-12-17 18:05:46 +01: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`);
2020-10-26 17:07:16 +01:00
Drv.geocode(config['address'], (results) => {
console.log(results);
const lat = results[0].geometry.location.lat();
const lng = results[0].geometry.location.lng();
console.log(
`${NAME}: setting up single lat/lng marker lat: ${lat} lng: ${lng}`,
);
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
});
} else if (config['lat'] && config['lng']) {
2020-10-26 17:07:16 +01:00
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(
`${NAME}: setting up single lat/lng marker lat: ${lat} lng: ${lng}`,
);
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() {
if (typeof W.localStorage !== 'undefined') {
2020-10-26 17:07:16 +01:00
return this.each(function () {
// 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-10-26 17:07:16 +01:00
$.fn[NAME].noConflict = function () {
2019-06-08 17:20:51 +02:00
$.fn[NAME] = JQUERY_NO_CONFLICT;
return MapAPI._jQueryInterface;
};
// auto-apply
$(W).on(`${Events.AJAX} ${Events.LOADED}`, () => {
$('.mapAPI-map-container').jsMapAPI();
});
return MapAPI;
})($);
export default MapAPI;