1
- import { getNameAttribute } from "../../foundation.js" ;
1
+ import { LitElement , property } from "lit-element" ;
2
+ import { getInstanceAttribute , getNameAttribute } from "../../foundation.js" ;
3
+
4
+ /** Base class for all containers inside the IED Editor. */
5
+ export class Container extends LitElement {
6
+ @property ( { attribute : false } )
7
+ element ! : Element ;
8
+
9
+ @property ( )
10
+ ancestors : Element [ ] = [ ] ;
11
+
12
+ constructor ( ) {
13
+ super ( ) ;
14
+
15
+ this . addEventListener ( 'focus' , ( event ) => {
16
+ event . stopPropagation ( ) ;
17
+ const pathOfAncestorNames = this . ancestors . map ( ancestor => getTitleForElementPath ( ancestor ) ! ) ;
18
+ pathOfAncestorNames . push ( getTitleForElementPath ( this . element ) ! ) ;
19
+
20
+ this . dispatchEvent (
21
+ newFullElementPathEvent (
22
+ pathOfAncestorNames
23
+ )
24
+ ) ;
25
+ } ) ;
26
+
27
+ this . addEventListener ( 'blur' , ( ) => {
28
+ this . dispatchEvent (
29
+ newFullElementPathEvent (
30
+ this . ancestors . map ( ancestor => getTitleForElementPath ( ancestor ) ! )
31
+ )
32
+ ) ;
33
+ } ) ;
34
+ }
35
+ }
2
36
3
37
/**
4
38
* Search for an element with a passed tag-name in the list of ancestors passed.
@@ -55,6 +89,24 @@ export function getInstanceDAElement(parentInstance: Element | null, da: Element
55
89
return null ;
56
90
}
57
91
92
+ export function getTitleForElementPath ( element : Element ) : string {
93
+ switch ( element . tagName ) {
94
+ case 'LN' :
95
+ case 'LN0' : {
96
+ return element . getAttribute ( 'lnClass' ) ! ;
97
+ }
98
+ case 'LDevice' : {
99
+ return ( getNameAttribute ( element ) ?? getInstanceAttribute ( element ) ) !
100
+ }
101
+ case 'Server' : {
102
+ return 'Server' ;
103
+ }
104
+ default : {
105
+ return element . getAttribute ( 'name' ) ! ;
106
+ }
107
+ }
108
+ }
109
+
58
110
/**
59
111
* Get the 'Val' element of another element.
60
112
* @param element - The element to search for an 'Val' element.
@@ -63,3 +115,25 @@ export function getInstanceDAElement(parentInstance: Element | null, da: Element
63
115
export function getValueElement ( element : Element ) : Element | null {
64
116
return element . querySelector ( 'Val' ) ;
65
117
}
118
+
119
+ export interface FullElementPathDetail {
120
+ elementNames : string [ ] ;
121
+ }
122
+ export type FullElementPathEvent = CustomEvent < FullElementPathDetail > ;
123
+ export function newFullElementPathEvent (
124
+ elementNames : string [ ] ,
125
+ eventInitDict ?: CustomEventInit < FullElementPathDetail >
126
+ ) : FullElementPathEvent {
127
+ return new CustomEvent < FullElementPathDetail > ( 'full-element-path' , {
128
+ bubbles : true ,
129
+ composed : true ,
130
+ ...eventInitDict ,
131
+ detail : { elementNames, ...eventInitDict ?. detail } ,
132
+ } ) ;
133
+ }
134
+
135
+ declare global {
136
+ interface ElementEventMap {
137
+ [ 'full-element-path' ] : FullElementPathEvent ;
138
+ }
139
+ }
0 commit comments