|
| 1 | +import {css, customElement, html, LitElement, property, TemplateResult} from "lit-element"; |
| 2 | +import {get, translate} from "lit-translate"; |
| 3 | + |
| 4 | +import {newLogEvent, newOpenDocEvent, newWizardEvent} from "../foundation.js"; |
| 5 | +import {getOpenScdElement} from "./foundation.js"; |
| 6 | + |
| 7 | +import {CompasSclAutoAlignmentService} from "../compas-services/CompasSclAutoAlignmentService.js"; |
| 8 | +import {createLogEvent} from "../compas-services/foundation.js"; |
| 9 | + |
| 10 | +@customElement('compas-auto-alignment') |
| 11 | +export default class CompasAutoAlignmentElement extends LitElement { |
| 12 | + @property({type: Document}) |
| 13 | + doc!: XMLDocument; |
| 14 | + @property({type: String}) |
| 15 | + docName!: string; |
| 16 | + @property({type: String}) |
| 17 | + docId?: string; |
| 18 | + |
| 19 | + getSelectedValues() : string[] { |
| 20 | + const selectedItems: string[] = []; |
| 21 | + this.shadowRoot!.querySelectorAll('mwc-check-list-item').forEach((item, key) => { |
| 22 | + if (item.selected) { |
| 23 | + selectedItems[key] = item.value; |
| 24 | + } |
| 25 | + }); |
| 26 | + return selectedItems; |
| 27 | + } |
| 28 | + |
| 29 | + valid(): boolean { |
| 30 | + return this.getSelectedValues().length > 0; |
| 31 | + } |
| 32 | + |
| 33 | + async execute(): Promise<void> { |
| 34 | + if (this.valid()) { |
| 35 | + await CompasSclAutoAlignmentService().updateSCL(this.doc, this.getSelectedValues()) |
| 36 | + .then(sclDocument => { |
| 37 | + const openScd = getOpenScdElement(); |
| 38 | + openScd.dispatchEvent(newLogEvent({kind: 'reset'})); |
| 39 | + openScd.dispatchEvent(newOpenDocEvent(sclDocument, this.docName, {detail: {docId: this.docId}})); |
| 40 | + |
| 41 | + openScd.dispatchEvent( |
| 42 | + newLogEvent({ |
| 43 | + kind: 'info', |
| 44 | + title: get('compas.autoAlignment.success') |
| 45 | + })); |
| 46 | + |
| 47 | + // Close the Save Dialog. |
| 48 | + this.dispatchEvent(newWizardEvent()); |
| 49 | + }) |
| 50 | + .catch(createLogEvent); |
| 51 | + |
| 52 | + // Close the Save Dialog. |
| 53 | + this.dispatchEvent(newWizardEvent()); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + render(): TemplateResult { |
| 58 | + return html ` |
| 59 | + ${this.doc?.querySelector(':root > Substation') |
| 60 | + ? html` |
| 61 | + <section id="substationsToAlign" tabindex="0"> |
| 62 | + <mwc-list multi required> |
| 63 | + ${Array.from(this.doc.querySelectorAll(':root > Substation') ?? []) |
| 64 | + .map(substation => |
| 65 | + html` |
| 66 | + <mwc-check-list-item left value="${substation.getAttribute('name')}"> |
| 67 | + ${substation.getAttribute('name')} |
| 68 | + ${substation.getAttribute('desc') ? html `(${substation.getAttribute('desc')})`: html ``} |
| 69 | + </mwc-check-list-item> |
| 70 | + ` |
| 71 | + )} |
| 72 | + </mwc-list> |
| 73 | + </section> |
| 74 | + ` |
| 75 | + : html` |
| 76 | + <section id="noSubstationsToAlign" tabindex="0"> |
| 77 | + <span>${translate('compas.autoAlignment.missing')}</span> |
| 78 | + </section> |
| 79 | + `} |
| 80 | + `; |
| 81 | + } |
| 82 | + |
| 83 | + static styles = css` |
| 84 | + #noSubstationsToAlign > span { |
| 85 | + color: var(--base1) |
| 86 | + } |
| 87 | + ` |
| 88 | +} |
0 commit comments