|
| 1 | +import { LitElement, html} from 'lit'; |
| 2 | +import { JsonRpc } from 'jsonrpc'; |
| 3 | +import '@vaadin/icon'; |
| 4 | +import '@vaadin/button'; |
| 5 | +import { until } from 'lit/directives/until.js'; |
| 6 | +import '@vaadin/grid'; |
| 7 | +import { columnBodyRenderer } from '@vaadin/grid/lit.js'; |
| 8 | +import '@vaadin/grid/vaadin-grid-sort-column.js'; |
| 9 | +import { unsafeHTML } from 'lit-html/directives/unsafe-html.js'; |
| 10 | + |
| 11 | + |
| 12 | +export class QwcSmallryeReactiveMessagingChannels extends LitElement { |
| 13 | + |
| 14 | + jsonRpc = new JsonRpc("SmallRyeReactiveMessaging"); |
| 15 | + |
| 16 | + static properties = { |
| 17 | + "_channels": {state: true, type: Array} |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * Called when displayed |
| 22 | + */ |
| 23 | + connectedCallback() { |
| 24 | + super.connectedCallback(); |
| 25 | + this.jsonRpc.getInfo().then(jsonRpcResponse => { |
| 26 | + this._channels = []; |
| 27 | + jsonRpcResponse.result.forEach(c => { |
| 28 | + this._channels.push(c); |
| 29 | + }); |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Called when it needs to render the components |
| 35 | + * @returns {*} |
| 36 | + */ |
| 37 | + render() { |
| 38 | + return html`${until(this._renderChannelTable(), html`<span>Loading channels...</span>`)}`; |
| 39 | + } |
| 40 | + |
| 41 | + _renderChannelTable() { |
| 42 | + if (this._channels) { |
| 43 | + return html` |
| 44 | + <vaadin-grid .items="${this._channels}" class="datatable" theme="no-border"> |
| 45 | + <vaadin-grid-column auto-width |
| 46 | + header="Channel" |
| 47 | + ${columnBodyRenderer(this._channelNameRenderer, [])}> |
| 48 | + </vaadin-grid-column> |
| 49 | +
|
| 50 | + <vaadin-grid-column auto-width |
| 51 | + header="Publisher" |
| 52 | + ${columnBodyRenderer(this._channelPublisherRenderer, [])}>> |
| 53 | + </vaadin-grid-column> |
| 54 | +
|
| 55 | + <vaadin-grid-column auto-width |
| 56 | + header="Subscriber(s)" |
| 57 | + ${columnBodyRenderer(this._channelSubscriberRenderer, [])} |
| 58 | + resizable> |
| 59 | + </vaadin-grid-column> |
| 60 | + </vaadin-grid>`; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + _channelNameRenderer(channel) { |
| 65 | + return html`<strong>${ channel.name }</strong>` |
| 66 | + } |
| 67 | + |
| 68 | + _channelSubscriberRenderer(channel) { |
| 69 | + const consumers = channel.consumers; |
| 70 | + if (consumers) { |
| 71 | + if (consumers.length === 1) { |
| 72 | + return this._renderComponent(consumers[0]); |
| 73 | + } else if (consumers.length > 1) { |
| 74 | + return html` |
| 75 | + <ul> |
| 76 | + ${consumers.map(item => html`<li>${this._renderComponent(item)}</li>`)} |
| 77 | + </ul> |
| 78 | + `; |
| 79 | + } else { |
| 80 | + return html`<em>No subscribers</em>` |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + _channelPublisherRenderer(channel) { |
| 86 | + const publisher = channel.publisher; |
| 87 | + if (publisher) { |
| 88 | + return this._renderComponent(publisher); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + _renderComponent(component) { |
| 93 | + switch (component.type) { |
| 94 | + case "PUBLISHER": |
| 95 | + return html`<vaadin-icon icon="font-awesome-solid:right-from-bracket" title="publisher"></vaadin-icon> ${unsafeHTML(component.description)}` |
| 96 | + case "SUBSCRIBER": |
| 97 | + return html`<vaadin-icon icon="font-awesome-solid:right-to-bracket" title="subscriber"></vaadin-icon> ${unsafeHTML(component.description)}` |
| 98 | + case "PROCESSOR": |
| 99 | + return html`<vaadin-icon icon="font-awesome-solid:arrows-turn-to-dots" title="processor" />"></vaadin-icon> ${unsafeHTML(component.description)}` |
| 100 | + case "CONNECTOR": |
| 101 | + return html`<vaadin-icon icon="font-awesome-solid:plug" title="connector"></vaadin-icon> ${unsafeHTML(component.description)}` |
| 102 | + case "EMITTER": |
| 103 | + return html`<vaadin-icon icon="font-awesome-solid:syringe" title="emitter"></vaadin-icon> ${unsafeHTML(component.description)}` |
| 104 | + case "CHANNEL": |
| 105 | + return html`<vaadin-icon icon="font-awesome-solid:syringe" title="channel"></vaadin-icon> ${unsafeHTML(component.description)}` |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | +customElements.define('qwc-smallrye-reactive-messaging-channels', QwcSmallryeReactiveMessagingChannels); |
0 commit comments