Display spinner icon when asynchronous tasks are done

This commit is contained in:
mossroy 2013-06-06 22:05:58 +02:00
parent ff6072bb26
commit ec5e4cd2f0
3 changed files with 339 additions and 329 deletions

BIN
img/spinner.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 583 B

View File

@ -1,93 +1,97 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Evopedia</title>
<meta name="description" content="Offline wikipedia reader">
<meta name="viewport" content="width=device-width">
<!--
Port of Evopedia (offline wikipedia reader) in HTML5/Javascript, with Firefox OS as the primary target
The original application is at http://www.evopedia.info/
It uses wikipedia dumps located at http://dumpathome.evopedia.info/dumps/finished
Author : Mossroy - mossroy@free.fr
License:
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public
License along with this program; if not, see
<http://www.gnu.org/licenses/>.
-->
<!-- TODO : implement CSS <link rel="stylesheet" href="css/app.css"> -->
</head>
<body>
<h1>Evopedia</h1>
<div id="content">
<input type="button" id="showHideAbout" value="About" />
<div id="about" style="display: none;">
This is a preliminary work on the port of Evopedia (offline wikipedia reader) in HTML5/Javascript, with Firefox OS as the primary target
<br />
The original application is at <a href="http://www.evopedia.info/">http://www.evopedia.info/</a>
<br />
<br />
To use it, you have to first download locally a dump from <a href="http://dumpathome.evopedia.info/dumps/finished">http://dumpathome.evopedia.info/dumps/finished</a> (with a Bittorrent client).
<br />
<ul>
<li>On desktops, it works on recent Firefox and Chrome, and maybe on other browsers. In this case, you have to select manually some files from your dump (see below)</li>
<li>On the Firefos OS simulator, you have to put the archive files in a "fake-sdcard" folder of your firefox profile (ex : ~/.mozilla/firefox/xxxx.default/extensions/r2d2b2g@mozilla.org/profile/fake-sdcard). It looks for file titles.idx in it. You need to install the application from the dashboard of the simulator (due to security restrictions in Firefox OS : only certified webapps can access the sdcard)</li>
<li>On a real Firefox OS device, you simply have to put the archive files anywhere in your sdcard, so that it finds titles.idx on it. For now, the application has to be installed manually (use the push feature of the Firefos OS Simulator)</li>
</ul>
<br />
It's still a beta version : there are many many ways this could be enhanced (suggestions and patches are welcome : the source code is on <a href="https://github.com/mossroy/evopedia-html5">github</a>). In particular :
<ul>
<li>The performance has to be optimized when searching titles and reading an article</li>
<li>Some searches (for example with prefix "a" on the French dump) do not give any result even if they should</li>
<li>In some cases, the links inside an article do not work, or do not lead to the right article</li>
<li>It is not easy to use on the Peak device from Geeksphone because the buttons and inputs are too small</li>
<li>Some features of the original application still have to be implemented : in particular geolocation of articles and math formulas</li>
</ul>
</div>
<div id="openLocalFiles" style="display: none;">
<br /> Please select the file titles.idx :<br /> <input type="file"
id="titleFile" /><br /> Please select the files wikipedia_*.dat
from the same dump :<br /> <input type="file" id="dataFiles" multiple />
</div>
<div id="scanningForArchives" style="display: none;">
<br /> Scanning your sdcard for archives... Please wait
</div>
<div id="chooseArchiveFromLocalStorage" style="display: none;">
<br /> Please select the archive you want to use : <select id="archiveList"></select>
</div>
<br /> Find titles starting with :
<input type="text" id="prefix" value="" />&nbsp;
<input type="button" id="searchTitles" value="Search titles" />
<br /> Choose a title from the filtered list :
<select id="titleList"></select>
<br />
<input type="button" id="readData" value="Read article from dump" />
<div id="articleContent">&nbsp;</div>
<hr />
</div>
<!-- Using require.js, a module system for javascript, include the
js files. This loads "main.js", which in turn can load other
files, all handled by require.js:
http://requirejs.org/docs/api.html#jsfiles -->
<script type="text/javascript"
data-main="js/init.js"
src="js/lib/require.js"></script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Evopedia</title>
<meta name="description" content="Offline wikipedia reader">
<meta name="viewport" content="width=device-width">
<!--
Port of Evopedia (offline wikipedia reader) in HTML5/Javascript, with Firefox OS as the primary target
The original application is at http://www.evopedia.info/
It uses wikipedia dumps located at http://dumpathome.evopedia.info/dumps/finished
Author : Mossroy - mossroy@free.fr
License:
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public
License along with this program; if not, see
<http://www.gnu.org/licenses/>.
-->
<!-- TODO : implement CSS <link rel="stylesheet" href="css/app.css"> -->
</head>
<body>
<h1>Evopedia</h1>
<div id="content">
<input type="button" id="showHideAbout" value="About" />
<div id="about" style="display: none;">
This is a preliminary work on the port of Evopedia (offline wikipedia reader) in HTML5/Javascript, with Firefox OS as the primary target
<br />
The original application is at <a href="http://www.evopedia.info/">http://www.evopedia.info/</a>
<br />
<br />
To use it, you have to first download locally a dump from <a href="http://dumpathome.evopedia.info/dumps/finished">http://dumpathome.evopedia.info/dumps/finished</a> (with a Bittorrent client).
<br />
<ul>
<li>On desktops, it works on recent Firefox and Chrome, and maybe on other browsers. In this case, you have to select manually some files from your dump (see below)</li>
<li>On the Firefos OS simulator, you have to put the archive files in a "fake-sdcard" folder of your firefox profile (ex : ~/.mozilla/firefox/xxxx.default/extensions/r2d2b2g@mozilla.org/profile/fake-sdcard). It looks for file titles.idx in it. You need to install the application from the dashboard of the simulator (due to security restrictions in Firefox OS : only certified webapps can access the sdcard)</li>
<li>On a real Firefox OS device, you simply have to put the archive files anywhere in your sdcard, so that it finds titles.idx on it. For now, the application has to be installed manually (use the push feature of the Firefos OS Simulator)</li>
</ul>
<br />
It's still a beta version : there are many many ways this could be enhanced (suggestions and patches are welcome : the source code is on <a href="https://github.com/mossroy/evopedia-html5">github</a>). In particular :
<ul>
<li>The performance has to be optimized when searching titles and reading an article</li>
<li>Some searches (for example with prefix "a" on the French dump) do not give any result even if they should</li>
<li>In some cases, the links inside an article do not work, or do not lead to the right article</li>
<li>It is not easy to use on the Peak device from Geeksphone because the buttons and inputs are too small</li>
<li>Some features of the original application still have to be implemented : in particular geolocation of articles and math formulas</li>
</ul>
</div>
<div id="openLocalFiles" style="display: none;">
<br /> Please select the file titles.idx :<br /> <input type="file"
id="titleFile" /><br /> Please select the files wikipedia_*.dat
from the same dump :<br /> <input type="file" id="dataFiles" multiple />
</div>
<div id="scanningForArchives" style="display: none;">
<br /> Scanning your sdcard for archives... Please wait <img src="img/spinner.gif" />
</div>
<div id="chooseArchiveFromLocalStorage" style="display: none;">
<br /> Please select the archive you want to use : <select id="archiveList"></select>
</div>
<br /> Find titles starting with :
<input type="text" id="prefix" value="" />&nbsp;
<input type="button" id="searchTitles" value="Search titles" />
<img id="searchingForTitles" src="img/spinner.gif" style="display: none;"/>
<br /> Choose a title from the filtered list :
<select id="titleList"></select>
<br />
<input type="button" id="readData" value="Read article from dump" />
<div id="readingArticle" style="display: none;">
Reading article from dump... Please wait <img src="img/spinner.gif" />
</div>
<div id="articleContent">&nbsp;</div>
<hr />
</div>
<!-- Using require.js, a module system for javascript, include the
js files. This loads "main.js", which in turn can load other
files, all handled by require.js:
http://requirejs.org/docs/api.html#jsfiles -->
<script type="text/javascript"
data-main="js/init.js"
src="js/lib/require.js"></script>
</body>
</html>

