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
|
|
|
|
|
|
|
|
*/
|
2019-06-17 19:48:17 +02:00
|
|
|
/* global screenfull, bitjs, Uint8Array, opera */
|
|
|
|
/* exported init, event */
|
|
|
|
|
|
|
|
|
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-11-21 12:46:03 +01:00
|
|
|
if (typeof 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,
|
2017-12-10 08:43:51 +01:00
|
|
|
SPACE: 32,
|
2017-09-17 12:36:32 +02:00
|
|
|
LEFT: 37,
|
|
|
|
UP: 38,
|
|
|
|
RIGHT: 39,
|
2017-12-10 08:43:51 +01:00
|
|
|
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,
|
2017-09-17 12:36:32 +02:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
// global variables
|
|
|
|
var unarchiver = null;
|
|
|
|
var currentImage = 0;
|
|
|
|
var imageFiles = [];
|
|
|
|
var imageFilenames = [];
|
|
|
|
var totalImages = 0;
|
2017-09-17 19:16:29 +02:00
|
|
|
|
2017-11-19 13:20:59 +01:00
|
|
|
var settings = {
|
2017-12-10 08:43:51 +01:00
|
|
|
hflip: false,
|
|
|
|
vflip: false,
|
2017-11-19 13:20:59 +01:00
|
|
|
rotateTimes: 0,
|
|
|
|
fitMode: kthoom.Key.B,
|
2019-04-21 11:20:15 +02:00
|
|
|
theme: "light",
|
|
|
|
direction: 0 // 0 = Left to Right, 1 = Right to Left
|
2017-11-19 13:20:59 +01:00
|
|
|
};
|
2017-09-17 12:36:32 +02:00
|
|
|
|
|
|
|
kthoom.saveSettings = function() {
|
2017-11-19 13:20:59 +01:00
|
|
|
localStorage.kthoomSettings = JSON.stringify(settings);
|
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-11-20 21:54:55 +01:00
|
|
|
if (!localStorage.kthoomSettings) {
|
2017-09-18 18:59:08 +02:00
|
|
|
return;
|
2017-09-20 21:13:32 +02:00
|
|
|
}
|
2017-11-19 13:20:59 +01:00
|
|
|
|
|
|
|
$.extend(settings, JSON.parse(localStorage.kthoomSettings));
|
|
|
|
|
|
|
|
kthoom.setSettings();
|
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-11-19 13:20:59 +01:00
|
|
|
kthoom.setSettings = function() {
|
|
|
|
// Set settings control values
|
|
|
|
$.each(settings, function(key, value) {
|
|
|
|
if (typeof value === "boolean") {
|
2017-11-20 21:54:55 +01:00
|
|
|
$("input[name=" + key + "]").prop("checked", value);
|
2017-11-19 13:20:59 +01:00
|
|
|
} else {
|
2017-11-20 21:54:55 +01:00
|
|
|
$("input[name=" + key + "]").val([value]);
|
2017-11-19 13:20:59 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-09-17 19:16:29 +02:00
|
|
|
var createURLFromArray = function(array, mimeType) {
|
2019-03-05 21:28:55 +01:00
|
|
|
var offset = array.byteOffset;
|
|
|
|
var len = array.byteLength;
|
2019-06-17 19:48:17 +02:00
|
|
|
// var url;
|
2017-09-17 19:16:29 +02:00
|
|
|
var blob;
|
|
|
|
|
2019-06-17 19:48:17 +02:00
|
|
|
if (mimeType === "image/xml+svg") {
|
|
|
|
var xmlStr = new TextDecoder("utf-8").decode(array);
|
|
|
|
return "data:image/svg+xml;UTF-8," + encodeURIComponent(xmlStr);
|
2019-03-05 21:28:55 +01:00
|
|
|
}
|
2018-03-31 17:05:15 +02:00
|
|
|
|
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.
|
|
|
|
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();
|
2020-12-17 16:53:24 +01:00
|
|
|
switch (fileExtension) {
|
|
|
|
case "jpg":
|
|
|
|
case "jpeg":
|
|
|
|
this.mimeType = "image/jpeg";
|
|
|
|
break;
|
2021-02-27 09:23:34 +01:00
|
|
|
case "png":
|
|
|
|
this.mimeType = "image/png";
|
|
|
|
break;
|
2020-12-17 16:53:24 +01:00
|
|
|
case "gif":
|
2021-01-31 11:21:09 +01:00
|
|
|
this.mimeType = "image/gif";
|
2020-12-17 16:53:24 +01:00
|
|
|
break;
|
|
|
|
case "svg":
|
|
|
|
this.mimeType = "image/svg+xml";
|
|
|
|
break;
|
|
|
|
case "webp":
|
|
|
|
this.mimeType = "image/webp";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
this.mimeType = undefined;
|
|
|
|
break;
|
|
|
|
}
|
2019-03-05 21:28:55 +01:00
|
|
|
if ( this.mimeType !== undefined) {
|
|
|
|
this.dataURI = createURLFromArray(file.fileData, this.mimeType);
|
|
|
|
this.data = file;
|
|
|
|
}
|
2017-09-17 12:36:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-12-10 14:02:49 +01:00
|
|
|
function initProgressClick() {
|
|
|
|
$("#progress").click(function(e) {
|
2021-04-17 21:25:45 +02:00
|
|
|
var offset = $(this).offset();
|
|
|
|
var x = e.pageX - offset.left;
|
|
|
|
var rate = settings.direction === 0 ? x / $(this).width() : 1 - x / $(this).width();
|
|
|
|
var page = Math.max(1, Math.ceil(rate * totalImages)) - 1;
|
2017-09-17 14:51:17 +02:00
|
|
|
currentImage = page;
|
|
|
|
updatePage();
|
2017-12-10 14:02:49 +01:00
|
|
|
});
|
2019-06-17 19:48:17 +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);
|
2019-05-13 07:53:25 +02:00
|
|
|
var pathToBitJS = "../../static/js/archive/";
|
2019-06-17 19:48:17 +02:00
|
|
|
var lastCompletion = 0;
|
2021-01-02 15:32:06 +01:00
|
|
|
if (h[0] === 0x52 && h[1] === 0x61 && h[2] === 0x72 && h[3] === 0x21) { //Rar!
|
|
|
|
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);
|
2019-06-17 19:48:17 +02:00
|
|
|
} else if (h[0] === 255 && h[1] === 216) { // JPEG
|
2019-05-31 08:43:51 +02:00
|
|
|
// ToDo: check
|
|
|
|
updateProgress(100);
|
|
|
|
lastCompletion = 100;
|
|
|
|
return;
|
2017-09-17 14:51:17 +02:00
|
|
|
} 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;
|
2019-03-05 21:28:55 +01:00
|
|
|
if (totalImages === 0) {
|
|
|
|
totalImages = e.totalFilesInArchive;
|
|
|
|
}
|
2019-06-17 19:48:17 +02:00
|
|
|
updateProgress(percentage * 100);
|
2017-09-17 14:51:17 +02:00
|
|
|
lastCompletion = percentage * 100;
|
2019-05-31 08:43:51 +02:00
|
|
|
});
|
|
|
|
unarchiver.addEventListener(bitjs.archive.UnarchiveEvent.Type.INFO,
|
|
|
|
function(e) {
|
2019-06-17 19:48:17 +02:00
|
|
|
// console.log(e.msg); // Enable debug output here
|
2017-09-17 14:51:17 +02:00
|
|
|
});
|
|
|
|
unarchiver.addEventListener(bitjs.archive.UnarchiveEvent.Type.EXTRACT,
|
|
|
|
function(e) {
|
2019-03-05 21:28:55 +01:00
|
|
|
// convert DecompressedFile into a bunch of ImageFiles
|
2017-09-17 14:51:17 +02:00
|
|
|
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) {
|
2019-03-05 21:28:55 +01:00
|
|
|
var test = new kthoom.ImageFile(f);
|
|
|
|
if ( test.mimeType !== undefined) {
|
|
|
|
imageFilenames.push(f.filename);
|
|
|
|
imageFiles.push(test);
|
|
|
|
// add thumbnails to the TOC list
|
|
|
|
$("#thumbnails").append(
|
|
|
|
"<li>" +
|
|
|
|
"<a data-page='" + imageFiles.length + "'>" +
|
|
|
|
"<img src='" + imageFiles[imageFiles.length - 1].dataURI + "'/>" +
|
|
|
|
"<span>" + imageFiles.length + "</span>" +
|
|
|
|
"</a>" +
|
|
|
|
"</li>"
|
|
|
|
);
|
|
|
|
// display first page if we haven't yet
|
|
|
|
if (imageFiles.length === currentImage + 1) {
|
|
|
|
updatePage(lastCompletion);
|
|
|
|
}
|
2019-06-17 19:48:17 +02:00
|
|
|
} else {
|
2019-03-05 21:28:55 +01:00
|
|
|
totalImages--;
|
|
|
|
}
|
2017-09-17 14:51:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
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");
|
2021-01-02 15:32:06 +01:00
|
|
|
}
|
2017-09-17 12:36:32 +02:00
|
|
|
}
|
|
|
|
|
2017-12-10 08:43:51 +01:00
|
|
|
function scrollTocToActive() {
|
|
|
|
// Scroll to the thumbnail in the TOC on page change
|
2019-06-17 19:48:17 +02:00
|
|
|
$("#tocView").stop().animate({
|
|
|
|
scrollTop: $("#tocView a.active").position().top
|
2017-12-10 08:43:51 +01:00
|
|
|
}, 200);
|
|
|
|
}
|
2017-09-17 12:36:32 +02:00
|
|
|
|
|
|
|
function updatePage() {
|
2019-06-17 19:48:17 +02:00
|
|
|
$(".page").text((currentImage + 1 ) + "/" + totalImages);
|
2017-12-10 08:43:51 +01:00
|
|
|
|
|
|
|
// Mark the current page in the TOC
|
2019-06-17 19:48:17 +02:00
|
|
|
$("#tocView a[data-page]")
|
2017-12-10 08:43:51 +01:00
|
|
|
// Remove the currently active thumbnail
|
2019-06-17 19:48:17 +02:00
|
|
|
.removeClass("active")
|
2017-12-10 08:43:51 +01:00
|
|
|
// Find the new one
|
2019-06-17 19:48:17 +02:00
|
|
|
.filter("[data-page=" + (currentImage + 1) + "]")
|
2017-12-10 08:43:51 +01:00
|
|
|
// Set it to active
|
2019-06-17 19:48:17 +02:00
|
|
|
.addClass("active");
|
2017-12-10 08:43:51 +01:00
|
|
|
|
|
|
|
scrollTocToActive();
|
|
|
|
updateProgress();
|
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-11-19 13:20:59 +01:00
|
|
|
|
2017-11-20 21:54:55 +01:00
|
|
|
$("body").toggleClass("dark-theme", settings.theme === "dark");
|
2017-11-19 13:20:59 +01:00
|
|
|
|
|
|
|
kthoom.setSettings();
|
|
|
|
kthoom.saveSettings();
|
2017-09-17 12:36:32 +02:00
|
|
|
}
|
|
|
|
|
2017-12-10 08:43:51 +01:00
|
|
|
function updateProgress(loadPercentage) {
|
2021-04-17 21:25:45 +02:00
|
|
|
if (settings.direction === 0) {
|
|
|
|
$("#progress .bar-read")
|
|
|
|
.removeClass("from-right")
|
|
|
|
.addClass("from-left");
|
|
|
|
$("#progress .bar-load")
|
|
|
|
.removeClass("from-right")
|
|
|
|
.addClass("from-left");
|
|
|
|
} else {
|
|
|
|
$("#progress .bar-read")
|
|
|
|
.removeClass("from-left")
|
|
|
|
.addClass("from-right");
|
|
|
|
$("#progress .bar-load")
|
|
|
|
.removeClass("from-left")
|
|
|
|
.addClass("from-right");
|
|
|
|
}
|
|
|
|
|
2017-12-10 08:43:51 +01:00
|
|
|
// Set the load/unzip progress if it's passed in
|
|
|
|
if (loadPercentage) {
|
|
|
|
$("#progress .bar-load").css({ width: loadPercentage + "%" });
|
|
|
|
|
|
|
|
if (loadPercentage === 100) {
|
|
|
|
$("#progress")
|
2019-06-17 19:48:17 +02:00
|
|
|
.removeClass("loading")
|
|
|
|
.find(".load").text("");
|
2017-12-10 08:43:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set page progress bar
|
|
|
|
$("#progress .bar-read").css({ width: totalImages === 0 ? 0 : Math.round((currentImage + 1) / totalImages * 100) + "%"});
|
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-11-19 18:08:55 +01:00
|
|
|
x.fillStyle = "black";
|
2017-11-20 21:54:55 +01:00
|
|
|
x.textAlign = "center";
|
2017-11-19 18:08:55 +01:00
|
|
|
x.font = "24px sans-serif";
|
2017-09-17 15:33:22 +02:00
|
|
|
x.strokeStyle = "black";
|
2017-11-20 21:54:55 +01:00
|
|
|
x.fillText("Loading Page #" + (currentImage + 1), innerWidth / 2, 100);
|
2017-09-17 14:51:17 +02:00
|
|
|
} else {
|
2017-11-19 18:08:55 +01:00
|
|
|
if (url === "error") {
|
2017-09-17 14:51:17 +02:00
|
|
|
updateScale(true);
|
2017-11-19 18:08:55 +01:00
|
|
|
canvas.width = innerWidth - 100;
|
|
|
|
canvas.height = 200;
|
|
|
|
x.fillStyle = "black";
|
2017-11-20 21:54:55 +01:00
|
|
|
x.textAlign = "center";
|
2017-11-19 18:08:55 +01:00
|
|
|
x.font = "24px sans-serif";
|
2017-09-17 15:33:22 +02:00
|
|
|
x.strokeStyle = "black";
|
2017-11-20 21:54:55 +01:00
|
|
|
x.fillText("Unable to decompress image #" + (currentImage + 1), innerWidth / 2, 100);
|
2017-11-19 18:08:55 +01:00
|
|
|
} else {
|
|
|
|
if ($("body").css("scrollHeight") / innerHeight > 1) {
|
|
|
|
$("body").css("overflowY", "scroll");
|
2017-09-17 14:51:17 +02:00
|
|
|
}
|
|
|
|
|
2017-11-19 18:08:55 +01:00
|
|
|
var img = new Image();
|
|
|
|
img.onerror = function() {
|
|
|
|
canvas.width = innerWidth - 100;
|
|
|
|
canvas.height = 300;
|
|
|
|
updateScale(true);
|
|
|
|
x.fillStyle = "black";
|
|
|
|
x.font = "50px sans-serif";
|
|
|
|
x.strokeStyle = "black";
|
|
|
|
x.fillText("Page #" + (currentImage + 1) + " (" +
|
2017-11-20 21:54:55 +01:00
|
|
|
imageFiles[currentImage].filename + ")", innerWidth / 2, 100);
|
2017-11-19 18:08:55 +01:00
|
|
|
x.fillStyle = "black";
|
2017-11-20 21:54:55 +01:00
|
|
|
x.fillText("Is corrupt or not an image", innerWidth / 2, 200);
|
2017-11-19 18:08:55 +01:00
|
|
|
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
if (/(html|htm)$/.test(imageFiles[currentImage].filename)) {
|
|
|
|
xhr.open("GET", url, true);
|
|
|
|
xhr.onload = function() {
|
|
|
|
$("#mainText").css("display", "");
|
|
|
|
$("#mainText").innerHTML("<iframe style=\"width:100%;height:700px;border:0\" src=\"data:text/html," + escape(xhr.responseText) + "\"></iframe>");
|
2019-06-17 19:48:17 +02:00
|
|
|
};
|
2017-11-19 18:08:55 +01:00
|
|
|
xhr.send(null);
|
2020-12-17 16:53:24 +01:00
|
|
|
} else if (!/(jpg|jpeg|png|gif|webp)$/.test(imageFiles[currentImage].filename) && imageFiles[currentImage].data.uncompressedSize < 10 * 1024) {
|
2017-11-19 18:08:55 +01:00
|
|
|
xhr.open("GET", url, true);
|
|
|
|
xhr.onload = function() {
|
|
|
|
$("#mainText").css("display", "");
|
|
|
|
$("#mainText").innerText(xhr.responseText);
|
|
|
|
};
|
|
|
|
xhr.send(null);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
img.onload = function() {
|
|
|
|
var h = img.height,
|
|
|
|
w = img.width,
|
|
|
|
sw = w,
|
|
|
|
sh = h;
|
|
|
|
settings.rotateTimes = (4 + settings.rotateTimes) % 4;
|
|
|
|
x.save();
|
|
|
|
if (settings.rotateTimes % 2 === 1) {
|
|
|
|
sh = w;
|
|
|
|
sw = h;
|
|
|
|
}
|
|
|
|
canvas.height = sh;
|
|
|
|
canvas.width = sw;
|
|
|
|
x.translate(sw / 2, sh / 2);
|
|
|
|
x.rotate(Math.PI / 2 * settings.rotateTimes);
|
|
|
|
x.translate(-w / 2, -h / 2);
|
|
|
|
if (settings.vflip) {
|
|
|
|
x.scale(1, -1);
|
|
|
|
x.translate(0, -h);
|
|
|
|
}
|
|
|
|
if (settings.hflip) {
|
|
|
|
x.scale(-1, 1);
|
|
|
|
x.translate(-w, 0);
|
|
|
|
}
|
|
|
|
canvas.style.display = "none";
|
|
|
|
scrollTo(0, 0);
|
|
|
|
x.drawImage(img, 0, 0);
|
|
|
|
|
2017-11-21 12:46:03 +01:00
|
|
|
updateScale(false);
|
2017-09-17 14:51:17 +02:00
|
|
|
|
2017-11-19 18:08:55 +01:00
|
|
|
canvas.style.display = "";
|
|
|
|
$("body").css("overflowY", "");
|
|
|
|
x.restore();
|
|
|
|
};
|
|
|
|
img.src = url;
|
|
|
|
}
|
2017-09-17 14:51:17 +02:00
|
|
|
}
|
2017-09-17 12:36:32 +02:00
|
|
|
}
|
|
|
|
|
2019-04-21 11:20:15 +02:00
|
|
|
function showLeftPage() {
|
|
|
|
if (settings.direction === 0) {
|
2019-06-17 19:48:17 +02:00
|
|
|
showPrevPage();
|
2019-04-21 11:20:15 +02:00
|
|
|
} else {
|
2019-06-17 19:48:17 +02:00
|
|
|
showNextPage();
|
2019-04-21 11:20:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function showRightPage() {
|
|
|
|
if (settings.direction === 0) {
|
2019-06-17 19:48:17 +02:00
|
|
|
showNextPage();
|
2019-04-21 11:20:15 +02:00
|
|
|
} else {
|
2019-06-17 19:48:17 +02:00
|
|
|
showPrevPage();
|
2019-04-21 11:20:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-11-19 13:20:59 +01:00
|
|
|
var maxheight = innerHeight - 50;
|
|
|
|
|
2017-11-21 12:46:03 +01:00
|
|
|
if (!clear) {
|
2017-11-22 22:08:29 +01:00
|
|
|
switch (settings.fitMode) {
|
2017-11-21 12:46:03 +01:00
|
|
|
case kthoom.Key.B:
|
|
|
|
mainImageStyle.maxWidth = "100%";
|
|
|
|
mainImageStyle.maxHeight = maxheight + "px";
|
|
|
|
break;
|
|
|
|
case kthoom.Key.H:
|
|
|
|
mainImageStyle.height = maxheight + "px";
|
|
|
|
break;
|
|
|
|
case kthoom.Key.W:
|
|
|
|
mainImageStyle.width = "100%";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-09-17 14:51:17 +02:00
|
|
|
}
|
2017-11-20 21:54:55 +01:00
|
|
|
$("#mainContent").css({maxHeight: maxheight + 5});
|
2017-11-19 13:20:59 +01:00
|
|
|
kthoom.setSettings();
|
2017-09-17 14:51:17 +02:00
|
|
|
kthoom.saveSettings();
|
2017-09-17 12:36:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function keyHandler(evt) {
|
2017-12-10 08:43:51 +01:00
|
|
|
var hasModifier = evt.ctrlKey || evt.shiftKey || evt.metaKey;
|
|
|
|
switch (evt.keyCode) {
|
2017-09-17 14:51:17 +02:00
|
|
|
case kthoom.Key.LEFT:
|
2017-12-10 08:43:51 +01:00
|
|
|
if (hasModifier) break;
|
2019-04-21 11:20:15 +02:00
|
|
|
showLeftPage();
|
2017-09-17 14:51:17 +02:00
|
|
|
break;
|
|
|
|
case kthoom.Key.RIGHT:
|
2017-12-10 08:43:51 +01:00
|
|
|
if (hasModifier) break;
|
2019-04-21 11:20:15 +02:00
|
|
|
showRightPage();
|
2017-09-17 14:51:17 +02:00
|
|
|
break;
|
|
|
|
case kthoom.Key.L:
|
2017-12-10 08:43:51 +01:00
|
|
|
if (hasModifier) break;
|
2017-11-19 13:20:59 +01:00
|
|
|
settings.rotateTimes--;
|
|
|
|
if (settings.rotateTimes < 0) {
|
|
|
|
settings.rotateTimes = 3;
|
2017-09-17 14:51:17 +02:00
|
|
|
}
|
|
|
|
updatePage();
|
|
|
|
break;
|
|
|
|
case kthoom.Key.R:
|
2017-12-10 08:43:51 +01:00
|
|
|
if (hasModifier) break;
|
2017-11-19 13:20:59 +01:00
|
|
|
settings.rotateTimes++;
|
|
|
|
if (settings.rotateTimes > 3) {
|
|
|
|
settings.rotateTimes = 0;
|
2017-09-17 14:51:17 +02:00
|
|
|
}
|
|
|
|
updatePage();
|
|
|
|
break;
|
|
|
|
case kthoom.Key.F:
|
2017-12-10 08:43:51 +01:00
|
|
|
if (hasModifier) break;
|
2017-11-19 13:20:59 +01:00
|
|
|
if (!settings.hflip && !settings.vflip) {
|
|
|
|
settings.hflip = true;
|
|
|
|
} else if (settings.hflip === true && settings.vflip === true) {
|
|
|
|
settings.vflip = false;
|
|
|
|
settings.hflip = false;
|
|
|
|
} else if (settings.hflip === true) {
|
|
|
|
settings.vflip = true;
|
|
|
|
settings.hflip = false;
|
|
|
|
} else if (settings.vflip === true) {
|
|
|
|
settings.hflip = true;
|
2017-09-17 14:51:17 +02:00
|
|
|
}
|
|
|
|
updatePage();
|
|
|
|
break;
|
|
|
|
case kthoom.Key.W:
|
2017-12-10 08:43:51 +01:00
|
|
|
if (hasModifier) break;
|
2017-11-19 13:20:59 +01:00
|
|
|
settings.fitMode = kthoom.Key.W;
|
2017-11-21 12:46:03 +01:00
|
|
|
updateScale(false);
|
2017-09-17 14:51:17 +02:00
|
|
|
break;
|
|
|
|
case kthoom.Key.H:
|
2017-12-10 08:43:51 +01:00
|
|
|
if (hasModifier) break;
|
2017-11-19 13:20:59 +01:00
|
|
|
settings.fitMode = kthoom.Key.H;
|
2017-11-21 12:46:03 +01:00
|
|
|
updateScale(false);
|
2017-09-17 14:51:17 +02:00
|
|
|
break;
|
|
|
|
case kthoom.Key.B:
|
2017-12-10 08:43:51 +01:00
|
|
|
if (hasModifier) break;
|
2017-11-19 13:20:59 +01:00
|
|
|
settings.fitMode = kthoom.Key.B;
|
2017-11-21 12:46:03 +01:00
|
|
|
updateScale(false);
|
2017-09-17 14:51:17 +02:00
|
|
|
break;
|
|
|
|
case kthoom.Key.N:
|
2017-12-10 08:43:51 +01:00
|
|
|
if (hasModifier) break;
|
2017-11-19 13:20:59 +01:00
|
|
|
settings.fitMode = kthoom.Key.N;
|
2017-11-21 12:46:03 +01:00
|
|
|
updateScale(false);
|
2017-09-17 14:51:17 +02:00
|
|
|
break;
|
2017-12-10 08:43:51 +01:00
|
|
|
case kthoom.Key.SPACE:
|
2019-06-17 19:48:17 +02:00
|
|
|
var container = $("#mainContent");
|
2021-04-17 21:25:45 +02:00
|
|
|
// var atTop = container.scrollTop() === 0;
|
|
|
|
// var atBottom = container.scrollTop() >= container[0].scrollHeight - container.height();
|
2017-12-10 08:43:51 +01:00
|
|
|
|
2021-04-17 21:25:45 +02:00
|
|
|
if (evt.shiftKey) {
|
2017-12-10 08:43:51 +01:00
|
|
|
evt.preventDefault();
|
|
|
|
// If it's Shift + Space and the container is at the top of the page
|
2021-04-17 20:20:41 +02:00
|
|
|
showPrevPage();
|
2021-04-17 21:25:45 +02:00
|
|
|
} else {
|
2017-12-10 08:43:51 +01:00
|
|
|
evt.preventDefault();
|
|
|
|
// If you're at the bottom of the page and you only pressed space
|
2021-04-17 20:20:41 +02:00
|
|
|
showNextPage();
|
2017-12-10 08:43:51 +01:00
|
|
|
}
|
2017-09-17 14:51:17 +02:00
|
|
|
break;
|
|
|
|
default:
|
2017-12-10 08:43:51 +01:00
|
|
|
//console.log('KeyCode', evt.keyCode);
|
2017-09-17 14:51:17 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-09-17 12:36:32 +02:00
|
|
|
}
|
|
|
|
|
2018-08-31 10:47:58 +02:00
|
|
|
/*function ImageLoadCallback() {
|
2017-11-21 12:46:03 +01:00
|
|
|
var jso = this.response;
|
2017-11-19 18:08:55 +01:00
|
|
|
// Unable to decompress file, or no response from server
|
2017-11-20 21:54:55 +01:00
|
|
|
if (jso === null) {
|
2017-11-19 18:08:55 +01:00
|
|
|
setImage("error");
|
2017-09-17 14:51:17 +02:00
|
|
|
} else {
|
2017-12-10 08:43:51 +01:00
|
|
|
// IE 11 sometimes sees the response as a string
|
|
|
|
if (typeof jso !== "object") {
|
|
|
|
jso = JSON.parse(jso);
|
|
|
|
}
|
|
|
|
|
2017-11-21 12:46:03 +01:00
|
|
|
if (jso.page !== jso.last) {
|
|
|
|
this.open("GET", this.fileid + "/" + (jso.page + 1));
|
|
|
|
this.addEventListener("load", ImageLoadCallback);
|
2017-11-19 18:08:55 +01:00
|
|
|
this.send();
|
|
|
|
}
|
2017-12-10 08:43:51 +01:00
|
|
|
|
2017-11-19 18:08:55 +01:00
|
|
|
loadFromArrayBuffer(jso);
|
2017-09-17 14:51:17 +02:00
|
|
|
}
|
2018-08-31 10:47:58 +02:00
|
|
|
}*/
|
|
|
|
function init(filename) {
|
2017-11-18 10:34:21 +01:00
|
|
|
var request = new XMLHttpRequest();
|
2018-08-31 10:47:58 +02:00
|
|
|
request.open("GET", filename);
|
|
|
|
request.responseType = "arraybuffer";
|
|
|
|
request.setRequestHeader("X-Test", "test1");
|
|
|
|
request.setRequestHeader("X-Test", "test2");
|
|
|
|
request.addEventListener("load", function(event) {
|
|
|
|
if (request.status >= 200 && request.status < 300) {
|
|
|
|
loadFromArrayBuffer(request.response);
|
|
|
|
} else {
|
|
|
|
console.warn(request.statusText, request.responseText);
|
|
|
|
}
|
|
|
|
});
|
2017-11-18 10:34:21 +01:00
|
|
|
request.send();
|
2017-12-10 14:02:49 +01:00
|
|
|
initProgressClick();
|
2017-11-18 10:34:21 +01:00
|
|
|
document.body.className += /AppleWebKit/.test(navigator.userAgent) ? " webkit" : "";
|
|
|
|
kthoom.loadSettings();
|
2017-11-20 21:54:55 +01:00
|
|
|
updateScale(true);
|
2017-12-10 08:43:51 +01:00
|
|
|
|
2017-11-18 10:34:21 +01:00
|
|
|
$(document).keydown(keyHandler);
|
|
|
|
|
|
|
|
$(window).resize(function() {
|
2017-11-21 12:46:03 +01:00
|
|
|
updateScale(false);
|
2017-11-18 10:34:21 +01:00
|
|
|
});
|
|
|
|
|
2017-11-20 21:54:55 +01:00
|
|
|
// Open TOC menu
|
2017-11-21 12:46:03 +01:00
|
|
|
$("#slider").click(function() {
|
2017-11-20 21:54:55 +01:00
|
|
|
$("#sidebar").toggleClass("open");
|
|
|
|
$("#main").toggleClass("closed");
|
|
|
|
$(this).toggleClass("icon-menu icon-right");
|
2017-12-10 08:43:51 +01:00
|
|
|
|
|
|
|
// We need this in a timeout because if we call it during the CSS transition, IE11 shakes the page ¯\_(ツ)_/¯
|
2019-06-17 19:48:17 +02:00
|
|
|
setTimeout(function() {
|
2017-12-10 08:43:51 +01:00
|
|
|
// Focus on the TOC or the main content area, depending on which is open
|
2019-06-17 19:48:17 +02:00
|
|
|
$("#main:not(.closed) #mainContent, #sidebar.open #tocView").focus();
|
2017-12-10 08:43:51 +01:00
|
|
|
scrollTocToActive();
|
|
|
|
}, 500);
|
2017-11-20 21:54:55 +01:00
|
|
|
});
|
2017-11-19 13:20:59 +01:00
|
|
|
|
2017-11-20 21:54:55 +01:00
|
|
|
// Open Settings modal
|
2017-11-21 12:46:03 +01:00
|
|
|
$("#setting").click(function() {
|
2017-11-20 21:54:55 +01:00
|
|
|
$("#settings-modal").toggleClass("md-show");
|
|
|
|
});
|
2017-11-19 13:20:59 +01:00
|
|
|
|
2017-11-20 21:54:55 +01:00
|
|
|
// On Settings input change
|
2017-11-21 12:46:03 +01:00
|
|
|
$("#settings input").on("change", function() {
|
2017-11-20 21:54:55 +01:00
|
|
|
// Get either the checked boolean or the assigned value
|
|
|
|
var value = this.type === "checkbox" ? this.checked : this.value;
|
2017-09-17 14:51:17 +02:00
|
|
|
|
2017-11-20 21:54:55 +01:00
|
|
|
// If it's purely numeric, parse it to an integer
|
|
|
|
value = /^\d+$/.test(value) ? parseInt(value) : value;
|
2017-11-19 13:20:59 +01:00
|
|
|
|
2017-11-20 21:54:55 +01:00
|
|
|
settings[this.name] = value;
|
|
|
|
updatePage();
|
2017-11-21 12:46:03 +01:00
|
|
|
updateScale(false);
|
2017-11-20 21:54:55 +01:00
|
|
|
});
|
2017-11-19 13:20:59 +01:00
|
|
|
|
2017-11-20 21:54:55 +01:00
|
|
|
// Close modal
|
2017-11-21 12:46:03 +01:00
|
|
|
$(".closer, .overlay").click(function() {
|
2017-11-20 21:54:55 +01:00
|
|
|
$(".md-show").removeClass("md-show");
|
|
|
|
});
|
|
|
|
|
|
|
|
// TOC thumbnail pagination
|
2017-11-21 12:46:03 +01:00
|
|
|
$("#thumbnails").on("click", "a", function() {
|
2017-11-20 21:54:55 +01:00
|
|
|
currentImage = $(this).data("page") - 1;
|
|
|
|
updatePage();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Fullscreen mode
|
|
|
|
if (typeof screenfull !== "undefined") {
|
2017-11-22 22:08:29 +01:00
|
|
|
$("#fullscreen").click(function() {
|
2017-11-21 12:46:03 +01:00
|
|
|
screenfull.toggle($("#container")[0]);
|
2017-09-17 14:51:17 +02:00
|
|
|
});
|
|
|
|
|
2017-11-20 21:54:55 +01:00
|
|
|
if (screenfull.raw) {
|
|
|
|
var $button = $("#fullscreen");
|
2017-11-21 12:46:03 +01:00
|
|
|
document.addEventListener(screenfull.raw.fullscreenchange, function() {
|
2017-11-20 21:54:55 +01:00
|
|
|
screenfull.isFullscreen
|
2017-11-21 12:46:03 +01:00
|
|
|
? $button.addClass("icon-resize-small").removeClass("icon-resize-full")
|
|
|
|
: $button.addClass("icon-resize-full").removeClass("icon-resize-small");
|
2017-11-19 13:20:59 +01:00
|
|
|
});
|
|
|
|
}
|
2017-11-20 21:54:55 +01:00
|
|
|
}
|
2017-11-19 13:20:59 +01:00
|
|
|
|
2017-12-10 08:43:51 +01:00
|
|
|
// Focus the scrollable area so that keyboard scrolling work as expected
|
2019-06-17 19:48:17 +02:00
|
|
|
$("#mainContent").focus();
|
2017-12-10 08:43:51 +01:00
|
|
|
|
2021-01-24 11:16:57 +01:00
|
|
|
$("#mainContent").swipe( {
|
|
|
|
swipeRight:function() {
|
|
|
|
showLeftPage();
|
|
|
|
},
|
|
|
|
swipeLeft:function() {
|
|
|
|
showRightPage();
|
|
|
|
},
|
|
|
|
});
|
2017-11-18 10:34:21 +01:00
|
|
|
$("#mainImage").click(function(evt) {
|
|
|
|
// Firefox does not support offsetX/Y so we have to manually calculate
|
|
|
|
// where the user clicked in the image.
|
|
|
|
var mainContentWidth = $("#mainContent").width();
|
|
|
|
var mainContentHeight = $("#mainContent").height();
|
|
|
|
var comicWidth = evt.target.clientWidth;
|
|
|
|
var comicHeight = evt.target.clientHeight;
|
|
|
|
var offsetX = (mainContentWidth - comicWidth) / 2;
|
|
|
|
var offsetY = (mainContentHeight - comicHeight) / 2;
|
2017-11-22 22:08:29 +01:00
|
|
|
var clickX = evt.offsetX ? evt.offsetX : (evt.clientX - offsetX);
|
|
|
|
var clickY = evt.offsetY ? evt.offsetY : (evt.clientY - offsetY);
|
2017-11-18 10:34:21 +01:00
|
|
|
|
|
|
|
// Determine if the user clicked/tapped the left side or the
|
|
|
|
// right side of the page.
|
2019-04-21 11:20:15 +02:00
|
|
|
var clickedLeft = false;
|
2017-11-20 21:54:55 +01:00
|
|
|
switch (settings.rotateTimes) {
|
2017-09-17 14:51:17 +02:00
|
|
|
case 0:
|
2019-04-21 11:20:15 +02:00
|
|
|
clickedLeft = clickX < (comicWidth / 2);
|
2017-11-20 21:54:55 +01:00
|
|
|
break;
|
2017-09-17 14:51:17 +02:00
|
|
|
case 1:
|
2019-04-21 11:20:15 +02:00
|
|
|
clickedLeft = clickY < (comicHeight / 2);
|
2017-11-20 21:54:55 +01:00
|
|
|
break;
|
2017-09-17 14:51:17 +02:00
|
|
|
case 2:
|
2019-04-21 11:20:15 +02:00
|
|
|
clickedLeft = clickX > (comicWidth / 2);
|
2017-11-20 21:54:55 +01:00
|
|
|
break;
|
2017-09-17 14:51:17 +02:00
|
|
|
case 3:
|
2019-04-21 11:20:15 +02:00
|
|
|
clickedLeft = clickY > (comicHeight / 2);
|
2017-11-20 21:54:55 +01:00
|
|
|
break;
|
2017-11-18 10:34:21 +01:00
|
|
|
}
|
2019-04-21 11:20:15 +02:00
|
|
|
if (clickedLeft) {
|
|
|
|
showLeftPage();
|
2017-11-18 10:34:21 +01:00
|
|
|
} else {
|
2019-04-21 11:20:15 +02:00
|
|
|
showRightPage();
|
2017-11-18 10:34:21 +01:00
|
|
|
}
|
|
|
|
});
|
2017-09-17 12:36:32 +02:00
|
|
|
}
|
2018-08-31 10:47:58 +02:00
|
|
|
|