Skip to content

Commit 85b7874

Browse files
bozdozbozdoz
andauthored
resolves #248 - adds filter for geojson on each feature (#249)
* resolves #248 * move tableview to js variable --------- Co-authored-by: bozdoz <howaboutben@gmail.com>
1 parent 28ff472 commit 85b7874

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

shortcodes/class.geojson-shortcode.php

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,36 @@ protected function getHTML($atts='', $content=null)
9999

100100
$options = $this->LM->json_sanitize($options, $args);
101101

102-
ob_start();
103-
?>/*<script>*/
102+
// set the default for onEachFeature
103+
ob_start();
104+
?>
105+
function onEachFeature (feature, layer) {
106+
var props = feature.properties || {};
107+
var text;
108+
if (table_view) {
109+
text = window.WPLeafletMapPlugin.propsToTable(props);
110+
} else {
111+
text = popup_property
112+
? props[ popup_property ]
113+
: window.WPLeafletMapPlugin.template(
114+
popup_text,
115+
feature.properties
116+
);
117+
}
118+
if (text) {
119+
layer.bindPopup( text );
120+
}
121+
}
122+
<?php
123+
$onEachFeature = ob_get_clean();
124+
125+
// use with: add_filter('leaflet_map_geojson_onEachFeature', 'example_callback', 10, 3);
126+
// function takes default onEachFeature
127+
$onEachFeature = apply_filters('leaflet_map_geojson_onEachFeature', $onEachFeature);
128+
129+
// create the geojson script
130+
ob_start();
131+
?>/*<script>*/
104132
var src = '<?php echo htmlspecialchars($src, ENT_QUOTES); ?>';
105133
var default_style = <?php echo $style_json; ?>;
106134
var rewrite_keys = {
@@ -124,6 +152,7 @@ protected function getHTML($atts='', $content=null)
124152
$popup_text
125153
); ?>");
126154
var popup_property = "<?php echo esc_js($popup_property); ?>";
155+
var table_view = <?php echo $table_view; ?>;
127156
var group = window.WPLeafletMapPlugin.getCurrentGroup();
128157
var markerOptions = window.WPLeafletMapPlugin.getIconOptions(<?php echo $options; ?>);
129158
layer.addTo( group );
@@ -151,23 +180,7 @@ function camelFun (_, first_letter) {
151180
}
152181
return L.Util.extend(style, default_style);
153182
}
154-
function onEachFeature (feature, layer) {
155-
var props = feature.properties || {};
156-
var text;
157-
if (<?php echo $table_view; ?>) {
158-
text = window.WPLeafletMapPlugin.propsToTable(props);
159-
} else {
160-
text = popup_property
161-
? props[ popup_property ]
162-
: window.WPLeafletMapPlugin.template(
163-
popup_text,
164-
feature.properties
165-
);
166-
}
167-
if (text) {
168-
layer.bindPopup( text );
169-
}
170-
}
183+
<?php echo $onEachFeature; ?>
171184
function pointToLayer (feature, latlng) {
172185
if (circleMarker) {
173186
return L.circleMarker(latlng);

0 commit comments

Comments
 (0)