-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfoundation.ts
More file actions
212 lines (184 loc) · 5.59 KB
/
Copy pathfoundation.ts
File metadata and controls
212 lines (184 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { TemplateResult } from 'lit';
import { Edit } from '@openenergytools/open-scd-core';
import { MdFilledSelect } from '@scopedelement/material-web/select/MdFilledSelect';
import { MdFilledTextField } from '@scopedelement/material-web/textfield/MdFilledTextField';
import { SclCheckbox } from '@openenergytools/scl-checkbox';
import { SclSelect } from '@openenergytools/scl-select';
import { SclTextField } from '@openenergytools/scl-text-field';
export type WizardInputElement =
| SclTextField
| SclSelect
| SclCheckbox
| MdFilledTextField
| MdFilledSelect;
export type WizardActor = (
inputs: WizardInputElement[],
wizard: Element
) => Edit[];
export interface Wizard {
title: string;
content?: TemplateResult[];
primary?: {
icon: string;
label: string;
action: WizardActor;
auto?: boolean;
};
secondary?: {
icon: string;
label: string;
};
}
/** @returns the `value` or `maybeValue` of `input` depending on type. */
export function getValue(input: WizardInputElement): string | null {
return input.value;
}
/** @returns a new [[`tag`]] element owned by [[`doc`]]. */
export function createElement(
doc: Document,
tag: string,
attrs: Record<string, string | null>
): Element {
const element = doc.createElementNS(doc.documentElement.namespaceURI, tag);
Object.entries(attrs)
.filter(([_, value]) => value !== null)
.forEach(([name, value]) => element.setAttribute(name, value!));
return element;
}
export function getChildElementsByTagName(
parent: Element | null | undefined,
tag: string | null | undefined
): Element[] {
if (!parent || !tag) return [];
return Array.from(parent.children).filter(element => element.tagName === tag);
}
/** @returns reserved siblings names attributes */
export function reservedNames(element: Element, tagName?: string): string[] {
if (tagName)
return getChildElementsByTagName(element, tagName).map(
sibling => sibling.getAttribute('name')!
);
if (!element.parentElement) return [];
return getChildElementsByTagName(element.parentElement, element.tagName)
.filter(sibling => sibling !== element)
.map(sibling => sibling.getAttribute('name')!);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isSclTextfield(type: any): type is SclTextField {
return 'value' in type && 'multiplier' in type;
}
/** @returns the `multiplier` of `input` if available. */
export function getMultiplier(input: WizardInputElement): string | null {
if (isSclTextfield(input)) return input.multiplier;
return null;
}
/** @returns a clone of `element` with attributes set to values from `attrs`. */
export function cloneElement(
element: Element,
attrs: Record<string, string | null>
): Element {
const newElement = <Element>element.cloneNode(false);
Object.entries(attrs).forEach(([name, value]) => {
if (value === null) newElement.removeAttribute(name);
else newElement.setAttribute(name, value);
});
return newElement;
}
/** @returns the cartesian product of `arrays` */
export function crossProduct<T>(...arrays: T[][]): T[][] {
return arrays.reduce<T[][]>(
(a, b) => <T[][]>a.flatMap(d => b.map(e => [d, e].flat())),
[[]]
);
}
/** Whether `P` element is required within `Address` */
export const typeNullable: Partial<Record<string, boolean>> = {
IP: false,
'IP-SUBNET': false,
'IP-GATEWAY': true,
'OSI-TSEL': true,
'OSI-SSEL': true,
'OSI-PSEL': true,
'OSI-AP-Title': true,
'OSI-AP-Invoke': true,
'OSI-AE-Qualifier': true,
'OSI-AE-Invoke': true,
'OSI-NSAP': true,
'MAC-Address': false,
APPID: false,
'VLAN-ID': true,
'VLAN-PRIORITY': true,
'SNTP-Port': true,
'MMS-Port': true,
DNSName: true,
'UDP-Port': true,
'TCP-Port': true,
'C37-118-IP-Port': true,
IPv6: true,
'IPv6-SUBNET': true,
'IPv6-GATEWAY': true,
IPv6FlowLabel: true,
IPv6ClassOfTraffic: true,
'IPv6-IGMPv3Src': true,
'IP-IGMPv3Sr': true,
'IP-ClassOfTraffic': true,
};
export function getTypes(element: Element): string[] {
if (!element.ownerDocument.documentElement) return [];
const pTypes2003: string[] = [
'IP',
'IP-SUBNET',
'IP-GATEWAY',
'OSI-TSEL',
'OSI-SSEL',
'OSI-PSEL',
'OSI-AP-Title',
'OSI-AP-Invoke',
'OSI-AE-Qualifier',
'OSI-AE-Invoke',
'OSI-NSAP',
'VLAN-ID',
'VLAN-PRIORITY',
];
const pTypes2007B: string[] = [
...pTypes2003,
'SNTP-Port',
'MMS-Port',
'DNSName',
'UDP-Port',
'TCP-Port',
'C37-118-IP-Port',
];
const pTypes2007B4: string[] = [
...pTypes2007B,
'IPv6',
'IPv6-SUBNET',
'IPv6-GATEWAY',
'IPv6FlowLabel',
'IPv6ClassOfTraffic',
'IPv6-IGMPv3Src',
'IP-IGMPv3Sr',
'IP-ClassOfTraffic',
];
const scl: Element = element.ownerDocument.documentElement;
const type =
(scl.getAttribute('version') ?? '2003') +
(scl.getAttribute('revision') ?? '') +
(scl.getAttribute('release') ?? '');
if (type === '2003') return pTypes2003;
if (type === '2007B') return pTypes2007B;
return pTypes2007B4;
}
/** Sorts selected `ListItem`s to the top and disabled ones to the bottom. */
export function compareNames(a: Element | string, b: Element | string): number {
if (typeof a === 'string' && typeof b === 'string') return a.localeCompare(b);
if (typeof a === 'object' && typeof b === 'string')
return (a.getAttribute('name') ?? '').localeCompare(b);
if (typeof a === 'string' && typeof b === 'object')
return a.localeCompare(b.getAttribute('name')!);
if (typeof a === 'object' && typeof b === 'object')
return (a.getAttribute('name') ?? '').localeCompare(
b.getAttribute('name') ?? ''
);
return 0;
}