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

108 lines
2.7 KiB
JavaScript
Raw Normal View History

2020-01-28 12:51:21 +01:00
function ImageViewer(viewer, file) {
2020-02-04 19:37:46 +01:00
this.viewer = viewer
this.file = file
this.zoomed = false
this.x = 0
this.y = 0
this.dragging = false
2020-01-20 19:55:51 +01:00
2020-03-10 17:06:52 +01:00
this.zoom = 1
2020-02-04 19:37:46 +01:00
this.container = document.createElement("dv")
this.container.classList = "image-container"
2020-01-20 19:55:51 +01:00
2020-02-04 19:37:46 +01:00
this.element = document.createElement("img")
2020-03-10 17:06:52 +01:00
this.element.classList = "pannable center drop_shadow"
2020-02-04 19:37:46 +01:00
this.element.src = this.file.get_href
this.element.addEventListener("dblclick", (e) => { return this.doubleclick(e) })
this.element.addEventListener("doubletap", (e) => { return this.doubleclick(e) })
this.element.addEventListener("mousedown", (e) => { return this.mousedown(e) })
2020-03-10 17:06:52 +01:00
this.element.addEventListener("mousedown", (e) => { return this.mousedown(e) })
this.element.addEventListener("wheel", (e) => { return this.scroll(e) })
2020-02-04 19:37:46 +01:00
document.addEventListener("mousemove", (e) => { return this.mousemove(e) })
document.addEventListener("mouseup", (e) => { return this.mouseup(e) })
2020-01-20 19:55:51 +01:00
2020-02-04 19:37:46 +01:00
this.container.appendChild(this.element)
2020-01-27 16:56:16 +01:00
}
2020-01-20 19:55:51 +01:00
2020-01-28 12:51:21 +01:00
ImageViewer.prototype.render = function(parent) {
2020-02-04 19:37:46 +01:00
parent.appendChild(this.container)
2020-01-27 16:56:16 +01:00
}
2020-03-10 17:06:52 +01:00
ImageViewer.prototype.scroll = function(e) {
if (!this.zoomed) {
this.doubleclick(e)
}
console.log(e.deltaY)
this.zoom = this.zoom - (e.deltaY * 0.01);
if (this.zoom < 1) { this.zoom = 1 }
if (this.zoom > 10) { this.zoom = 10 }
this.element.style.width = this.zoom * 100 + "%"
this.element.style.height = "auto"
this.element.style.maxWidth = "1000%"
this.element.style.maxHeight = "1000%"
e.preventDefault()
e.stopPropagation()
return false
}
2020-01-28 12:51:21 +01:00
ImageViewer.prototype.doubleclick = function(e) {
if (this.zoomed) {
2020-03-10 17:06:52 +01:00
this.element.removeAttribute("style")
this.container.style.overflow = ""
2020-02-04 19:37:46 +01:00
this.zoomed = false
2020-01-27 16:56:16 +01:00
} else {
2020-02-04 19:37:46 +01:00
this.element.style.maxWidth = "none"
this.element.style.maxHeight = "none"
this.element.style.top = "0"
this.element.style.left = ""
this.element.style.transform = "none"
this.container.style.overflow = "scroll"
this.zoomed = true
2020-01-20 19:55:51 +01:00
}
2020-02-04 19:37:46 +01:00
e.preventDefault()
e.stopPropagation()
return false
2020-01-27 16:56:16 +01:00
}
2020-01-28 12:51:21 +01:00
ImageViewer.prototype.mousedown = function(e) {
if (!this.dragging && e.which === 1 && this.zoomed) {
2020-02-04 19:37:46 +01:00
this.x = e.pageX
this.y = e.pageY
this.dragging = true
2020-01-20 19:55:51 +01:00
2020-02-04 19:37:46 +01:00
e.preventDefault()
e.stopPropagation()
return false
2020-01-20 19:55:51 +01:00
}
2020-01-27 16:56:16 +01:00
}
2020-01-20 19:55:51 +01:00
2020-01-28 12:51:21 +01:00
ImageViewer.prototype.mousemove = function(e) {
if (this.dragging) {
2020-02-04 19:37:46 +01:00
this.container.scrollLeft = this.container.scrollLeft - (e.pageX - this.x)
this.container.scrollTop = this.container.scrollTop - (e.pageY - this.y)
2020-01-20 19:55:51 +01:00
2020-02-04 19:37:46 +01:00
this.x = e.pageX
this.y = e.pageY
2020-01-20 19:55:51 +01:00
2020-02-04 19:37:46 +01:00
e.preventDefault()
e.stopPropagation()
return false
2020-01-20 19:55:51 +01:00
}
2020-01-27 16:56:16 +01:00
}
2020-01-28 12:51:21 +01:00
ImageViewer.prototype.mouseup = function(e) {
if (this.dragging) {
2020-02-04 19:37:46 +01:00
this.dragging = false
2020-01-20 19:55:51 +01:00
2020-02-04 19:37:46 +01:00
e.preventDefault()
e.stopPropagation()
return false
2020-01-20 19:55:51 +01:00
}
}