-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditorEntryBase.js
More file actions
118 lines (100 loc) · 2.89 KB
/
EditorEntryBase.js
File metadata and controls
118 lines (100 loc) · 2.89 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
<!--# include file="ListenUtils.js" -->
class EditorEntryBase extends ListenUtils {
constructor(id) {
super();
this.elem_ = document.createElement('li');
this.elem_.tabIndex = 0;
this.elem_.id = (id || ('entry' + idSource.getId()));
this.listen(this.elem_, 'focus', () => this.onElemFocus());
this.listen(this.elem_, 'keydown', (e) => this.onKeyDown(e));
this.elem_.xArchObj = this;
this.elem_.setAttribute('data-arch-class', this.constructor.name);
}
serialize(base) {
base.id = this.getId();
base.highlight = this.elem_.classList.contains('highlight');
return base;
}
remove() {
if (document.activeElement == this.elem_ ||
document.activeElement == document.body) {
if (this.elem_.nextElementSibling) {
this.elem_.nextElementSibling.focus();
} else if (this.elem_.previousElementSibling) {
this.elem_.previousElementSibling.focus();
} else if (this.elem_.parentElement) {
this.elem_.parentElement.focus();
}
}
this.elem_.remove();
this.clearListeners();
this.elem_.xArchObj = null;
}
wantFocus() {
return false;
}
getElement() {
return this.elem_;
}
getId() {
return this.elem_.id;
}
setHighlight(highlight) {
this.elem_.classList.toggle('highlight', highlight);
for (let elem of document.getElementsByClassName('grid-' + this.getId())) {
elem.classList.toggle('highlight', highlight);
}
// Do NOT refresh: this bypasses the rendering pipeline
this.elem_.dispatchEvent(
new CustomEvent('snapshotRequest', { bubbles: true }));
}
toggleHighlight() {
this.setHighlight(!this.elem_.classList.contains('highlight'));
}
onElemFocus() {
this.elem_.scrollIntoView({block: 'nearest'});
}
onKeyDown(e) {
switch (e.key) {
case ' ':
this.toggleHighlight();
e.stopPropagation();
e.preventDefault();
break;
}
}
afterDomAdd() {
}
requestRender() {
this.elem_.dispatchEvent(
new CustomEvent('renderRequest', { bubbles: true }));
}
static addBefore(container, elem, ...rest) {
let entry = new this(null, ...rest);
container.insertBefore(entry.getElement(), elem);
entry.afterDomAdd();
return entry;
}
static addAfter(container, elem, ...rest) {
let entry = new this(null, ...rest);
container.insertBefore(entry.getElement(), elem ? elem.nextSibling : null);
entry.afterDomAdd();
return entry;
}
static unserialize(ser) {
switch (ser.type) {
case 'group':
return EditorGroup.unserialize(ser);
case 'help':
return EditorHelp.unserialize(ser);
case 'label':
return EditorLabel.unserialize(ser);
case 'link':
return EditorLink.unserialize(ser);
case 'node':
return EditorNode.unserialize(ser);
case 'tag':
return EditorTag.unserialize(ser);
}
}
}