Skip to content

Commit

Permalink
Merge pull request #490 from bmlt-enabled/insert-js-into-footer
Browse files Browse the repository at this point in the history
Insert js into footer
  • Loading branch information
otrok7 authored Sep 11, 2024
2 parents 113a23c + 5ad1bff commit 6309986
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 57 deletions.
53 changes: 34 additions & 19 deletions crouton.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class Crouton
"root_server" => '',
"service_body" => '',
"service_body_parent" => '',
"jsInFooter" => true,
"venue_types" => '',
"formats" => '',
"has_tabs" => '1',
Expand Down Expand Up @@ -304,14 +305,25 @@ private function sharedRender()

return $output;
}

private function inlineScript($s)
{
wp_add_inline_script('croutonjs', $s);
}
private function outputScript($s)
{
if ($this->options['jsInFooter']) {
wp_add_inline_script('croutonjs', $s);
$s = "";
}
return $this->waitMsg.sprintf('%s<div id="bmlt-tabs" class="bmlt-tabs hide">%s</div>', $this->sharedRender(), $s);
}
public function tabbedUi($atts, $content = null)
{
$this->hasMap = true;
if (isset($_GET['meeting-id'])) {
return do_shortcode($this->getDefaultMeetingDetailsPageContents());
}
return $this->waitMsg.sprintf('%s<div id="bmlt-tabs" class="bmlt-tabs hide">%s</div>', $this->sharedRender(), $this->renderTable($atts));
return $this->outputScript($this->renderTable($atts));
}
public function bmltHandlebar($atts, $template = null)
{
Expand All @@ -327,7 +339,7 @@ public function bmltHandlebar($atts, $template = null)
}
}
if (!$this->has_handlebars) {
add_action("wp_footer", [$this,'handlebarFooter']);
$this->handlebarFooter();
}
$this->has_handlebars = true;
return sprintf('<bmlt-handlebar style="display:none;"><span style="display:none;">%s</span>Fetching...</bmlt-handlebar>', htmlspecialchars($template));
Expand All @@ -338,7 +350,7 @@ public function croutonMap($atts, $content = null)
if (isset($_GET['meeting-id'])) {
return do_shortcode($this->getDefaultMeetingDetailsPageContents());
}
return sprintf('%s<div id="bmlt-tabs" class="bmlt-tabs hide">%s</div>', $this->sharedRender(), $this->renderMap($atts));
return $this->outputScript($this->renderMap($atts));
}
public function meetingMap($atts, $content = null)
{
Expand All @@ -351,7 +363,7 @@ public function meetingMap($atts, $content = null)
} else {
$atts = ["has_venues" => "0"];
}
return sprintf('%s<div id="bmlt-tabs" class="bmlt-tabs hide">%s</div>', $this->sharedRender(), $this->renderMap($atts, false));
return $this->outputScript($this->renderMap($atts, false));
}
private function getMapInitialization($mapConfig)
{
Expand Down Expand Up @@ -437,16 +449,9 @@ public function handlebarFooter()
'strict_datafields' => false];
[$config, $mapConfig] = $this->getCroutonJsConfig($attr);
$croutonMap = $this->getMapInitialization($mapConfig);
?>
<script type='text/javascript'>
var crouton;

