Random articles.

This commit is contained in:
Peter 2014-02-14 02:26:56 +01:00
parent 9430603001
commit 3f7abc7a24
3 changed files with 33 additions and 1 deletions

View File

@ -76,6 +76,7 @@
or <a href="#" id="btnArticlesNearby">Find articles around me (work in progress)</a><br/>
Longitude/Latitude:
<input type="text" id="longitude" size="8" /> <input type="text" id="latitude" 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..." />
</div>

View File

@ -73,6 +73,14 @@ define(function(require) {
$('#navbarToggle').click();
}
});
$("#btnRandomArticle").on("click", function(e) {
goToRandomArticle();
$("#welcomeText").hide();
$("#readingArticle").hide();
if ($('#navbarToggle').is(":visible") && $('#liHomeNav').is(':visible')) {
$('#navbarToggle').click();
}
});
// Bottom bar :
$('#btnBack').on('click', function(e) {
history.back();
@ -580,4 +588,18 @@ define(function(require) {
}
}
function goToRandomArticle() {
localArchive.getRandomTitle(function(title) {
if (title === null || title === undefined) {
alert("Error finding random article.");
}
else {
$("#articleName").html(title.name);
$("#readingArticle").show();
$("#articleContent").html("");
readArticle(title);
}
});
}
});

View File

@ -361,7 +361,16 @@ define(function(require) {
* @param callbackFunction
*/
LocalArchive.prototype.getRandomTitle = function(callbackFunction) {
// TODO to be implemented
var that = this;
var offset = Math.floor(Math.random() * this.titleFile.size);
jQuery.when().then(function() {
var iterator = new titleIterators.SequentialTitleIterator(that, offset);
// call advance twice because we are probably not at the beginning
// of a title
return iterator.advance().then(function() {
return iterator.advance();
});
}).then(callbackFunction, errorHandler);
};
/**