Files
architype/EditorLink.js

101 lines
2.0 KiB
JavaScript
Raw Normal View History

2019-07-03 01:42:17 +00:00
class EditorLink extends EditorEntryBase {
constructor() {
super();
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-03 01:42:17 +00:00
this.nodes_.addNodeAfter();
this.nodes_.addNodeAfter();
this.elem_.appendChild(nodeList);
}
afterDomAdd() {
this.nodes_.selectNext();
this.nodes_.getSelected().xArchObj.startEdit();
2019-07-03 01:42:17 +00:00
}
serialize() {
return {
type: 'link',
label: this.getLabel(),
from: this.getFrom().serialize(),
to: this.getTo().serialize(),
};
}
exportGraphviz() {
if (this.getFrom().getLabel() == '' || this.getTo().getLabel() == '') {
return [];
}
let label = '';
if (this.getLabel() != '') {
label = ' [label="' + this.getLabel() + '"]';
}
let ret = [];
for (let from of this.from) {
for (let to of this.to) {
ret.push('"' + from.id + '" -> "' + to.id + '"' + label + ';');
}
}
return ret;
};
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-10 08:14:53 +00:00
let label = this.nodes_.getEntries(EditorLabel)[0];
return label ? label.getLabel() : null;
2019-07-03 01:42:17 +00:00
}
setLabel(label) {
2019-07-10 08:14:53 +00:00
let obj = this.nodes_.getEntries(EditorLabel)[0];
if (obj) {
obj.setLabel(label);
} else {
this.nodes_.addLabelBefore();
this.setLabel(label);
}
2019-07-03 01:42:17 +00:00
}
getElement() {
return this.elem_;
}
onKeyDown(e) {
super.onKeyDown(e);
switch (e.key) {
case 'Enter':
case 'ArrowRight':
case 'l':
this.nodes_.selectNext();
e.stopPropagation();
e.preventDefault();
2019-07-03 01:42:17 +00:00
break;
}
}
static unserialize(ser) {
let link = new EditorLink();
link.nodes_.clear();
2019-07-10 08:14:53 +00:00
link.setLabel(ser.label);
2019-07-03 01:42:17 +00:00
link.nodes_.unserialize([ser.from, ser.to]);
return link.getElement();
}
}