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

115 lines
2.6 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
const MapAPI = (($) => {
// 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'] = [
(config['lng'] ? config['lng'] : $BODY.data('default-lng')),
(config['lat'] ? config['lat'] : $BODY.data('default-lat')),
2019-06-08 17:20:51 +02:00
];
config['font-family'] = $BODY.css('font-family');
console.log(`${NAME}: initializing ${Drv.getName()}...`);
ui.map = Drv.init($el, config);
2019-06-08 17:20:51 +02:00
$el.on(Events.MAPLOADED, (e) => {
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`);
Drv.geocode(config['address'], (result) => {
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);
}
console.log(`${NAME}: Map is loaded`);
2019-06-08 17:20:51 +02:00
});
$el.data(DATA_KEY, ui);
$el.addClass(`${NAME}-active`);
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;