-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoscd-menu-open.ts
More file actions
41 lines (30 loc) · 1005 Bytes
/
oscd-menu-open.ts
File metadata and controls
41 lines (30 loc) · 1005 Bytes
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
import { newOpenEvent } from '@omicronenergy/oscd-api/utils.js';
class OscdMenuOpen extends HTMLElement {
private input: HTMLInputElement;
constructor() {
super();
const shadow = this.attachShadow({ mode: 'open' });
this.input = document.createElement('input');
this.input.type = 'file';
this.input.addEventListener('click', (event: MouseEvent) => {
(event.target as HTMLInputElement).value = '';
});
this.input.addEventListener('change', this.openDoc.bind(this));
shadow.appendChild(this.input);
}
run() {
this.input.click();
}
async openDoc(event: Event): Promise<void> {
const file = (event.target as HTMLInputElement)?.files?.item(0);
if (!file) {
return;
}
const text = await file.text();
const docName = file.name;
const doc = new DOMParser().parseFromString(text, 'application/xml');
this.dispatchEvent(newOpenEvent(doc, docName));
this.input.onchange = null;
}
}
export default OscdMenuOpen;