|
| 1 | +import { css, html, LitElement } from 'lit'; |
| 2 | +import { customElement, query } from 'lit/decorators.js'; |
| 3 | + |
| 4 | +import { Insert, newEditEvent } from '@openscd/open-scd-core'; |
| 5 | + |
| 6 | +import { SclTextField } from '@openenergytools/scl-text-field'; |
| 7 | +import { createElement } from '@openenergytools/scl-lib/dist/foundation/utils.js'; |
| 8 | + |
| 9 | +import { Dialog } from '@material/mwc-dialog'; |
| 10 | +import { Button } from '@material/mwc-button'; |
| 11 | +import { ScopedElementsMixin } from '@open-wc/scoped-elements/lit-element.js'; |
| 12 | +import { |
| 13 | + createPowerSystemRelationElement, |
| 14 | + getProcessPath, |
| 15 | +} from '../foundation/scl.js'; |
| 16 | + |
| 17 | +// eslint-disable-next-line no-shadow |
| 18 | +export enum DialogMode { |
| 19 | + PowerTransformer = 'PowerTransformer', |
| 20 | + ConductingEquipment = 'ConductingEquipment', |
| 21 | +} |
| 22 | + |
| 23 | +interface FormValue { |
| 24 | + name: string | null; |
| 25 | + desc: string | null; |
| 26 | + type: string | null; |
| 27 | +} |
| 28 | + |
| 29 | +@customElement('add-function-dialog-632c87ac') |
| 30 | +export default class AddFunctionDialog extends ScopedElementsMixin(LitElement) { |
| 31 | + static scopedElements = { |
| 32 | + 'scl-text-field': SclTextField, |
| 33 | + 'mwc-dialog': Dialog, |
| 34 | + 'mwc-button': Button, |
| 35 | + }; |
| 36 | + |
| 37 | + @query('#add-function-dialog-internal') dialog!: Dialog; |
| 38 | + |
| 39 | + @query('scl-text-field[label="name"]') nameTextField!: SclTextField; |
| 40 | + |
| 41 | + @query('scl-text-field[label="desc"]') descTextField!: SclTextField; |
| 42 | + |
| 43 | + @query('scl-text-field[label="type"]') typeTextField!: SclTextField; |
| 44 | + |
| 45 | + private element: Element | null = null; |
| 46 | + |
| 47 | + private mode: DialogMode = DialogMode.ConductingEquipment; |
| 48 | + |
| 49 | + public show(element: Element, mode: DialogMode) { |
| 50 | + this.element = element; |
| 51 | + this.mode = mode; |
| 52 | + this.dialog.show(); |
| 53 | + } |
| 54 | + |
| 55 | + public close() { |
| 56 | + this.resetForm(); |
| 57 | + this.dialog.close(); |
| 58 | + } |
| 59 | + |
| 60 | + private resetForm(): void { |
| 61 | + this.nameTextField.value = ''; |
| 62 | + this.descTextField.value = ''; |
| 63 | + this.typeTextField.value = ''; |
| 64 | + } |
| 65 | + |
| 66 | + private onSave(): void { |
| 67 | + const name = this.nameTextField.value; |
| 68 | + const desc = this.descTextField.value; |
| 69 | + const type = this.typeTextField.value; |
| 70 | + |
| 71 | + const values: FormValue = { name, desc, type }; |
| 72 | + |
| 73 | + if (this.element === null) { |
| 74 | + throw new Error(`No element provided to add function dialog`); |
| 75 | + } |
| 76 | + |
| 77 | + if (this.mode === DialogMode.PowerTransformer) { |
| 78 | + // Add function to parent bay, VoltageLevel or substation |
| 79 | + // Add PowerSystemRelation -> PowerTransformer |
| 80 | + |
| 81 | + const bayVoltageLevelOrSubstation = this.element.closest( |
| 82 | + 'Bay, VoltageLevel, Substation' |
| 83 | + ); |
| 84 | + |
| 85 | + if (bayVoltageLevelOrSubstation === null) { |
| 86 | + throw new Error( |
| 87 | + `Element ${this.element.getAttribute( |
| 88 | + 'name' |
| 89 | + )} does not have parent bay, voltage level or substation` |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + const isPowerTransformer = this.element.tagName === 'PowerTransformer'; |
| 94 | + const powerTransformer = isPowerTransformer |
| 95 | + ? this.element |
| 96 | + : this.element.parentElement; |
| 97 | + |
| 98 | + if (!powerTransformer) { |
| 99 | + throw new Error(`Powertransformer not found`); |
| 100 | + } |
| 101 | + |
| 102 | + const pathToPowerTransformer = getProcessPath(powerTransformer); |
| 103 | + const powerSystemRelationElement = createPowerSystemRelationElement( |
| 104 | + this.element.ownerDocument, |
| 105 | + pathToPowerTransformer |
| 106 | + ); |
| 107 | + |
| 108 | + const functionElement = createElement( |
| 109 | + bayVoltageLevelOrSubstation.ownerDocument, |
| 110 | + 'Function', |
| 111 | + { |
| 112 | + ...values, |
| 113 | + } |
| 114 | + ); |
| 115 | + |
| 116 | + functionElement.appendChild(powerSystemRelationElement); |
| 117 | + |
| 118 | + const functionInsert: Insert = { |
| 119 | + parent: bayVoltageLevelOrSubstation, |
| 120 | + node: functionElement, |
| 121 | + reference: null, |
| 122 | + }; |
| 123 | + |
| 124 | + this.dispatchEvent(newEditEvent(functionInsert)); |
| 125 | + } else { |
| 126 | + // Add function to parent bay |
| 127 | + // Add PowerSystemRelation -> ConductingEquipment |
| 128 | + |
| 129 | + const conductingEquipment = this.element; |
| 130 | + const bay = conductingEquipment.closest('Bay'); |
| 131 | + |
| 132 | + if (bay === null) { |
| 133 | + throw new Error( |
| 134 | + `ConductingEquipment ${conductingEquipment.getAttribute( |
| 135 | + 'name' |
| 136 | + )} does not have parent bay` |
| 137 | + ); |
| 138 | + } |
| 139 | + |
| 140 | + const functionElement = createElement(bay.ownerDocument, 'Function', { |
| 141 | + ...values, |
| 142 | + }); |
| 143 | + |
| 144 | + const pathToConductingEquipment = getProcessPath(conductingEquipment); |
| 145 | + const powerSystemRelationElement = createPowerSystemRelationElement( |
| 146 | + this.element.ownerDocument, |
| 147 | + pathToConductingEquipment |
| 148 | + ); |
| 149 | + |
| 150 | + functionElement.appendChild(powerSystemRelationElement); |
| 151 | + |
| 152 | + const functionInsert: Insert = { |
| 153 | + parent: bay, |
| 154 | + node: functionElement, |
| 155 | + reference: null, |
| 156 | + }; |
| 157 | + |
| 158 | + this.dispatchEvent(newEditEvent(functionInsert)); |
| 159 | + } |
| 160 | + this.close(); |
| 161 | + } |
| 162 | + |
| 163 | + protected render() { |
| 164 | + return html`<mwc-dialog |
| 165 | + id="add-function-dialog-internal" |
| 166 | + heading="Add function" |
| 167 | + > |
| 168 | + <div class="input-container"> |
| 169 | + <scl-text-field |
| 170 | + label="name" |
| 171 | + .value=${''} |
| 172 | + required |
| 173 | + dialogInitialFocus |
| 174 | + ></scl-text-field> |
| 175 | + <scl-text-field |
| 176 | + label="desc" |
| 177 | + .maybeValue=${''} |
| 178 | + nullable |
| 179 | + ></scl-text-field> |
| 180 | + <scl-text-field |
| 181 | + label="type" |
| 182 | + .maybeValue=${''} |
| 183 | + nullable |
| 184 | + ></scl-text-field> |
| 185 | + </div> |
| 186 | + <mwc-button |
| 187 | + slot="secondaryAction" |
| 188 | + label="close" |
| 189 | + @click=${this.close} |
| 190 | + ></mwc-button> |
| 191 | + <mwc-button |
| 192 | + slot="primaryAction" |
| 193 | + label="save" |
| 194 | + icon="save" |
| 195 | + @click="${this.onSave}" |
| 196 | + ></mwc-button> |
| 197 | + </mwc-dialog>`; |
| 198 | + } |
| 199 | + |
| 200 | + static styles = css` |
| 201 | + .input-container { |
| 202 | + display: flex; |
| 203 | + flex-direction: column; |
| 204 | + gap: 16px; |
| 205 | + } |
| 206 | +
|
| 207 | + * { |
| 208 | + --mdc-theme-primary: var(--fedit-primary); |
| 209 | + --mdc-theme-secondary: var(--fedit-secondary); |
| 210 | +
|
| 211 | + --md-sys-color-primary: var(--fedit-primary); |
| 212 | + --md-sys-color-secondary: var(--fedit-secondary); |
| 213 | + --md-sys-typescale-body-large-font: var( |
| 214 | + --oscd-theme-text-font, |
| 215 | + 'Roboto', |
| 216 | + sans-serif |
| 217 | + ); |
| 218 | + --md-filled-text-field-input-text-color: var(--fedit-text-color); |
| 219 | +
|
| 220 | + --md-sys-color-surface: var(--fedit-surface); |
| 221 | + --md-sys-color-on-surface: var(--fedit-text-color); |
| 222 | + --md-sys-color-on-primary: var(--fedit-text-color); |
| 223 | + --md-sys-color-on-surface-variant: var(--fedit-text-color); |
| 224 | + --md-menu-container-color: var(--fedit-surface); |
| 225 | + font-family: var(--oscd-theme-text-font, 'Roboto', sans-serif); |
| 226 | + --md-sys-color-surface-container-highest: var(--fedit-surface); |
| 227 | + } |
| 228 | + `; |
| 229 | +} |
0 commit comments