Created
March 29, 2020 12:56
-
-
Save ondrasak/05b338b34b2848bbbfbb9fbafa0d180c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import {fullscreenChangeEvents} from 'shared/utils/fullscreen'; | |
| type IframeResizerOptions = { | |
| iframeElement: HTMLElement, | |
| containerElement: HTMLElement | |
| }; | |
| export class IframeResizer { | |
| private originalWidth: number; | |
| private originalHeight: number; | |
| private originalRatio: number; | |
| constructor(private options: IframeResizerOptions) { | |
| // TODO: if height/width is not defined by element attr -> pick real measurement directly from DOM | |
| this.originalWidth = Number(options.iframeElement.getAttribute('width')); | |
| this.originalHeight = Number(options.iframeElement.getAttribute('height')); | |
| this.originalRatio = this.originalWidth / this.originalHeight; | |
| this.resize(); | |
| // Register listeners | |
| window.addEventListener('orientationchange', this.onScreenChange); | |
| window.addEventListener('resize', this.onScreenChange); | |
| $(document).on(fullscreenChangeEvents.join(' '), this.onScreenChange); | |
| } | |
| onScreenChange = () => { | |
| this.resize(); | |
| } | |
| resize() { | |
| const containerWidth = this.options.containerElement.clientWidth; | |
| this.options.iframeElement.setAttribute('width', `${containerWidth}px`); | |
| this.options.iframeElement.setAttribute('height', `${containerWidth / this.originalRatio}px`); | |
| } | |
| destroy() { | |
| // Unregister listeners | |
| window.removeEventListener('orientationchange', this.onScreenChange); | |
| window.removeEventListener('resize', this.onScreenChange); | |
| $(document).off(fullscreenChangeEvents.join(' '), this.onScreenChange); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment