Ghost/ghost/admin/app/services/modals.js
Kevin Ansfield 85d7932e45 Resolved deprecation warnings for dynamic modal component binding (#2303)
refs https://github.com/TryGhost/Team/issues/559
refs 054a5f15f5

- with the update of `ember-promise-modals` we started to get deprecation warnings when using `modals.open('modal-component-name')`
  - upcoming Ember build updates will introduce tree shaking but using run-time lookup of modal components by name works against that because it's not statically analysable
- switched to importing components and passing the component class directly, eg. `modals.open(ModalComponent)`
- standardized modal component class names with a `MyModal` style to get better behaviour in code editors when it auto generates imports
- dropped the modal defaults from the modals service because we can now use a static `modalOptions` property on the modal components themselves when we want to override the defaults
2022-03-14 10:52:04 +00:00

77 lines
2.5 KiB
JavaScript

import EPMModalsService from 'ember-promise-modals/services/modals';
import {bind} from '@ember/runloop';
import {inject as service} from '@ember/service';
export default class ModalsService extends EPMModalsService {
@service dropdown;
DEFAULT_OPTIONS = {
className: 'fullscreen-modal-action fullscreen-modal-wide'
};
// we manually close modals on backdrop clicks and escape rather than letting focus-trap
// handle it so we can intercept/abort closing for things like unsaved change confirmations
focusTrapOptions = {
allowOutsideClick: true,
clickOutsideDeactivates: false,
escapeDeactivates: false
};
open(modal, data, options) {
const mergedOptions = Object.assign({}, this.DEFAULT_OPTIONS, modal.modalOptions, options);
return super.open(modal, data, mergedOptions);
}
_onFirstModalAdded() {
super._onFirstModalAdded(...arguments);
this.addEventHandlers();
this.dropdown.closeDropdowns();
}
_onLastModalRemoved() {
super._onLastModalRemoved(...arguments);
this.removeEventHandlers();
}
addEventHandlers() {
if (!this.backdropClickHandler) {
this.backdropClickHandler = bind(this, this.handleBackdropClick);
document.body.addEventListener('click', this.backdropClickHandler, {capture: true, passive: false});
}
if (!this.escapeKeyHandler) {
this.escapeKeyHandler = bind(this, this.handleEscapeKey);
document.addEventListener('keydown', this.escapeKeyHandler, {capture: true, passive: false});
}
}
removeEventHandlers() {
document.body.removeEventListener('click', this.backdropClickHandler, {capture: true, passive: false});
this.backdropClickHandler = null;
document.removeEventListener('keydown', this.escapeKeyHandler, {capture: true, passive: false});
this.escapeKeyHandler = null;
}
handleBackdropClick(event) {
let shouldClose = true;
for (const elem of (event.path || event.composedPath())) {
if (elem.matches?.('.modal-content, .fullscreen-modal-total-overlay, .ember-basic-dropdown-content, a[download]')) {
shouldClose = false;
break;
}
}
if (shouldClose) {
this.top.close();
}
}
handleEscapeKey(event) {
if (event.key === 'Escape') {
this.top.close();
}
}
}