2017-09-17 12:36:32 +02:00
|
|
|
/*
|
|
|
|
* kthoom.js
|
|
|
|
*
|
|
|
|
* Licensed under the MIT License
|
|
|
|
*
|
|
|
|
* Copyright(c) 2011 Google Inc.
|
|
|
|
* Copyright(c) 2011 antimatter15
|
2017-09-17 14:51:17 +02:00
|
|
|
*/
|
2017-09-17 12:36:32 +02:00
|
|
|
|
|
|
|
/* Reference Documentation:
|
|
|
|
|
|
|
|
* Web Workers: http://www.whatwg.org/specs/web-workers/current-work/
|
|
|
|
* Web Workers in Mozilla: https://developer.mozilla.org/En/Using_web_workers
|
|
|
|
* File API (FileReader): http://www.w3.org/TR/FileAPI/
|
|
|
|
* Typed Arrays: http://www.khronos.org/registry/typedarray/specs/latest/#6
|
|
|
|
|
|
|
|
*/
|
2017-09-18 18:59:08 +02:00
|
|
|
/* global bitjs */
|
2017-09-17 12:36:32 +02:00
|
|
|
|
|
|
|
if (window.opera) {
|
2017-09-17 19:16:29 +02:00
|
|
|
window.console.log = function(str) {
|
|
|
|
opera.postError(str);
|
|
|
|
};
|
2017-09-17 12:36:32 +02:00
|
|
|
}
|
|
|
|
|
2017-09-17 19:16:29 +02:00
|
|
|
var kthoom;
|
|
|
|
|
2017-09-17 12:36:32 +02:00
|
|
|
// gets the element with the given id
|
|
|
|
function getElem(id) {
|
2017-09-17 14:51:17 +02:00
|
|
|
if (document.documentElement.querySelector) {
|
|
|
|
// querySelector lookup
|
2017-09-17 15:33:22 +02:00
|
|
|
return document.body.querySelector("#" + id);
|
2017-09-17 14:51:17 +02:00
|
|
|
}
|
|
|
|
// getElementById lookup
|
|
|
|
return document.getElementById(id);
|
2017-09-17 12:36:32 +02:00
|
|
|
}
|
|
|
|
|
2017-09-17 15:33:22 +02:00
|
|
|
if (window.kthoom === undefined) {
|
2017-09-17 19:16:29 +02:00
|
|
|
kthoom = {};
|
2017-09-17 12:36:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// key codes
|
|
|
|
kthoom.Key = {
|
|
|
|
ESCAPE: 27,
|
|
|
|
LEFT: 37,
|
|
|
|
UP: 38,
|
|
|
|
RIGHT: 39,
|
|
|
|
DOWN: 40,
|
|
|
|
A: 65, B: 66, C: 67, D: 68, E: 69, F: 70, G: 71, H: 72, I: 73, J: 74, K: 75, L: 76, M: 77,
|
|
|
|
N: 78, O: 79, P: 80, Q: 81, R: 82, S: 83, T: 84, U: 85, V: 86, W: 87, X: 88, Y: 89, Z: 90,
|
|
|
|
QUESTION_MARK: 191,
|
|
|
|
LEFT_SQUARE_BRACKET: 219,
|
|
|
|
RIGHT_SQUARE_BRACKET: 221
|
|
|
|
};
|
|
|
|
|
|
|
|
// The rotation orientation of the comic.
|
|
|
|
kthoom.rotateTimes = 0;
|
|
|
|
|
|
|
|
// global variables
|
|
|
|
var unarchiver = null;
|
|
|
|
var currentImage = 0;
|
|
|
|
var imageFiles = [];
|
|
|
|
var imageFilenames = [];
|
|
|
|
var totalImages = 0;
|
|
|
|
var lastCompletion = 0;
|
2017-09-17 19:16:29 +02:00
|
|
|
|
2017-09-17 12:36:32 +02:00
|
|
|
var hflip = false, vflip = false, fitMode = kthoom.Key.B;
|
|
|
|
var canKeyNext = true, canKeyPrev = true;
|
|
|
|
|
|
|
|
kthoom.saveSettings = function() {
|
2017-09-17 19:16:29 +02:00
|
|
|
localStorage.kthoomSettings = JSON.stringify({
|
2017-09-17 14:51:17 +02:00
|
|
|
rotateTimes: kthoom.rotateTimes,
|
|
|
|
hflip: hflip,
|
|
|
|
vflip: vflip,
|
|
|
|
fitMode: fitMode
|
|
|
|
});
|
2017-09-17 19:16:29 +02:00
|
|
|
};
|
2017-09-17 12:36:32 +02:00
|
|
|
|
|
|
|
kthoom.loadSettings = function() {
|
2017-09-17 14:51:17 +02:00
|
|
|
try {
|
2017-09-20 21:13:32 +02:00
|
|
|
if (localStorage.kthoomSettings.length < 10){
|
2017-09-18 18:59:08 +02:00
|
|
|
return;
|
2017-09-20 21:13:32 +02:00
|
|
|
}
|
2017-09-17 19:16:29 +02:00
|
|
|
var s = JSON.parse(localStorage.kthoomSettings);
|
2017-09-17 14:51:17 +02:00
|
|
|
kthoom.rotateTimes = s.rotateTimes;
|
|
|
|
hflip = s.hflip;
|
|
|
|
vflip = s.vflip;
|
|
|
|
fitMode = s.fitMode;
|
2017-09-17 15:33:22 +02:00
|
|
|
} catch (err) {
|
|
|
|
alert("Error load settings");
|
2017-09-17 14:51:17 +02:00
|
|
|
}
|
2017-09-18 18:59:08 +02:00
|
|
|
};
|
2017-09-17 12:36:32 +02:00
|
|
|
|
2017-09-17 19:16:29 +02:00
|
|
|
var createURLFromArray = function(array, mimeType) {
|
|
|
|
var offset = array.byteOffset, len = array.byteLength;
|
2017-09-18 18:59:08 +02:00
|
|
|
var url;
|
2017-09-17 19:16:29 +02:00
|
|
|
var blob;
|
|
|
|
|
2018-03-31 17:05:15 +02:00
|
|
|
if (mimeType === 'image/xml+svg') {
|
|
|
|
const xmlStr = new TextDecoder('utf-8').decode(array);
|
|
|
|
return 'data:image/svg+xml;UTF-8,' + encodeURIComponent(xmlStr);
|
|
|
|
}
|
|
|
|
|
2017-09-17 19:16:29 +02:00
|
|
|
// TODO: Move all this browser support testing to a common place
|
|
|
|
// and do it just once.
|
|
|
|
|
|
|
|
// Blob constructor, see http://dev.w3.org/2006/webapi/FileAPI/#dfn-Blob.
|
2017-09-18 18:59:08 +02:00
|
|
|
if (typeof Blob === "function") {
|
2017-09-17 19:16:29 +02:00
|
|
|
blob = new Blob([array], {type: mimeType});
|
|
|
|
} else {
|
2017-09-18 18:59:08 +02:00
|
|
|
throw "Browser support for Blobs is missing.";
|
2017-09-17 19:16:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (blob.slice) {
|
|
|
|
blob = blob.slice(offset, offset + len, mimeType);
|
|
|
|
} else {
|
2017-09-18 18:59:08 +02:00
|
|
|
throw "Browser support for Blobs is missing.";
|
2017-09-17 19:16:29 +02:00
|
|
|
}
|
|
|
|
|
2017-09-20 21:13:32 +02:00
|
|
|
if ((typeof URL !== "function" && typeof URL !== "object") ||
|
2017-09-18 18:59:08 +02:00
|
|
|
typeof URL.createObjectURL !== "function") {
|
2017-09-17 19:16:29 +02:00
|
|
|
throw "Browser support for Object URLs is missing";
|
|
|
|
}
|
|
|
|
|
|
|
|
return URL.createObjectURL(blob);
|
2017-09-20 21:13:32 +02:00
|
|
|
};
|
2017-09-17 19:16:29 +02:00
|
|
|
|
|
|
|
|
2017-09-17 12:36:32 +02:00
|
|
|
// Stores an image filename and its data: URI.
|
|
|
|
// TODO: investigate if we really need to store as base64 (leave off ;base64 and just
|
|
|
|
// non-safe URL characters are encoded as %xx ?)
|
|
|
|
// This would save 25% on memory since base64-encoded strings are 4/3 the size of the binary
|
|
|
|
kthoom.ImageFile = function(file) {
|
2017-09-17 14:51:17 +02:00
|
|
|
this.filename = file.filename;
|
2017-09-17 15:33:22 +02:00
|
|
|
var fileExtension = file.filename.split(".").pop().toLowerCase();
|
2017-09-17 19:16:29 +02:00
|
|
|
var mimeType = fileExtension === "png" ? "image/png" :
|
2017-09-17 15:33:22 +02:00
|
|
|
(fileExtension === "jpg" || fileExtension === "jpeg") ? "image/jpeg" :
|
2018-03-31 17:05:15 +02:00
|
|
|
fileExtension === "gif" ? "image/gif" : fileExtension == 'svg' ? 'image/xml+svg' : undefined;
|
2017-09-17 14:51:17 +02:00
|
|
|
this.dataURI = createURLFromArray(file.fileData, mimeType);
|
|
|
|
this.data = file;
|
2017-09-17 12:36:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
kthoom.initProgressMeter = function() {
|
2017-09-17 15:33:22 +02:00
|
|
|
var svgns = "http://www.w3.org/2000/svg";
|
|
|
|
var pdiv = $("#progress")[0];
|
|
|
|
var svg = document.createElementNS(svgns, "svg");
|
|
|
|
svg.style.width = "100%";
|
|
|
|
svg.style.height = "100%";
|
2017-09-17 14:51:17 +02:00
|
|
|
|
2017-09-17 15:33:22 +02:00
|
|
|
var defs = document.createElementNS(svgns, "defs");
|
2017-09-17 14:51:17 +02:00
|
|
|
|
2017-09-17 15:33:22 +02:00
|
|
|
var patt = document.createElementNS(svgns, "pattern");
|
|
|
|
patt.id = "progress_pattern";
|
|
|
|
patt.setAttribute("width", "30");
|
|
|
|
patt.setAttribute("height", "20");
|
|
|
|
patt.setAttribute("patternUnits", "userSpaceOnUse");
|
2017-09-17 14:51:17 +02:00
|
|
|
|
2017-09-17 15:33:22 +02:00
|
|
|
var rect = document.createElementNS(svgns, "rect");
|
|
|
|
rect.setAttribute("width", "100%");
|
|
|
|
rect.setAttribute("height", "100%");
|
|
|
|
rect.setAttribute("fill", "#cc2929");
|
2017-09-17 14:51:17 +02:00
|
|
|
|
2017-09-17 15:33:22 +02:00
|
|
|
var poly = document.createElementNS(svgns, "polygon");
|
|
|
|
poly.setAttribute("fill", "yellow");
|
|
|
|
poly.setAttribute("points", "15,0 30,0 15,20 0,20");
|
2017-09-17 14:51:17 +02:00
|
|
|
|
|
|
|
patt.appendChild(rect);
|
|
|
|
patt.appendChild(poly);
|
|
|
|
defs.appendChild(patt);
|
|
|
|
|
|
|
|
svg.appendChild(defs);
|
|
|
|
|
2017-09-17 15:33:22 +02:00
|
|
|
var g = document.createElementNS(svgns, "g");
|
2017-09-17 14:51:17 +02:00
|
|
|
|
2017-09-17 15:33:22 +02:00
|
|
|
var outline = document.createElementNS(svgns, "rect");
|
|
|
|
outline.setAttribute("y", "1");
|
|
|
|
outline.setAttribute("width", "100%");
|
|
|
|
outline.setAttribute("height", "15");
|
|
|
|
outline.setAttribute("fill", "#777");
|
|
|
|
outline.setAttribute("stroke", "white");
|
|
|
|
outline.setAttribute("rx", "5");
|
|
|
|
outline.setAttribute("ry", "5");
|
2017-09-17 14:51:17 +02:00
|
|
|
g.appendChild(outline);
|
|
|
|
|
2017-09-17 15:33:22 +02:00
|
|
|
var title = document.createElementNS(svgns, "text");
|
|
|
|
title.id = "progress_title";
|
|
|
|
title.appendChild(document.createTextNode("0%"));
|
|
|
|
title.setAttribute("y", "13");
|
|
|
|
title.setAttribute("x", "99.5%");
|
|
|
|
title.setAttribute("fill", "white");
|
|
|
|
title.setAttribute("font-size", "12px");
|
|
|
|
title.setAttribute("text-anchor", "end");
|
2017-09-17 14:51:17 +02:00
|
|
|
g.appendChild(title);
|
|
|
|
|
2017-09-17 15:33:22 +02:00
|
|
|
var meter = document.createElementNS(svgns, "rect");
|
|
|
|
meter.id = "meter";
|
|
|
|
meter.setAttribute("width", "0%");
|
|
|
|
meter.setAttribute("height", "17");
|
|
|
|
meter.setAttribute("fill", "url(#progress_pattern)");
|
|
|
|
meter.setAttribute("rx", "5");
|
|
|
|
meter.setAttribute("ry", "5");
|
|
|
|
|
|
|
|
var meter2 = document.createElementNS(svgns, "rect");
|
|
|
|
meter2.id = "meter2";
|
|
|
|
meter2.setAttribute("width", "0%");
|
|
|
|
meter2.setAttribute("height", "17");
|
|
|
|
meter2.setAttribute("opacity", "0.8");
|
|
|
|
meter2.setAttribute("fill", "#007fff");
|
|
|
|
meter2.setAttribute("rx", "5");
|
|
|
|
meter2.setAttribute("ry", "5");
|
2017-09-17 14:51:17 +02:00
|
|
|
|
|
|
|
g.appendChild(meter);
|
|
|
|
g.appendChild(meter2);
|
|
|
|
|
2017-09-17 15:33:22 +02:00
|
|
|
var page = document.createElementNS(svgns, "text");
|
|
|
|
page.id = "page";
|
|
|
|
page.appendChild(document.createTextNode("0/0"));
|
|
|
|
page.setAttribute("y", "13");
|
|
|
|
page.setAttribute("x", "0.5%");
|
|
|
|
page.setAttribute("fill", "white");
|
|
|
|
page.setAttribute("font-size", "12px");
|
2017-09-17 14:51:17 +02:00
|
|
|
g.appendChild(page);
|
2017-09-17 12:36:32 +02:00
|
|
|
|
|
|
|
|
2017-09-17 14:51:17 +02:00
|
|
|
svg.appendChild(g);
|
|
|
|
pdiv.appendChild(svg);
|
2017-09-18 18:59:08 +02:00
|
|
|
var l;
|
2017-09-17 14:51:17 +02:00
|
|
|
svg.onclick = function(e) {
|
2017-09-17 19:16:29 +02:00
|
|
|
for (var x = pdiv, l = 0; x !== document.documentElement; x = x.parentNode) l += x.offsetLeft;
|
|
|
|
var page = Math.max(1, Math.ceil(((e.clientX - l) / pdiv.offsetWidth) * totalImages)) - 1;
|
2017-09-17 14:51:17 +02:00
|
|
|
currentImage = page;
|
|
|
|
updatePage();
|
|
|
|
};
|
2017-09-17 12:36:32 +02:00
|
|
|
}
|
|
|
|
|
2017-09-17 19:16:29 +02:00
|
|
|
kthoom.setProgressMeter = function(pct, optLabel) {
|
|
|
|
pct = (pct * 100);
|
|
|
|
var part = 1 / totalImages;
|
|
|
|
var remain = ((pct - lastCompletion) / 100) / part;
|
2017-09-17 14:51:17 +02:00
|
|
|
var fract = Math.min(1, remain);
|
2017-09-18 18:59:08 +02:00
|
|
|
var smartpct = ((imageFiles.length / totalImages) + (fract * part)) * 100;
|
2017-09-17 19:16:29 +02:00
|
|
|
if (totalImages === 0) smartpct = pct;
|
2017-09-17 12:36:32 +02:00
|
|
|
|
2017-09-17 19:16:29 +02:00
|
|
|
// + Math.min((pct - lastCompletion), 100/totalImages * 0.9 + (pct - lastCompletion - 100/totalImages)/2, 100/totalImages);
|
|
|
|
var oldval = parseFloat(getElem("meter").getAttribute("width"));
|
|
|
|
if (isNaN(oldval)) oldval = 0;
|
|
|
|
var weight = 0.5;
|
|
|
|
smartpct = ((weight * smartpct) + ((1 - weight) * oldval));
|
2017-09-18 18:59:08 +02:00
|
|
|
if (pct === 100) smartpct = 100;
|
2017-09-17 19:16:29 +02:00
|
|
|
|
|
|
|
if (!isNaN(smartpct)) {
|
2017-09-17 15:33:22 +02:00
|
|
|
getElem("meter").setAttribute("width", smartpct + "%");
|
2017-09-17 19:16:29 +02:00
|
|
|
}
|
|
|
|
var title = getElem("progress_title");
|
|
|
|
while (title.firstChild) title.removeChild(title.firstChild);
|
|
|
|
|
|
|
|
var labelText = pct.toFixed(2) + "% " + imageFiles.length + "/" + totalImages + "";
|
|
|
|
if (optLabel) {
|
|
|
|
labelText = optLabel + " " + labelText;
|
|
|
|
}
|
|
|
|
title.appendChild(document.createTextNode(labelText));
|
|
|
|
|
|
|
|
getElem("meter2").setAttribute("width",
|
2017-09-18 18:59:08 +02:00
|
|
|
100 * (totalImages === 0 ? 0 : ((currentImage + 1) / totalImages)) + "%");
|
2017-09-17 19:16:29 +02:00
|
|
|
|
2017-09-18 18:59:08 +02:00
|
|
|
var titlePage = getElem("page");
|
|
|
|
while (titlePage.firstChild) titlePage.removeChild(titlePage.firstChild);
|
|
|
|
titlePage.appendChild(document.createTextNode( (currentImage + 1) + "/" + totalImages ));
|
2017-09-17 19:16:29 +02:00
|
|
|
|
|
|
|
if (pct > 0) {
|
2017-09-17 14:51:17 +02:00
|
|
|
//getElem('nav').className = '';
|
2017-09-18 18:59:08 +02:00
|
|
|
getElem("progress").className = "";
|
2017-09-17 19:16:29 +02:00
|
|
|
}
|
2017-09-17 12:36:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function loadFromArrayBuffer(ab) {
|
2017-09-17 14:51:17 +02:00
|
|
|
var start = (new Date).getTime();
|
|
|
|
var h = new Uint8Array(ab, 0, 10);
|
2017-09-17 15:33:22 +02:00
|
|
|
var pathToBitJS = "../../static/js/";
|
2017-09-18 18:59:08 +02:00
|
|
|
if (h[0] === 0x52 && h[1] === 0x61 && h[2] === 0x72 && h[3] === 0x21) { //Rar!
|
2017-09-17 14:51:17 +02:00
|
|
|
unarchiver = new bitjs.archive.Unrarrer(ab, pathToBitJS);
|
2017-09-18 18:59:08 +02:00
|
|
|
} else if (h[0] === 80 && h[1] === 75) { //PK (Zip)
|
2017-09-17 14:51:17 +02:00
|
|
|
unarchiver = new bitjs.archive.Unzipper(ab, pathToBitJS);
|
|
|
|
} else { // Try with tar
|
|
|
|
unarchiver = new bitjs.archive.Untarrer(ab, pathToBitJS);
|
|
|
|
}
|
|
|
|
// Listen for UnarchiveEvents.
|
|
|
|
if (unarchiver) {
|
|
|
|
unarchiver.addEventListener(bitjs.archive.UnarchiveEvent.Type.PROGRESS,
|
|
|
|
function(e) {
|
|
|
|
var percentage = e.currentBytesUnarchived / e.totalUncompressedBytesInArchive;
|
|
|
|
totalImages = e.totalFilesInArchive;
|
2017-09-17 15:33:22 +02:00
|
|
|
kthoom.setProgressMeter(percentage, "Unzipping");
|
2017-09-17 14:51:17 +02:00
|
|
|
// display nav
|
|
|
|
lastCompletion = percentage * 100;
|
|
|
|
});
|
|
|
|
unarchiver.addEventListener(bitjs.archive.UnarchiveEvent.Type.EXTRACT,
|
|
|
|
function(e) {
|
|
|
|
// convert DecompressedFile into a bunch of ImageFiles
|
|
|
|
if (e.unarchivedFile) {
|
|
|
|
var f = e.unarchivedFile;
|
|
|
|
// add any new pages based on the filename
|
2017-09-18 18:59:08 +02:00
|
|
|
if (imageFilenames.indexOf(f.filename) === -1) {
|
2017-09-17 14:51:17 +02:00
|
|
|
imageFilenames.push(f.filename);
|
|
|
|
imageFiles.push(new kthoom.ImageFile(f));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// display first page if we haven't yet
|
2017-09-20 21:13:32 +02:00
|
|
|
if (imageFiles.length === currentImage + 1) {
|
2017-09-17 14:51:17 +02:00
|
|
|
updatePage();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
unarchiver.addEventListener(bitjs.archive.UnarchiveEvent.Type.FINISH,
|
2017-09-18 18:59:08 +02:00
|
|
|
function() {
|
|
|
|
var diff = ((new Date).getTime() - start) / 1000;
|
2017-09-17 19:16:29 +02:00
|
|
|
console.log("Unarchiving done in " + diff + "s");
|
|
|
|
});
|
2017-09-17 14:51:17 +02:00
|
|
|
unarchiver.start();
|
|
|
|
} else {
|
2017-09-17 15:33:22 +02:00
|
|
|
alert("Some error");
|
2017-09-17 14:51:17 +02:00
|
|
|
}
|
2017-09-17 12:36:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function updatePage() {
|
2017-09-17 15:33:22 +02:00
|
|
|
var title = getElem("page");
|
2017-09-17 14:51:17 +02:00
|
|
|
while (title.firstChild) title.removeChild(title.firstChild);
|
2017-09-20 21:13:32 +02:00
|
|
|
title.appendChild(document.createTextNode( (currentImage + 1 ) + "/" + totalImages ));
|
2017-09-17 14:51:17 +02:00
|
|
|
|
2017-09-18 18:59:08 +02:00
|
|
|
getElem("meter2").setAttribute("width",
|
2017-09-20 21:13:32 +02:00
|
|
|
100 * (totalImages === 0 ? 0 : ((currentImage + 1 ) / totalImages)) + "%");
|
2017-09-17 14:51:17 +02:00
|
|
|
if (imageFiles[currentImage]) {
|
|
|
|
setImage(imageFiles[currentImage].dataURI);
|
|
|
|
} else {
|
2017-09-17 15:33:22 +02:00
|
|
|
setImage("loading");
|
2017-09-17 14:51:17 +02:00
|
|
|
}
|
2017-09-17 12:36:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function setImage(url) {
|
2017-09-17 14:51:17 +02:00
|
|
|
var canvas = $("#mainImage")[0];
|
2017-09-17 15:33:22 +02:00
|
|
|
var x = $("#mainImage")[0].getContext("2d");
|
|
|
|
$("#mainText").hide();
|
2017-09-18 18:59:08 +02:00
|
|
|
if (url === "loading") {
|
2017-09-17 14:51:17 +02:00
|
|
|
updateScale(true);
|
|
|
|
canvas.width = innerWidth - 100;
|
|
|
|
canvas.height = 200;
|
2017-09-17 15:33:22 +02:00
|
|
|
x.fillStyle = "red";
|
|
|
|
x.font = "50px sans-serif";
|
|
|
|
x.strokeStyle = "black";
|
2017-09-18 18:59:08 +02:00
|
|
|
x.fillText("Loading Page #" + (currentImage + 1), 100, 100);
|
2017-09-17 14:51:17 +02:00
|
|
|
} else {
|
2017-09-20 21:13:32 +02:00
|
|
|
if ($("body").css("scrollHeight") / innerHeight > 1) {
|
2017-09-17 15:33:22 +02:00
|
|
|
$("body").css("overflowY", "scroll");
|
2017-09-17 12:36:32 +02:00
|
|
|
}
|
2017-09-17 14:51:17 +02:00
|
|
|
|
|
|
|
var img = new Image();
|
2017-09-18 18:59:08 +02:00
|
|
|
img.onerror = function() {
|
2017-09-17 14:51:17 +02:00
|
|
|
canvas.width = innerWidth - 100;
|
|
|
|
canvas.height = 300;
|
|
|
|
updateScale(true);
|
2017-09-17 15:33:22 +02:00
|
|
|
x.fillStyle = "orange";
|
|
|
|
x.font = "50px sans-serif";
|
|
|
|
x.strokeStyle = "black";
|
2017-09-18 18:59:08 +02:00
|
|
|
x.fillText("Page #" + (currentImage + 1) + " (" +
|
|
|
|
imageFiles[currentImage].filename + ")", 100, 100);
|
2017-09-17 15:33:22 +02:00
|
|
|
x.fillStyle = "red";
|
|
|
|
x.fillText("Is corrupt or not an image", 100, 200);
|
2017-09-17 14:51:17 +02:00
|
|
|
|
2017-09-18 18:59:08 +02:00
|
|
|
var xhr = new XMLHttpRequest();
|
2017-09-17 14:51:17 +02:00
|
|
|
if (/(html|htm)$/.test(imageFiles[currentImage].filename)) {
|
2017-09-17 15:33:22 +02:00
|
|
|
xhr.open("GET", url, true);
|
2017-09-17 14:51:17 +02:00
|
|
|
xhr.onload = function() {
|
|
|
|
//document.getElementById('mainText').style.display = '';
|
|
|
|
$("#mainText").css("display", "");
|
2017-09-18 18:59:08 +02:00
|
|
|
$("#mainText").innerHTML("<iframe style=\"width:100%;height:700px;border:0\" src=\"data:text/html," + escape(xhr.responseText) + "\"></iframe>");
|
2017-09-17 14:51:17 +02:00
|
|
|
}
|
|
|
|
xhr.send(null);
|
2017-09-18 18:59:08 +02:00
|
|
|
} else if (!/(jpg|jpeg|png|gif)$/.test(imageFiles[currentImage].filename) && imageFiles[currentImage].data.uncompressedSize < 10 * 1024) {
|
2017-09-17 15:33:22 +02:00
|
|
|
xhr.open("GET", url, true);
|
2017-09-17 14:51:17 +02:00
|
|
|
xhr.onload = function() {
|
|
|
|
$("#mainText").css("display", "");
|
|
|
|
$("#mainText").innerText(xhr.responseText);
|
|
|
|
};
|
|
|
|
xhr.send(null);
|
|
|
|
}
|
2017-09-17 12:36:32 +02:00
|
|
|
};
|
2017-09-17 14:51:17 +02:00
|
|
|
img.onload = function() {
|
|
|
|
var h = img.height,
|
|
|
|
w = img.width,
|
|
|
|
sw = w,
|
|
|
|
sh = h;
|
|
|
|
kthoom.rotateTimes = (4 + kthoom.rotateTimes) % 4;
|
|
|
|
x.save();
|
2017-09-20 21:13:32 +02:00
|
|
|
if (kthoom.rotateTimes % 2 === 1) {
|
|
|
|
sh = w;
|
|
|
|
sw = h;
|
|
|
|
}
|
2017-09-17 14:51:17 +02:00
|
|
|
canvas.height = sh;
|
|
|
|
canvas.width = sw;
|
2017-09-20 21:13:32 +02:00
|
|
|
x.translate(sw / 2, sh / 2);
|
|
|
|
x.rotate(Math.PI / 2 * kthoom.rotateTimes);
|
|
|
|
x.translate(-w / 2, -h / 2);
|
2017-09-17 14:51:17 +02:00
|
|
|
if (vflip) {
|
2017-09-18 18:59:08 +02:00
|
|
|
x.scale(1, -1);
|
2017-09-17 14:51:17 +02:00
|
|
|
x.translate(0, -h);
|
2017-09-18 18:59:08 +02:00
|
|
|
}
|
2017-09-17 14:51:17 +02:00
|
|
|
if (hflip) {
|
2017-09-18 18:59:08 +02:00
|
|
|
x.scale(-1, 1);
|
2017-09-17 14:51:17 +02:00
|
|
|
x.translate(-w, 0);
|
|
|
|
}
|
2017-09-17 15:33:22 +02:00
|
|
|
canvas.style.display = "none";
|
2017-09-18 18:59:08 +02:00
|
|
|
scrollTo(0, 0);
|
2017-09-17 14:51:17 +02:00
|
|
|
x.drawImage(img, 0, 0);
|
|
|
|
|
|
|
|
updateScale();
|
|
|
|
|
2017-09-18 18:59:08 +02:00
|
|
|
canvas.style.display = "";
|
2017-09-17 14:51:17 +02:00
|
|
|
$("body").css("overflowY", "");
|
|
|
|
x.restore();
|
|
|
|
};
|
|
|
|
img.src = url;
|
|
|
|
}
|
2017-09-17 12:36:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function showPrevPage() {
|
2017-09-17 14:51:17 +02:00
|
|
|
currentImage--;
|
|
|
|
if (currentImage < 0) {
|
2017-09-17 19:16:29 +02:00
|
|
|
// Freeze on the current page.
|
|
|
|
currentImage++;
|
|
|
|
} else {
|
|
|
|
updatePage();
|
2017-09-17 12:36:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function showNextPage() {
|
2017-09-17 14:51:17 +02:00
|
|
|
currentImage++;
|
|
|
|
if (currentImage >= Math.max(totalImages, imageFiles.length)) {
|
2017-09-17 19:16:29 +02:00
|
|
|
// Freeze on the current page.
|
|
|
|
currentImage--;
|
|
|
|
} else {
|
|
|
|
updatePage();
|
2017-09-17 12:36:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateScale(clear) {
|
2017-09-20 21:13:32 +02:00
|
|
|
var mainImageStyle = getElem("mainImage").style;
|
|
|
|
mainImageStyle.width = "";
|
|
|
|
mainImageStyle.height = "";
|
|
|
|
mainImageStyle.maxWidth = "";
|
|
|
|
mainImageStyle.maxHeight = "";
|
2017-09-17 14:51:17 +02:00
|
|
|
var maxheight = innerHeight - 15;
|
2017-09-20 21:13:32 +02:00
|
|
|
if (!/main/.test(getElem("titlebar").className)) {
|
2017-09-17 14:51:17 +02:00
|
|
|
maxheight -= 25;
|
|
|
|
}
|
2017-09-20 21:13:32 +02:00
|
|
|
if (clear || fitMode === kthoom.Key.N) {
|
|
|
|
} else if (fitMode === kthoom.Key.B) {
|
|
|
|
mainImageStyle.maxWidth = "100%";
|
|
|
|
mainImageStyle.maxHeight = maxheight + "px";
|
|
|
|
} else if (fitMode === kthoom.Key.H) {
|
|
|
|
mainImageStyle.height = maxheight + "px";
|
|
|
|
} else if (fitMode === kthoom.Key.W) {
|
|
|
|
mainImageStyle.width = "100%";
|
2017-09-17 14:51:17 +02:00
|
|
|
}
|
|
|
|
kthoom.saveSettings();
|
2017-09-17 12:36:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function keyHandler(evt) {
|
2017-09-17 14:51:17 +02:00
|
|
|
var code = evt.keyCode;
|
|
|
|
|
2017-09-20 21:13:32 +02:00
|
|
|
if ($("#progress").css("display") === "none"){
|
2017-09-17 19:16:29 +02:00
|
|
|
return;
|
2017-09-20 21:13:32 +02:00
|
|
|
}
|
|
|
|
canKeyNext = (($("body").css("offsetWidth") + $("body").css("scrollLeft")) / $("body").css("scrollWidth")) >= 1;
|
2017-09-17 14:51:17 +02:00
|
|
|
canKeyPrev = (scrollX <= 0);
|
|
|
|
|
|
|
|
if (evt.ctrlKey || evt.shiftKey || evt.metaKey) return;
|
2017-09-20 21:13:32 +02:00
|
|
|
switch (code) {
|
2017-09-17 14:51:17 +02:00
|
|
|
case kthoom.Key.LEFT:
|
|
|
|
if (canKeyPrev) showPrevPage();
|
|
|
|
break;
|
|
|
|
case kthoom.Key.RIGHT:
|
|
|
|
if (canKeyNext) showNextPage();
|
|
|
|
break;
|
|
|
|
case kthoom.Key.L:
|
|
|
|
kthoom.rotateTimes--;
|
|
|
|
if (kthoom.rotateTimes < 0) {
|
|
|
|
kthoom.rotateTimes = 3;
|
|
|
|
}
|
|
|
|
updatePage();
|
|
|
|
break;
|
|
|
|
case kthoom.Key.R:
|
|
|
|
kthoom.rotateTimes++;
|
|
|
|
if (kthoom.rotateTimes > 3) {
|
|
|
|
kthoom.rotateTimes = 0;
|
|
|
|
}
|
|
|
|
updatePage();
|
|
|
|
break;
|
|
|
|
case kthoom.Key.F:
|
|
|
|
if (!hflip && !vflip) {
|
|
|
|
hflip = true;
|
2017-09-20 21:13:32 +02:00
|
|
|
} else if (hflip === true) {
|
2017-09-17 14:51:17 +02:00
|
|
|
vflip = true;
|
|
|
|
hflip = false;
|
2017-09-20 21:13:32 +02:00
|
|
|
} else if (vflip === true) {
|
2017-09-17 14:51:17 +02:00
|
|
|
vflip = false;
|
|
|
|
}
|
|
|
|
updatePage();
|
|
|
|
break;
|
|
|
|
case kthoom.Key.W:
|
|
|
|
fitMode = kthoom.Key.W;
|
|
|
|
updateScale();
|
|
|
|
break;
|
|
|
|
case kthoom.Key.H:
|
|
|
|
fitMode = kthoom.Key.H;
|
|
|
|
updateScale();
|
|
|
|
break;
|
|
|
|
case kthoom.Key.B:
|
|
|
|
fitMode = kthoom.Key.B;
|
|
|
|
updateScale();
|
|
|
|
break;
|
|
|
|
case kthoom.Key.N:
|
|
|
|
fitMode = kthoom.Key.N;
|
|
|
|
updateScale();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
//console.log('KeyCode = ' + code);
|
|
|
|
break;
|
|
|
|
}
|
2017-09-17 12:36:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function init(filename) {
|
2017-09-17 14:51:17 +02:00
|
|
|
if (!window.FileReader) {
|
2017-09-17 15:33:22 +02:00
|
|
|
alert("Sorry, kthoom will not work with your browser because it does not support the File API. Please try kthoom with Chrome 12+ or Firefox 7+");
|
2017-09-17 14:51:17 +02:00
|
|
|
} else {
|
|
|
|
var request = new XMLHttpRequest();
|
2017-09-20 21:13:32 +02:00
|
|
|
request.open("GET", filename);
|
|
|
|
request.responseType = "arraybuffer";
|
|
|
|
request.setRequestHeader("X-Test", "test1");
|
|
|
|
request.setRequestHeader("X-Test", "test2");
|
2017-09-17 15:33:22 +02:00
|
|
|
request.addEventListener("load", function(event) {
|
2017-09-17 14:51:17 +02:00
|
|
|
if (request.status >= 200 && request.status < 300) {
|
|
|
|
loadFromArrayBuffer(request.response);
|
|
|
|
} else {
|
|
|
|
console.warn(request.statusText, request.responseText);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
request.send();
|
|
|
|
kthoom.initProgressMeter();
|
2017-09-17 15:33:22 +02:00
|
|
|
document.body.className += /AppleWebKit/.test(navigator.userAgent) ? " webkit" : "";
|
2017-09-18 18:59:08 +02:00
|
|
|
updateScale(true);
|
2017-09-17 14:51:17 +02:00
|
|
|
kthoom.loadSettings();
|
|
|
|
$(document).keydown(keyHandler);
|
|
|
|
|
|
|
|
$(window).resize(function() {
|
|
|
|
var f = (screen.width - innerWidth < 4 && screen.height - innerHeight < 4);
|
2017-09-17 15:33:22 +02:00
|
|
|
getElem("titlebar").className = f ? "main" : "";
|
2017-09-17 14:51:17 +02:00
|
|
|
updateScale();
|
|
|
|
});
|
|
|
|
|
2017-09-17 15:33:22 +02:00
|
|
|
$("#mainImage").click(function(evt) {
|
2017-09-17 14:51:17 +02:00
|
|
|
// Firefox does not support offsetX/Y so we have to manually calculate
|
|
|
|
// where the user clicked in the image.
|
2017-09-17 19:16:29 +02:00
|
|
|
var mainContentWidth = $("#mainContent").width();
|
|
|
|
var mainContentHeight = $("#mainContent").height();
|
2017-09-17 14:51:17 +02:00
|
|
|
var comicWidth = evt.target.clientWidth;
|
|
|
|
var comicHeight = evt.target.clientHeight;
|
|
|
|
var offsetX = (mainContentWidth - comicWidth) / 2;
|
|
|
|
var offsetY = (mainContentHeight - comicHeight) / 2;
|
|
|
|
var clickX = !!evt.offsetX ? evt.offsetX : (evt.clientX - offsetX);
|
|
|
|
var clickY = !!evt.offsetY ? evt.offsetY : (evt.clientY - offsetY);
|
|
|
|
|
|
|
|
// Determine if the user clicked/tapped the left side or the
|
|
|
|
// right side of the page.
|
|
|
|
var clickedPrev = false;
|
|
|
|
switch (kthoom.rotateTimes) {
|
|
|
|
case 0:
|
|
|
|
clickedPrev = clickX < (comicWidth / 2);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
clickedPrev = clickY < (comicHeight / 2);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
clickedPrev = clickX > (comicWidth / 2);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
clickedPrev = clickY > (comicHeight / 2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (clickedPrev) {
|
2017-09-17 19:16:29 +02:00
|
|
|
showPrevPage();
|
2017-09-17 14:51:17 +02:00
|
|
|
} else {
|
2017-09-17 19:16:29 +02:00
|
|
|
showNextPage();
|
2017-09-17 14:51:17 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-09-17 12:36:32 +02:00
|
|
|
}
|