2019-07-03 01:42:17 +00:00
|
|
|
class EditorNode extends EditorEntryBase {
|
2019-07-11 04:25:06 +00:00
|
|
|
constructor(label) {
|
2019-07-03 01:42:17 +00:00
|
|
|
super();
|
|
|
|
|
|
|
|
|
|
this.elem_.classList.add('node');
|
|
|
|
|
|
|
|
|
|
this.input_ = document.createElement('input');
|
|
|
|
|
this.input_.type = 'text';
|
|
|
|
|
this.input_.placeholder = 'node name';
|
|
|
|
|
this.listen(this.input_, 'keydown', (e) => this.onInputKeyDown(e));
|
|
|
|
|
this.listen(this.input_, 'input', (e) => this.onInput());
|
|
|
|
|
this.elem_.appendChild(this.input_);
|
2019-07-11 04:25:06 +00:00
|
|
|
|
|
|
|
|
if (label) {
|
|
|
|
|
this.setLabel(label);
|
|
|
|
|
}
|
2019-07-03 01:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
afterDomAdd() {
|
|
|
|
|
this.input_.focus();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
serialize() {
|
|
|
|
|
return {
|
|
|
|
|
type: 'node',
|
|
|
|
|
label: this.getLabel(),
|
2019-07-10 23:19:39 +00:00
|
|
|
highlight: this.elem_.classList.contains('highlight'),
|
2019-07-03 01:42:17 +00:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getLabel() {
|
|
|
|
|
return this.input_.value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setLabel(label) {
|
|
|
|
|
this.input_.value = label;
|
|
|
|
|
this.onInput();
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-10 23:19:39 +00:00
|
|
|
setHighlight(highlight) {
|
|
|
|
|
this.elem_.classList.toggle('highlight', highlight);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-03 01:42:17 +00:00
|
|
|
wantFocus() {
|
|
|
|
|
return this.getLabel() == '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isSoft() {
|
|
|
|
|
// Nested nodes are presumed to be references to other nodes if they exist
|
|
|
|
|
let iter = this.elem_.parentElement;
|
|
|
|
|
for (let iter = this.elem_.parentElement; iter; iter = iter.parentElement) {
|
|
|
|
|
if (iter.xArchObj) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onInput() {
|
2019-07-11 04:44:33 +00:00
|
|
|
this.input_.setAttribute('data-arch-refresh', '');
|
2019-07-03 01:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onInputKeyDown(e) {
|
|
|
|
|
switch (e.key) {
|
|
|
|
|
case 'Enter':
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
if (this.elem_.nextElementSibling &&
|
|
|
|
|
this.elem_.nextElementSibling.xArchObj &&
|
|
|
|
|
this.elem_.nextElementSibling.xArchObj.wantFocus()) {
|
|
|
|
|
this.elem_.nextElementSibling.xArchObj.startEdit();
|
|
|
|
|
} else {
|
|
|
|
|
this.stopEdit();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'Escape':
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
this.stopEdit();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'ArrowUp':
|
|
|
|
|
case 'ArrowDown':
|
|
|
|
|
case 'PageUp':
|
|
|
|
|
case 'PageDown':
|
|
|
|
|
this.stopEdit();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onKeyDown(e) {
|
|
|
|
|
super.onKeyDown(e);
|
|
|
|
|
|
|
|
|
|
switch (e.key) {
|
|
|
|
|
case 'Enter':
|
|
|
|
|
this.startEdit();
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
break;
|
2019-07-10 23:19:39 +00:00
|
|
|
|
|
|
|
|
case ' ':
|
|
|
|
|
this.elem_.classList.toggle('highlight');
|
|
|
|
|
this.onInput();
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
break;
|
2019-07-03 01:42:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
startEdit() {
|
|
|
|
|
this.input_.focus();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stopEdit() {
|
|
|
|
|
this.elem_.focus();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static unserialize(ser) {
|
|
|
|
|
let node = new EditorNode();
|
|
|
|
|
node.setLabel(ser.label);
|
2019-07-10 23:19:39 +00:00
|
|
|
node.setHighlight(ser.highlight);
|
2019-07-03 01:42:17 +00:00
|
|
|
return node.getElement();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|