From 4cb3486cd78725a4fdc579a8dfd73254def0d07d Mon Sep 17 00:00:00 2001 From: mossroy Date: Fri, 28 Mar 2014 14:02:13 +0100 Subject: [PATCH] Nearby article search : allow the user to enlarge or reduce the maximum distance, depending on the number of results Closes #9 --- www/index.html | 8 +++++- www/js/app.js | 64 ++++++++++++++++++++++++++++++++++++------- www/js/lib/archive.js | 4 +-- 3 files changed, 63 insertions(+), 13 deletions(-) diff --git a/www/index.html b/www/index.html index fed96ee0..11f0c214 100644 --- a/www/index.html +++ b/www/index.html @@ -218,7 +218,13 @@
-
+ + +
diff --git a/www/js/app.js b/www/js/app.js index 1e544cc2..de555262 100644 --- a/www/js/app.js +++ b/www/js/app.js @@ -43,10 +43,13 @@ define(function(require) { // In fact, we use a square around the user, not a circle // This square has a length of twice the value of this constant // 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; + // 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 $('#searchTitles').on('click', function(e) { searchTitlesFromPrefix($('#prefix').val()); @@ -75,11 +78,31 @@ define(function(require) { $('#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) { goToRandomArticle(); $("#welcomeText").hide(); $('#titleList').hide(); $('#titleListHeaderMessage').hide(); + $('#suggestEnlargeMaxDistance').hide(); + $('#suggestReduceMaxDistance').hide(); $("#readingArticle").hide(); $('#geolocationProgress').hide(); $('#searchingForTitles').hide(); @@ -122,6 +145,8 @@ define(function(require) { $('#prefix').focus(); $("#titleList").empty(); $('#titleListHeaderMessage').empty(); + $('#suggestEnlargeMaxDistance').hide(); + $('#suggestReduceMaxDistance').hide(); $("#readingArticle").hide(); $('#geolocationProgress').hide(); $("#articleContent").empty(); @@ -143,6 +168,8 @@ define(function(require) { $("#welcomeText").hide(); $('#titleList').hide(); $('#titleListHeaderMessage').hide(); + $('#suggestEnlargeMaxDistance').hide(); + $('#suggestReduceMaxDistance').hide(); $("#readingArticle").hide(); $('#geolocationProgress').hide(); $('#articleContent').hide(); @@ -164,6 +191,8 @@ define(function(require) { $("#welcomeText").hide(); $('#titleList').hide(); $('#titleListHeaderMessage').hide(); + $('#suggestEnlargeMaxDistance').hide(); + $('#suggestReduceMaxDistance').hide(); $("#readingArticle").hide(); $('#geolocationProgress').hide(); $('#articleContent').hide(); @@ -339,8 +368,10 @@ define(function(require) { * Display the list of titles with the given array of titles * @param {type} titleArray * @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 nbTitles = 0; @@ -350,15 +381,25 @@ define(function(require) { var message; if (maxTitles >= 0 && nbTitles >= maxTitles) { - message = maxTitles + " first titles below (refine your search) :"; + message = maxTitles + " first titles below (refine your search)."; } else { - message = nbTitles + " titles found :"; + message = nbTitles + " titles found."; } if (nbTitles === 0) { - message = "No titles found"; + message = "No titles found."; } + 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 titleListDivHtml = ""; @@ -409,6 +450,8 @@ define(function(require) { var titleId = event.target.getAttribute("titleId"); $("#titleList").empty(); $('#titleListHeaderMessage').empty(); + $('#suggestEnlargeMaxDistance').hide(); + $('#suggestReduceMaxDistance').hide(); findTitleFromTitleIdAndLaunchArticleRead(titleId); var title = evopediaTitle.Title.parseTitleId(localArchive, titleId); pushBrowserHistoryState(title._name); @@ -578,9 +621,10 @@ define(function(require) { $('#configuration').hide(); $('#titleList').hide(); $('#titleListHeaderMessage').hide(); + $('#suggestEnlargeMaxDistance').hide(); + $('#suggestReduceMaxDistance').hide(); $('#articleContent').empty(); if (localArchive !== null && localArchive._titleFile !== null) { - var maxDistance = MAX_DISTANCE_ARTICLES_NEARBY; if (navigator.geolocation) { var geo_options = { enableHighAccuracy: false, @@ -596,10 +640,10 @@ define(function(require) { + "
Now looking for articles around this location..."); var rectangle = new geometry.rect( - crd.latitude - maxDistance, - crd.longitude - maxDistance, - maxDistance * 2, - maxDistance * 2); + crd.latitude - maxDistanceArticlesNearbySearch, + crd.longitude - maxDistanceArticlesNearbySearch, + maxDistanceArticlesNearbySearch * 2, + maxDistanceArticlesNearbySearch * 2); localArchive.getTitlesInCoords(rectangle, MAX_SEARCH_RESULT_SIZE, populateListOfTitles); } diff --git a/www/js/lib/archive.js b/www/js/lib/archive.js index 7ecd0fcf..3fb8573a 100644 --- a/www/js/lib/archive.js +++ b/www/js/lib/archive.js @@ -681,7 +681,7 @@ define(function(require) { LocalArchive.readTitlesFromTitleCoordsInTitleFile(localArchive, titlePositionsFound, 0, new Array(), maxTitles, callbackFunction); } else { - callbackFunction(titlePositionsFound, maxTitles); + callbackFunction(titlePositionsFound, maxTitles, true); } }; @@ -708,7 +708,7 @@ define(function(require) { LocalArchive.readTitlesFromTitleCoordsInTitleFile(localArchive, titlePositionsFound, i, titlesFound, maxTitles, callbackFunction); } else { - callbackFunction(titlesFound, maxTitles); + callbackFunction(titlesFound, maxTitles, true); } } else {