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:
@echo "Running jshint..."
@./node_modules/.bin/jshint lib/ComicBook.js --config lib/.jshintrc
@echo "Compiling Handlebars templates..."
@./node_modules/.bin/handlebars templates/*.handlebars -f lib/templates.js
@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:
@ -13,7 +13,6 @@
- full browser test - IE9 / FF3.6+ / Chrome / Safari / Opera
Nice 2 have:
- lint
- jump to page?
- make page draggable with the cursor
- enable menu items via config, allow for custom items
@ -26,14 +25,18 @@
- unit test / integrate with travis CI
*/
/**
var ComicBook = (function ($) {
"use strict";
/**
* Merge two arrays. Any properties in b will replace the same properties in
* a. New properties from b will be added to a.
*
* @param a {Object}
* @param b {Object}
*/
function merge(a, b) {
function merge(a, b) {
var prop;
@ -47,25 +50,25 @@ function merge(a, b) {
}
return b;
}
}
/**
/**
* Exception class. Always throw an instance of this when throwing exceptions.
*
* @param {String} type
* @param {Object} object
* @returns {ComicBookException}
*/
var ComicBookException = {
var ComicBookException = {
INVALID_ACTION: "invalid action",
INVALID_PAGE: "invalid page",
INVALID_PAGE_TYPE: "invalid page type",
UNDEFINED_CONTROL: "undefined control",
INVALID_ZOOM_MODE: "invalid zoom mode",
INVALID_NAVIGATION_EVENT: "invalid navigation event"
};
};
function ComicBook(id, srcs, opts) {
function ComicBook(id, srcs, opts) {
var self = this;
var canvas_id = id; // canvas element id
@ -192,14 +195,14 @@ function ComicBook(id, srcs, opts) {
controls[name] = $template;
// 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 trigger = $this.data('trigger');
var action = $this.data('action');
var trigger = $this.data("trigger");
var action = $this.data("action");
// 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]);
}
@ -217,7 +220,7 @@ function ComicBook(id, srcs, opts) {
ComicBook.prototype.getControl = function(control) {
if (typeof this.controls[control] !== "object") {
throw ComicBookException.UNDEFINED_CONTROL + ' ' + control;
throw ComicBookException.UNDEFINED_CONTROL + " " + control;
}
return this.controls[control];
};
@ -255,7 +258,7 @@ function ComicBook(id, srcs, opts) {
ComicBook.prototype.getPage = function(i) {
if (i < 0 || i > srcs.length-1) {
throw ComicBookException.INVALID_PAGE+' '+i;
throw ComicBookException.INVALID_PAGE + " " + i;
}
if (typeof pages[i] === "object") {
@ -305,7 +308,7 @@ function ComicBook(id, srcs, opts) {
};
ComicBook.prototype.fitWidth = function () {
options.zoomMode = "fitWidth"
options.zoomMode = "fitWidth";
ComicBook.prototype.drawPage();
};
@ -338,8 +341,7 @@ function ComicBook(id, srcs, opts) {
var buffer = (options.displayMode === "double" && pointer < srcs.length-1) ? 1 : 0;
// 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))
) {
// 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) {
var reset_scroll = (typeof reset_scroll !== "undefined") ? reset_scroll : true;
var scrollY = reset_scroll ? 0 : window.scrollY;
var 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 (typeof page_no === "number" && page_no < srcs.length && page_no > 0) {
@ -431,7 +435,7 @@ function ComicBook(id, srcs, opts) {
}
if (typeof page !== "object") {
throw ComicBookException.INVALID_PAGE_TYPE+' '+typeof page;
throw ComicBookException.INVALID_PAGE_TYPE + " " + typeof page;
}
var width = page.width;
@ -442,9 +446,9 @@ function ComicBook(id, srcs, opts) {
// show double page spreads on a single page
is_double_page_spread = (
typeof page2 === "object"
&& (page.width > page.height || page2.width > page2.height)
&& options.displayMode === "double"
typeof page2 === "object" &&
(page.width > page.height || page2.width > page2.height) &&
options.displayMode === "double"
);
if (is_double_page_spread) { options.displayMode = "single"; }
@ -468,16 +472,16 @@ function ComicBook(id, srcs, opts) {
case "fitWidth":
document.body.style.overflowX = "hidden";
zoom_scale = (windowWidth() > width)
? ((windowWidth() - width) / windowWidth()) + 1 // scale up if the window is wider than the page
: windowWidth() / width; // scale down if the window is narrower than the page
// scale up if the window is wider than the page, 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
scale = zoom_scale
scale = zoom_scale;
break;
default:
throw ComicBookException.INVALID_ZOOM_MODE+' '+options.zoomMode;
throw ComicBookException.INVALID_ZOOM_MODE + " " + options.zoomMode;
}
var canvas_width = page.width * zoom_scale;
@ -524,10 +528,11 @@ function ComicBook(id, srcs, opts) {
self.enhance[action](options);
});
var current_page = (options.displayMode === "double" && pointer+2 <= srcs.length)
? (pointer+1) + "-" + (pointer+2) : pointer+1
var current_page =
(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)
.end()
.find("#page-count").text(srcs.length);
@ -610,15 +615,15 @@ function ComicBook(id, srcs, opts) {
ComicBook.prototype.brightness = function () {
self.enhance.brightness({ brightness: $(this).val() });
}
};
ComicBook.prototype.contrast = function () {
self.enhance.brightness({ contrast: $(this).val() });
}
};
ComicBook.prototype.sharpen = function () {
self.enhance.sharpen({ strength: $(this).val() });
}
};
ComicBook.prototype.desaturate = function () {
if ($(this).is(":checked")) {
@ -662,7 +667,8 @@ function ComicBook(id, srcs, opts) {
* Pixastic progress callback
* @param {float} progress
*/
progress: function (progress) {
// progress: function (progress) {
progress: function () {
// console.info(Math.floor(progress * 100));
},
@ -768,7 +774,7 @@ function ComicBook(id, srcs, opts) {
break;
default:
throw ComicBookException.INVALID_NAVIGATION_EVENT+' '+e.type;
throw ComicBookException.INVALID_NAVIGATION_EVENT + " " + e.type;
}
if (side) {
@ -792,19 +798,23 @@ function ComicBook(id, srcs, opts) {
ComicBook.prototype.destroy = function () {
$.each(book.controls, function (name, $control) {
$.each(this.controls, function (name, $control) {
$control.remove();
});
canvas.width = 0;
canvas.height = 0;
window.removeEventListener("keydown", self.navigation, false);
window.removeEventListener("keydown", this.navigation, false);
window.removeEventListener("hashchange", checkHash, false);
setHash('');
setHash("");
// $(this).trigger("destroy");
};
}
}
return ComicBook;
})(jQuery);

View file

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