diff --git a/lib/ComicBook.js b/lib/ComicBook.js index adf9312..e339285 100755 --- a/lib/ComicBook.js +++ b/lib/ComicBook.js @@ -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,9 +508,17 @@ function ComicBook(id, srcs, opts) { * @see #drawPage */ ComicBook.prototype.drawNextPage = function () { - if (pointer + 1 < pages.length) { - pointer += (options.displayMode === "single" || is_double_page_spread) ? 1 : 2; - this.drawPage(); + 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,12 +528,19 @@ 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 - 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(); + 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) {