Merge pull request #68 from mossroy/pr/66

Fixes for pull request 66
This commit is contained in:
mossroy 2014-02-28 17:24:46 +01:00
commit 34fe7c6ff0
7 changed files with 54 additions and 40 deletions

View File

@ -2,12 +2,12 @@
"version": "1.1.0-SNAPSHOT",
"name": "Evopedia",
"description": "Offline Wikipedia Viewer",
"launch_path": "/index.html",
"launch_path": "/www/index.html",
"icons": {
"16": "/img/icons/evopedia-16.png",
"48": "/img/icons/evopedia-48.png",
"60": "/img/icons/evopedia-60.png",
"128": "/img/icons/evopedia-128.png"
"16": "/www/img/icons/evopedia-16.png",
"48": "/www/img/icons/evopedia-48.png",
"60": "/www/img/icons/evopedia-60.png",
"128": "/www/img/icons/evopedia-128.png"
},
"developer": {
"name": "Mossroy <mossroy@free.fr>",

View File

@ -33,7 +33,7 @@
http://requirejs.org/docs/api.html#jsfiles -->
<script type="text/javascript"
data-main="tests/init.js"
src="js/lib/require.js"></script>
src="www/js/lib/require.js"></script>
</head>

View File

@ -21,7 +21,7 @@
* along with Evopedia (file LICENSE-GPLv3.txt). If not, see <http://www.gnu.org/licenses/>
*/
require.config({
baseUrl: 'js/lib',
baseUrl: 'www/js/lib',
paths: {
'zepto': 'zepto',
'jquery': 'jquery-2.0.3',
@ -30,4 +30,4 @@ require.config({
}
});
requirejs(['../../tests/tests']);
requirejs(['../../../tests/tests']);

View File

@ -76,6 +76,7 @@ define(function(require) {
$("#btnRandomArticle").on("click", function(e) {
goToRandomArticle();
$("#welcomeText").hide();
$('#titleList').hide();
$("#readingArticle").hide();
if ($('#navbarToggle').is(":visible") && $('#liHomeNav').is(':visible')) {
$('#navbarToggle').click();

View File

@ -24,25 +24,27 @@ define(function(require) {
// Module dependencies
var normalize_string = require('normalize_string');
var utf8 = require('utf8');
var evopediaTitle = require('title');
var util = require('util');
var geometry = require('geometry');
var jQuery = require('jquery');
var titleIterators = require('titleIterators');
// Declare the webworker that can uncompress with bzip2 algorithm
var webworkerBzip2 = new Worker("js/lib/webworker_bzip2.js");
var webworkerBzip2;
try {
// When using the application normally
webworkerBzip2 = new Worker("js/lib/webworker_bzip2.js");
}
catch(e) {
// When using unit tests
webworkerBzip2 = new Worker("www/js/lib/webworker_bzip2.js");
}
// Size of chunks read in the dump files : 128 KB
var CHUNK_SIZE = 131072;
// The maximum number of titles that can have the same name after normalizing
// This is used by the algorithm that searches for a specific article by its name
var MAX_TITLES_WITH_SAME_NORMALIZED_NAME = 30;
// Maximum length of a title
// 300 bytes is arbitrary : we actually do not really know how long the titles will be
// But mediawiki titles seem to be limited to ~200 bytes, so 300 should be more than enough
var MAX_TITLE_LENGTH = 300;
// A rectangle representing all the earth globe
var GLOBE_RECTANGLE = new geometry.rect(-181, -90, 361, 181);
@ -106,11 +108,11 @@ define(function(require) {
currentLocalArchiveInstance.readDataFilesFromStorage(storage, directory,
index + 1);
}, function(error) {
// TODO there must be a better to way to detect a FileNotFound
// if (error != "NotFoundError") {
// alert("Error reading data file " + index + " in directory "
// + directory + " : " + error);
// }
// TODO there must be a better way to detect a FileNotFound
if (error != "NotFoundError") {
alert("Error reading data file " + index + " in directory "
+ directory + " : " + error);
}
});
};
@ -137,11 +139,11 @@ define(function(require) {
currentLocalArchiveInstance.readCoordinateFilesFromStorage(storage, directory,
index + 1);
}, function(error) {
// TODO there must be a better to way to detect a FileNotFound
// if (error != "NotFoundError") {
// alert("Error reading coordinates file " + index + " in directory "
// + directory + " : " + error);
// }
// TODO there must be a better way to detect a FileNotFound
if (error != "NotFoundError") {
alert("Error reading coordinates file " + index + " in directory "
+ directory + " : " + error);
}
});
};
@ -313,14 +315,15 @@ define(function(require) {
*/
LocalArchive.prototype.getTitlesStartingAtOffset = function(titleOffset, titleCount, callbackFunction) {
var titles = [];
var currentLocalArchiveInstance = this;
jQuery.when().then(function() {
var iterator = new titleIterators.SequentialTitleIterator(this, titleOffset);
var iterator = new titleIterators.SequentialTitleIterator(currentLocalArchiveInstance, titleOffset);
function addNext() {
if (titles.length >= titleCount) {
return titles;
}
return iterator.advance().then(function(title) {
if (title == null)
if (title === null)
return titles;
titles.push(title);
return addNext();
@ -341,7 +344,7 @@ define(function(require) {
var normalize = this.getNormalizeFunction();
var normalizedTitleName = normalize(titleName);
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);
function check(title) {
if (title == null || normalize(title.name) !== normalizedTitleName) {
@ -367,6 +370,8 @@ define(function(require) {
var iterator = new titleIterators.SequentialTitleIterator(that, offset);
// call advance twice because we are probably not at the beginning
// of a title
// TODO : we need to find a better way to reach the beginning of the title
// As it is now, the first advance() can fail on utf8 decoding
return iterator.advance().then(function() {
return iterator.advance();
});
@ -385,7 +390,7 @@ define(function(require) {
var normalize = this.getNormalizeFunction();
prefix = normalize(prefix);
titleIterators.FindPrefixOffset(this.titleFile, prefix, normalize).then(function(offset) {
titleIterators.findPrefixOffset(this.titleFile, prefix, normalize).then(function(offset) {
var iterator = new titleIterators.SequentialTitleIterator(that, offset);
function addNext() {
if (titles.length >= maxSize) {
@ -837,7 +842,7 @@ define(function(require) {
return storage.scanForDirectoriesContainingFile('titles.idx')
.then(function(dirs) {
jQuery.merge(directories, dirs);
return true
return true;
});
});
jQuery.when.apply(null, promises).then(function() {
@ -875,7 +880,7 @@ define(function(require) {
if (this.normalizedTitles === true) {
return normalize_string.normalizeString;
} else {
return function(string) { return string; }
return function(string) { return string; };
}
};

View File

@ -30,13 +30,15 @@ define(['utf8', 'title', 'util', 'jquery'], function(utf8, evopediaTitle, util,
* Iterates over all titles starting at the given offset.
* The asynchronous method advance has to be called before this.title is
* valid.
* @param archive
* @param offset
*/
function SequentialTitleIterator(archive, offset) {
this._titleFile = archive.titleFile;
this._archive = archive;
this._offset = offset;
this.title = null;
}
};
/**
* Advances to the next title (or the first), if possible.
* @returns jQuery promise containing the next title or null if there is no
@ -51,7 +53,7 @@ define(['utf8', 'title', 'util', 'jquery'], function(utf8, evopediaTitle, util,
return util.readFileSlice(this._titleFile, this._offset,
this._offset + MAX_TITLE_LENGTH).then(function(byteArray) {
var newLineIndex = 15;
while (newLineIndex < byteArray.length && byteArray[newLineIndex] != 10) {
while (newLineIndex < byteArray.length && byteArray[newLineIndex] !== 10) {
newLineIndex++;
}
var encodedTitle = byteArray.subarray(0, newLineIndex);
@ -59,20 +61,23 @@ define(['utf8', 'title', 'util', 'jquery'], function(utf8, evopediaTitle, util,
that._offset += newLineIndex + 1;
return that._title;
});
}
};
/**
* Searches for the offset into the given title file where the first title
* with the given prefix (or lexicographically larger) is located.
* The given function normalize is applied to every title before comparison.
* @param titleFile
* @param prefix
* @param normalize function to be applied to every title before comparison
* @returns jQuery promise giving the offset
*/
function FindPrefixOffset(titleFile, prefix, normalize) {
function findPrefixOffset(titleFile, prefix, normalize) {
prefix = normalize(prefix);
var lo = 0;
var hi = titleFile.size;
var iterate = function() {
if (lo >= hi) {
if (lo >= hi - 1) {
if (lo > 0)
lo += 2; // Let lo point to the start of an entry
return jQuery.when(lo);
@ -107,7 +112,7 @@ define(['utf8', 'title', 'util', 'jquery'], function(utf8, evopediaTitle, util,
return iterate();
});
}
}
};
return iterate();
}
@ -116,6 +121,6 @@ define(['utf8', 'title', 'util', 'jquery'], function(utf8, evopediaTitle, util,
*/
return {
SequentialTitleIterator : SequentialTitleIterator,
FindPrefixOffset : FindPrefixOffset
findPrefixOffset : findPrefixOffset
};
});

View File

@ -117,6 +117,9 @@ define(function(require) {
/**
* Reads a Uint8Array from the given file starting at byte offset begin and
* not including byte offset end.
* @param file
* @param begin
* @param end
* @returns jQuery promise
*/
function readFileSlice(file, begin, end) {
@ -124,10 +127,10 @@ define(function(require) {
var reader = new FileReader();
reader.onload = function(e) {
deferred.resolve(new Uint8Array(e.target.result));
}
};
reader.onerror = reader.onabort = function(e) {
deferred.reject(e);
}
};
reader.readAsArrayBuffer(file.slice(begin, end));
return deferred.promise();
}