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

122 lines
2.8 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:53:29 +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');
console.log(`${NAME}: initializing ${Drv.getName()}...`);
2020-01-29 11:26:07 +01:00
Drv.init($el, config);
ui.drv = Drv;
2020-01-31 19:53:29 +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-01-31 19:53:29 +01:00
Drv.geocode(config['address'], result => {
2019-12-17 18:05:46 +01:00
console.log(result);
});
} else if (config['lat'] && config['lng']) {
console.log(`${NAME}: setting up single lat/lng marker`);
if (!config['icon']) {
config['icon'] = '<i class="fas fa-map-marker-alt"></i>';
}
Drv.addMarker([config['lng'], config['lat']], config);
}
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') {
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;
$.fn[NAME].noConflict = function() {
$.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;