Vinoteca: модуль, шаблон и компоненты интернет-магазина вина на 1С-Битрикс
This commit is contained in:
commit
f6ba0d2174
470 files changed
+26454
No files matched your search
@@ -0,0 +1,125 @@
|
||||
(function (run) {
|
||||
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', run);
|
||||
else run();
|
||||
})(function () {
|
||||
var box = document.querySelector('[data-map]');
|
||||
if (!box) return;
|
||||
var canvas = box.querySelector('[data-map-canvas]');
|
||||
var stub = box.querySelector('[data-map-stub]');
|
||||
var note = box.querySelector('[data-map-note]');
|
||||
var allow = box.querySelector('[data-map-allow]');
|
||||
var started = false;
|
||||
var map = null;
|
||||
var library = null;
|
||||
var near = !('IntersectionObserver' in window);
|
||||
|
||||
function allowed() {
|
||||
return !!(window.VinoConsent && window.VinoConsent.allowed());
|
||||
}
|
||||
|
||||
function text(name) {
|
||||
return box.getAttribute('data-text-' + name) || '';
|
||||
}
|
||||
|
||||
function reset(message) {
|
||||
if (map) {
|
||||
map.remove();
|
||||
map = null;
|
||||
}
|
||||
started = false;
|
||||
note.textContent = message;
|
||||
stub.hidden = false;
|
||||
}
|
||||
|
||||
function localize() {
|
||||
var lang = box.getAttribute('data-lang');
|
||||
var names = lang === 'ru' ? ['name:ru', 'name'] : ['name:' + lang, 'name:latin', 'name'];
|
||||
var field = ['coalesce'].concat(names.map(function (key) { return ['get', key]; }));
|
||||
map.getStyle().layers.forEach(function (layer) {
|
||||
var current = layer.layout && layer.layout['text-field'];
|
||||
if (layer.type !== 'symbol' || !current || JSON.stringify(current).indexOf('"ref"') !== -1) return;
|
||||
map.setLayoutProperty(layer.id, 'text-field', field);
|
||||
});
|
||||
}
|
||||
|
||||
function build() {
|
||||
var lon = parseFloat(box.getAttribute('data-lon'));
|
||||
var lat = parseFloat(box.getAttribute('data-lat'));
|
||||
try {
|
||||
map = new maplibregl.Map({
|
||||
container: canvas,
|
||||
style: box.getAttribute('data-style'),
|
||||
center: [lon, lat],
|
||||
zoom: parseInt(box.getAttribute('data-zoom'), 10),
|
||||
cooperativeGestures: true,
|
||||
locale: {
|
||||
'CooperativeGesturesHandler.WindowsHelpText': text('windows'),
|
||||
'CooperativeGesturesHandler.MacHelpText': text('mac'),
|
||||
'CooperativeGesturesHandler.MobileHelpText': text('mobile'),
|
||||
'NavigationControl.ZoomIn': text('zoom-in'),
|
||||
'NavigationControl.ZoomOut': text('zoom-out')
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
reset(text('fail'));
|
||||
return;
|
||||
}
|
||||
map.on('load', function () {
|
||||
localize();
|
||||
stub.hidden = true;
|
||||
});
|
||||
map.on('error', function () {
|
||||
if (map && !map.loaded()) reset(text('fail'));
|
||||
});
|
||||
map.addControl(new maplibregl.NavigationControl({ showCompass: false }), 'top-right');
|
||||
var pin = document.createElement('span');
|
||||
pin.className = 'vino-about__pin';
|
||||
new maplibregl.Marker({ element: pin }).setLngLat([lon, lat]).addTo(map);
|
||||
}
|
||||
|
||||
function load() {
|
||||
if (window.maplibregl) return build();
|
||||
if (library) return;
|
||||
var link = document.createElement('link');
|
||||
link.rel = 'stylesheet';
|
||||
link.href = box.getAttribute('data-lib-css');
|
||||
document.head.appendChild(link);
|
||||
library = document.createElement('script');
|
||||
library.src = box.getAttribute('data-lib-js');
|
||||
library.onload = build;
|
||||
library.onerror = function () {
|
||||
link.remove();
|
||||
library.remove();
|
||||
library = null;
|
||||
reset(text('fail'));
|
||||
};
|
||||
document.head.appendChild(library);
|
||||
}
|
||||
|
||||
function start() {
|
||||
if (started || !near || !allowed()) return;
|
||||
started = true;
|
||||
load();
|
||||
}
|
||||
|
||||
function consent() {
|
||||
if (allowed()) start();
|
||||
else if (started) reset(text('stub'));
|
||||
}
|
||||
|
||||
if (allow) allow.addEventListener('click', function () {
|
||||
if (window.VinoConsent) window.VinoConsent.accept();
|
||||
});
|
||||
if (!near) {
|
||||
var watcher = new IntersectionObserver(function (entries) {
|
||||
if (!entries.some(function (entry) { return entry.isIntersecting; })) return;
|
||||
near = true;
|
||||
watcher.disconnect();
|
||||
start();
|
||||
}, { rootMargin: '200px 0px' });
|
||||
watcher.observe(box);
|
||||
}
|
||||
document.addEventListener('vino:consent', consent);
|
||||
window.addEventListener('load', start);
|
||||
start();
|
||||
});
|
||||
Reference in new issue
Block a user