jshint, use strict

This commit is contained in:
Bala Clark 2013-05-06 21:42:15 +02:00
parent c0ddf82df8
commit 9fdcb272d0
4 changed files with 707 additions and 674 deletions

View file

@ -1,5 +1,7 @@
build: build:
@echo "Running jshint..."
@./node_modules/.bin/jshint lib/ComicBook.js --config lib/.jshintrc
@echo "Compiling Handlebars templates..." @echo "Compiling Handlebars templates..."
@./node_modules/.bin/handlebars templates/*.handlebars -f lib/templates.js @./node_modules/.bin/handlebars templates/*.handlebars -f lib/templates.js
@echo "Compiling and minifying javascript..." @echo "Compiling and minifying javascript..."

20
lib/.jshintrc Normal file
View file

@ -0,0 +1,20 @@
{
"predef": [
"jQuery",
"Handlebars",
"Pixastic"
],
"forin": true,
"noarg": true,
"noempty": true,
"eqeqeq": true,
"bitwise": true,
"strict": true,
"undef": true,
"unused": true,
"curly": true,
"browser": true,
"maxerr": 50,
"quotmark": "double",
"trailing": false
}

View file

@ -1,4 +1,4 @@
/*jslint browser: true, on: true, eqeqeq: true, newcap: true, immed: true */ /* exported ComicBook */
/* /*
TODOs: TODOs:
@ -13,7 +13,6 @@
- full browser test - IE9 / FF3.6+ / Chrome / Safari / Opera - full browser test - IE9 / FF3.6+ / Chrome / Safari / Opera
Nice 2 have: Nice 2 have:
- lint
- jump to page? - jump to page?
- make page draggable with the cursor - make page draggable with the cursor
- enable menu items via config, allow for custom items - enable menu items via config, allow for custom items
@ -26,14 +25,18 @@
- unit test / integrate with travis CI - unit test / integrate with travis CI
*/ */
/** var ComicBook = (function ($) {
"use strict";
/**
* Merge two arrays. Any properties in b will replace the same properties in * Merge two arrays. Any properties in b will replace the same properties in
* a. New properties from b will be added to a. * a. New properties from b will be added to a.
* *
* @param a {Object} * @param a {Object}
* @param b {Object} * @param b {Object}
*/ */
function merge(a, b) { function merge(a, b) {
var prop; var prop;
@ -47,25 +50,25 @@ function merge(a, b) {
} }
return b; return b;
} }
/** /**
* Exception class. Always throw an instance of this when throwing exceptions. * Exception class. Always throw an instance of this when throwing exceptions.
* *
* @param {String} type * @param {String} type
* @param {Object} object * @param {Object} object
* @returns {ComicBookException} * @returns {ComicBookException}
*/ */
var ComicBookException = { var ComicBookException = {
INVALID_ACTION: "invalid action", INVALID_ACTION: "invalid action",
INVALID_PAGE: "invalid page", INVALID_PAGE: "invalid page",
INVALID_PAGE_TYPE: "invalid page type", INVALID_PAGE_TYPE: "invalid page type",
UNDEFINED_CONTROL: "undefined control", UNDEFINED_CONTROL: "undefined control",
INVALID_ZOOM_MODE: "invalid zoom mode", INVALID_ZOOM_MODE: "invalid zoom mode",
INVALID_NAVIGATION_EVENT: "invalid navigation event" INVALID_NAVIGATION_EVENT: "invalid navigation event"
}; };
function ComicBook(id, srcs, opts) { function ComicBook(id, srcs, opts) {
var self = this; var self = this;
var canvas_id = id; // canvas element id var canvas_id = id; // canvas element id
@ -192,14 +195,14 @@ function ComicBook(id, srcs, opts) {
controls[name] = $template; controls[name] = $template;
// add event listeners to controls that specify callbacks // add event listeners to controls that specify callbacks
$template.find('*').andSelf().filter("[data-action][data-trigger]").each(function () { $template.find("*").andSelf().filter("[data-action][data-trigger]").each(function () {
var $this = $(this); var $this = $(this);
var trigger = $this.data('trigger'); var trigger = $this.data("trigger");
var action = $this.data('action'); var action = $this.data("action");
// trigger a direct method if exists // trigger a direct method if exists
if (typeof self[$this.data('action')] === "function") { if (typeof self[$this.data("action")] === "function") {
$this.on(trigger, self[action]); $this.on(trigger, self[action]);
} }
@ -217,7 +220,7 @@ function ComicBook(id, srcs, opts) {
ComicBook.prototype.getControl = function(control) { ComicBook.prototype.getControl = function(control) {
if (typeof this.controls[control] !== "object") { if (typeof this.controls[control] !== "object") {
throw ComicBookException.UNDEFINED_CONTROL + ' ' + control; throw ComicBookException.UNDEFINED_CONTROL + " " + control;
} }
return this.controls[control]; return this.controls[control];
}; };
@ -255,7 +258,7 @@ function ComicBook(id, srcs, opts) {
ComicBook.prototype.getPage = function(i) { ComicBook.prototype.getPage = function(i) {
if (i < 0 || i > srcs.length-1) { if (i < 0 || i > srcs.length-1) {
throw ComicBookException.INVALID_PAGE+' '+i; throw ComicBookException.INVALID_PAGE + " " + i;
} }
if (typeof pages[i] === "object") { if (typeof pages[i] === "object") {
@ -305,7 +308,7 @@ function ComicBook(id, srcs, opts) {
}; };
ComicBook.prototype.fitWidth = function () { ComicBook.prototype.fitWidth = function () {
options.zoomMode = "fitWidth" options.zoomMode = "fitWidth";
ComicBook.prototype.drawPage(); ComicBook.prototype.drawPage();
}; };
@ -338,8 +341,7 @@ function ComicBook(id, srcs, opts) {
var buffer = (options.displayMode === "double" && pointer < srcs.length-1) ? 1 : 0; var buffer = (options.displayMode === "double" && pointer < srcs.length-1) ? 1 : 0;
// start rendering the comic when the requested page is ready // start rendering the comic when the requested page is ready
if ((rendered === false && ($.inArray(pointer + buffer, loaded) !== -1) if ((rendered === false && ($.inArray(pointer + buffer, loaded) !== -1) ||
||
(typeof page_requested === "number" && $.inArray(page_requested, loaded) !== -1)) (typeof page_requested === "number" && $.inArray(page_requested, loaded) !== -1))
) { ) {
// if the user is waiting for a page to be loaded, render that one instead of the default pointer // if the user is waiting for a page to be loaded, render that one instead of the default pointer
@ -406,8 +408,10 @@ function ComicBook(id, srcs, opts) {
*/ */
ComicBook.prototype.drawPage = function(page_no, reset_scroll) { ComicBook.prototype.drawPage = function(page_no, reset_scroll) {
var reset_scroll = (typeof reset_scroll !== "undefined") ? reset_scroll : true; var scrollY;
var scrollY = reset_scroll ? 0 : window.scrollY;
reset_scroll = (typeof reset_scroll !== "undefined") ? reset_scroll : true;
scrollY = reset_scroll ? 0 : window.scrollY;
// if a specific page is given try to render it, if not bail and wait for preload() to render it // if a specific page is given try to render it, if not bail and wait for preload() to render it
if (typeof page_no === "number" && page_no < srcs.length && page_no > 0) { if (typeof page_no === "number" && page_no < srcs.length && page_no > 0) {
@ -431,7 +435,7 @@ function ComicBook(id, srcs, opts) {
} }
if (typeof page !== "object") { if (typeof page !== "object") {
throw ComicBookException.INVALID_PAGE_TYPE+' '+typeof page; throw ComicBookException.INVALID_PAGE_TYPE + " " + typeof page;
} }
var width = page.width; var width = page.width;
@ -442,9 +446,9 @@ function ComicBook(id, srcs, opts) {
// show double page spreads on a single page // show double page spreads on a single page
is_double_page_spread = ( is_double_page_spread = (
typeof page2 === "object" typeof page2 === "object" &&
&& (page.width > page.height || page2.width > page2.height) (page.width > page.height || page2.width > page2.height) &&
&& options.displayMode === "double" options.displayMode === "double"
); );
if (is_double_page_spread) { options.displayMode = "single"; } if (is_double_page_spread) { options.displayMode = "single"; }
@ -468,16 +472,16 @@ function ComicBook(id, srcs, opts) {
case "fitWidth": case "fitWidth":
document.body.style.overflowX = "hidden"; document.body.style.overflowX = "hidden";
zoom_scale = (windowWidth() > width) // scale up if the window is wider than the page, scale down if the window
? ((windowWidth() - width) / windowWidth()) + 1 // scale up if the window is wider than the page // is narrower than the page
: windowWidth() / width; // scale down if the window is narrower than the page zoom_scale = (windowWidth() > width) ? ((windowWidth() - width) / windowWidth()) + 1 : windowWidth() / width;
// update the interal scale var so switching zoomModes while zooming will be smooth // update the interal scale var so switching zoomModes while zooming will be smooth
scale = zoom_scale scale = zoom_scale;
break; break;
default: default:
throw ComicBookException.INVALID_ZOOM_MODE+' '+options.zoomMode; throw ComicBookException.INVALID_ZOOM_MODE + " " + options.zoomMode;
} }
var canvas_width = page.width * zoom_scale; var canvas_width = page.width * zoom_scale;
@ -524,10 +528,11 @@ function ComicBook(id, srcs, opts) {
self.enhance[action](options); self.enhance[action](options);
}); });
var current_page = (options.displayMode === "double" && pointer+2 <= srcs.length) var current_page =
? (pointer+1) + "-" + (pointer+2) : pointer+1 (options.displayMode === "double" &&
pointer + 2 <= srcs.length) ? (pointer + 1) + "-" + (pointer + 2) : pointer + 1;
this.getControl('toolbar') this.getControl("toolbar")
.find("#current-page").text(current_page) .find("#current-page").text(current_page)
.end() .end()
.find("#page-count").text(srcs.length); .find("#page-count").text(srcs.length);
@ -610,15 +615,15 @@ function ComicBook(id, srcs, opts) {
ComicBook.prototype.brightness = function () { ComicBook.prototype.brightness = function () {
self.enhance.brightness({ brightness: $(this).val() }); self.enhance.brightness({ brightness: $(this).val() });
} };
ComicBook.prototype.contrast = function () { ComicBook.prototype.contrast = function () {
self.enhance.brightness({ contrast: $(this).val() }); self.enhance.brightness({ contrast: $(this).val() });
} };
ComicBook.prototype.sharpen = function () { ComicBook.prototype.sharpen = function () {
self.enhance.sharpen({ strength: $(this).val() }); self.enhance.sharpen({ strength: $(this).val() });
} };
ComicBook.prototype.desaturate = function () { ComicBook.prototype.desaturate = function () {
if ($(this).is(":checked")) { if ($(this).is(":checked")) {
@ -662,7 +667,8 @@ function ComicBook(id, srcs, opts) {
* Pixastic progress callback * Pixastic progress callback
* @param {float} progress * @param {float} progress
*/ */
progress: function (progress) { // progress: function (progress) {
progress: function () {
// console.info(Math.floor(progress * 100)); // console.info(Math.floor(progress * 100));
}, },
@ -768,7 +774,7 @@ function ComicBook(id, srcs, opts) {
break; break;
default: default:
throw ComicBookException.INVALID_NAVIGATION_EVENT+' '+e.type; throw ComicBookException.INVALID_NAVIGATION_EVENT + " " + e.type;
} }
if (side) { if (side) {
@ -792,19 +798,23 @@ function ComicBook(id, srcs, opts) {
ComicBook.prototype.destroy = function () { ComicBook.prototype.destroy = function () {
$.each(book.controls, function (name, $control) { $.each(this.controls, function (name, $control) {
$control.remove(); $control.remove();
}); });
canvas.width = 0; canvas.width = 0;
canvas.height = 0; canvas.height = 0;
window.removeEventListener("keydown", self.navigation, false); window.removeEventListener("keydown", this.navigation, false);
window.removeEventListener("hashchange", checkHash, false); window.removeEventListener("hashchange", checkHash, false);
setHash(''); setHash("");
// $(this).trigger("destroy"); // $(this).trigger("destroy");
};
} }
} return ComicBook;
})(jQuery);

View file

@ -17,6 +17,7 @@
], ],
"devDependencies": { "devDependencies": {
"handlebars": "1.0.10", "handlebars": "1.0.10",
"uglify-js": "1.3.4" "uglify-js": "1.3.4",
"jshint": "1.1.0"
} }
} }