1
0
Fork 0
mirror of https://github.com/futurepress/epub.js.git synced 2025-10-05 15:32:55 +02:00

Added online toggle

This commit is contained in:
Fred Chasen 2013-01-23 11:28:09 -08:00
parent 868b057e02
commit 65fbc6d034
33 changed files with 110 additions and 1804 deletions

View file

@ -2,7 +2,8 @@ FP.app = {};
FP.app.init = (function($){
var Book;
var Book,
offline = false;
function init(bookURL){
var search = window.location.search.match(/book=(.*)/),
@ -19,6 +20,8 @@ FP.app.init = (function($){
Book.listen("book:metadataReady", meta);
Book.listen("book:tocReady", toc);
Book.listen("book:online", goOnline);
Book.listen("book:offline", goOffline);
Book.start(bookURL + "/");
@ -78,7 +81,20 @@ FP.app.init = (function($){
}
function goOnline(){
var $icon = $("#store");
offline = false;
$icon.attr("src", "img/save.png");
}
function goOffline(){
var $icon = $("#store");
offline = true;
$icon.attr("src", "img/saved.png");
}
function controls(){
var $next = $("#next"),
@ -87,6 +103,7 @@ FP.app.init = (function($){
$sidebar = $("#sidebar"),
$open = $("#open"),
$icon = $open.find("img"),
$network = $("#network"),
$window = $(window),
sidebarWidth = 40;
@ -138,6 +155,11 @@ FP.app.init = (function($){
$icon.attr("src", "img/close.png");
}
});
$network.on("click", function(){
offline = !offline;
Book.fromStorage(offline);
});
}

View file

@ -16,6 +16,8 @@ FP.Book = function(elem, bookUrl){
this.createEvent("book:chapterReady");
this.createEvent("book:resized");
this.createEvent("book:stored");
this.createEvent("book:online");
this.createEvent("book:offline");
this.initialize(this.el);
@ -52,10 +54,12 @@ FP.Book.prototype.listeners = function(){
window.addEventListener("offline", function(e) {
that.online = false;
that.tell("book:offline");
}, false);
window.addEventListener("online", function(e) {
that.online = true;
that.tell("book:online");
}, false);
//-- TODO: listener for offline
@ -359,9 +363,9 @@ FP.Book.prototype.startDisplay = function(){
this.tell("book:bookReady");
this.displayChapter(this.spinePos, false, function(){
if(this.online){
this.storeOffline();
}
if(this.online){
this.storeOffline();
}
}.bind(this));
@ -460,6 +464,26 @@ FP.Book.prototype.availableOffline = function() {
return this.stored > 0 ? true : false;
}
FP.Book.prototype.fromStorage = function(stored) {
if(!stored){
this.online = true;
this.tell("book:online");
}else{
if(!this.availableOffline){
this.storeOffline(function(){
this.online = false;
this.tell("book:offline");
}.bind(this));
}else{
this.online = false;
console.log("gone offline")
this.tell("book:offline");
}
}
}
FP.Book.prototype.determineStorageMethod = function(override) {
var method = 'ram';
// options: none | ram | websql | indexedDB | filesystem

View file

@ -200,20 +200,26 @@ FP.Chapter.prototype.replaceLinks = function(callback){
items = Array.prototype.slice.call(links),
count = items.length;
items.forEach(function(link){
var path,
href = link.getAttribute("href"),
src = link.getAttribute("src"),
full = href ? this.book.basePath + href : this.book.basePath + src;
FP.storage.get(full, function(url){
if(href) link.setAttribute("href", url);
if(src) link.setAttribute("src", url);
count--;
if(count <= 0 && callback) callback();
});
}.bind(this));
if(FP.storage.getStorageType() == "filesystem") {
//-- filesystem api links are relative, so no need to fix
if(callback) callback();
return false;
}
items.forEach(function(link){
var path,
href = link.getAttribute("href"),
src = link.getAttribute("src"),
full = href ? this.book.basePath + href : this.book.basePath + src;
FP.storage.get(full, function(url){
if(href) link.setAttribute("href", url);
if(src) link.setAttribute("src", url);
count--;
if(count <= 0 && callback) callback();
});
}.bind(this));
}

View file

@ -13,7 +13,7 @@ self.onmessage = function(event){
self.request(path, function(file){
self.save(path, file, function(){
self.postMessage("succeeded");
self.postMessage(path);
});
});

View file

@ -39,13 +39,18 @@ FP.storage = function(){
function _error(err){
console.log("error", err);
}
function getStorageType(){
return this.storageType;
}
return {
"get" : get,
"preload" : preload,
"batch" : batch,
"storageMethod": storageMethod,
"getURL": getURL
"getURL": getURL,
"getStorageType" : getStorageType
}
}();

View file

@ -49,16 +49,20 @@ FP.storage.filesystem = function() {
var url;
//-- should only be checking urls? but blank on reload?
if(fromCache){
//console.log("c")
url = getURL(path, fromCache);
if(typeof(callback) != "undefined"){
callback(url);
}
}else{
_queue.add(path, function(file){
url = getURL(path, file);
if(typeof(callback) != "undefined"){
callback(url);
}
_queue.add(path, function(url){
console.log("url", url)
check(url, function(file){
url = getURL(path, file);
if(typeof(callback) != "undefined"){
callback(url);
}
});
}, true);
}