-
Notifications
You must be signed in to change notification settings - Fork 0
/
flowChartNode-behavior.html
339 lines (308 loc) · 9.75 KB
/
flowChartNode-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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<link rel="import" href="scripts-element.html"><script>
"use strict";
var UIBehavior = {
properties: {
/** Helper for placing Nodes in Columns */
placed: {
type: Boolean,
value: false
},
draggable: {
value: "true",
reflectToAttribute: true
},
nodeId: {
type: Number
},
id: {
computed: 'setId(type, nodeId)',
reflectToAttribute: true
},
/** Direction in Drag and Drop events: from or to */
direction: String,
/** Parent Quest or Conversation Root Element, limits the range of selectNone() */
parent_: Object
},
listeners: {
'dragstart': 'onDragStart',
'dragover': 'onDragOver',
'dragenter': 'onDragEnter',
'dragleave': 'onDragLeave',
'drop': 'onDrop',
'dragend': 'onDragEnd',
'tap': 'fireSelectedEvent'
},
setId: function(type, nodeId) {
return type + nodeId; // node id sollte unique bleiben, TODO enforce davon
},
factoryImpl: function(parent_) {
this.parent_ = parent_;
},
fireSelectedEvent: function(event) {
this.fire('iron-signal', {
name: "flow-chart-node-selected",
data: [this]
});
this.parent_.selectNone(this);
if (this.$.dialoguenode !== undefined) {
this.toggleClass("selected", true, this.$.dialoguenode.$.flowchartnode);
this.$.dialoguenode.$.flowchartnode.selected = 4;
} else {
this.toggleClass("selected", true, this.$.flowchartnode);
this.$.flowchartnode.selected = 4;
}
Object.keys(this.links).forEach(function(link) {
if (this.links[link].startElement == this) { // die node selbst hat nur ausgehende links
this.links[link].connect("#92eb68");
}
}, this);
},
setText: function(defaultText, femaleText) {
this.defaultText = defaultText;
this.femaleText = femaleText;
},
/** Add links between elements on attach of the node. Then the calculations of the links will work. */
attached: function() {
var links = this.links;
if(typeof(this.links) == "function") {
links = this.links();
}
Object.keys(links).forEach(function(link) {
this.parentElement.parentElement.appendChild(links[link]);
}, this);
},
//http://www.quirksmode.org/blog/archives/2009/09/the_html5_drag.html Ja verdammt, drag and drop ist fürn arsch, und das wird auch in HTML 5.1 so bleiben
onDragStart: function(event) {
event.dataTransfer.effectsAllowed = 'link';
//event.dataTransfer.setData(this.type, this.id); // type could be anything, as long as it is string, oh and data have to be string too …, haha! exeption IE there type MUST be 'text', so this yust dont work, great!
event.dataTransfer.setData('text', this.type);
localStorage.setItem(this.type, this.id);
this.direction = "from";
},
onDragEnter: function(event) {
var knownTypes = ["TalkNode", "PlayerResponseNode"]; // usw.
var type = {val: 0};
knownTypes.some(function(knownType) {
this.val = localStorage.getItem(knownType);
return this.val !== undefined;
}, type);
// if (type === this.type) { // nur gleiche typen lassen sich linken // todo questnode und objective node sind gleich
if (type.val !== undefined) {
event.preventDefault();
}
},
onDragOver: function(event) {
var knownTypes = ["TalkNode", "PlayerResponseNode"]; // usw.
var type = {val: 0};
knownTypes.some(function(knownType) {
this.val = localStorage.getItem(knownType);
return this.val !== undefined;
}, type);
// if (type === this.type) { // nur gleiche typen lassen sich linken
if (type.val !== undefined) {
event.preventDefault();
event.dataTransfer.dropEffect = 'link';
if (this.direction !== "from") {
this.direction = "to";
}
}
},
onDragLeave: function(event) {
if (this.direction !== "from") {
this.direction = "";
}
},
onDrop: function(event) {
event.preventDefault();
// todo: handling für verschiedene nodes, prototypisch für überhaupt links
var type = event.dataTransfer.getData('text');
var id = localStorage.getItem(type);
localStorage.removeItem(type);
if (id !== this.id) { // selbst linken ist nicht
var from = document.getElementById(id);
if (this.links[from.id] === undefined) { // links sind nur in eine richtung zu ziehen // todo sichtbar machen der richtung
from.createLink(this);
}
}
this.direction = ""; // drag target
},
onDragEnd: function(event) {
console.log("Fin");
this.direction = ""; // drag source
}
};
var ChildNodeBehavior = {
properties: {
links: {
type: Object,
value: function() { return {}; }
}
}
};
var FlowChartNodeBehavior = {
properties: {
type: String,
nodeId: { // no same name allowed
type: Number, // benötigt iron-input >= 1.0.5
notify: true,
},
comments: {
type: Array,
value: function() { return []; }
},
links: {
type: Object,
value: function() { return {}; }
},
enterScripts: {
type: Array,
value: function() { return []; }
},
exitScripts: {
type: Array,
value: function() { return []; }
},
conditions: {
type: Array,
value: function() { return []; }
},
conditionCount: Number,
editElements: {
type: Array,
value: function() { return []; }
},
// TODO language
/** String from Stringtable */
defaultText: String,
/** Female String from Stringtable */
femaleText: String,
linkXML: Object,
/** Copy from UIBehavior, only for displaying in the template*/
direction: String
},
addComment: function(event, eventDetail, text) {
var comment = new Comment(this, this.comments.length + 1);
if (text !== undefined)
comment.setText(text);
this.push("comments", comment);
Polymer.dom(this.$$("comments")).appendChild(comment);
},
removeComment: function(index) {
console.log("remove Comment");
var comment = this.splice("comments", index - 1, 1)[0]; // splice liefert ein array
Polymer.dom(this.$$("comments")).removeChild(comment); // die remove child erwartet eine node, deswegen über comment und nicht über id
},
addCondition: function(event, eventDetail) {
var condition = new ConditionElement(0, false); // TODO
// type = ?
condition.fullFunction = PoeQuestEditor.Conditionals.get("Boolean IsActive(Guid)");
this.push("conditions", condition);
},
toggleEdit: function() {
this.$.editpanel.hidden = !this.$.editpanel.hidden;
},
toggleConditions: function() {
this.$.conditions.hidden = !this.$.conditions.hidden;
this.conditions.forEach(function(condition) {
condition.show();
});
},
toggleEnterScripts: function() {
this.$.enterScripts.hidden = !this.$.enterScripts.hidden;
this.enterScripts.forEach(function(script) {
script.show();
});
},
toggleExitScripts: function() {
this.$.exitScripts.hidden = !this.$.exitScripts.hidden;
this.exitScripts.forEach(function(script) {
script.show();
});
},
fromXML: function(xml) {
if (xml.hasChildNodes()) {
this.type = xml.attributes[0].value;
var node;
for (var x = 0; x < xml.childNodes.length; x++) {
node = xml.childNodes[x];
if (node.nodeName == "NodeID") {
//this.nodeId = parseInt(node.textContent);
this.nodeId = node.textContent;
}
if (node.nodeName == "Comments") {
var _;
for (var c = 0; c < node.childNodes.length; c++) {
this.addComment(_, _, node.childNodes[c].textContent);
}
}
if (node.nodeName == "Links") {
this.linkXML = node;
}
if (node.nodeName == "Conditionals") {
var conditionElement = new ConditionElement(0, true);
this.conditionCount = conditionElement.fromXML(node);
this.push('conditions', conditionElement);
Polymer.dom(this.$$("conditions")).appendChild(conditionElement);
}
if (node.nodeName == "OnEnterScripts") {
this.scriptsFromXML("enterScripts", node);
}
if (node.nodeName == "OnExitScripts") {
this.scriptsFromXML("exitScripts", node);
}
// onUpdateScripts werden nicht verwendet
}
}
return this.nodeId;
},
toXML: function(xmlstring) {
var result = "<NodeID>" + this.nodeId + "</NodeID><Comments>";
this.comments.forEach(function (comment) {
result += comment.toXML();
});
result += "</Comments>";
result += "<PackageID>1</PackageID>"; // siehe xsd, 1 steht für base game und ist momentan der Einzige sinnvolle Wert
result += "<ContainerNodeID>-1</ContainerNodeID>"; // todo ?
result += "<Links>";
Object.keys(this.links).forEach(function (link) {
result += this.links[link].toXML();
}, this);
result += "</Links>";
result += "<ClassExtender><ExtendedProperties /></ClassExtender>";
result += "<Conditionals>";
this.conditions.forEach(function(condition) {
result += condition.toXML(0);
});
result += "</Conditionals>";
result += "<OnEnterScripts>";
this.enterScripts.forEach(function(scriptCall) {
result += "<ScriptCall>";
result += scriptCall.functionCallToXML();
result += "</ScriptCall>";
});
result += "</OnEnterScripts>";
result += "<OnExitScripts>";
this.exitScripts.forEach(function(scriptCall) {
result += "<ScriptCall>";
result += scriptCall.functionCallToXML();
result += "</ScriptCall>";
});
result += "</OnExitScripts>";
result += "<OnUpdateScripts />";
return result + xmlstring;
},
/** @param target - Name of Variable enterScripts or exitScripts */
scriptsFromXML: function(target, xml) {
for (var s = 0; s < xml.childNodes.length; s++) {
if (xml.childNodes[s].hasChildNodes()) {
for (var d = 0; d < xml.childNodes[s].childNodes.length; d++) {
var scriptElement = new ScriptElement();
scriptElement.functionCallFromXML(xml.childNodes[s].childNodes[d]);
this.push(target, scriptElement);
Polymer.dom(this.$$(target)).appendChild(scriptElement);
}
}
}
},
};
</script>