2022-02-08 13:13:18 +03:00
|
|
|
import Modifier from 'ember-modifier';
|
|
|
|
import {bind, throttle} from '@ember/runloop';
|
2022-08-02 11:23:43 +03:00
|
|
|
import {registerDestructor} from '@ember/destroyable';
|
2022-02-08 13:13:18 +03:00
|
|
|
|
|
|
|
export default class RatioZoom extends Modifier {
|
|
|
|
resizeHandler = null;
|
|
|
|
|
2022-08-02 11:23:43 +03:00
|
|
|
constructor(owner, args) {
|
|
|
|
super(owner, args);
|
|
|
|
registerDestructor(this, this.cleanup);
|
|
|
|
}
|
2022-02-08 13:13:18 +03:00
|
|
|
|
2022-08-02 11:23:43 +03:00
|
|
|
modify(element, positional, {zoomed, ratio}) {
|
2022-02-08 13:13:18 +03:00
|
|
|
if (zoomed) {
|
2022-08-02 11:23:43 +03:00
|
|
|
this.setZoomedSize(element, {ratio});
|
2022-02-08 13:13:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-02 11:23:43 +03:00
|
|
|
cleanup = () => {
|
2022-02-08 13:13:18 +03:00
|
|
|
this.removeResizeEventListener();
|
2022-08-02 11:23:43 +03:00
|
|
|
};
|
2022-02-08 13:13:18 +03:00
|
|
|
|
2022-08-02 11:23:43 +03:00
|
|
|
setZoomedSize(element, {ratio}) {
|
|
|
|
element.style.width = '100%';
|
|
|
|
element.style.height = '100%';
|
2022-02-08 13:13:18 +03:00
|
|
|
|
2022-08-02 11:23:43 +03:00
|
|
|
const offsets = element.getBoundingClientRect();
|
2022-02-08 13:13:18 +03:00
|
|
|
|
|
|
|
let maxHeight = {
|
|
|
|
width: offsets.height / ratio,
|
|
|
|
height: offsets.height
|
|
|
|
};
|
|
|
|
|
|
|
|
let maxWidth = {
|
|
|
|
width: offsets.width,
|
|
|
|
height: offsets.width * ratio
|
|
|
|
};
|
|
|
|
|
|
|
|
let usableSize = null;
|
|
|
|
|
|
|
|
if (ratio <= 1) {
|
|
|
|
usableSize = maxWidth.height > offsets.height ? maxHeight : maxWidth;
|
|
|
|
} else {
|
|
|
|
usableSize = maxHeight.width > offsets.width ? maxWidth : maxHeight;
|
|
|
|
}
|
|
|
|
|
2022-08-02 11:23:43 +03:00
|
|
|
element.style.width = `${usableSize.width}px`;
|
|
|
|
element.style.height = `${usableSize.height}px`;
|
2022-02-08 13:13:18 +03:00
|
|
|
|
|
|
|
this.addResizeEventListener();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleResize() {
|
|
|
|
throttle(this, this.setZoomedSize, 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
addResizeEventListener() {
|
|
|
|
if (!this.resizeHandler) {
|
|
|
|
this.resizeHandler = bind(this, this.handleResize);
|
|
|
|
window.addEventListener('resize', this.resizeHandler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
removeResizeEventListener() {
|
|
|
|
if (this.resizeHandler) {
|
|
|
|
window.removeEventListener('resize', this.resizeHandler);
|
|
|
|
this.resizeHandler = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|