2021-08-03 15:52:21 +00:00
|
|
|
import {
|
|
|
|
LitElement,
|
|
|
|
html,
|
|
|
|
customElement,
|
|
|
|
TemplateResult,
|
|
|
|
property,
|
|
|
|
CSSResult,
|
|
|
|
css,
|
|
|
|
} from "lit-element";
|
2020-12-12 19:46:02 +00:00
|
|
|
import "./Message";
|
2021-07-22 11:47:27 +00:00
|
|
|
import { APIMessage } from "./Message";
|
2021-03-17 12:23:33 +00:00
|
|
|
import PFAlertGroup from "@patternfly/patternfly/components/AlertGroup/alert-group.css";
|
|
|
|
import PFBase from "@patternfly/patternfly/patternfly-base.css";
|
2021-07-22 11:47:27 +00:00
|
|
|
import { EVENT_WS_MESSAGE, WS_MSG_TYPE_MESSAGE } from "../../constants";
|
|
|
|
import { WSMessage } from "../../common/ws";
|
2020-10-16 13:26:51 +00:00
|
|
|
|
2020-12-12 19:46:02 +00:00
|
|
|
export function showMessage(message: APIMessage): void {
|
|
|
|
const container = document.querySelector<MessageContainer>("ak-message-container");
|
|
|
|
if (!container) {
|
|
|
|
throw new Error("failed to find message container");
|
|
|
|
}
|
2021-03-12 11:41:17 +00:00
|
|
|
container.addMessage(message);
|
2020-12-12 19:46:02 +00:00
|
|
|
container.requestUpdate();
|
2020-11-20 21:08:00 +00:00
|
|
|
}
|
2020-10-16 13:26:51 +00:00
|
|
|
|
2020-12-12 19:46:02 +00:00
|
|
|
@customElement("ak-message-container")
|
|
|
|
export class MessageContainer extends LitElement {
|
2021-08-03 15:52:21 +00:00
|
|
|
@property({ attribute: false })
|
2020-12-12 19:46:02 +00:00
|
|
|
messages: APIMessage[] = [];
|
|
|
|
|
2021-03-16 16:09:52 +00:00
|
|
|
static get styles(): CSSResult[] {
|
2021-08-03 15:52:21 +00:00
|
|
|
return [
|
|
|
|
PFBase,
|
|
|
|
PFAlertGroup,
|
|
|
|
css`
|
|
|
|
/* Fix spacing between messages */
|
|
|
|
ak-message {
|
|
|
|
display: block;
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
];
|
2020-10-16 13:26:51 +00:00
|
|
|
}
|
|
|
|
|
2020-11-26 16:23:29 +00:00
|
|
|
constructor() {
|
|
|
|
super();
|
2021-07-22 11:47:27 +00:00
|
|
|
this.addEventListener(EVENT_WS_MESSAGE, ((e: CustomEvent<WSMessage>) => {
|
2021-07-22 12:15:54 +00:00
|
|
|
if (e.detail.message_type !== WS_MSG_TYPE_MESSAGE) return;
|
2021-07-22 11:47:27 +00:00
|
|
|
this.addMessage(e.detail as unknown as APIMessage);
|
|
|
|
}) as EventListener);
|
2020-10-16 18:11:24 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 11:41:17 +00:00
|
|
|
addMessage(message: APIMessage): void {
|
2021-09-09 18:09:31 +00:00
|
|
|
this.messages.push(message);
|
2021-03-12 11:41:17 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 16:27:19 +00:00
|
|
|
render(): TemplateResult {
|
2020-12-12 19:46:02 +00:00
|
|
|
return html`<ul class="pf-c-alert-group pf-m-toast">
|
|
|
|
${this.messages.map((m) => {
|
2021-03-01 19:33:00 +00:00
|
|
|
return html`<ak-message
|
2020-12-12 19:46:02 +00:00
|
|
|
.message=${m}
|
|
|
|
.onRemove=${(m: APIMessage) => {
|
2021-03-01 19:33:00 +00:00
|
|
|
this.messages = this.messages.filter((v) => v !== m);
|
2021-03-12 11:41:17 +00:00
|
|
|
this.requestUpdate();
|
2021-08-03 15:52:21 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
</ak-message>`;
|
|
|
|
})}
|
2020-12-12 19:46:02 +00:00
|
|
|
</ul>`;
|
2020-10-16 13:26:51 +00:00
|
|
|
}
|
|
|
|
}
|