diff --git a/www/index.html b/www/index.html index dd69b5eb..f5282755 100644 --- a/www/index.html +++ b/www/index.html @@ -76,6 +76,7 @@ or Find articles around me (work in progress)
Longitude/Latitude:
+ or Go to a random article
diff --git a/www/js/app.js b/www/js/app.js index 4d5e99c9..8f9af7db 100644 --- a/www/js/app.js +++ b/www/js/app.js @@ -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); + } + }); + } + }); diff --git a/www/js/lib/archive.js b/www/js/lib/archive.js index 9cbe6795..5be8ecb5 100644 --- a/www/js/lib/archive.js +++ b/www/js/lib/archive.js @@ -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); }; /**