Improvement in nearby article search, to limit the number of results, and display this number to the user

This commit is contained in:
mossroy 2014-03-18 14:37:02 +01:00
parent ee101bced8
commit d7fe45cf0b
3 changed files with 1818 additions and 1807 deletions

View File

@ -74,8 +74,8 @@
</span>
</div>
or <a href="#" id="btnArticlesNearby">Find articles around me (work in progress)</a><br/>
Longitude/Latitude/MaxDistance:
<input type="text" id="longitude" size="8" /> <input type="text" id="latitude" size="8" /> <input type="text" id="maxDistance" size="8" /><br/>
Latitude/Longitude/MaxDistance:
<input type="text" id="latitude" size="8" /> <input type="text" id="longitude" size="8" /> <input type="text" id="maxDistance" size="8" /><br/>
or <a href="#" id="btnRandomArticle">Go to a random article</a><br/>
<div id="searchingForTitles" style="position: relative; z-index: 10; top: 20px; height: 0px; display: none;">
<img src="img/spinner.gif" alt="Please wait..." />

View File

@ -43,7 +43,7 @@ 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.1;
var MAX_DISTANCE_ARTICLES_NEARBY = 0.05;
var localArchive = null;
@ -323,10 +323,21 @@ define(function(require) {
/**
* Display the list of titles with the given array of titles
* @param {type} titleArray
* @param maxTitles
*/
function populateListOfTitles(titleArray) {
function populateListOfTitles(titleArray, maxTitles) {
var titleListDiv = $('#titleList');
var titleListDivHtml = "";
var nbTitles = 0;
if (titleArray) {
nbTitles = titleArray.length;
}
var titleListDivHtml;
if (nbTitles >= maxTitles) {
titleListDivHtml = "More than " + maxTitles + " titles found (only " + maxTitles + " displayed) :<br/>";
}
else {
titleListDivHtml = nbTitles + " titles found :<br/>";
}
for (var i = 0; i < titleArray.length; i++) {
var title = titleArray[i];
titleListDivHtml += "<a href='#' titleid='" + title.toStringId()
@ -570,6 +581,7 @@ define(function(require) {
function geo_error(err) {
alert("Unable to geolocate your device : " + err.code + " : " + err.message);
$('#searchingForTitles').hide();
};
navigator.geolocation.getCurrentPosition(geo_success, geo_error, geo_options);
@ -581,8 +593,8 @@ define(function(require) {
else {
// The user gave a latitude/longitude : let's use it to find articles around that location
var rectangle = new geometry.rect(
longitude - maxDistance,
latitude - maxDistance,
longitude - maxDistance,
maxDistance * 2,
maxDistance * 2);

View File

@ -344,7 +344,7 @@ define(function(require) {
titleIterators.findPrefixOffset(this.titleFile, titleName, normalize).then(function(offset) {
var iterator = new titleIterators.SequentialTitleIterator(that, offset);
function check(title) {
if (title == null || normalize(title.name) !== normalizedTitleName) {
if (title === null || normalize(title.name) !== normalizedTitleName) {
return null;
} else if (title.name === titleName) {
return title;
@ -393,15 +393,15 @@ define(function(require) {
var iterator = new titleIterators.SequentialTitleIterator(that, offset);
function addNext() {
if (titles.length >= maxSize) {
return titles;
return jQuery.Deferred().resolve(titles, maxSize);
}
return iterator.advance().then(function(title) {
if (title == null)
return titles;
if (title === null)
return jQuery.Deferred().resolve(titles, maxSize);
// check whether this title really starts with the prefix
var name = normalize(title.name);
if (name.length < prefix.length || name.substring(0, prefix.length) != prefix)
return titles;
if (name.length < prefix.length || name.substring(0, prefix.length) !== prefix)
return jQuery.Deferred().resolve(titles, maxSize);
titles.push(title);
return addNext();
});
@ -672,18 +672,16 @@ define(function(require) {
*
* @param {type} localArchive
* @param {type} titlePositionsFound
* @param {type} i : index of the coordinate file
* @param {type} maxTitles
* @param {type} normalizedRectangle
* @param {type} callbackFunction
*/
LocalArchive.callbackGetTitlesInCoordsInt = function(localArchive, titlePositionsFound, maxTitles, callbackFunction) {
// Search is over : now let's convert the title positions into Title instances
if (titlePositionsFound && titlePositionsFound.length > 0) {
LocalArchive.readTitlesFromTitleCoordsInTitleFile(localArchive, titlePositionsFound, 0, new Array(), callbackFunction);
LocalArchive.readTitlesFromTitleCoordsInTitleFile(localArchive, titlePositionsFound, 0, new Array(), maxTitles, callbackFunction);
}
else {
callbackFunction(titlePositionsFound);
callbackFunction(titlePositionsFound, maxTitles);
}
};
@ -697,19 +695,20 @@ define(function(require) {
* @param {type} titlePositionsFound
* @param {type} i
* @param {type} titlesFound
* @param maxTitles
* @param {type} callbackFunction
*/
LocalArchive.readTitlesFromTitleCoordsInTitleFile = function (localArchive, titlePositionsFound, i, titlesFound, callbackFunction) {
LocalArchive.readTitlesFromTitleCoordsInTitleFile = function (localArchive, titlePositionsFound, i, titlesFound, maxTitles, callbackFunction) {
var titleOffset = titlePositionsFound[i];
localArchive.getTitlesStartingAtOffset(titleOffset, 1, function(titleList) {
if (titleList && titleList.length === 1) {
titlesFound.push(titleList[0]);
i++;
if (i<titlePositionsFound.length) {
LocalArchive.readTitlesFromTitleCoordsInTitleFile(localArchive, titlePositionsFound, i, titlesFound, callbackFunction);
LocalArchive.readTitlesFromTitleCoordsInTitleFile(localArchive, titlePositionsFound, i, titlesFound, maxTitles, callbackFunction);
}
else {
callbackFunction(titlesFound);
callbackFunction(titlesFound, maxTitles);
}
}
else {
@ -755,6 +754,14 @@ define(function(require) {
reader.onload = function(e) {
callbackCounterForTitlesInCoordsSearch--;
if (maxTitles >= 0 && titlePositionsFound.length >= maxTitles) {
console.log("Maximum titles already found : we do not look further");
if (callbackCounterForTitlesInCoordsSearch === 0) {
console.log("callbackCounterForTitlesInCoordsSearch reached 0 : return the titles found");
callbackGetTitlesInCoordsInt(localArchive, titlePositionsFound, maxTitles, callbackFunction);
}
return;
}
var binaryTitleFile = e.target.result;
var byteArray = new Uint8Array(binaryTitleFile);
// Compute selector
@ -813,10 +820,6 @@ define(function(require) {
LocalArchive.getTitlesInCoordsInt(localArchive, coordinateFileIndex, pos3, targetRect, rectNE, maxTitles, titlePositionsFound, callbackFunction, callbackGetTitlesInCoordsInt);
}
console.log("end");
if (callbackCounterForTitlesInCoordsSearch === 0) {
console.log("callbackCounterForTitlesInCoordsSearch reached 0 : return the titles found")
callbackGetTitlesInCoordsInt(localArchive, titlePositionsFound, maxTitles, callbackFunction);
}
}
else {
// This is a leaf node : let's see if its articles are in the
@ -838,16 +841,12 @@ define(function(require) {
console.log("target rectangle contains this point : adding to the list");
titlePositionsFound.push(title_pos);
console.log("maxTitles="+maxTitles+" titlePositionsFound.length="+titlePositionsFound.length);
// TODO : reactivate to enforce the maximum titles to be searched
// if (maxTitles >= 0 && titlePositionsFound.length >= maxTitles) {
// return;
// }
}
}
if (callbackCounterForTitlesInCoordsSearch === 0) {
console.log("callbackCounter reached 0 : return the titles found")
console.log("callbackCounterForTitlesInCoordsSearch reached 0 : return the titles found");
callbackGetTitlesInCoordsInt(localArchive, titlePositionsFound, maxTitles, callbackFunction);
}
}
};
// Read 22 bytes in the coordinate files, at coordFilePos index, in order to read the selector and the coordinates