2023-03-14 13:18:55 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
* SPDX-License-Identifier: BUSL-1.1
|
2023-03-14 13:18:55 +00:00
|
|
|
*/
|
|
|
|
|
2021-11-24 18:14:07 +00:00
|
|
|
import Modifier from 'ember-modifier';
|
|
|
|
import { inject as service } from '@ember/service';
|
2023-10-17 13:27:42 +00:00
|
|
|
import { registerDestructor } from '@ember/destroyable';
|
2021-11-24 18:14:07 +00:00
|
|
|
|
2023-10-17 13:27:42 +00:00
|
|
|
function cleanup(instance) {
|
|
|
|
if (instance && instance?.named?.sticky) {
|
|
|
|
instance.notify?.clearMessages();
|
|
|
|
}
|
|
|
|
}
|
2021-11-24 18:14:07 +00:00
|
|
|
export default class NotificationModifier extends Modifier {
|
|
|
|
@service('flashMessages') notify;
|
|
|
|
|
2023-10-17 13:27:42 +00:00
|
|
|
modify(element, _, named) {
|
|
|
|
this.named = named;
|
|
|
|
element.setAttribute('role', 'alert');
|
|
|
|
element.dataset['notification'] = null;
|
|
|
|
|
2021-11-24 18:14:07 +00:00
|
|
|
const options = {
|
|
|
|
timeout: 6000,
|
|
|
|
extendedTimeout: 300,
|
2023-10-17 13:27:42 +00:00
|
|
|
...named,
|
2021-11-24 18:14:07 +00:00
|
|
|
};
|
2023-10-17 13:27:42 +00:00
|
|
|
options.dom = element.outerHTML;
|
|
|
|
element.remove();
|
2021-11-24 18:14:07 +00:00
|
|
|
this.notify.clearMessages();
|
|
|
|
if (typeof options.after === 'function') {
|
2022-09-15 08:43:17 +00:00
|
|
|
Promise.resolve()
|
|
|
|
.then((_) => options.after())
|
|
|
|
.catch((e) => {
|
2021-11-24 18:14:07 +00:00
|
|
|
if (e.name !== 'TransitionAborted') {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
})
|
2023-10-17 13:27:42 +00:00
|
|
|
.then((_) => {
|
2021-11-24 18:14:07 +00:00
|
|
|
this.notify.add(options);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.notify.add(options);
|
|
|
|
}
|
2023-10-17 13:27:42 +00:00
|
|
|
|
|
|
|
registerDestructor(this, cleanup);
|
2021-11-24 18:14:07 +00:00
|
|
|
}
|
|
|
|
}
|