|
| 1 | +import { html, LitElement } from 'lit-element'; |
| 2 | +import { get } from 'lit-translate'; |
| 3 | + |
| 4 | +import { |
| 5 | + cloneElement, |
| 6 | + identity, |
| 7 | + isPublic, |
| 8 | + newWizardEvent, |
| 9 | + SCLTag, |
| 10 | + selector, |
| 11 | + Wizard, |
| 12 | + WizardAction, |
| 13 | + WizardActor, |
| 14 | + WizardInput, |
| 15 | +} from '../foundation.js'; |
| 16 | + |
| 17 | +import { List } from '@material/mwc-list'; |
| 18 | +import { ListItemBase } from '@material/mwc-list/mwc-list-item-base'; |
| 19 | + |
| 20 | +interface addDescItem { |
| 21 | + desc: string; |
| 22 | + tag: SCLTag; |
| 23 | + identity: string | number; |
| 24 | +} |
| 25 | + |
| 26 | +function addDescriptionAction(doc: XMLDocument): WizardActor { |
| 27 | + return ( |
| 28 | + _: WizardInput[], |
| 29 | + wizard: Element, |
| 30 | + list: List | null | undefined |
| 31 | + ): WizardAction[] => { |
| 32 | + const selectedItems = <ListItemBase[]>list!.selected; |
| 33 | + |
| 34 | + const actions = selectedItems.map(item => { |
| 35 | + const desc = (<Element>item.querySelector('span')).textContent; |
| 36 | + const [tag, identity] = item.value.split(' | '); |
| 37 | + |
| 38 | + const oldElement = doc.querySelector(selector(tag, identity))!; |
| 39 | + const newElement = cloneElement(oldElement, { desc }); |
| 40 | + return { old: { element: oldElement }, new: { element: newElement } }; |
| 41 | + }); |
| 42 | + |
| 43 | + return [ |
| 44 | + { |
| 45 | + title: get('updatedesc.abb'), |
| 46 | + actions, |
| 47 | + }, |
| 48 | + ]; |
| 49 | + }; |
| 50 | +} |
| 51 | + |
| 52 | +function createLogWizard(doc: XMLDocument, items: addDescItem[]): Wizard { |
| 53 | + return [ |
| 54 | + { |
| 55 | + title: get('wizard.title.add', { tagName: 'desc' }), |
| 56 | + primary: { |
| 57 | + label: get('save'), |
| 58 | + icon: 'save', |
| 59 | + action: addDescriptionAction(doc), |
| 60 | + }, |
| 61 | + content: [ |
| 62 | + html`<filtered-list multi |
| 63 | + >${Array.from( |
| 64 | + items.map( |
| 65 | + item => |
| 66 | + html`<mwc-check-list-item |
| 67 | + twoline |
| 68 | + selected |
| 69 | + value="${item.tag + ' | ' + item.identity}" |
| 70 | + ><span>${item.desc}</span |
| 71 | + ><span slot="secondary" |
| 72 | + >${item.tag + ' | ' + item.identity}</span |
| 73 | + ></mwc-check-list-item |
| 74 | + >` |
| 75 | + ) |
| 76 | + )}</filtered-list |
| 77 | + >`, |
| 78 | + ], |
| 79 | + }, |
| 80 | + ]; |
| 81 | +} |
| 82 | + |
| 83 | +function addDescriptionToABB(ied: Element): addDescItem[] { |
| 84 | + return Array.from(ied.getElementsByTagName('ExtRef')) |
| 85 | + .filter(element => isPublic(element)) |
| 86 | + .filter(extRef => extRef.getAttribute('intAddr')) |
| 87 | + .map(extRef => { |
| 88 | + const intAddr = extRef.getAttribute('intAddr')!; |
| 89 | + const internalMapping = intAddr.split(',')[3]; //this might change as is vendor dependant!! |
| 90 | + const oldDesc = extRef.getAttribute('desc'); |
| 91 | + const newDesc = oldDesc |
| 92 | + ? oldDesc + '-' + internalMapping |
| 93 | + : internalMapping; |
| 94 | + |
| 95 | + return { |
| 96 | + desc: newDesc, |
| 97 | + tag: <SCLTag>'ExtRef', |
| 98 | + identity: identity(extRef), |
| 99 | + }; |
| 100 | + }); |
| 101 | +} |
| 102 | + |
| 103 | +/** Plug-in that enriched ExtRefs desc attribute based on intAddr attribute (ABB)*/ |
| 104 | +export default class UpdateDescriptionAbb extends LitElement { |
| 105 | + /** The document being edited as provided to plugins by [[`OpenSCD`]]. */ |
| 106 | + doc!: XMLDocument; |
| 107 | + |
| 108 | + /** Entry point for this plug-in */ |
| 109 | + async run(): Promise<void> { |
| 110 | + const items = Array.from(this.doc.querySelectorAll(':root > IED')).flatMap( |
| 111 | + ied => addDescriptionToABB(ied) |
| 112 | + ); |
| 113 | + |
| 114 | + this.dispatchEvent(newWizardEvent(createLogWizard(this.doc, items))); |
| 115 | + } |
| 116 | +} |
0 commit comments