forked from y-lohse/graphink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
328 lines (295 loc) · 7.31 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Graphink</title>
<style>
/*------------------------------------*\
RESET
\*------------------------------------*/
/* http://meyerweb.com/eric/tools/css/reset/
v2.0b1 | 201101
NOTE:WORK IN PROGRESS
USE WITH CAUTION AND TEST WITH ABANDON */
html,body,div,span,applet,object,iframe,
h1,h2,h3,h4,h5,h6,p,blockquote,pre,
a,abbr,acronym,address,big,cite,code,
del,dfn,em,img,ins,kbd,q,s,samp,
small,strike,strong,sub,sup,tt,var,
b,u,i,center,
dl,dt,dd,ol,ul,li,
fieldset,form,label,legend,
table,caption,tbody,tfoot,thead,tr,th,td,
article,aside,canvas,details,figcaption,figure,
footer,header,hgroup,menu,nav,section,summary,
time,mark,audio,video{
margin:0;
padding:0;
border:0;
outline:0;
font-size:100%;
font:inherit;
vertical-align:baseline;
}
/* HTML5 display-role reset for older browsers */
article,aside,details,figcaption,figure,
footer,header,hgroup,menu,nav,section{
display:block;
}
body{
line-height:1;
}
ol,ul{
list-style:none;
}
blockquote,q{
quotes:none;
}
blockquote:before,blockquote:after,
q:before,q:after{
content:’’;
content:none;
}
/* remember to define visible focus styles!
:focus{
outline:?????;
} */
/* remember to highlight inserts somehow! */
ins{
text-decoration:none;
}
del{
text-decoration:line-through;
}
table{
border-collapse:collapse;
border-spacing:0;
}
/*custom styles*/
body,html{
width: 100%;
height: 100%;
}
#selection{
max-width: 650px;
margin: 20px auto;
text-align: center;
}
h1{
margin-bottom: 10px;
}
#file-input{
display: none;
}
.loader{
font-size: 14px;
font-weight: normal;
color: #1467a3;
cursor: pointer;
background: none;
border: none;
padding: 0;
text-decoration: underline;
}
#cy {
display: block;
width: 100%;
height: 100%;
visibility: hidden;
}
</style>
<link href='https://fonts.googleapis.com/css?family=Montserrat' rel='stylesheet' type='text/css'>
</head>
<body>
<div id="selection">
<h1>
Graphink
</h1>
<p>
Playing around with ink and graphs.
</p>
<input type="file" id="file-input" accept=".json" />
<label for="file-input" class="loader">Load a .json file</label>
or
<button class="loader" onclick="loadSample()">The Intercept</button>
</div>
<div id="cy">
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/es6-promise/3.2.2/es6-promise.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fetch/1.0.0/fetch.min.js"></script>
<script src="ink.js"></script>
<script src="cytoscape.js"></script>
<script>
var myStory,
nodes = {},
palette = ['#AD343E', '#0B3954', '#F2AF29', '#2E4057', '#2EC4B6', '#FF570A', '#774C60', '#1A1423', '#5E0B15'];
document.addEventListener('DOMContentLoaded', function(){
document.querySelector('#file-input').addEventListener('change', function(event){
var file = event.target.files[0];
if (!file) return;
var reader = new FileReader();
reader.onload = function(e) {
var inkFile = e.target.result;
init(inkFile);
};
reader.readAsText(file);
});
});
function loadSample(){
fetch('intercept.ink.json')
.then(function(response){
return response.text();
})
.then(function(inkFile){
init(inkFile);
});
}
function init(inkFile){
//generate the story tree
myStory = new inkjs.Story(inkFile);
exploreRecursively(null);
console.log(nodes)
//set up the graph
var graphNodes = [];
for (var id in nodes){
var node = nodes[id];
graphNodes.push({
group: 'nodes',
data: {
id: node.id,
text: node.text.substring(0, 20)+'...',
fulltext: node.text,
color: getBackgroundColorFromId(node.id),
}
});
}
for (var id in nodes){
var node = nodes[id];
node.choices.forEach(choice => {
if (!nodes[choice.next]){
console.log('skipping choice, path '+choice.next+' not generated')
return;
}
graphNodes.push({
group: 'edges',
selectable: false,
grabable: false,
data: {
id: node.id + '-c' + choice.index,
text: choice.text,
source: node.id,
target: choice.next,
color: getBackgroundColorFromId(node.id),
}
});
});
}
var cy = cytoscape({
container: document.getElementById('cy'),
elements: graphNodes,
style: [{
selector: 'node',
style: {
'width': '30px',
'height': '30px',
'background-color': 'data(color)',
'label': 'data(text)',
'color': '#333',
'transition-property': 'width, height',
'transition-duration': .6,
}
},
{
selector: 'node:selected',
style: {
'width': '50px',
'height': '50px',
'label': 'data(fulltext)',
'text-wrap': 'wrap',
'text-max-width': '300px',
'text-background-color': '#fff',
'text-background-opacity': '.8'
}
},
{
selector: 'edge',
style: {
'width': 3,
'line-color': 'data(color)',
'source-label': 'data(text)',
'source-text-offset': '100%',
'text-opacity': .6,
}
}],
layout: {
name: 'breadthfirst',
directed: true,
}
});
document.querySelector('#selection').style.display = 'none';
document.querySelector('#cy').style.visibility = 'visible';
}
function getCurrentNodeId(){
return myStory.state.currentPath ? myStory.state.currentPath.toString() : '';
}
function getBackgroundColorFromId(id){
id = id.indexOf('.') > 0 ? id.substr(0, id.indexOf('.')) : id;
var value = id.split().map(c => c.charCodeAt(0)).reduce((a, b) => a + b, 0);
return palette[value % palette.length]
}
function exploreRecursively(parent){
var id = getCurrentNodeId();
var text = '';
try{
text = myStory.ContinueMaximally()
}
catch(e){
console.warn(e.message+' at '+id);
text = e.message;
}
var node = {
id: id,
text: text,
parent: parent,
choices: new Array(myStory.currentChoices.length)
};
//check if we need to create a variation of an existing node
var variantCounter = 0,
originalId = id;
while(nodes[id]){
var textVariation = nodes[id].text != node.text,
choiceVariation = nodes[id].choices.length != node.choices.length;
if (!(choiceVariation && textVariation)){
//te node with the same id has the same choices, we can discard this one
return id;
}
if (++variantCounter > 10){
console.log('exited after '+variantCounter+' identical variations');
return originalId;
}
node.id = id = originalId + '.v-' + variantCounter;
}
nodes[id] = node;
if (myStory.currentChoices.length > 0){
for (let i = 0; i < myStory.currentChoices.length; ++i) {
// console.log(node.id + ': choice ' + i);
let choice = myStory.currentChoices[i];
let choiceNode = {
id: choice.pathStringOnChoice,
index: choice.index,
text: choice.text,
next: null
};
//explore the choice
let stateBeforeChoice = myStory.state.toJson();
myStory.ChooseChoiceIndex(i);
choiceNode.next = exploreRecursively(node.id);
node.choices[i] = choiceNode;
myStory.state.LoadJson(stateBeforeChoice);
}
}
return id;
}
</script>
</body>
</html>