478
js/app.js
View File

@ -1,236 +1,242 @@
// This uses require.js to structure javascript:
// http://requirejs.org/docs/api.html#define
define(function(require) {
// Zepto provides nice js and DOM methods (very similar to jQuery,
// and a lot smaller):
// http://zeptojs.com/
var $ = require('zepto');
// Evopedia javascript dependencies
var evopedia = require('evopedia');
var localArchive = null;
// Define behavior of HTML elements
$('#about').hide();
$('#showHideAbout').on('click', function(e) {
$('#about').toggle();
});
$('#searchTitles').on('click', function(e) {
searchTitlesFromPrefix($('#prefix').val());
});
$('#readData').on('click', function(e) {
var titleId = $('#titleList').val();
findTitleFromTitleIdAndLaunchArticleRead(titleId);
var title = evopedia.Title.parseTitleId(localArchive,titleId);
pushBrowserHistoryState(title.name);
});
$('#prefix').on('keyup', function(e) {
onKeyUpPrefix(e);
});
// Detect if DeviceStorage is available
var storage = null;
if ($.isFunction(navigator.getDeviceStorage)) {
storage = navigator.getDeviceStorage('sdcard');
}
if (storage != null) {
// If DeviceStorage is available, we look for archives in it
$('#scanningForArchives').show();
evopedia.LocalArchive.scanForArchives(storage,populateDropDownListOfArchives);
}
else {
// If DeviceStorage is not available, we display the file select components
displayFileSelect();
setLocalArchiveFromFileSelect();
}
// Display the article when the user goes back in the browser history
window.onpopstate = function(event) {
var titleName = event.state.titleName;
goToArticle(titleName);
};
/**
* Populate the drop-down list of titles with the given list
*/
function populateDropDownListOfArchives(archiveDirectories) {
$('#scanningForArchives').hide();
$('#chooseArchiveFromLocalStorage').show();
var comboArchiveList = document.getElementById('archiveList');
comboArchiveList.options.length = 0;
for (var i=0; i<archiveDirectories.length; i++) {
var archiveDirectory = archiveDirectories[i];
comboArchiveList.options[i] = new Option (archiveDirectory, archiveDirectory);
}
$('#archiveList').on('change', setLocalArchiveFromArchiveList);
if (archiveDirectories.length>0) {
// Set the localArchive from the first result
setLocalArchiveFromArchiveList();
}
else {
alert("No Evopedia archive found in your sdcard. Please see 'About' for more info");
}
}
/**
* Sets the localArchive from the selected archive in the drop-down list
*/
function setLocalArchiveFromArchiveList() {
var archiveDirectory = $('#archiveList').val();
localArchive = new evopedia.LocalArchive();
localArchive.readTitleFile(storage, archiveDirectory);
localArchive.readDataFiles(storage, archiveDirectory, 0);
}
/**
* Displays the zone to select files from the dump
*/
function displayFileSelect() {
$('#openLocalFiles').show();
$('#dataFiles').on('change', setLocalArchiveFromFileSelect);
$('#titleFile').on('change', setLocalArchiveFromFileSelect);
}
/**
* Sets the localArchive from the File selects populated by user
*/
function setLocalArchiveFromFileSelect() {
dataFiles=document.getElementById('dataFiles').files;
titleFile=document.getElementById('titleFile').files[0];
localArchive = new evopedia.LocalArchive();
localArchive.dataFiles = dataFiles;
localArchive.titleFile = titleFile;
}
/**
* Handle Enter key in the prefix input zone
*/
function onKeyUpPrefix(evt) {
if (evt.keyCode == 13) {
document.getElementById("searchTitles").click();
}
}
/**
* Search the index for titles that start with the given prefix (implemented
* with a binary search inside the index file)
*/
function searchTitlesFromPrefix(prefix) {
if (localArchive.titleFile) {
localArchive.findTitlesWithPrefix(prefix.trim(), populateDropDownListOfTitles);
} else {
alert("Title file not set");
}
}
/**
* Populate the drop-down list of titles with the given list
*/
function populateDropDownListOfTitles(titleList) {
var comboTitleList = document.getElementById('titleList');
// Remove previous results
comboTitleList.options.length = 0;
for (var i=0; i<titleList.length; i++) {
var title = titleList[i];
comboTitleList.options[i] = new Option (title.name, title.toStringId());
}
}
/**
* Creates an instance of title from given titleId (including resolving redirects),
* and call the function to read the corresponding article
*/
function findTitleFromTitleIdAndLaunchArticleRead(titleId) {
if (localArchive.dataFiles && localArchive.dataFiles.length>0) {
var title = evopedia.Title.parseTitleId(localArchive,titleId);
$("#articleContent").html("Loading from dump article " + title.name + " ...");
if (title.fileNr == 255) {
localArchive.resolveRedirect(title, readArticle);
}
else {
readArticle(title);
}
}
else {
alert("Data files not set");
}
}
/**
* Read the article corresponding to the given title
*/
function readArticle(title) {
if ($.isArray(title)) {
title = title[0];
if (title.fileNr == 255) {
localArchive.resolveRedirect(title, readArticle);
return;
}
}
localArchive.readArticle(title, displayArticleInForm);
}
/**
* Display the the given HTML article in the web page,
* and convert links to javascript calls
*/
function displayArticleInForm(title, htmlArticle) {
// Display the article inside the web page.
$('#articleContent').html(htmlArticle);
// Convert links into javascript calls
$('#articleContent').find('a').each(function(){
// Store current link's url
var url = $(this).attr("href");
if(url.slice(0, 1) == "#") {
// It's an anchor link : do nothing
}
else if (url.substring(0,4) === "http") {
// It's an external link : do nothing
}
else if (url.substring(0,2) === ".." || url.substring(0,4) === "./..") {
// It's a link to another language : TODO redirect to the online article?
}
else {
// It's a link to another article : add an onclick event to go to this article
// instead of following the link
$(this).on('click', function(e) {
var titleName = decodeURIComponent($(this).attr("href"));
pushBrowserHistoryState(titleName);
goToArticle(titleName);
return false;
});
}
});
}
/**
* Changes the URL of the browser page
*/
function pushBrowserHistoryState(titleName) {
if (titleName) {
var stateObj = { titleName: titleName};
window.history.pushState(stateObj,"Wikipedia Article : " + titleName,"#" + titleName);
}
}
/**
* Replace article content with the one of the given title
*/
function goToArticle(titleName) {
$("#articleContent").html("Loading from dump article " + titleName + " ...");
localArchive.getTitleByName(titleName, readArticle);
}
});
// This uses require.js to structure javascript:
// http://requirejs.org/docs/api.html#define
define(function(require) {
// Zepto provides nice js and DOM methods (very similar to jQuery,
// and a lot smaller):
// http://zeptojs.com/
var $ = require('zepto');
// Evopedia javascript dependencies
var evopedia = require('evopedia');
var localArchive = null;
// Define behavior of HTML elements
$('#about').hide();
$('#showHideAbout').on('click', function(e) {
$('#about').toggle();
});
$('#searchTitles').on('click', function(e) {
searchTitlesFromPrefix($('#prefix').val());
});
$('#readData').on('click', function(e) {
var titleId = $('#titleList').val();
findTitleFromTitleIdAndLaunchArticleRead(titleId);
var title = evopedia.Title.parseTitleId(localArchive,titleId);
pushBrowserHistoryState(title.name);
});
$('#prefix').on('keyup', function(e) {
onKeyUpPrefix(e);
});
// Detect if DeviceStorage is available
var storage = null;
if ($.isFunction(navigator.getDeviceStorage)) {
storage = navigator.getDeviceStorage('sdcard');
}
if (storage != null) {
// If DeviceStorage is available, we look for archives in it
$('#scanningForArchives').show();
evopedia.LocalArchive.scanForArchives(storage,populateDropDownListOfArchives);
}
else {
// If DeviceStorage is not available, we display the file select components
displayFileSelect();
setLocalArchiveFromFileSelect();
}
// Display the article when the user goes back in the browser history
window.onpopstate = function(event) {
var titleName = event.state.titleName;
goToArticle(titleName);
};
/**
* Populate the drop-down list of titles with the given list
*/
function populateDropDownListOfArchives(archiveDirectories) {
$('#scanningForArchives').hide();
$('#chooseArchiveFromLocalStorage').show();
var comboArchiveList = document.getElementById('archiveList');
comboArchiveList.options.length = 0;
for (var i=0; i<archiveDirectories.length; i++) {
var archiveDirectory = archiveDirectories[i];
comboArchiveList.options[i] = new Option (archiveDirectory, archiveDirectory);
}
$('#archiveList').on('change', setLocalArchiveFromArchiveList);
if (archiveDirectories.length>0) {
// Set the localArchive from the first result
setLocalArchiveFromArchiveList();
}
else {
alert("No Evopedia archive found in your sdcard. Please see 'About' for more info");
}
}
/**
* Sets the localArchive from the selected archive in the drop-down list
*/
function setLocalArchiveFromArchiveList() {
var archiveDirectory = $('#archiveList').val();
localArchive = new evopedia.LocalArchive();
localArchive.readTitleFile(storage, archiveDirectory);
localArchive.readDataFiles(storage, archiveDirectory, 0);
}
/**
* Displays the zone to select files from the dump
*/
function displayFileSelect() {
$('#openLocalFiles').show();
$('#dataFiles').on('change', setLocalArchiveFromFileSelect);
$('#titleFile').on('change', setLocalArchiveFromFileSelect);
}
/**
* Sets the localArchive from the File selects populated by user
*/
function setLocalArchiveFromFileSelect() {
dataFiles=document.getElementById('dataFiles').files;
titleFile=document.getElementById('titleFile').files[0];
localArchive = new evopedia.LocalArchive();
localArchive.dataFiles = dataFiles;
localArchive.titleFile = titleFile;
}
/**
* Handle Enter key in the prefix input zone
*/
function onKeyUpPrefix(evt) {
if (evt.keyCode == 13) {
document.getElementById("searchTitles").click();
}
}
/**
* Search the index for titles that start with the given prefix (implemented
* with a binary search inside the index file)
*/
function searchTitlesFromPrefix(prefix) {
$('#searchingForTitles').show();
if (localArchive.titleFile) {
localArchive.findTitlesWithPrefix(prefix.trim(), populateDropDownListOfTitles);
} else {
alert("Title file not set");
}
}
/**
* Populate the drop-down list of titles with the given list
*/
function populateDropDownListOfTitles(titleList) {
var comboTitleList = document.getElementById('titleList');
// Remove previous results
comboTitleList.options.length = 0;
for (var i=0; i<titleList.length; i++) {
var title = titleList[i];
comboTitleList.options[i] = new Option (title.name, title.toStringId());
}
$('#searchingForTitles').hide();
}
/**
* Creates an instance of title from given titleId (including resolving redirects),
* and call the function to read the corresponding article
*/
function findTitleFromTitleIdAndLaunchArticleRead(titleId) {
if (localArchive.dataFiles && localArchive.dataFiles.length>0) {
var title = evopedia.Title.parseTitleId(localArchive,titleId);
$("#readingArticle").show();
$("#articleContent").html("Loading article " + title.name);
if (title.fileNr == 255) {
localArchive.resolveRedirect(title, readArticle);
}
else {
readArticle(title);
}
}
else {
alert("Data files not set");
}
}
/**
* Read the article corresponding to the given title
*/
function readArticle(title) {
if ($.isArray(title)) {
title = title[0];
if (title.fileNr == 255) {
localArchive.resolveRedirect(title, readArticle);
return;
}
}
localArchive.readArticle(title, displayArticleInForm);
}
/**
* Display the the given HTML article in the web page,
* and convert links to javascript calls
*/
function displayArticleInForm(title, htmlArticle) {
$("#readingArticle").hide();
// Display the article inside the web page.
$('#articleContent').html(htmlArticle);
// Convert links into javascript calls
$('#articleContent').find('a').each(function(){
// Store current link's url
var url = $(this).attr("href");
if(url.slice(0, 1) == "#") {
// It's an anchor link : do nothing
}
else if (url.substring(0,4) === "http") {
// It's an external link : do nothing
}
else if (url.substring(0,2) === ".." || url.substring(0,4) === "./..") {
// It's a link to another language : TODO redirect to the online article?
}
else {
// It's a link to another article : add an onclick event to go to this article
// instead of following the link
$(this).on('click', function(e) {
var titleName = decodeURIComponent($(this).attr("href"));
pushBrowserHistoryState(titleName);
goToArticle(titleName);
return false;
});
}
});
}
/**
* Changes the URL of the browser page
*/
function pushBrowserHistoryState(titleName) {
if (titleName) {
var stateObj = { titleName: titleName};
window.history.pushState(stateObj,"Wikipedia Article : " + titleName,"#" + titleName);
}
}
/**
* Replace article content with the one of the given title
*/
function goToArticle(titleName) {
$("#readingArticle").show();
$("#articleContent").html("Loading article " + titleName);
localArchive.getTitleByName(titleName, readArticle);
}
});