jQuery(document).ready(function() {
<?php echo $croutonMap ?> crouton = new Crouton(<?php echo $config; ?>);
crouton.doHandlebars();
});
</script>
<?php
$ret = "var crouton;"
."jQuery(document).ready(function() { $croutonMap crouton = new Crouton($config); crouton.doHandlebars();})";
$this->inlineScript($ret);
}
/**
* @desc Adds the options sub-panel
Expand Down Expand Up @@ -511,6 +516,7 @@ public function adminOptionsPage()
}
$this->options['root_server'] = $_POST['root_server'];
$this->options['service_body_1'] = $_POST['service_body_1'];
$this->options['jsInFooter'] = isset($_POST['jsInFooter']);
$this->options['time_format'] = $_POST['time_format'];
$this->options['language'] = $_POST['language'];
$this->options['strict_datafields'] = isset($_POST['strict_datafields']);
Expand Down Expand Up @@ -569,6 +575,9 @@ public function adminOptionsPage()
} else {
$this->options['extra_meetings_enabled'] = 1;
}
if (!isset($this->options['jsInFooter'])) {
$this->options['jsInFooter'] = true;
}
?>
<div class="wrap">
<div id="tallyBannerContainer">
Expand Down Expand Up @@ -699,6 +708,13 @@ public function adminOptionsPage()
</li>
</ul>
</div>
<div style="padding: 0 15px;" class="postbox">
<h3><a id="config-advanced" class="anchor"></a>Advanced Options</h3>
<p>Should the generated Javascript be placed in the footer or in the body.</p>
<div>
<input type="checkbox" name="jsInFooter" value="1" <?php echo ($this->options['jsInFooter'] == 1 ? 'checked' : '') ?> />Place Javascript in Footer
</div>
</div>
</div>
<div id="crouton-ui" class=tab-content>
<div style="padding: 0 15px;" class="postbox">
Expand Down Expand Up @@ -1040,13 +1056,12 @@ private function getAllFields($root_server)
private function templateToParameter($atts, $name)
{
if (isset($atts[$name]) && $atts[$name] !== null && $atts[$name] !== "") {
$template = $atts[$name];
return $atts[$name];
} elseif (isset($this->options[$name])) {
$template = $this->options[$name];
return $this->options[$name];
} else {
$template = "";
return "";
}
return html_entity_decode($template);
}
private function getAllMeetings($root_server)
{
Expand Down
3 changes: 2 additions & 1 deletion croutonjs/meetingMap/js/meeting_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ function MeetingMap(inConfig) {
showGeocodingDialog();
}
} else if (config.goto) {
gDelegate.callGeocoder(goto, filterMeetingsAndBounds);
gDelegate.callGeocoder(config.goto, filterMeetingsAndBounds);
}
}
function createCityHash(allMeetings) {
Expand Down Expand Up @@ -363,6 +363,7 @@ function MeetingMap(inConfig) {
const newTop = -window.scrollY+'px';
body.style.overflowX = 'hidden';
body.style.position = 'fixed';
body.style.width="100%";
body.style.setProperty('top', newTop, 'important');
}
}
Expand Down
64 changes: 29 additions & 35 deletions croutonjs/meetingMap/meeting_map.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,48 +188,42 @@ private function hsc($field)
}
private function createJavascriptConfig($options)
{
$ret = '{';
$ret = [];
//$ret .= 'BMLTPlugin_files_uri:\''.$this->hsc($this->getPluginPath()).'?\',' . (defined('_DEBUG_MODE_') ? "\n" : '');
$ret .= "BMLTPlugin_images:'".$this->hsc(plugin_dir_url(__FILE__)."map_images")."'," . (defined('_DEBUG_MODE_') ? "\n" : '');
$ret .= "BMLTPlugin_lang_dir:'".$this->hsc(plugin_dir_url(__FILE__)."lang")."'," . (defined('_DEBUG_MODE_') ? "\n" : '');
$ret .= "BMLTPlugin_throbber_img_src:'".$this->hsc(plugin_dir_url(__FILE__)."map_images/Throbber.gif")."'," . (defined('_DEBUG_MODE_') ? "\n" : '');
$ret .= 'region:"'.$options['region_bias'].'",';
$ret .= 'bounds:{';
$ret .= ' "north": "'.$options['bounds_north'].'",';
$ret .= ' "east": "'.$options['bounds_east'].'",';
$ret .= ' "south": "'.$options['bounds_south'].'",';
$ret .= ' "west": "'.$options['bounds_west'].'"';
$ret .= '},';
$ret .= 'tileUrl:"'.$options['tile_url'].'",';
$ret .= 'clustering:"'.$options['clustering'].'",';
$ret .= 'nominatimUrl:"'.$options['nominatim_url'].'",';
$ret .= 'tileOptions:{';
foreach ($options['tile_params'] as $key => $value) {
$ret .= " '".$key."': '".$value."',";
}
$ret .= '},';
$ret .= 'api_key:"'.$options['api_key'].'",';
$ret .= 'lat:"'.$options['lat'].'",';
$ret .= 'lng:"'.$options['lng'].'",';
$ret .= 'zoom:"'.$options['zoom'].'",';
$ret .= 'minZoom:"'.$options['min_zoom'].'",';
$ret .= 'maxZoom:"'.$options['max_zoom'].'",';
$ret["BMLTPlugin_images"] = $this->hsc(plugin_dir_url(__FILE__)."map_images");
$ret["BMLTPlugin_lang_dir"] = $this->hsc(plugin_dir_url(__FILE__)."lang");
$ret["BMLTPlugin_throbber_img_src"] = $this->hsc(plugin_dir_url(__FILE__)."map_images/Throbber.gif");
$ret['region'] = $options['region_bias'];
$ret['bounds'] = [
"north" => $options['bounds_north'],
"east" => $options['bounds_east'],
"south" => $options['bounds_south'],
"west" => $options['bounds_west']
];
$ret['tileUrl'] = $options['tile_url'];
$ret['clustering'] = $options['clustering'];
$ret['nominatimUrl'] = $options['nominatim_url'];
$ret['tileOptions'] = $options['tile_params'];
$ret['api_key'] = $options['api_key'];
$ret['lat'] = $options['lat'];
$ret['lng'] = $options['lng'];
$ret['zoom'] = $options['zoom'];
$ret['minZoom'] = $options['min_zoom'];
$ret['maxZoom'] = $options['max_zoom'];
if (!empty($options['center_me'])) {
$ret .= 'centerMe:"'.$options['center_me'].'",';
$ret['centerMe'] = $options['center_me'];
}
if (!empty($options['goto'])) {
$ret .= 'goto:"'.$options['goto'].'",';
$ret['goto'] = $options['goto'];
if (isset($_GET['goto'])) {
$ret['centerMe'] = '';
}
}
if (isset($options['map_search'])) {
$ret .= 'map_search: {';
foreach ($options['map_search'] as $key => $value) {
$ret .= $key.':"'.$value.'",';
}
$ret .= '},';
$ret['map_search'] = $options['map_search'];
}
$ret .= '"marker_contents_template":'.json_encode($options['marker_contents_template']);
$ret .= '},';
return $ret;
$ret["marker_contents_template"] = $options['marker_contents_template'];
return json_encode($ret);
}
public function adminSection()
{
Expand Down
1 change: 0 additions & 1 deletion js/bmlt_tabs_admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ function numbersonly(myfield, e, dec)
return false;
};
function resetCodemirrorToDefault(textAreaId) {
console.log("in Function");
let cm = jQuery('#'+textAreaId).next()[0].CodeMirror;
cm.setValue(croutonDefaultTemplates[textAreaId]);
}
Expand Down
3 changes: 2 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ https://demo.bmlt.app/crouton
== Changelog ==

= 3.19.5 =
* Fix InfoWindow close button on GoogleMaps
* Fix InfoWindow close button on GoogleMaps
* Add option to place generated JavaScript in footer

= 3.19.4 =
* Bug fix
Expand Down

0 comments on commit 6309986

Please sign in to comment.