From c821554438a2252d4899848bd314068fc079256e Mon Sep 17 00:00:00 2001 From: David Arnold Date: Sun, 11 Jun 2023 14:14:40 -0500 Subject: [PATCH] feat: add style function to geojson controller This no-op function can be overriden / inherited in custom controllers to craft richer map experiences --- .../js/frappe/form/controls/geolocation.js | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/frappe/public/js/frappe/form/controls/geolocation.js b/frappe/public/js/frappe/form/controls/geolocation.js index ada729fd66f4..84b19894007d 100644 --- a/frappe/public/js/frappe/form/controls/geolocation.js +++ b/frappe/public/js/frappe/form/controls/geolocation.js @@ -60,7 +60,11 @@ frappe.ui.form.ControlGeolocation = class ControlGeolocation extends frappe.ui.f this.clear_editable_layers(); const data_layers = new L.FeatureGroup().addLayer( - L.geoJson(JSON.parse(value), { pointToLayer: this.point_to_layer }) + L.geoJson(JSON.parse(value), { + pointToLayer: this.point_to_layer, + style: this.set_style, + onEachFeature: this.on_each_feature, + }) ); this.add_non_group_layers(data_layers, this.editableLayers); this.editableLayers.addTo(this.map); @@ -70,6 +74,8 @@ frappe.ui.form.ControlGeolocation = class ControlGeolocation extends frappe.ui.f /** * Defines custom rules for how geoJSON data is rendered on the map. * + * Can be inherited in custom map controllers. + * * @param {Object} geoJsonPoint - The geoJSON object to be rendered on the map. * @param {Object} latlng - The latitude and longitude where the geoJSON data should be rendered on the map. * @returns {Object} - Returns the Leaflet layer object to be rendered on the map. @@ -85,6 +91,28 @@ frappe.ui.form.ControlGeolocation = class ControlGeolocation extends frappe.ui.f } } + /** + * Defines custom styles for how geoJSON Line and LineString data is rendered on the map. + * + * Can be inherited in custom map controllers. + * + * @param {Object} geoJsonFeature - The geoJSON object to be rendered on the map. + * @returns {Object} - Returns the style object for the geoJSON object. + */ + set_style(geoJsonFeature) { + return {}; + } + + /** + * Is called after each feature is rendered and styles, can be used to attache popups, tooltips and other events + * + * Can be inherited in custom map controllers. + * + * @param {Object} feature - The leaflet object representing a geojson feature. + * @param {Object} layer - The leaflet layer object. + */ + on_each_feature(feature, layer) {} + bind_leaflet_map() { const circleToGeoJSON = L.Circle.prototype.toGeoJSON; L.Circle.include({