2015-11-18 13:50:48 +03:00
|
|
|
/* global key */
|
2017-08-22 10:53:26 +03:00
|
|
|
import Component from '@ember/component';
|
|
|
|
import {run} from '@ember/runloop';
|
2015-11-18 13:50:48 +03:00
|
|
|
|
|
|
|
export default Component.extend({
|
|
|
|
tagName: 'section',
|
|
|
|
classNames: 'modal-content',
|
|
|
|
|
|
|
|
_previousKeymasterScope: null,
|
|
|
|
|
2018-03-20 17:57:59 +03:00
|
|
|
// Allowed Actions
|
|
|
|
closeModal: () => {},
|
|
|
|
|
2018-01-11 20:43:23 +03:00
|
|
|
didInsertElement() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this._setupShortcuts();
|
|
|
|
},
|
|
|
|
|
|
|
|
willDestroyElement() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this._removeShortcuts();
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
confirm() {
|
|
|
|
throw new Error('You must override the "confirm" action in your modal component');
|
|
|
|
},
|
|
|
|
|
|
|
|
closeModal() {
|
2018-03-20 17:57:59 +03:00
|
|
|
this.closeModal();
|
2018-01-11 20:43:23 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-01-19 19:00:44 +03:00
|
|
|
_setupShortcuts() {
|
2015-11-18 13:50:48 +03:00
|
|
|
run(function () {
|
|
|
|
document.activeElement.blur();
|
|
|
|
});
|
2019-06-18 13:47:21 +03:00
|
|
|
|
2015-11-18 13:50:48 +03:00
|
|
|
this._previousKeymasterScope = key.getScope();
|
|
|
|
|
|
|
|
key('enter', 'modal', () => {
|
|
|
|
this.send('confirm');
|
|
|
|
});
|
|
|
|
|
2021-02-23 21:37:12 +03:00
|
|
|
key('escape', 'modal', (event) => {
|
|
|
|
if (!event.target.dataset.preventEscapeCloseModal) {
|
|
|
|
this.send('closeModal');
|
|
|
|
}
|
2015-11-18 13:50:48 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
key.setScope('modal');
|
2016-01-19 19:00:44 +03:00
|
|
|
},
|
2015-11-18 13:50:48 +03:00
|
|
|
|
2016-01-19 19:00:44 +03:00
|
|
|
_removeShortcuts() {
|
2015-11-18 13:50:48 +03:00
|
|
|
key.unbind('enter', 'modal');
|
|
|
|
key.unbind('escape', 'modal');
|
|
|
|
key.setScope(this._previousKeymasterScope);
|
|
|
|
}
|
|
|
|
});
|