added an exception class, alert when trying to navigate to non-existant pages (will improve the crappy alerts...)

This commit is contained in:
Bala Clark 2011-09-01 12:40:14 +02:00
parent 621cb2bb38
commit 32b4992117

View file

@ -40,6 +40,25 @@ function merge(a, b) {
return b;
}
/**
* Exception class. Always throw an instance of this when throwing exceptions.
*
* @param {String} type
* @param {mixed} object
* @returns {ComicBookException}
*/
function ComicBookException(type, object) {
this.type = type;
this.object = object;
this.INVALID_PAGE = "invalid page";
this.INVALID_PAGE_TYPE = "invalid page type";
this.UNDEFINED_CONTROL = "undefined control";
this.INVALID_ZOOM_MODE = "invalid zoom mode";
this.INVALID_NAVIGATION_EVENT = "invalid navigation event";
};
function ComicBook(id, srcs, opts) {
var canvas_id = id; // canvas element id
@ -251,7 +270,7 @@ function ComicBook(id, srcs, opts) {
ComicBook.prototype.getControl = function(control) {
if (typeof this.control[control] === "undefined") {
throw "invalid control: " + control;
throw new ComicBookException(ComicBookException.UNDEFINED_CONTROL, control);
}
return this.control[control];
@ -283,7 +302,7 @@ function ComicBook(id, srcs, opts) {
if (typeof pages[i] === "object") {
return pages[i];
} else {
throw "Invalid page index: " + i;
throw new ComicBookException(ComicBookException.INVALID_PAGE, i);
}
};
@ -377,10 +396,13 @@ function ComicBook(id, srcs, opts) {
var zoom_scale;
var offsetW = 0, offsetH = 0;
var page = this.getPage(pointer);
var page2 = this.getPage(pointer + 1);
if (typeof page !== "object") { throw "invalid page type '"+ typeof page +"'"; }
if (typeof page !== "object") {
throw new ComicBookException(ComicBookException.INVALID_PAGE_TYPE, typeof page);
}
var width = page.width;
@ -416,7 +438,7 @@ function ComicBook(id, srcs, opts) {
break;
default:
throw "invalid zoomMode";
throw new ComicBookException(ComicBookException.INVALID_ZOOM_MODE, options.zoomMode);
}
var canvas_width = page.width * zoom_scale;
@ -486,10 +508,18 @@ function ComicBook(id, srcs, opts) {
* @see #drawPage
*/
ComicBook.prototype.drawNextPage = function () {
try {
if (pointer + 1 < pages.length) {
pointer += (options.displayMode === "single" || is_double_page_spread) ? 1 : 2;
this.drawPage();
}
} catch (e) {
if (e.type === ComicBookException.INVALID_PAGE) {
alert("you are at the end of the comic");
} else {
throw e;
}
}
};
/**
@ -498,13 +528,20 @@ function ComicBook(id, srcs, opts) {
* @see #drawPage
*/
ComicBook.prototype.drawPrevPage = function () {
try {
is_double_page_spread = (this.getPage(pointer-1).width > this.getPage(pointer-1).height); // need to run double page check again here as we are going backwards
if (pointer > 0) {
pointer -= (options.displayMode === "single" || is_double_page_spread) ? 1 : 2;
this.drawPage();
}
} catch (e) {
if (e.type === ComicBookException.INVALID_PAGE) {
alert("you are at the start of the comic");
} else {
throw e;
}
}
};
/**
@ -629,7 +666,9 @@ function ComicBook(id, srcs, opts) {
}
break;
default:
throw "invalid navigation event: " + e.type;
throw new ComicBookException(
ComicBookException.INVALID_NAVIGATION_EVENT, e.type
);
}
if (side) {