1
0
Fork 0
mirror of https://github.com/TryGhost/Ghost-Admin.git synced 2023-12-14 02:33:04 +01:00
Ghost-Admin/app/components/aspect-ratio-box.js
Kevin Ansfield 1142729697
Added ability to upload integration icons (#1058)
no issue
- add `{{aspect-ratio-box}}` to make it easier to adjust box sizes at a fixed ratio based on container height which isn't possible with CSS directly
- used `{{gh-uploader}}` to add upload facility to the icon shown on the integration screen
2018-10-23 10:29:49 +01:00

51 lines
1.3 KiB
JavaScript

import Component from '@ember/component';
import {assert} from '@ember/debug';
import {debounce, run} from '@ember/runloop';
export default Component.extend({
ratio: '1/1',
base: 'height',
isResizing: true,
_ratio: 1,
init() {
this._super(...arguments);
this._onResizeHandler = () => {
debounce(this, this._resize, 200);
};
},
didReceiveAttrs() {
assert(
'{{aspect-ratio-box}} requires a `ratio` property in the format `"16/9"`',
this.ratio.match(/\d+\/\d+/)
);
this._ratio = this.ratio.split('/').reduce((prev, curr) => prev / curr);
},
didInsertElement() {
this._resize();
window.addEventListener('resize', this._onResizeHandler);
},
willDestroyElement() {
this._super(...arguments);
window.removeEventListener('resize', this._onResizeHandler);
},
_resize() {
this.set('isResizing', true);
run.schedule('afterRender', this, function () {
if (this.base === 'height') {
this.element.style.width = `${this.element.clientHeight * this._ratio}px`;
} else {
this.element.style.height = `${this.element.clientWidth * this._ratio}px`;
}
this.set('isResizing', false);
});
}
});