|
| 1 | +import { |
| 2 | + customElement, |
| 3 | + html, |
| 4 | + LitElement, |
| 5 | + property, |
| 6 | + query, |
| 7 | + state, |
| 8 | + TemplateResult, |
| 9 | +} from 'lit-element'; |
| 10 | + |
| 11 | +import { Menu } from '@material/mwc-menu'; |
| 12 | + |
| 13 | +import '@material/mwc-menu'; |
| 14 | +import '@material/mwc-list'; |
| 15 | +import '@material/mwc-icon'; |
| 16 | +import '@material/mwc-icon-button'; |
| 17 | + |
| 18 | +import { IconButton } from '@material/mwc-icon-button'; |
| 19 | + |
| 20 | +import { Create, createElement, newActionEvent } from '../../foundation.js'; |
| 21 | + |
| 22 | +import '../../action-pane.js'; |
| 23 | +import '../../action-icon.js'; |
| 24 | + |
| 25 | +import { |
| 26 | + SIEMENS_SITIPE_IED_REF, |
| 27 | + SIEMENS_SITIPE_BAY_TEMPLATE, |
| 28 | +} from './foundation.js'; |
| 29 | + |
| 30 | +import { |
| 31 | + BayTypical, |
| 32 | + getBayTypicalComponents, |
| 33 | + getImportedBTComponentData, |
| 34 | + getImportedBTComponents, |
| 35 | +} from './sitipe-service.js'; |
| 36 | + |
| 37 | +/** [[`Sitipe`]] plugin subeditor for editing `Sitipe` configuration. */ |
| 38 | +@customElement('sitipe-bay') |
| 39 | +export class SitipeBay extends LitElement { |
| 40 | + /** The document being edited as provided to editor by [[`Sitipe`]]. */ |
| 41 | + @property({ attribute: false }) |
| 42 | + doc!: XMLDocument; |
| 43 | + /** The edited `Element`, a common property of all Sitipe subeditors. */ |
| 44 | + @property({ attribute: false }) |
| 45 | + bay!: Element; |
| 46 | + |
| 47 | + @property() |
| 48 | + bayTypicals: BayTypical[] = []; |
| 49 | + |
| 50 | + @state() |
| 51 | + loading: boolean = false; |
| 52 | + |
| 53 | + @state() |
| 54 | + bayHeader(): string { |
| 55 | + const name = this.bay.getAttribute('name') ?? ''; |
| 56 | + const desc = this.bay.getAttribute('desc'); |
| 57 | + |
| 58 | + return `${name} ${desc ? `(${desc})` : ''}`; |
| 59 | + } |
| 60 | + |
| 61 | + @query('mwc-menu') |
| 62 | + menu?: Menu; |
| 63 | + |
| 64 | + @query('mwc-icon-button[icon="playlist_add"]') |
| 65 | + iconButton?: IconButton; |
| 66 | + |
| 67 | + updated(): void { |
| 68 | + if (this.menu && this.iconButton) { |
| 69 | + this.menu!.anchor = <HTMLElement>this.iconButton!; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + get bayTypicalTemplate(): string { |
| 74 | + return ( |
| 75 | + this.bay.querySelector(`Private[type="${SIEMENS_SITIPE_BAY_TEMPLATE}"]`) |
| 76 | + ?.textContent ?? '' |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + private renderIEDs(): TemplateResult { |
| 81 | + return html` |
| 82 | + <div> |
| 83 | + ${Array.from( |
| 84 | + this.bay.querySelectorAll( |
| 85 | + `Private[type="${SIEMENS_SITIPE_IED_REF}"]` ?? [] |
| 86 | + ) |
| 87 | + ).map( |
| 88 | + iedTemplate => |
| 89 | + html`<action-icon |
| 90 | + .label=${iedTemplate.textContent |
| 91 | + ? `${iedTemplate.textContent} (${this.bayTypicalTemplate})` |
| 92 | + : ''} |
| 93 | + icon="developer_board" |
| 94 | + ></action-icon>` |
| 95 | + )} |
| 96 | + </div> |
| 97 | + `; |
| 98 | + } |
| 99 | + |
| 100 | + protected renderMenu(): TemplateResult { |
| 101 | + return html`<mwc-menu |
| 102 | + class="actions-menu" |
| 103 | + corner="BOTTOM_RIGHT" |
| 104 | + menuCorner="END" |
| 105 | + > |
| 106 | + ${this.bayTypicals.map(bayTypical => { |
| 107 | + return html`<mwc-list-item |
| 108 | + @click=${() => this.handleSelected(bayTypical)} |
| 109 | + .disabled=${this.isDisabled(bayTypical)} |
| 110 | + >${bayTypical.name}</mwc-list-item |
| 111 | + >`; |
| 112 | + })} |
| 113 | + </mwc-menu>`; |
| 114 | + } |
| 115 | + |
| 116 | + render(): TemplateResult { |
| 117 | + return html`<action-pane label="${this.bayHeader()}"> |
| 118 | + <abbr slot="action" title="Add" style="position:relative;"> |
| 119 | + <mwc-icon-button |
| 120 | + icon="playlist_add" |
| 121 | + @click="${() => { |
| 122 | + this.menu!.open = true; |
| 123 | + }}" |
| 124 | + ></mwc-icon-button> |
| 125 | + ${this.renderMenu()} |
| 126 | + </abbr> |
| 127 | + ${this.renderIEDs()}</action-pane |
| 128 | + >`; |
| 129 | + } |
| 130 | + |
| 131 | + private isDisabled(bayTypical: BayTypical): boolean { |
| 132 | + return bayTypical.name === this.bayTypicalTemplate; |
| 133 | + } |
| 134 | + |
| 135 | + private handleSelected(bayTypical: BayTypical) { |
| 136 | + const bayTypicalElement: Element = createElement(this.doc, 'Private', { |
| 137 | + type: SIEMENS_SITIPE_BAY_TEMPLATE, |
| 138 | + }); |
| 139 | + |
| 140 | + bayTypicalElement.textContent = bayTypical.name; |
| 141 | + |
| 142 | + const action: Create = { |
| 143 | + new: { |
| 144 | + parent: this.bay, |
| 145 | + element: bayTypicalElement, |
| 146 | + }, |
| 147 | + }; |
| 148 | + |
| 149 | + this.dispatchEvent(newActionEvent(action)); |
| 150 | + |
| 151 | + getBayTypicalComponents(bayTypical.accessId).then(btComponents => { |
| 152 | + btComponents.forEach(btComponent => { |
| 153 | + getImportedBTComponents(btComponent.accessId).then( |
| 154 | + importedBtComponents => { |
| 155 | + importedBtComponents.forEach(imported => { |
| 156 | + getImportedBTComponentData(imported.id).then(data => { |
| 157 | + console.log('data'); |
| 158 | + console.log(data); |
| 159 | + }); |
| 160 | + }); |
| 161 | + } |
| 162 | + ); |
| 163 | + }); |
| 164 | + }); |
| 165 | + } |
| 166 | +} |
0 commit comments