Files
fnx_web/res/include/script/file_viewer/viewer_scripts/ImageViewer.js

91 lines
2.2 KiB
JavaScript
Raw Normal View History

2020-01-20 19:55:51 +01:00
class ImageViewer {
2020-01-21 15:33:09 +01:00
constructor(viewer, file) {let v = this;
2020-01-22 19:48:58 +01:00
v.viewer = viewer;
v.file = file;
v.zoomed = false;
v.x = 0;
v.y = 0;
v.dragging = false;
2020-01-20 19:55:51 +01:00
2020-01-21 15:33:09 +01:00
v.container = document.createElement("dv");
v.container.classList = "image-container";
// v.container.style.lineHeight = "0";
2020-01-20 19:55:51 +01:00
2020-01-21 15:33:09 +01:00
v.element = document.createElement("img");
v.element.classList = "pannable center drop-shadow";
v.element.src = apiEndpoint+"/file/"+v.file.id;
v.element.addEventListener("dblclick", (e) => { return v.doubleclick(e); });
v.element.addEventListener("doubletap", (e) => { return v.doubleclick(e); });
v.element.addEventListener("mousedown", (e) => { return v.mousedown(e); });
document.addEventListener("mousemove", (e) => { return v.mousemove(e); });
document.addEventListener("mouseup", (e) => { return v.mouseup(e); });
2020-01-20 19:55:51 +01:00
2020-01-21 15:33:09 +01:00
v.container.appendChild(v.element);
2020-01-20 19:55:51 +01:00
}
2020-01-21 15:33:09 +01:00
render(parent) {let v = this;
parent.appendChild(v.container);
2020-01-20 19:55:51 +01:00
}
2020-01-21 15:33:09 +01:00
doubleclick(e) {let v = this;
if (v.zoomed) {
v.element.style.maxWidth = "100%";
v.element.style.maxHeight = "100%";
v.element.style.top = "50%";
v.element.style.left = "auto";
v.element.style.transform = "translateY(-50%)";
v.container.style.overflow = "hidden";
v.zoomed = false;
2020-01-20 19:55:51 +01:00
} else {
2020-01-21 15:33:09 +01:00
v.element.style.maxWidth = "none";
v.element.style.maxHeight = "none";
v.element.style.top = "0";
v.element.style.left = "";
v.element.style.transform = "none";
v.container.style.overflow = "scroll";
v.zoomed = true;
2020-01-20 19:55:51 +01:00
}
e.preventDefault();
e.stopPropagation();
return false;
}
2020-01-21 15:33:09 +01:00
mousedown(e) {let v = this;
if (!v.dragging && e.which === 1 && v.zoomed) {
v.x = e.pageX;
v.y = e.pageY;
v.dragging = true;
2020-01-20 19:55:51 +01:00
e.preventDefault();
e.stopPropagation();
return false;
}
}
2020-01-21 15:33:09 +01:00
mousemove(e) {let v = this;
if (v.dragging) {
v.container.scrollLeft = v.container.scrollLeft - (e.pageX - v.x);
v.container.scrollTop = v.container.scrollTop - (e.pageY - v.y);
2020-01-20 19:55:51 +01:00
2020-01-21 15:33:09 +01:00
v.x = e.pageX;
v.y = e.pageY;
2020-01-20 19:55:51 +01:00
e.preventDefault();
e.stopPropagation();
return false;
}
}
2020-01-21 15:33:09 +01:00
mouseup(e) {let v = this;
if (v.dragging) {
v.dragging = false;
2020-01-20 19:55:51 +01:00
e.preventDefault();
e.stopPropagation();
return false;
}
}
}