import React, { createRef } from "react";
import path from "path";
import ReactCSSTransitionGroup from "react-addons-css-transition-group";
import { MenuBar } from "./menubar";
import { Bundle, Icon, NgIf, Loader, EventEmitter, EventReceiver } from "../../components/";
import { alert, randomString } from "../../helpers/";
import { Pager } from "./pager";
import { t } from "../../locales/";
import "./imageviewer.scss";
import "./pager.scss";
const SmallExif = (props) => (
{(Comp) => }
);
const LargeExif = (props) => (
{(Comp) => }
);
export class ImageViewerComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
preload: null,
_: null,
show_exif: false,
is_loaded: false,
draggable: true,
};
this.shortcut= (e) => {
if (e.keyCode === 27) this.setState({ show_exif: false });
else if (e.keyCode === 73) this.setState({ show_exif: !this.state.show_exif });
};
this.refresh = () => this.setState({ "_": randomString() });
this.$container = createRef();
}
componentDidMount() {
this.props.subscribe("media::preload", (preload) => {
this.setState({ preload: preload });
});
document.addEventListener("webkitfullscreenchange", this.refresh);
document.addEventListener("mozfullscreenchange", this.refresh);
document.addEventListener("fullscreenchange", this.refresh);
document.addEventListener("keydown", this.shortcut);
}
componentWillUnmount() {
this.props.unsubscribe("media::preload");
document.removeEventListener("webkitfullscreenchange", this.refresh);
document.removeEventListener("mozfullscreenchange", this.refresh);
document.removeEventListener("fullscreenchange", this.refresh);
document.removeEventListener("keydown", this.shortcut);
}
UNSAFE_componentWillReceiveProps(props) {
if (props.data !== this.props.data) {
this.setState({ is_loaded: false });
}
}
toggleExif() {
if (window.innerWidth < 580) {
alert.now();
} else {
this.setState({
show_exif: !this.state.show_exif,
});
}
}
requestFullScreen() {
if ("webkitRequestFullscreen" in document.body) {
this.$container.current.webkitRequestFullscreen();
} else if ("mozRequestFullScreen" in document.body) {
this.$container.current.mozRequestFullScreen();
}
}
render() {
const hasExif = (filename) => {
const ext = path.extname(filename).toLowerCase().substring(1);
return ["jpg", "jpeg", "tiff", "tif"].indexOf(ext) !== -1;
};
return (
this.setState({ is_loaded: true })}
url={this.props.data} />
this.setState({ draggable: files.length > 1 ? true : false })}
next={(e) => this.setState({ preload: e })} />
);
}
}
export const ImageViewer = EventReceiver(EventEmitter(ImageViewerComponent));
class ImageFancyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
isLoading: true,
isError: false,
drag_init: { x: null, t: null },
drag_current: { x: null, t: null },
hasAction: false,
};
this.img = new Image();
this.img.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=";
}
UNSAFE_componentWillReceiveProps(nextProp) {
if (nextProp.url !== this.props.url) {
this.setState({
isLoading: true,
isError: false,
drag_current: { x: 0 },
hasAction: false,
});
}
}
onLoad() {
this.setState({ isLoading: false });
this.props.onLoad();
}
onError(w) {
this.setState({ isError: true });
}
imageDragStart(e) {
const t = new Date();
if (e.touches) {
this.setState({
drag_init: { x: e.touches[0].clientX, t: t },
hasAction: true,
});
} else {
this.setState({
drag_init: { x: e.pageX, t: t },
hasAction: true,
});
}
if (e.dataTransfer) e.dataTransfer.setDragImage(this.img, 0, 0);
}
imageDragEnd(e) {
const drag_end = {
x: function(dragX, touch) {
if (dragX !== null) return dragX;
if (touch && touch[0]) {
return touch[0].clientX;
}
return 0;
}(e.pageX || null, e.changedTouches || null),
t: new Date(),
};
const direction = function(x_current, x_init) {
if (x_current.t - x_init.t > 200 &&
Math.abs(x_current.x - x_init.x) <
(window.innerWidth < 500 ? window.innerWidth / 3 : 250)) {
return "neutral";
}
return x_current.x > x_init.x ? "right" : "left";
}(drag_end, this.state.drag_init);
if (direction === "left") {
return this.setState({
drag_current: { x: - window.innerWidth },
hasAction: false,
}, () => {
this.props.emit("media::next");
});
} else if (direction === "right") {
return this.setState({
drag_current: { x: + window.innerWidth },
hasAction: false,
}, () => {
this.props.emit("media::previous");
});
}
return this.setState({
drag_current: { x: 0 },
hasAction: false,
});
}
imageDrag(e) {
if (e.pageX > 0) {
this.setState({
drag_current: { x: e.pageX - this.state.drag_init.x },
});
} else if (e.touches && e.touches[0].clientX > 0) {
this.setState({
drag_current: { x: e.touches[0].clientX - this.state.drag_init.x },
});
}
}
render() {
if (this.state.isError) {
return (
{ t("Can't load this picture") }
);
}
if (this.state.isLoading) {
return (
);
}
return (
);
}
}
const ImageFancy = EventEmitter(ImageFancyComponent);
function Img({ src, ...props }) {
const image_url = (url, size) => {
return url+"&size="+parseInt(Math.max(window.innerWidth*size, window.innerHeight*size));
};
if (!src) return null;
return (
);
}