140 lines
4.6 KiB
JavaScript
140 lines
4.6 KiB
JavaScript
class SearchFeature {
|
|
constructor(options) {
|
|
this.map = options.mapCore.map;
|
|
this.target = options.target;
|
|
this.sources = options.sources;
|
|
this.proxySource = new ol.source.Vector();
|
|
this.sourceIcons = new Map();
|
|
this.highlightLayer = this.createHighlightLayer();
|
|
this.map.addLayer(this.highlightLayer);
|
|
|
|
this.initSources();
|
|
this.initSearchControl();
|
|
|
|
this.map.on('click', () => {
|
|
this.searchControl.clearHistory();
|
|
});
|
|
}
|
|
|
|
initSources() {
|
|
this.sources.forEach(({ source, default_icon }) => {
|
|
this.proxySource.addFeatures(source.getFeatures());
|
|
this.sourceIcons.set(source, default_icon);
|
|
|
|
source.on('addfeature', (e) => {
|
|
this.proxySource.addFeature(e.feature);
|
|
});
|
|
|
|
source.on('removefeature', (e) => {
|
|
this.proxySource.removeFeature(e.feature);
|
|
});
|
|
|
|
source.on('clear', () => {
|
|
this.proxySource.clear();
|
|
this.sources.forEach(({ source }) => {
|
|
this.proxySource.addFeatures(source.getFeatures());
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
findSourceForFeature(feature) {
|
|
for (const [source, icon] of this.sourceIcons.entries()) {
|
|
if (source.hasFeature(feature)) {
|
|
return source;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
getDefaultIcon(feature) {
|
|
const source = this.findSourceForFeature(feature);
|
|
return this.sourceIcons.get(source) || '/static/img/marker.png';
|
|
}
|
|
|
|
createHighlightLayer() {
|
|
return new ol.layer.Vector({
|
|
source: new ol.source.Vector(),
|
|
style: () => new ol.style.Style({
|
|
image: new ol.style.Circle({
|
|
radius: 15,
|
|
fill: new ol.style.Fill({ color: 'rgba(255,0,0,0.3)' }),
|
|
stroke: new ol.style.Stroke({ color: 'red', width: 2 })
|
|
})
|
|
})
|
|
});
|
|
}
|
|
|
|
initSearchControl() {
|
|
const property = $(".options select").val(); // Лучше передавать как опцию
|
|
this.searchControl = new ol.control.SearchFeature({
|
|
source: this.proxySource,
|
|
property: property,
|
|
placeholder: "Поиск...",
|
|
className: "search-input",
|
|
minLength: 3,
|
|
target: this.target,
|
|
sort: (f1, f2) => {
|
|
const s1 = this.searchControl.getSearchString(f1);
|
|
const s2 = this.searchControl.getSearchString(f2);
|
|
return s1.localeCompare(s2);
|
|
},
|
|
getTitle: (feature) => {
|
|
const iconUrl = feature.get('icon') || this.getDefaultIcon(feature);
|
|
let title = feature.get('name') || 'Без имени';
|
|
if (title.length > 20) {
|
|
title = title.substring(0, 20) + "...";
|
|
}
|
|
|
|
const nowDate = Date.now();
|
|
const featureDate = Date.parse(feature.get('iso_time'));
|
|
const deltaInHours = ((nowDate - featureDate) / 3600000).toFixed(0);
|
|
|
|
return `
|
|
<div class="search-item">
|
|
<div>
|
|
<img src="${iconUrl}" width="20" height="20" class="search-icon"/>
|
|
<span>${title}</span>
|
|
</div>
|
|
<div>
|
|
<span>${deltaInHours}ч.</span>
|
|
</div>
|
|
</div>
|
|
`;
|
|
}
|
|
});
|
|
|
|
this.map.addControl(this.searchControl);
|
|
|
|
this.searchControl.on('select', (e) => {
|
|
this.onFeatureSelect(e.search);
|
|
});
|
|
}
|
|
|
|
onFeatureSelect(feature) {
|
|
this.searchControl.clearHistory();
|
|
|
|
const coord = feature.getGeometry().getFirstCoordinate();
|
|
this.map.getView().animate({ center: coord, duration: 1000, zoom: 16 });
|
|
|
|
const flashFeature = new ol.Feature(new ol.geom.Point(coord));
|
|
const source = this.highlightLayer.getSource();
|
|
source.clear();
|
|
source.addFeature(flashFeature);
|
|
|
|
let count = 0;
|
|
const interval = setInterval(() => {
|
|
if (source.hasFeature(flashFeature)) {
|
|
source.removeFeature(flashFeature);
|
|
} else {
|
|
source.addFeature(flashFeature);
|
|
}
|
|
count++;
|
|
if (count > 5) {
|
|
clearInterval(interval);
|
|
source.removeFeature(flashFeature);
|
|
}
|
|
}, 400);
|
|
}
|
|
}
|