|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import { |
| 4 | + css, |
| 5 | + html, |
| 6 | + LitElement, |
| 7 | + property, |
| 8 | + TemplateResult, |
| 9 | + query, |
| 10 | + queryAll, |
| 11 | +} from 'lit-element'; |
| 12 | +import { translate } from 'lit-translate'; |
| 13 | + |
| 14 | +import '@material/mwc-button'; |
| 15 | +import { Button } from '@material/mwc-button'; |
| 16 | +import { List, MWCListIndex } from '@material/mwc-list'; |
| 17 | +import { ListItem } from '@material/mwc-list/mwc-list-item.js'; |
| 18 | +import '@material/mwc-list/mwc-check-list-item.js'; |
| 19 | +import '../filtered-list.js'; |
| 20 | + |
| 21 | +import { |
| 22 | + Delete, |
| 23 | + identity, |
| 24 | + isPublic, |
| 25 | + newSubWizardEvent, |
| 26 | + newActionEvent, |
| 27 | +} from '../foundation.js'; |
| 28 | + |
| 29 | +import { editDataSetWizard } from '../wizards/dataset.js'; |
| 30 | +import { styles } from './templates/foundation.js'; |
| 31 | + |
| 32 | +/** An editor [[`plugin`]] for cleaning SCL references and definitions. */ |
| 33 | +export default class Cleanup extends LitElement { |
| 34 | + /** The document being edited as provided to plugins by [[`OpenSCD`]]. */ |
| 35 | + @property() |
| 36 | + doc!: XMLDocument; |
| 37 | + @property() |
| 38 | + disableDataSetClean = false; |
| 39 | + @property() |
| 40 | + unreferencedDataSets: Element[] = []; |
| 41 | + @property() |
| 42 | + selectedItems: MWCListIndex | [] = []; |
| 43 | + |
| 44 | + @query('.cleanupUnreferencedDataSetsDeleteButton') |
| 45 | + _cleanUnreferencedDataSetsButton!: Button; |
| 46 | + @query('.cleanupUnreferencedDataSetsList') |
| 47 | + _cleanUnreferencedDataSetsList: List | undefined; |
| 48 | + @queryAll('mwc-check-list-item') |
| 49 | + _cleanUnreferencedDataSetItems: ListItem[] | undefined; |
| 50 | + |
| 51 | + /** |
| 52 | + * Set a class variable for selected items to allow processing and UI interaction |
| 53 | + */ |
| 54 | + private getSelectedUnreferencedDataSetItems() { |
| 55 | + this.selectedItems = (<List>( |
| 56 | + this.shadowRoot!.querySelector('.cleanupUnreferencedDataSetsList') |
| 57 | + )).index; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Clean datasets as requested by removing DataSet elements specified by the user from the SCL file |
| 62 | + * @returns an actions array to support undo/redo |
| 63 | + */ |
| 64 | + public cleanDataSets(cleanItems: Element[]): Delete[] { |
| 65 | + const actions: Delete[] = []; |
| 66 | + if (cleanItems) { |
| 67 | + cleanItems.forEach(item => { |
| 68 | + actions.push({ |
| 69 | + old: { |
| 70 | + parent: <Element>item.parentElement!, |
| 71 | + element: item, |
| 72 | + reference: <Node | null>item!.nextSibling, |
| 73 | + }, |
| 74 | + }); |
| 75 | + }); |
| 76 | + } |
| 77 | + return actions; |
| 78 | + } |
| 79 | + |
| 80 | + async firstUpdated(): Promise<void> { |
| 81 | + this._cleanUnreferencedDataSetsList?.addEventListener('selected', () => { |
| 82 | + this.getSelectedUnreferencedDataSetItems(); |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Render a user selectable table of unreferenced datasets if any exist, otherwise indicate this is not an issue. |
| 88 | + * @returns html for table and action button. |
| 89 | + */ |
| 90 | + private renderUnreferencedDataSets() { |
| 91 | + const unreferencedDataSets: Element[] = []; |
| 92 | + Array.from(this.doc?.querySelectorAll('DataSet') ?? []) |
| 93 | + .filter(isPublic) |
| 94 | + .forEach(dataSet => { |
| 95 | + const parent = dataSet.parentElement; |
| 96 | + const name = dataSet.getAttribute('name'); |
| 97 | + const isReferenced = Array.from( |
| 98 | + parent?.querySelectorAll( |
| 99 | + 'GSEControl, ReportControl, SampledValueControl, LogControl' |
| 100 | + ) ?? [] |
| 101 | + ).some(cb => cb.getAttribute('datSet') === name); |
| 102 | + |
| 103 | + if (parent && (!name || !isReferenced)) |
| 104 | + unreferencedDataSets.push(dataSet); |
| 105 | + }); |
| 106 | + |
| 107 | + this.unreferencedDataSets = unreferencedDataSets.sort((a, b) => { |
| 108 | + // sorting using the identity ensures sort order includes IED |
| 109 | + const aId = identity(a); |
| 110 | + const bId = identity(b); |
| 111 | + if (aId < bId) { |
| 112 | + return -1; |
| 113 | + } |
| 114 | + if (aId > bId) { |
| 115 | + return 1; |
| 116 | + } |
| 117 | + // names must be equal |
| 118 | + return 0; |
| 119 | + }); |
| 120 | + |
| 121 | + return html` |
| 122 | + <h1> |
| 123 | + ${translate('cleanup.unreferencedDataSets.title')} |
| 124 | + (${unreferencedDataSets.length}) |
| 125 | + <abbr slot="action"> |
| 126 | + <mwc-icon-button |
| 127 | + icon="info" |
| 128 | + title="${translate('cleanup.unreferencedDataSets.tooltip')}" |
| 129 | + > |
| 130 | + </mwc-icon-button> |
| 131 | + </abbr> |
| 132 | + </h1> |
| 133 | + <filtered-list multi class="cleanupUnreferencedDataSetsList" |
| 134 | + >${Array.from( |
| 135 | + unreferencedDataSets.map( |
| 136 | + item => |
| 137 | + html`<mwc-check-list-item twoline left value="${identity(item)}" |
| 138 | + ><span class="unreferencedDataSet" |
| 139 | + >${item.getAttribute('name')!} |
| 140 | + </span> |
| 141 | + <span> |
| 142 | + <mwc-icon-button |
| 143 | + label="Edit" |
| 144 | + icon="edit" |
| 145 | + class="editUnreferencedDataSet" |
| 146 | + @click=${(e: MouseEvent) => { |
| 147 | + e.stopPropagation(); |
| 148 | + e.target?.dispatchEvent( |
| 149 | + newSubWizardEvent(() => editDataSetWizard(item)) |
| 150 | + ); |
| 151 | + }} |
| 152 | + ></mwc-icon-button> |
| 153 | + </span> |
| 154 | + <span slot="secondary" |
| 155 | + >${item.closest('IED')?.getAttribute('name')} |
| 156 | + (${item.closest('IED')?.getAttribute('manufacturer') ?? |
| 157 | + 'No manufacturer defined'}) |
| 158 | + - |
| 159 | + ${item.closest('IED')?.getAttribute('type') ?? |
| 160 | + 'No Type Defined'}</span |
| 161 | + > |
| 162 | + </mwc-check-list-item>` |
| 163 | + ) |
| 164 | + )} |
| 165 | + </filtered-list> |
| 166 | + <footer> |
| 167 | + <mwc-button |
| 168 | + outlined |
| 169 | + icon="delete" |
| 170 | + class="cleanupUnreferencedDataSetsDeleteButton" |
| 171 | + label="${translate('cleanup.unreferencedDataSets.deleteButton')} (${(< |
| 172 | + Set<number> |
| 173 | + >this.selectedItems).size || '0'})" |
| 174 | + ?disabled=${(<Set<number>>this.selectedItems).size === 0 || |
| 175 | + (Array.isArray(this.selectedItems) && !this.selectedItems.length)} |
| 176 | + @click=${(e: MouseEvent) => { |
| 177 | + const cleanItems = Array.from( |
| 178 | + (<Set<number>>this.selectedItems).values() |
| 179 | + ).map(index => this.unreferencedDataSets[index]); |
| 180 | + const deleteActions = this.cleanDataSets(cleanItems); |
| 181 | + deleteActions.forEach(deleteAction => |
| 182 | + e.target?.dispatchEvent(newActionEvent(deleteAction)) |
| 183 | + ); |
| 184 | + }} |
| 185 | + ></mwc-button> |
| 186 | + </footer> |
| 187 | + `; |
| 188 | + } |
| 189 | + |
| 190 | + render(): TemplateResult { |
| 191 | + return html` |
| 192 | + <div class="cleanupUnreferencedDataSets"> |
| 193 | + <section tabindex="0">${this.renderUnreferencedDataSets()}</section> |
| 194 | + </div> |
| 195 | + `; |
| 196 | + } |
| 197 | + |
| 198 | + static styles = css` |
| 199 | + ${styles} |
| 200 | +
|
| 201 | + :host { |
| 202 | + width: 100vw; |
| 203 | + } |
| 204 | +
|
| 205 | + .cleanupUnreferencedDataSets { |
| 206 | + display: grid; |
| 207 | + grid-gap: 12px; |
| 208 | + padding: 8px 12px 16px; |
| 209 | + box-sizing: border-box; |
| 210 | + grid-template-columns: repeat(auto-fit, minmax(316px, 50%)); |
| 211 | + } |
| 212 | +
|
| 213 | + @media (max-width: 387px) { |
| 214 | + .cleanupUnreferencedDataSets { |
| 215 | + grid-template-columns: repeat(auto-fit, minmax(196px, auto)); |
| 216 | + } |
| 217 | + } |
| 218 | +
|
| 219 | + .editUnreferencedDataSet { |
| 220 | + --mdc-icon-size: 16px; |
| 221 | + } |
| 222 | +
|
| 223 | + .cleanupUnreferencedDataSetsDeleteButton { |
| 224 | + float: right; |
| 225 | + margin-bottom: 10px; |
| 226 | + margin-right: 10px; |
| 227 | + } |
| 228 | +
|
| 229 | + footer { |
| 230 | + display: flex; |
| 231 | + align-items: center; |
| 232 | + justify-content: flex-end; |
| 233 | + } |
| 234 | + `; |
| 235 | +} |
0 commit comments