3
0
Fork 0
mirror of https://github.com/farmOS/farmOS.git synced 2024-02-23 11:37:38 +01:00

Add a setting to configure if the map popup behavior is enabled.

This commit is contained in:
paul121 2021-04-02 11:49:26 -07:00 committed by Michael Stenta
parent 848f271882
commit 19f8851543
4 changed files with 47 additions and 26 deletions

View file

@ -10,6 +10,9 @@ description: 'Renders a geofield widget using farmOS-map.'
behaviors:
- asset_type_layers
options:
behaviors:
popup:
enabled: false
asset_type_layers:
all_locations:
label: All locations

View file

@ -40,4 +40,5 @@ behavior_asset_type_layers:
js/farmOS.map.behaviors.asset_type_layers.js: { }
dependencies:
- farm_map/farm_map
# NOTICE: This included the popup behavior anytime the asset_type_layers behavior is loaded.
- farm_map/behavior_popup

View file

@ -2,37 +2,44 @@
farmOS.map.behaviors.popup = {
attach: function (instance) {
// Create a popup and add it to the instance for future reference.
instance.popup = instance.addPopup(function (event) {
var content = '';
var feature = instance.map.forEachFeatureAtPixel(event.pixel, function(feature, layer) { return feature; });
if (feature) {
// Add a popup if enabled on the instance. Popups are enabled by default.
const enabled = drupalSettings.farm_map[instance.target]?.behaviors?.popup?.enabled ?? true;
if (enabled) {
// If the feature is a cluster, then create a list of names and add it
// to the overall feature's description.
var features = feature.get('features');
if (features !== undefined) {
var names = [];
features.forEach(function (item) {
if (item.get('name') !== undefined) {
names.push(item.get('name'));
// Create a popup and add it to the instance for future reference.
instance.popup = instance.addPopup(function (event) {
var content = '';
var feature = instance.map.forEachFeatureAtPixel(event.pixel, function (feature, layer) {
return feature;
});
if (feature) {
// If the feature is a cluster, then create a list of names and add it
// to the overall feature's description.
var features = feature.get('features');
if (features !== undefined) {
var names = [];
features.forEach(function (item) {
if (item.get('name') !== undefined) {
names.push(item.get('name'));
}
});
if (names.length !== 0) {
feature.set('description', '<ul><li>' + names.join('</li><li>') + '</li></ul>');
}
});
if (names.length !== 0) {
feature.set('description', '<ul><li>' + names.join('</li><li>') + '</li></ul>');
feature.set('name', names.length + ' item(s):');
}
feature.set('name', names.length + ' item(s):');
}
var name = feature.get('name') || '';
var description = feature.get('description') || '';
var measurement = instance.measureGeometry(feature.getGeometry(), instance.units);
if (name !== '' || measurement !== '' || description !== '') {
content = '<h4 class="ol-popup-name">' + name + '</h4><div class="ol-popup-measurement"><small>' + measurement + '</small></div><div class="ol-popup-description">' + description + '</div>';
var name = feature.get('name') || '';
var description = feature.get('description') || '';
var measurement = instance.measureGeometry(feature.getGeometry(), instance.units);
if (name !== '' || measurement !== '' || description !== '') {
content = '<h4 class="ol-popup-name">' + name + '</h4><div class="ol-popup-measurement"><small>' + measurement + '</small></div><div class="ol-popup-description">' + description + '</div>';
}
}
}
return content;
});
return content;
});
}
}
};
}());

View file

@ -72,6 +72,16 @@ class MapRenderEventSubscriber implements EventSubscriberInterface {
if (in_array($event->getMapType()->id(), ['geofield_widget'])) {
$event->addBehavior('wkt');
$event->addBehavior('geofield');
// Disable popups for the geofield_widget map.
// @todo Allow this to be set on the map type config entity.
// This should be a proper instance specific setting.
// In the map type config, it should be possible to add behavior specific
// settings in the top-level "behaviors" list (right now it just accepts
// behavior IDs). Each behavior could provide schema for its valid
// setting options.
$settings[$event->getMapTargetId()]['behaviors']['popup']['enabled'] = FALSE;
$event->addSettings($settings);
}
// Add asset layers to dashbaord map.