Skip to content

Commit

Permalink
Merge pull request #481 from bmlt-enabled/pj/store-coords
Browse files Browse the repository at this point in the history
store/retireve coords from global var
  • Loading branch information
otrok7 authored Jun 20, 2024
2 parents 6b9466d + a5fa898 commit 23d3ff7
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 46 deletions.
2 changes: 1 addition & 1 deletion crouton.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Description: A tabbed based display for showing meeting information.
Author: bmlt-enabled
Author URI: https://bmlt.app
Version: 3.19.1
Version: 3.19.2
*/
/* Disallow direct access to the plugin file */
if (basename($_SERVER['PHP_SELF']) == basename(__FILE__)) {
Expand Down
13 changes: 13 additions & 0 deletions croutonjs/meetingMap/css/meeting_map.css
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ div.bmlt_map_container_div button {
float: right;
font-size: 28px;
font-weight: bold;
position: absolute;
top: 10px;
right: 5px;
line-height: 0;
}
.table-close {
Expand All @@ -285,6 +288,16 @@ div.bmlt_map_container_div button {
font-weight:bold;
font-size: 20px;
}
.modal-search {
padding-top: 10px;
padding-bottom: 10px;
}
.modal-search .filter-button {
width: 100%
}
#goto-button {
margin-top: 10px;
}
.modal-close:hover,
.modal-close:focus {
color: black;
Expand Down
54 changes: 37 additions & 17 deletions croutonjs/meetingMap/js/meeting_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ function MeetingMap(inConfig) {
function apiLoadedCallback() {
loadedCallbackFunction(...loadedCallbackArgs);
}

function retrieveGeolocation() {
return new Promise((resolve, reject) => {
if (window.storedGeolocation) {
resolve(window.storedGeolocation);
} else if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((position) => {
window.storedGeolocation = {
latitude: position.coords.latitude,
longitude: position.coords.longitude
};
resolve(window.storedGeolocation);
}, (error) => {
reject(new Error('Error getting geolocation: ' + error.message));
});
} else {
reject(new Error('Geolocation is not supported by this browser.'));
}
});
}

/************************************************************************************//**
* \brief Load the map and set it up. *
****************************************************************************************/
Expand Down Expand Up @@ -126,14 +147,15 @@ function MeetingMap(inConfig) {
var controlDiv = document.createElement('div');
controlDiv.innerHTML = template(menuContext);
controlDiv.querySelector("#nearbyMeetings").addEventListener('click', function (e) {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
coords = position.coords;
gDelegate.setViewToPosition(coords, filterMeetingsAndBounds);
});
}
retrieveGeolocation().then(position => {
gDelegate.setViewToPosition(position, filterMeetingsAndBounds);
}).catch(error => {
console.error(error.message);
$('.geo').removeClass("hide").addClass("show").html(`<p>${error.message}</p>`);
});
dropdownContent = document.getElementById("map-menu-dropdown").style.display = "none";
});

