mirror of
https://github.com/kiwix/kiwix-js.git
synced 2025-09-24 04:54:51 -04:00
Improvement in nearby article search, to limit the number of results, and display this number to the user
This commit is contained in:
parent
ee101bced8
commit
d7fe45cf0b
@ -74,8 +74,8 @@
|
|||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
or <a href="#" id="btnArticlesNearby">Find articles around me (work in progress)</a><br/>
|
or <a href="#" id="btnArticlesNearby">Find articles around me (work in progress)</a><br/>
|
||||||
Longitude/Latitude/MaxDistance:
|
Latitude/Longitude/MaxDistance:
|
||||||
<input type="text" id="longitude" size="8" /> <input type="text" id="latitude" size="8" /> <input type="text" id="maxDistance" size="8" /><br/>
|
<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/>
|
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;">
|
<div id="searchingForTitles" style="position: relative; z-index: 10; top: 20px; height: 0px; display: none;">
|
||||||
<img src="img/spinner.gif" alt="Please wait..." />
|
<img src="img/spinner.gif" alt="Please wait..." />
|
||||||
|
@ -43,7 +43,7 @@ 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.1;
|
var MAX_DISTANCE_ARTICLES_NEARBY = 0.05;
|
||||||
|
|
||||||
var localArchive = null;
|
var localArchive = null;
|
||||||
|
|
||||||
@ -323,10 +323,21 @@ 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
|
||||||
*/
|
*/
|
||||||
function populateListOfTitles(titleArray) {
|
function populateListOfTitles(titleArray, maxTitles) {
|
||||||
var titleListDiv = $('#titleList');
|
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++) {
|
for (var i = 0; i < titleArray.length; i++) {
|
||||||
var title = titleArray[i];
|
var title = titleArray[i];
|
||||||
titleListDivHtml += "<a href='#' titleid='" + title.toStringId()
|
titleListDivHtml += "<a href='#' titleid='" + title.toStringId()
|
||||||
@ -570,6 +581,7 @@ define(function(require) {
|
|||||||
|
|
||||||
function geo_error(err) {
|
function geo_error(err) {
|
||||||
alert("Unable to geolocate your device : " + err.code + " : " + err.message);
|
alert("Unable to geolocate your device : " + err.code + " : " + err.message);
|
||||||
|
$('#searchingForTitles').hide();
|
||||||
};
|
};
|
||||||
|
|
||||||
navigator.geolocation.getCurrentPosition(geo_success, geo_error, geo_options);
|
navigator.geolocation.getCurrentPosition(geo_success, geo_error, geo_options);
|
||||||
@ -581,8 +593,8 @@ define(function(require) {
|
|||||||
else {
|
else {
|
||||||
// The user gave a latitude/longitude : let's use it to find articles around that location
|
// The user gave a latitude/longitude : let's use it to find articles around that location
|
||||||
var rectangle = new geometry.rect(
|
var rectangle = new geometry.rect(
|
||||||
longitude - maxDistance,
|
|
||||||
latitude - maxDistance,
|
latitude - maxDistance,
|
||||||
|
longitude - maxDistance,
|
||||||
maxDistance * 2,
|
maxDistance * 2,
|
||||||
maxDistance * 2);
|
maxDistance * 2);
|
||||||
|
|
||||||
|
@ -344,7 +344,7 @@ define(function(require) {
|
|||||||
titleIterators.findPrefixOffset(this.titleFile, titleName, normalize).then(function(offset) {
|
titleIterators.findPrefixOffset(this.titleFile, titleName, normalize).then(function(offset) {
|
||||||
var iterator = new titleIterators.SequentialTitleIterator(that, offset);
|
var iterator = new titleIterators.SequentialTitleIterator(that, offset);
|
||||||
function check(title) {
|
function check(title) {
|
||||||
if (title == null || normalize(title.name) !== normalizedTitleName) {
|
if (title === null || normalize(title.name) !== normalizedTitleName) {
|
||||||
return null;
|
return null;
|
||||||
} else if (title.name === titleName) {
|
} else if (title.name === titleName) {
|
||||||
return title;
|
return title;
|
||||||
@ -393,15 +393,15 @@ define(function(require) {
|
|||||||
var iterator = new titleIterators.SequentialTitleIterator(that, offset);
|
var iterator = new titleIterators.SequentialTitleIterator(that, offset);
|
||||||
function addNext() {
|
function addNext() {
|
||||||
if (titles.length >= maxSize) {
|
if (titles.length >= maxSize) {
|
||||||
return titles;
|
return jQuery.Deferred().resolve(titles, maxSize);
|
||||||
}
|
}
|
||||||
return iterator.advance().then(function(title) {
|
return iterator.advance().then(function(title) {
|
||||||
if (title == null)
|
if (title === null)
|
||||||
return titles;
|
return jQuery.Deferred().resolve(titles, maxSize);
|
||||||
// check whether this title really starts with the prefix
|
// check whether this title really starts with the prefix
|
||||||
var name = normalize(title.name);
|
var name = normalize(title.name);
|
||||||
if (name.length < prefix.length || name.substring(0, prefix.length) != prefix)
|
if (name.length < prefix.length || name.substring(0, prefix.length) !== prefix)
|
||||||
return titles;
|
return jQuery.Deferred().resolve(titles, maxSize);
|
||||||
titles.push(title);
|
titles.push(title);
|
||||||
return addNext();
|
return addNext();
|
||||||
});
|
});
|
||||||
@ -672,18 +672,16 @@ define(function(require) {
|
|||||||
*
|
*
|
||||||
* @param {type} localArchive
|
* @param {type} localArchive
|
||||||
* @param {type} titlePositionsFound
|
* @param {type} titlePositionsFound
|
||||||
* @param {type} i : index of the coordinate file
|
|
||||||
* @param {type} maxTitles
|
* @param {type} maxTitles
|
||||||
* @param {type} normalizedRectangle
|
|
||||||
* @param {type} callbackFunction
|
* @param {type} callbackFunction
|
||||||
*/
|
*/
|
||||||
LocalArchive.callbackGetTitlesInCoordsInt = function(localArchive, titlePositionsFound, maxTitles, callbackFunction) {
|
LocalArchive.callbackGetTitlesInCoordsInt = function(localArchive, titlePositionsFound, maxTitles, callbackFunction) {
|
||||||
// Search is over : now let's convert the title positions into Title instances
|
// Search is over : now let's convert the title positions into Title instances
|
||||||
if (titlePositionsFound && titlePositionsFound.length > 0) {
|
if (titlePositionsFound && titlePositionsFound.length > 0) {
|
||||||
LocalArchive.readTitlesFromTitleCoordsInTitleFile(localArchive, titlePositionsFound, 0, new Array(), callbackFunction);
|
LocalArchive.readTitlesFromTitleCoordsInTitleFile(localArchive, titlePositionsFound, 0, new Array(), maxTitles, callbackFunction);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
callbackFunction(titlePositionsFound);
|
callbackFunction(titlePositionsFound, maxTitles);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
@ -697,19 +695,20 @@ define(function(require) {
|
|||||||
* @param {type} titlePositionsFound
|
* @param {type} titlePositionsFound
|
||||||
* @param {type} i
|
* @param {type} i
|
||||||
* @param {type} titlesFound
|
* @param {type} titlesFound
|
||||||
|
* @param maxTitles
|
||||||
* @param {type} callbackFunction
|
* @param {type} callbackFunction
|
||||||
*/
|
*/
|
||||||
LocalArchive.readTitlesFromTitleCoordsInTitleFile = function (localArchive, titlePositionsFound, i, titlesFound, callbackFunction) {
|
LocalArchive.readTitlesFromTitleCoordsInTitleFile = function (localArchive, titlePositionsFound, i, titlesFound, maxTitles, callbackFunction) {
|
||||||
var titleOffset = titlePositionsFound[i];
|
var titleOffset = titlePositionsFound[i];
|
||||||
localArchive.getTitlesStartingAtOffset(titleOffset, 1, function(titleList) {
|
localArchive.getTitlesStartingAtOffset(titleOffset, 1, function(titleList) {
|
||||||
if (titleList && titleList.length === 1) {
|
if (titleList && titleList.length === 1) {
|
||||||
titlesFound.push(titleList[0]);
|
titlesFound.push(titleList[0]);
|
||||||
i++;
|
i++;
|
||||||
if (i<titlePositionsFound.length) {
|
if (i<titlePositionsFound.length) {
|
||||||
LocalArchive.readTitlesFromTitleCoordsInTitleFile(localArchive, titlePositionsFound, i, titlesFound, callbackFunction);
|
LocalArchive.readTitlesFromTitleCoordsInTitleFile(localArchive, titlePositionsFound, i, titlesFound, maxTitles, callbackFunction);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
callbackFunction(titlesFound);
|
callbackFunction(titlesFound, maxTitles);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -755,6 +754,14 @@ define(function(require) {
|
|||||||
|
|
||||||
reader.onload = function(e) {
|
reader.onload = function(e) {
|
||||||
callbackCounterForTitlesInCoordsSearch--;
|
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 binaryTitleFile = e.target.result;
|
||||||
var byteArray = new Uint8Array(binaryTitleFile);
|
var byteArray = new Uint8Array(binaryTitleFile);
|
||||||
// Compute selector
|
// Compute selector
|
||||||
@ -813,10 +820,6 @@ define(function(require) {
|
|||||||
LocalArchive.getTitlesInCoordsInt(localArchive, coordinateFileIndex, pos3, targetRect, rectNE, maxTitles, titlePositionsFound, callbackFunction, callbackGetTitlesInCoordsInt);
|
LocalArchive.getTitlesInCoordsInt(localArchive, coordinateFileIndex, pos3, targetRect, rectNE, maxTitles, titlePositionsFound, callbackFunction, callbackGetTitlesInCoordsInt);
|
||||||
}
|
}
|
||||||
console.log("end");
|
console.log("end");
|
||||||
if (callbackCounterForTitlesInCoordsSearch === 0) {
|
|
||||||
console.log("callbackCounterForTitlesInCoordsSearch reached 0 : return the titles found")
|
|
||||||
callbackGetTitlesInCoordsInt(localArchive, titlePositionsFound, maxTitles, callbackFunction);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// This is a leaf node : let's see if its articles are in the
|
// 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");
|
console.log("target rectangle contains this point : adding to the list");
|
||||||
titlePositionsFound.push(title_pos);
|
titlePositionsFound.push(title_pos);
|
||||||
console.log("maxTitles="+maxTitles+" titlePositionsFound.length="+titlePositionsFound.length);
|
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")
|
|
||||||
callbackGetTitlesInCoordsInt(localArchive, titlePositionsFound, maxTitles, callbackFunction);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (callbackCounterForTitlesInCoordsSearch === 0) {
|
||||||
|
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
|
// Read 22 bytes in the coordinate files, at coordFilePos index, in order to read the selector and the coordinates
|
||||||
|
Loading…
x
Reference in New Issue
Block a user