mirror of
https://github.com/kiwix/kiwix-js.git
synced 2025-09-22 12:01:15 -04:00
Nearby article search : allow the user to enlarge or reduce the maximum distance, depending on the number of results
Closes #9
This commit is contained in:
parent
a47c8fa7f3
commit
4cb3486cd7
@ -218,7 +218,13 @@
|
|||||||
|
|
||||||
<!-- List of articles matching the typed prefix -->
|
<!-- List of articles matching the typed prefix -->
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<div id="titleListHeaderMessage"></div>
|
<span id="titleListHeaderMessage"></span>
|
||||||
|
<span id="suggestEnlargeMaxDistance" style="display: none;">
|
||||||
|
You can <a href="#" id="btnEnlargeMaxDistance">look more far away</a>
|
||||||
|
</span>
|
||||||
|
<span id="suggestReduceMaxDistance" style="display: none;">
|
||||||
|
You can <a href="#" id="btnReduceMaxDistance">look closer</a> to your location
|
||||||
|
</span>
|
||||||
<div id="titleList" class="list-group">
|
<div id="titleList" class="list-group">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -43,10 +43,13 @@ define(function(require) {
|
|||||||
// In fact, we use a square around the user, not a circle
|
// In fact, we use a square around the user, not a circle
|
||||||
// This square has a length of twice the value of this constant
|
// This square has a length of twice the value of this constant
|
||||||
// One degree is ~111 km at the equator
|
// One degree is ~111 km at the equator
|
||||||
var MAX_DISTANCE_ARTICLES_NEARBY = 0.05;
|
var DEFAULT_MAX_DISTANCE_ARTICLES_NEARBY = 0.01;
|
||||||
|
|
||||||
var localArchive = null;
|
var localArchive = null;
|
||||||
|
|
||||||
|
// This max distance has a default value, but the user can make it change
|
||||||
|
var maxDistanceArticlesNearbySearch = DEFAULT_MAX_DISTANCE_ARTICLES_NEARBY;
|
||||||
|
|
||||||
// Define behavior of HTML elements
|
// Define behavior of HTML elements
|
||||||
$('#searchTitles').on('click', function(e) {
|
$('#searchTitles').on('click', function(e) {
|
||||||
searchTitlesFromPrefix($('#prefix').val());
|
searchTitlesFromPrefix($('#prefix').val());
|
||||||
@ -75,11 +78,31 @@ define(function(require) {
|
|||||||
$('#navbarToggle').click();
|
$('#navbarToggle').click();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
$("#btnEnlargeMaxDistance").on("click", function(e) {
|
||||||
|
maxDistanceArticlesNearbySearch = maxDistanceArticlesNearbySearch * 2 ;
|
||||||
|
searchTitlesNearby();
|
||||||
|
$("#welcomeText").hide();
|
||||||
|
$("#readingArticle").hide();
|
||||||
|
if ($('#navbarToggle').is(":visible") && $('#liHomeNav').is(':visible')) {
|
||||||
|
$('#navbarToggle').click();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
$("#btnReduceMaxDistance").on("click", function(e) {
|
||||||
|
maxDistanceArticlesNearbySearch = maxDistanceArticlesNearbySearch / 2 ;
|
||||||
|
searchTitlesNearby();
|
||||||
|
$("#welcomeText").hide();
|
||||||
|
$("#readingArticle").hide();
|
||||||
|
if ($('#navbarToggle').is(":visible") && $('#liHomeNav').is(':visible')) {
|
||||||
|
$('#navbarToggle').click();
|
||||||
|
}
|
||||||
|
});
|
||||||
$("#btnRandomArticle").on("click", function(e) {
|
$("#btnRandomArticle").on("click", function(e) {
|
||||||
goToRandomArticle();
|
goToRandomArticle();
|
||||||
$("#welcomeText").hide();
|
$("#welcomeText").hide();
|
||||||
$('#titleList').hide();
|
$('#titleList').hide();
|
||||||
$('#titleListHeaderMessage').hide();
|
$('#titleListHeaderMessage').hide();
|
||||||
|
$('#suggestEnlargeMaxDistance').hide();
|
||||||
|
$('#suggestReduceMaxDistance').hide();
|
||||||
$("#readingArticle").hide();
|
$("#readingArticle").hide();
|
||||||
$('#geolocationProgress').hide();
|
$('#geolocationProgress').hide();
|
||||||
$('#searchingForTitles').hide();
|
$('#searchingForTitles').hide();
|
||||||
@ -122,6 +145,8 @@ define(function(require) {
|
|||||||
$('#prefix').focus();
|
$('#prefix').focus();
|
||||||
$("#titleList").empty();
|
$("#titleList").empty();
|
||||||
$('#titleListHeaderMessage').empty();
|
$('#titleListHeaderMessage').empty();
|
||||||
|
$('#suggestEnlargeMaxDistance').hide();
|
||||||
|
$('#suggestReduceMaxDistance').hide();
|
||||||
$("#readingArticle").hide();
|
$("#readingArticle").hide();
|
||||||
$('#geolocationProgress').hide();
|
$('#geolocationProgress').hide();
|
||||||
$("#articleContent").empty();
|
$("#articleContent").empty();
|
||||||
@ -143,6 +168,8 @@ define(function(require) {
|
|||||||
$("#welcomeText").hide();
|
$("#welcomeText").hide();
|
||||||
$('#titleList').hide();
|
$('#titleList').hide();
|
||||||
$('#titleListHeaderMessage').hide();
|
$('#titleListHeaderMessage').hide();
|
||||||
|
$('#suggestEnlargeMaxDistance').hide();
|
||||||
|
$('#suggestReduceMaxDistance').hide();
|
||||||
$("#readingArticle").hide();
|
$("#readingArticle").hide();
|
||||||
$('#geolocationProgress').hide();
|
$('#geolocationProgress').hide();
|
||||||
$('#articleContent').hide();
|
$('#articleContent').hide();
|
||||||
@ -164,6 +191,8 @@ define(function(require) {
|
|||||||
$("#welcomeText").hide();
|
$("#welcomeText").hide();
|
||||||
$('#titleList').hide();
|
$('#titleList').hide();
|
||||||
$('#titleListHeaderMessage').hide();
|
$('#titleListHeaderMessage').hide();
|
||||||
|
$('#suggestEnlargeMaxDistance').hide();
|
||||||
|
$('#suggestReduceMaxDistance').hide();
|
||||||
$("#readingArticle").hide();
|
$("#readingArticle").hide();
|
||||||
$('#geolocationProgress').hide();
|
$('#geolocationProgress').hide();
|
||||||
$('#articleContent').hide();
|
$('#articleContent').hide();
|
||||||
@ -339,8 +368,10 @@ define(function(require) {
|
|||||||
* Display the list of titles with the given array of titles
|
* Display the list of titles with the given array of titles
|
||||||
* @param {type} titleArray
|
* @param {type} titleArray
|
||||||
* @param maxTitles
|
* @param maxTitles
|
||||||
|
* @param isNearbySearchSuggestions : it set to true, allow suggestions to
|
||||||
|
* reduce or enlarge the distance where to look for articles nearby
|
||||||
*/
|
*/
|
||||||
function populateListOfTitles(titleArray, maxTitles) {
|
function populateListOfTitles(titleArray, maxTitles, isNearbySearchSuggestions) {
|
||||||
|
|
||||||
var titleListHeaderMessageDiv = $('#titleListHeaderMessage');
|
var titleListHeaderMessageDiv = $('#titleListHeaderMessage');
|
||||||
var nbTitles = 0;
|
var nbTitles = 0;
|
||||||
@ -350,15 +381,25 @@ define(function(require) {
|
|||||||
|
|
||||||
var message;
|
var message;
|
||||||
if (maxTitles >= 0 && nbTitles >= maxTitles) {
|
if (maxTitles >= 0 && nbTitles >= maxTitles) {
|
||||||
message = maxTitles + " first titles below (refine your search) :";
|
message = maxTitles + " first titles below (refine your search).";
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
message = nbTitles + " titles found :";
|
message = nbTitles + " titles found.";
|
||||||
}
|
}
|
||||||
if (nbTitles === 0) {
|
if (nbTitles === 0) {
|
||||||
message = "No titles found";
|
message = "No titles found.";
|
||||||
}
|
}
|
||||||
|
|
||||||
titleListHeaderMessageDiv.html(message);
|
titleListHeaderMessageDiv.html(message);
|
||||||
|
|
||||||
|
// In case of nearby search, and too few or too many results,
|
||||||
|
// suggest the user to change the max Distance
|
||||||
|
if (isNearbySearchSuggestions && nbTitles <= maxTitles * 0.8) {
|
||||||
|
$('#suggestEnlargeMaxDistance').show();
|
||||||
|
}
|
||||||
|
if (isNearbySearchSuggestions && maxTitles >=0 && nbTitles >= maxTitles) {
|
||||||
|
$('#suggestReduceMaxDistance').show();
|
||||||
|
}
|
||||||
|
|
||||||
var titleListDiv = $('#titleList');
|
var titleListDiv = $('#titleList');
|
||||||
var titleListDivHtml = "";
|
var titleListDivHtml = "";
|
||||||
@ -409,6 +450,8 @@ define(function(require) {
|
|||||||
var titleId = event.target.getAttribute("titleId");
|
var titleId = event.target.getAttribute("titleId");
|
||||||
$("#titleList").empty();
|
$("#titleList").empty();
|
||||||
$('#titleListHeaderMessage').empty();
|
$('#titleListHeaderMessage').empty();
|
||||||
|
$('#suggestEnlargeMaxDistance').hide();
|
||||||
|
$('#suggestReduceMaxDistance').hide();
|
||||||
findTitleFromTitleIdAndLaunchArticleRead(titleId);
|
findTitleFromTitleIdAndLaunchArticleRead(titleId);
|
||||||
var title = evopediaTitle.Title.parseTitleId(localArchive, titleId);
|
var title = evopediaTitle.Title.parseTitleId(localArchive, titleId);
|
||||||
pushBrowserHistoryState(title._name);
|
pushBrowserHistoryState(title._name);
|
||||||
@ -578,9 +621,10 @@ define(function(require) {
|
|||||||
$('#configuration').hide();
|
$('#configuration').hide();
|
||||||
$('#titleList').hide();
|
$('#titleList').hide();
|
||||||
$('#titleListHeaderMessage').hide();
|
$('#titleListHeaderMessage').hide();
|
||||||
|
$('#suggestEnlargeMaxDistance').hide();
|
||||||
|
$('#suggestReduceMaxDistance').hide();
|
||||||
$('#articleContent').empty();
|
$('#articleContent').empty();
|
||||||
if (localArchive !== null && localArchive._titleFile !== null) {
|
if (localArchive !== null && localArchive._titleFile !== null) {
|
||||||
var maxDistance = MAX_DISTANCE_ARTICLES_NEARBY;
|
|
||||||
if (navigator.geolocation) {
|
if (navigator.geolocation) {
|
||||||
var geo_options = {
|
var geo_options = {
|
||||||
enableHighAccuracy: false,
|
enableHighAccuracy: false,
|
||||||
@ -596,10 +640,10 @@ define(function(require) {
|
|||||||
+ "<br/>Now looking for articles around this location...");
|
+ "<br/>Now looking for articles around this location...");
|
||||||
|
|
||||||
var rectangle = new geometry.rect(
|
var rectangle = new geometry.rect(
|
||||||
crd.latitude - maxDistance,
|
crd.latitude - maxDistanceArticlesNearbySearch,
|
||||||
crd.longitude - maxDistance,
|
crd.longitude - maxDistanceArticlesNearbySearch,
|
||||||
maxDistance * 2,
|
maxDistanceArticlesNearbySearch * 2,
|
||||||
maxDistance * 2);
|
maxDistanceArticlesNearbySearch * 2);
|
||||||
|
|
||||||
localArchive.getTitlesInCoords(rectangle, MAX_SEARCH_RESULT_SIZE, populateListOfTitles);
|
localArchive.getTitlesInCoords(rectangle, MAX_SEARCH_RESULT_SIZE, populateListOfTitles);
|
||||||
}
|
}
|
||||||
|
@ -681,7 +681,7 @@ define(function(require) {
|
|||||||
LocalArchive.readTitlesFromTitleCoordsInTitleFile(localArchive, titlePositionsFound, 0, new Array(), maxTitles, callbackFunction);
|
LocalArchive.readTitlesFromTitleCoordsInTitleFile(localArchive, titlePositionsFound, 0, new Array(), maxTitles, callbackFunction);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
callbackFunction(titlePositionsFound, maxTitles);
|
callbackFunction(titlePositionsFound, maxTitles, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
@ -708,7 +708,7 @@ define(function(require) {
|
|||||||
LocalArchive.readTitlesFromTitleCoordsInTitleFile(localArchive, titlePositionsFound, i, titlesFound, maxTitles, callbackFunction);
|
LocalArchive.readTitlesFromTitleCoordsInTitleFile(localArchive, titlePositionsFound, i, titlesFound, maxTitles, callbackFunction);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
callbackFunction(titlesFound, maxTitles);
|
callbackFunction(titlesFound, maxTitles, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user