controlDiv.querySelector("#lookupLocation").addEventListener('click', showGeocodingDialog);
controlDiv.querySelector("#filterMeetings").addEventListener('click', showFilterDialog);
controlDiv.querySelector("#showAsTable").addEventListener('click', showListView);
Expand Down Expand Up @@ -193,17 +215,15 @@ function MeetingMap(inConfig) {
searchResponseCallback(fitDuringFilter);
};
function nearMeSearch() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
function (position) {
showThrobber();
crouton.searchByCoordinates(position.coords.latitude, position.coords.longitude, config.map_search.width);
},
showBmltSearchDialog
)
}
retrieveGeolocation().then(position => {
showThrobber();
crouton.searchByCoordinates(position.latitude, position.longitude, config.map_search.width);
}).catch(error => {
console.error(error.message);
});
showBmltSearchDialog();
closeModalWindow(gSearchModal);
}
};
function clickSearch(e) {
gDelegate.clickSearch(e, function(lat,lng) {
showThrobber();
Expand Down Expand Up @@ -650,4 +670,4 @@ MeetingMap.prototype.refreshMeetings = null;

MeetingMap.prototype.openModalWindow = null;
MeetingMap.prototype.closeModalWindow = null;
MeetingMap.prototype.loadPopupMap = null;
MeetingMap.prototype.loadPopupMap = null;
55 changes: 36 additions & 19 deletions croutonjs/src/js/crouton-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,27 @@ crouton_Handlebars.registerHelper("startup", () => '');
crouton_Handlebars.registerHelper("enrich", () => '');
crouton_Handlebars.registerHelper('selectFormatPopup', () => "formatPopup");
crouton_Handlebars.registerHelper('selectObserver', () => "observerTemplate");

const retrieveGeolocation = () => {
return new Promise((resolve, reject) => {
if (window.storedGeolocation) {
resolve(window.storedGeolocation);
} else if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((position) => {
window.storedGeolocation = {
latitude: position.coords.latitude,
longitude: position.coords.longitude
};
resolve(window.storedGeolocation);
}, (error) => {
reject(new Error('Error getting geolocation: ' + error.message));
});
} else {
reject(new Error('Geolocation is not supported by this browser.'));
}
});
};

function Crouton(config) {
var self = this;
self.mutex = false;
Expand Down Expand Up @@ -250,18 +271,14 @@ function Crouton(config) {
}

if (self.config['distance_search'] !== 0) {
if (navigator.geolocation) {
return new Promise(function (resolve, reject) {
navigator.geolocation.getCurrentPosition(resolve, reject);
}).then(function(position) {
url += '&lat_val=' + position.coords.latitude
+ '&long_val=' + position.coords.longitude
+ '&sort_results_by_distance=1';

url += (self.config['distance_units'] === "km" ? '&geo_width_km=' : '&geo_width=') + self.config['distance_search'];
return self.getMeetings(url);
});
}
return retrieveGeolocation().then((position) => {
url += '&lat_val=' + position.latitude + '&long_val=' + position.longitude + '&sort_results_by_distance=1';
url += (self.config['distance_units'] === "km" ? '&geo_width_km=' : '&geo_width=') + self.config['distance_search'];
return self.getMeetings(url);
}).catch((error) => {
console.error(error.message);
return self.getMeetings(url); // Proceed without geolocation if error occurs
});
} else if (self.config['custom_query'] != null) {
url += self.config['custom_query'] + '&sort_keys=' + self.config['sort_keys'];
return self.getMeetings(url);
Expand Down Expand Up @@ -479,11 +496,11 @@ function Crouton(config) {
croutonMap.initialize(self.createBmltMapElement(),self.meetingData,context,null,fitBounds,callback);
}
self.getCurrentLocation = function(callback) {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(callback, self.errorHandler);
} else {
$('.geo').removeClass("hide").addClass("show").html('<p>Geolocation is not supported by your browser</p>');
}
retrieveGeolocation().then(position => {
callback(position);
}).catch(error => {
jQuery('.geo').removeClass("hide").addClass("show").html(`<p>${error.message}</p>`);
});
};
self.renderView = function (selector, context, callback, fitBounds) {
hbs_Crouton['localization'] = self.localization;
Expand Down Expand Up @@ -561,8 +578,8 @@ function Crouton(config) {
}

self.showLocation = function(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var latitude = position.latitude;
var longitude = position.longitude;
var distanceUnit;
var distanceCalculation;

Expand Down
4 changes: 1 addition & 3 deletions croutonjs/src/templates/mapMenu.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@
<div class="modal-search">
{{getWord 'Enter a city or zip code'}}
<input id="goto-text" type="text">
<p>
<button id="goto-button" class="filter-button">Los</button>
</p>
<button id="goto-button" class="filter-button">Los</button>
</div>
</div>
</div>
Expand Down
7 changes: 2 additions & 5 deletions croutonjs/src/templates/mapSearch.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,18 @@
</button>
<div id="bmltsearch_modal" class="modal" style="display: none;">
<div id="bmltsearch_content" class="modal-content">
<span class="modal-title">{{getWord 'Search for meetings'}}</span><span id="close_search" class="modal-close">x</span>
<span id="close_search" class="modal-close">x</span>
<span class="modal-title">{{getWord 'Search for meetings'}}</span>
<table>
<tr><td><div class="modal-search">
<input id="bmltsearch-goto-text" type="text" placeholder="{{getWord 'Enter a city or zip code'}}" style="margin-bottom:5px;">
<button id="bmltsearch-text-button" class="filter-button">{{getWord 'text_search'}}</button>
</div></td></tr>
<tr><td><div class="modal-search">
<p>
<button id="bmltsearch-nearbyMeetings" class="filter-button">{{getWord 'near_me'}}</button>
</p>
</div></td></tr>
<tr><td><div class="modal-search">
<p>
<button id="bmltsearch-clicksearch" class="filter-button">{{getWord 'click_search'}}</button>
</p>
</div></td></tr></table>
</div>
</div>
5 changes: 4 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Tags: na, meeting list, meeting finder, maps, recovery, addiction, webservant, b
Requires at least: 4.0
Required PHP: 8.0
Tested up to: 6.4.2
Stable tag: 3.19.1
Stable tag: 3.19.2
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
crouton implements a Tabbed UI for BMLT.
Expand Down Expand Up @@ -36,6 +36,9 @@ https://demo.bmlt.app/crouton

== Changelog ==

= 3.19.2 =
* Only ask for user's location one time

= 3.19.1 =
* Fix compatibility issue with Elementor.

Expand Down

0 comments on commit 23d3ff7

Please sign in to comment.