2021-09-03 20:14:28 +03:00
|
|
|
import EPMModalsService from 'ember-promise-modals/services/modals';
|
2021-09-16 22:07:32 +03:00
|
|
|
import {bind} from '@ember/runloop';
|
2021-09-03 20:14:28 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
|
|
|
|
|
|
|
export default class ModalsService extends EPMModalsService {
|
|
|
|
@service dropdown;
|
2021-10-11 21:30:58 +03:00
|
|
|
@service themeManagement;
|
|
|
|
|
|
|
|
DEFAULT_MODAL_OPTIONS = {
|
|
|
|
'modals/confirm-unsaved-changes': {
|
|
|
|
className: 'fullscreen-modal-action fullscreen-modal-wide'
|
|
|
|
},
|
2021-10-12 16:27:51 +03:00
|
|
|
'modals/design/confirm-delete-theme': {
|
|
|
|
className: 'fullscreen-modal-action fullscreen-modal-wide'
|
|
|
|
},
|
2021-10-12 18:47:33 +03:00
|
|
|
'modals/design/install-theme': {
|
|
|
|
className: 'fullscreen-modal-action fullscreen-modal-wide'
|
|
|
|
},
|
2021-10-12 16:27:51 +03:00
|
|
|
'modals/design/theme-errors': {
|
|
|
|
className: 'fullscreen-modal-action fullscreen-modal-wide'
|
|
|
|
},
|
2021-10-11 21:30:58 +03:00
|
|
|
'modals/design/upload-theme': {
|
|
|
|
className: 'fullscreen-modal-action fullscreen-modal-wide',
|
|
|
|
beforeClose: () => {
|
|
|
|
if (this.themeManagement.isUploading) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2021-10-12 16:27:51 +03:00
|
|
|
'modals/design/view-theme': {
|
|
|
|
className: 'fullscreen-modal-total-overlay',
|
|
|
|
omitBackdrop: true
|
2021-10-11 21:30:58 +03:00
|
|
|
},
|
|
|
|
'modals/limits/custom-theme': {
|
|
|
|
className: 'fullscreen-modal-action fullscreen-modal-wide'
|
|
|
|
}
|
|
|
|
}
|
2021-09-03 20:14:28 +03:00
|
|
|
|
2021-09-16 22:07:32 +03:00
|
|
|
// 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
|
|
|
|
allowOutsideClick = true;
|
|
|
|
clickOutsideDeactivates = false;
|
|
|
|
escapeDeactivates = false;
|
2021-09-03 20:14:28 +03:00
|
|
|
|
2021-10-05 13:19:52 +03:00
|
|
|
open(modal, data, options) {
|
2021-10-11 21:30:58 +03:00
|
|
|
const mergedOptions = Object.assign({}, this.DEFAULT_MODAL_OPTIONS[modal], options);
|
2021-10-05 13:30:42 +03:00
|
|
|
return super.open(modal, data, mergedOptions);
|
2021-10-05 13:19:52 +03:00
|
|
|
}
|
|
|
|
|
2021-09-16 22:07:32 +03:00
|
|
|
_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});
|
2021-09-03 20:14:28 +03:00
|
|
|
}
|
|
|
|
|
2021-09-16 22:07:32 +03:00
|
|
|
if (!this.escapeKeyHandler) {
|
|
|
|
this.escapeKeyHandler = bind(this, this.handleEscapeKey);
|
|
|
|
document.addEventListener('keydown', this.escapeKeyHandler, {capture: true, passive: false});
|
|
|
|
}
|
2021-09-03 20:14:28 +03:00
|
|
|
}
|
|
|
|
|
2021-09-16 22:07:32 +03:00
|
|
|
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;
|
2021-09-03 20:14:28 +03:00
|
|
|
|
2021-09-16 22:07:32 +03:00
|
|
|
for (const elem of (event.path || event.composedPath())) {
|
2021-10-04 18:34:13 +03:00
|
|
|
if (elem.matches?.('.modal-content, .fullscreen-modal-total-overlay, .ember-basic-dropdown-content')) {
|
2021-09-16 22:07:32 +03:00
|
|
|
shouldClose = false;
|
2021-09-03 20:14:28 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-16 22:07:32 +03:00
|
|
|
if (shouldClose) {
|
|
|
|
this.top.close();
|
|
|
|
}
|
2021-09-03 20:14:28 +03:00
|
|
|
}
|
|
|
|
|
2021-09-16 22:07:32 +03:00
|
|
|
handleEscapeKey(event) {
|
|
|
|
if (event.key === 'Escape') {
|
|
|
|
this.top.close();
|
|
|
|
}
|
2021-09-03 20:14:28 +03:00
|
|
|
}
|
|
|
|
}
|