2019-07-03 01:42:17 +00:00
|
|
|
class EditorLink extends EditorEntryBase {
|
2019-07-12 01:05:04 +00:00
|
|
|
constructor(id, entries) {
|
|
|
|
|
super(id);
|
2019-07-03 01:42:17 +00:00
|
|
|
|
2019-07-09 17:57:35 +00:00
|
|
|
this.elem_.innerText = '↓';
|
2019-07-03 01:42:17 +00:00
|
|
|
this.elem_.classList.add('link');
|
|
|
|
|
|
|
|
|
|
let nodeList = document.createElement('div');
|
2019-07-10 08:14:53 +00:00
|
|
|
this.nodes_ = new Editor(nodeList, [
|
|
|
|
|
[EditorNode, [2, 2]],
|
|
|
|
|
[EditorLabel, [0, 1]],
|
|
|
|
|
]);
|
2019-07-11 04:40:43 +00:00
|
|
|
this.nodes_.addNodeAfter(
|
|
|
|
|
entries && entries[0] ? entries[0].getLabel() : null);
|
|
|
|
|
this.nodes_.addNodeAfter(
|
|
|
|
|
entries && entries[1] ? entries[1].getLabel() : null);
|
2019-07-03 01:42:17 +00:00
|
|
|
this.elem_.appendChild(nodeList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
afterDomAdd() {
|
2019-07-09 17:57:35 +00:00
|
|
|
this.nodes_.selectNext();
|
2019-07-11 16:23:41 +00:00
|
|
|
let node = this.nodes_.getSelected().xArchObj;
|
|
|
|
|
if (node.getLabel() == '') {
|
|
|
|
|
node.startEdit();
|
2019-07-11 16:15:53 +00:00
|
|
|
}
|
2019-07-03 01:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
2019-07-14 03:19:15 +00:00
|
|
|
// TODO: factor out common base code with EditorGroup
|
2019-07-03 01:42:17 +00:00
|
|
|
serialize() {
|
2019-07-14 02:26:00 +00:00
|
|
|
return super.serialize({
|
2019-07-03 01:42:17 +00:00
|
|
|
type: 'link',
|
|
|
|
|
label: this.getLabel(),
|
2019-07-14 03:19:15 +00:00
|
|
|
labelObj: this.getLabelObj().serialize(),
|
2019-07-03 01:42:17 +00:00
|
|
|
from: this.getFrom().serialize(),
|
|
|
|
|
to: this.getTo().serialize(),
|
2019-07-14 02:26:00 +00:00
|
|
|
});
|
2019-07-03 01:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getFrom() {
|
2019-07-10 08:14:53 +00:00
|
|
|
return this.nodes_.getEntries(EditorNode)[0];
|
2019-07-03 01:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getTo() {
|
2019-07-10 08:14:53 +00:00
|
|
|
return this.nodes_.getEntries(EditorNode)[1];
|
2019-07-03 01:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getLabel() {
|
2019-07-14 03:19:15 +00:00
|
|
|
let label = this.getLabelObj();
|
2019-07-10 08:14:53 +00:00
|
|
|
return label ? label.getLabel() : null;
|
2019-07-03 01:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
2019-07-14 03:19:15 +00:00
|
|
|
setLabel(label, labelId) {
|
|
|
|
|
let obj = this.getLabelObj();
|
2019-07-10 08:14:53 +00:00
|
|
|
if (obj) {
|
|
|
|
|
obj.setLabel(label);
|
|
|
|
|
} else {
|
2019-07-14 03:19:15 +00:00
|
|
|
this.nodes_.addLabelBefore(labelId);
|
2019-07-10 08:14:53 +00:00
|
|
|
this.setLabel(label);
|
|
|
|
|
}
|
2019-07-03 01:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
2019-07-14 03:19:15 +00:00
|
|
|
getLabelObj() {
|
|
|
|
|
return this.nodes_.getEntries(EditorLabel)[0];
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-11 20:08:29 +00:00
|
|
|
flip() {
|
|
|
|
|
let entries = this.nodes_.getEntries(EditorNode);
|
|
|
|
|
let fromElem = entries[0].getElement();
|
|
|
|
|
let toElem = entries[1].getElement();
|
|
|
|
|
let fromHasFocus = document.activeElement == fromElem;
|
|
|
|
|
let toHasFocus = document.activeElement == toElem;
|
|
|
|
|
|
|
|
|
|
toElem.parentElement.insertBefore(toElem, fromElem);
|
|
|
|
|
|
|
|
|
|
if (fromHasFocus) {
|
|
|
|
|
fromElem.focus();
|
|
|
|
|
} else if (toHasFocus) {
|
|
|
|
|
toElem.focus();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-03 01:42:17 +00:00
|
|
|
onKeyDown(e) {
|
|
|
|
|
super.onKeyDown(e);
|
|
|
|
|
|
|
|
|
|
switch (e.key) {
|
|
|
|
|
case 'Enter':
|
|
|
|
|
case 'ArrowRight':
|
|
|
|
|
case 'l':
|
|
|
|
|
this.nodes_.selectNext();
|
2019-07-09 17:57:35 +00:00
|
|
|
e.stopPropagation();
|
|
|
|
|
e.preventDefault();
|
2019-07-03 01:42:17 +00:00
|
|
|
break;
|
2019-07-10 23:41:41 +00:00
|
|
|
|
2019-07-11 20:08:29 +00:00
|
|
|
case 'f':
|
|
|
|
|
this.flip();
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
break;
|
2019-07-03 01:42:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static unserialize(ser) {
|
2019-07-12 01:05:04 +00:00
|
|
|
let link = new EditorLink(ser.id);
|
2019-07-03 01:42:17 +00:00
|
|
|
link.nodes_.clear();
|
2019-07-10 21:35:03 +00:00
|
|
|
if (ser.label != null) {
|
2019-07-14 03:19:15 +00:00
|
|
|
link.setLabel(ser.label, ser.labelObj.id);
|
|
|
|
|
link.getLabelObj().setHighlight(ser.labelObj.highlight);
|
2019-07-10 21:35:03 +00:00
|
|
|
}
|
2019-07-10 23:41:41 +00:00
|
|
|
link.setHighlight(ser.highlight);
|
2019-07-03 01:42:17 +00:00
|
|
|
link.nodes_.unserialize([ser.from, ser.to]);
|
|
|
|
|
return link.getElement();
|
|
|
|
|
}
|
|
|
|
|
}
|