-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser-behavior.html
206 lines (194 loc) · 6 KB
/
parser-behavior.html
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<script>
"use strict";
/** Cleans text xml input of whitespace-elements */
var ParserBehavior = {
properties: {
type: String,
nextNodeId: {
type: Number,
value: 1,
},
nodes: {
type: Array,
value: function() { return []; }
},
buckets: {
type: Array,
value: function() { return []; }
},
nodesMap: {
type: Object,
value: function() { return {}; }
},
parser: {
type: Object,
value: new window.DOMParser()
},
/** Stringtable file data */
stringTableFileLink: String,
nextEntryID: Number,
entryCount: Number,
nextEventID: Number,
nextEndStateID: Number,
nextAddendumID: Number,
questType: String,
questEvents: {
type: Array,
value: function() { return []; }
}
},
cleanFormatting: function(node) {
var reBlank = /^\s*$/;
var child, next;
switch (node.nodeType) {
case 3: // Text node
if (reBlank.test(node.nodeValue)) {
node.parentNode.removeChild(node);
}
break;
case 1: // Element node
case 9: // Document node
child = node.firstChild;
while (child) {
next = child.nextSibling;
this.cleanFormatting(child);
child = next;
}
break;
}
},
basetypecontent_toXML: function() {
var result = '<?xml version="1.0" encoding="utf-8"?><' + this.type + ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">';
result += '<NextNodeID>' + this.nextNodeId + '</NextNodeID>';
result += '<Nodes>';
this.nodes.forEach(function(node) {
result += node.toXML();
});
result += '</Nodes>';
// Nur ein Bookmark um den Game Parser zufrieden zu stellen. Der Questeditor hat keine eigenen Bookmarks und ingame sind sie überhaupt nicht relevant
result += '<Bookmarks><Bookmark><Type>Unassigned</Type><Name /><NodeID>-1</NodeID><GhostNodeParentID>-1</GhostNodeParentID></Bookmark></Bookmarks>';
result += '<ClassExtender><ExtendedProperties /></ClassExtender>';
return result;
},
stringsFromXML: function(xml) {
if(typeof(xml) == "string") { // schon geparstes ist object
xml = this.parser.parseFromString(xml, 'text/xml');
this.cleanFormatting(xml);
}
if(xml.hasChildNodes()) {
var node;
for (var x = 0; x < xml.childNodes.length; x++) {
node = xml.childNodes[x];
if(node.nodeName == "StringTableFile") {
this.stringsFromXML(node); // outer layer
}
if(node.nodeName == "Name") {
this.stringTableFileLink = node.textContent;
}
if(node.nodeName == "NextEntryID") {
this.nextEntryID = node.textContent; // nicht wirklich wichtig, vom tool aber es gibt illegale Werte bei Obsidian Files
}
if(node.nodeName == "EntryCount") {
this.entryCount = node.textContent;
}
if(node.nodeName == "Entries") {
// muss der Subtyp implementieren
this.stringsFromXMLNode(node);
}
}
}
},
stringsToXML: function() {
var result = '<?xml version="1.0" encoding="utf-8"?><StringTableFile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">';
result += '<Name>' + this.stringTableFileLink + '</Name>';
result += '<NextEntryID>' + this.nextEntryID + '</NextEntryID>'; // Das was Obsidian verhunzt hat ordentlich machen
result += '<EntryCount>' + (this.nodes.length - 1) + '</EntryCount>'; // Conversation Node 0 has no text
result += '<Entries>';
for(var i = 0; i < this.nodes.length; i++) {
var text = this.nodes[i].defaultText;
result += '<Entry>';
result += '<ID>' + this.nodes[i].nodeId + '</ID>';
if(text !== undefined && text !== "") {
result += '<DefaultText>' + text + '</DefaultText>';
} else {
result += '<DefaultText />';
}
text = this.nodes[i].femaleText;
if(text !== undefined && text !== "") {
result += '<FemaleText>' + text + '</FemaleText>';
} else {
result += '<FemaleText />';
}
result += '</Entry>';
}
result += '</Entries>';
return result;
},
/** Place link targets in buckets, new reachable elements get in a new bucket */
place: function(links, i) {
var atLeastOne = false;
// breite zuerst, mit nur einer schleife wirds tiefe zuerst, dann landen noch mehr linien übereinander
Object.keys(links).forEach(function(link) {
if(links[link].endElement.placed === false) {
atLeastOne = true;
if(this.buckets[i] === undefined) this.buckets[i] = [];
this.buckets[i].push(links[link].endElement);
links[link].endElement.placed = true;
}
}, this);
Object.keys(links).forEach(function(link) {
if(links[link].working === undefined) {
if(atLeastOne) i += 1;
links[link].working = true; // prevent infinite loop on circles
this.place(links[link].endElement.links, i);
}
}, this);
},
/** gehört eigentlich in ein eigenes behavior */
selectNone: function(exceptThis) {
this.nodes.forEach(function(node) {
if(node.$.dialoguenode !== undefined) {
this.toggleClass("selected", false, node.$.dialoguenode.$.flowchartnode);
node.$.dialoguenode.$.flowchartnode.selected = 1;
} else {
this.toggleClass("selected", false, node.$.flowchartnode);
node.$.flowchartnode.selected = 1;
}
Object.keys(node.links).forEach(function(link) {
if(node.links[link].endElement == exceptThis) {
node.links[link].connect('#128bd8');
} else {
node.links[link].connect('#B22222');
}
});
}, this);
},
removeAllIncommingLinks: function(target) {
this.nodes.forEach(function(node) {
Object.keys(node.links).forEach(function(link) {
if(node.links[link].endElement == target) {
var l = node.links[link];
delete node.links[link];
this.logicalParent.node.removeChild(l);
}
}, this);
}, this);
},
/* Alle Referenzen auf die Node entfernen */
removeNode: function(node) {
for(var i = 0; i < this.buckets.length; i++) {
for(var j = 0; j < this.buckets[i].length; j++) {
if(this.buckets[i][j] == node) {
this.buckets[i].splice(j, 1);
}
}
}
this.nodes.forEach(function(value, index) {
if(value == node) {
delete this.nodes[index];
}
}, this);
delete this.nodesMap[node.id];
}
};
</script>