mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-14 18:52:05 +03:00
f0432ba82c
refs https://github.com/TryGhost/Ghost/issues/14101 - extracted the ratio zoom handling to a `{{ratio-zoom}}` modifier to clean up the component and avoid needing lifecycle hooks that don't exist in Glimmer components - disabled `no-nested-interactive` linting in the template - not ideal but we'd need a much bigger design refactor to eliminate the nested links
69 lines
1.7 KiB
JavaScript
69 lines
1.7 KiB
JavaScript
import Modifier from 'ember-modifier';
|
|
import {bind, throttle} from '@ember/runloop';
|
|
|
|
export default class RatioZoom extends Modifier {
|
|
resizeHandler = null;
|
|
|
|
didReceiveArguments() {
|
|
const {zoomed} = this.args.named;
|
|
|
|
if (zoomed) {
|
|
this.setZoomedSize();
|
|
}
|
|
}
|
|
|
|
willDestroy() {
|
|
this.removeResizeEventListener();
|
|
}
|
|
|
|
setZoomedSize() {
|
|
const {ratio} = this.args.named;
|
|
|
|
this.element.style.width = '100%';
|
|
this.element.style.height = '100%';
|
|
|
|
const offsets = this.element.getBoundingClientRect();
|
|
|
|
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;
|
|
}
|
|
|
|
this.element.style.width = `${usableSize.width}px`;
|
|
this.element.style.height = `${usableSize.height}px`;
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|