-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSEMap.html
589 lines (523 loc) · 24.7 KB
/
SEMap.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
<!DOCTYPE html>
<html>
<head>
<title>SE Map</title>
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1, maximum-scale=1">
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/cytoscape/3.2.5/cytoscape.min.js"></script>
<script src="https://cdn.rawgit.com/cytoscape/cytoscape.js-cola/1.6.0/cola.js"></script>
<script src="https://cdn.rawgit.com/cytoscape/cytoscape.js-cola/1.6.0/cytoscape-cola.js"></script>
<link rel="stylesheet" href="mini-default.min.css">
<style>
body { margin: 0; }
.responsive-label { align-items: center; }
.wide { margin: auto; }
#cy
{
border: solid;
border-width: 1;
height: calc(100vh - 70px);
background-color: #eeeeee;
}
</style>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-3425939-8"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-3425939-8');
</script>
<script>
function databaseHTTP()
{
var credentials = {};
var bIsDynamicWebsite = true;
this.isDynamicWebsite = function () { return bIsDynamicWebsite; }
this.ajax = function (_type, _data, _successFunction, _error)
{
if (bIsDynamicWebsite == false)
return;
$.ajax({
type: _type,
url: "./SEMapDatabase.php",
dataType: "json",
data: _data,
success: function (data)
{
if (data.res!="OK")
{
alert(data.res);
}
else
{
if (_successFunction!=undefined)
_successFunction(data);
}
},
error: function (data) { if (_error!=undefined) _error(data); }
});
}
this.setCredentials = function (_credentials, successFunction)
{
credentials = _credentials;
this.ajax("POST", JSON.stringify({credentials:credentials}), successFunction);
}
this.POST = function (nodes, edges, successFunction)
{
this.ajax("POST", JSON.stringify({nodes:nodes, edges:edges, credentials:credentials }), successFunction);
}
this.DELETE = function (nodes, edges, successFunction)
{
this.ajax("DELETE", JSON.stringify({nodes:nodes, edges:edges, credentials:credentials }), successFunction);
}
this.LoadDataFromDatabase = function (successFunction)
{
this.ajax("GET", undefined, successFunction, function(data)
{
// We failed to get the data from the php backend,
// this means we are in a static website, in this case
// we try to get the data from a static json file.
// This json file can be generated with the generateJSONs.sh script
bIsDynamicWebsite = false;
$.ajax({
type: "GET",
url: "./SEMap_en.json",
dataType: "json",
success: function(data)
{
successFunction(data);
}
});
});
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Database loading, deleting, saving
//
function databaseIO(_backend)
{
var backend = _backend;
this.isDynamicWebsite = function () { return backend.isDynamicWebsite(); }
this.setCredentials = function (user,pass, callback)
{
backend.setCredentials({ username: user, password : pass}, callback)
}
this.POST = function(eles, successFunction)
{
var nodes = [];
var edges = [];
for(var i=0;i<eles.length;i++)
{
var e = eles[i];
if (e.isNode())
{
nodes.push({id:e.id(),l:e.data("label"), x:e.position().x, y:e.position().y});
}
else if (e.isEdge())
{
edges.push({id:e.id(), s:e.data("source"), t:e.data("target"), l:e.data("label") });
}
}
backend.POST(nodes,edges, successFunction);
}
this.DELETE = function(eles, successFunction)
{
var nodes = [];
var edges = [];
for(var i=0;i<eles.length;i++)
{
var e = eles[i];
if (e.isNode())
{
nodes.push(e.id());
}
else if (e.isEdge())
{
edges.push(e.id());
}
}
backend.DELETE(nodes,edges, successFunction);
}
this.LoadDataFromDatabase = function ( doneFunction )
{
// load elements from database and build diagram
backend.LoadDataFromDatabase( function(data)
{
var elms = []
var nodes = data["nodes"];
for(var node in nodes)
{
var n = nodes[node];
elms.push( { group: "nodes", data: { id: n["id"], label: n["q"] }, position:{x:n["x"], y:n["y"]} })
}
var edges = data["edges"];
for(var edge in edges)
{
var e = edges[edge];
elms.push( { group: "edges", data: { id: e["id"], source: e["s"], target: e["t"], label: e["l"] } })
}
cy.add(elms);
SetStyle();
cy.fit();
cy.autolock( true );
doneFunction();
});
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Dynamics
//
var data = new databaseHTTP();
var base = new databaseIO(new databaseHTTP());
var bIsLoggedIn = false;
function SetStyle()
{
var nds = cy.nodes()
for(var i=0;i<nds.length;i++)
{
if (nds[i].incomers().length==0)
{
nds[i].style({'border-color': 'red', "border-width":10})
}
else
{
nds[i].style({'border-color': 'black', "border-width":5})
}
}
}
// Will handle editing and logging in/out
//
function SetUIForDynamicWebsite()
{
$( "#loginlogout" ).click(function()
{
if (bIsLoggedIn)
{
// we need to log out
$("#loginlogout").text("LogIn"); bIsLoggedIn=false;
$("#username").val("")
$("#password").val("")
$(".admin").hide();
cy.autolock( true );
}
else
{
// show dialog to log in
$("#modal-login").prop("checked", true);
cy.autolock( false );
}
});
$( "#login" ).click(function()
{
base.setCredentials($("#username").val(), $("#password").val(), function(res)
{
if (res.res=="OK")
{
$("#modal-login").prop("checked", false);
$("#loginlogout").text("Logout"); bIsLoggedIn=true;
$(".admin").show();
$("#modal-admin-help").prop("checked", true);
}
else
{
alert(res.res);
}
});
});
// tapholding brings up a dialog to edit the node and the enges text
cy.on('taphold', 'node', function(evt)
{
if (bIsLoggedIn==false) return;
$("#dialog_label").text("Question")
$("#dialog_input").attr("data", evt.target.id());
$("#dialog_input").val(evt.target.data().label)
$("#modal-edit").prop("checked", true);
});
// tapholding brings up a dialog to edit the node and the enges text
cy.on('taphold', 'edge', function(evt)
{
if (bIsLoggedIn==false) return;
$("#dialog_label").text("Answer")
$("#dialog_input").attr("data", evt.target.id());
$("#dialog_input").val(evt.target.data().label);
$("#modal-edit").prop("checked", true);
});
// save handler for the text editing dialog
$( "#save" ).click(function()
{
if (bIsLoggedIn==false) return;
var q = $("#dialog_input")[0];
var id = q.attributes["data"].nodeValue;
cy.$( "#"+id ).data("label", q.value);
base.POST(cy.$( "#"+id ));
$("#modal-edit").prop("checked", false);
});
//
function generateRandomID()
{
var id = ""+ Math.random();
return id.split(".").join("");
}
//right clicking on a edge inserts a new node
cy.on('cxttap', 'edge', function(evt)
{
if (bIsLoggedIn==false) return;
var source = evt.target.source();
var target = evt.target.target();
var sp = source.position();
var st = target.position();
var id = generateRandomID();
var eles = cy.add([
{ group: "nodes", data: { id: id, label: "New Node" }, position: { x: (sp.x + st.x)*.5, y: (sp.y + st.y)*.5 } },
{ group: "edges", data: { id: generateRandomID(), source: source.id(), target: id } },
{ group: "edges", data: { id: generateRandomID(), source: id, target: target.id(), label: evt.target.data().label } }
]);
base.POST(eles, function(data)
{
var eles = cy.remove(cy.getElementById(evt.target.id()));
SetStyle();
base.DELETE( eles );
});
});
//right clicking on the background creates a node
cy.on('cxttap', function(evt)
{
if (bIsLoggedIn==false) return;
if (evt.target===cy)
{
var eles = cy.add([{ group: "nodes", data: { id: generateRandomID(), label: "No question" }, renderedPosition: evt.originalEvent }]);
base.POST(eles);
}
});
// shift selecting 2 nodes creates a link
cy.on('select', 'node', function(evt)
{
if (bIsLoggedIn==false) return;
var node = evt.target;
var selected = cy.elements(":selected");
if (selected.length==2)
{
var source = (node.id()==selected[1].id())?selected[0].id():selected[1].id();
var target = node.id();
var eles = cy.add([{ group: "edges", data: { id: generateRandomID(), source: source, target: target, label: "No answer" } }]);
SetStyle();
base.POST(eles);
}
});
// the delete key erases the selected nodes (sometimes it doesnt work! :( )
$('html').keydown(function(e)
{
if(e.keyCode == 46)
{
if($(event.target).is('textarea') || $(event.target).is('input'))
return;
if (bIsLoggedIn==false) return;
var eles = cy.remove(":selected");
base.DELETE(eles);
}
});
$( "#saveDatabase" ).click(function()
{
if (bIsLoggedIn==false) return;
base.POST(cy.nodes());
base.POST(cy.edges());
});
}
// Init stuff once the document loads
//
document.addEventListener('DOMContentLoaded', function()
{
$(".admin").hide();
$( "#applyLayout" ).click(function()
{
if (bIsLoggedIn==false) return;
cy.layout({
name: 'cola',
edgeLength:400
}).start(10)
});
$( "#help" ).click(function()
{
if (bIsLoggedIn==true)
$('#modal-admin-help').prop('checked', true);
else
$('#modal-help').prop('checked', true);
});
// Show a splash screen with some basic info
//
$("#modal-help").prop("checked", true);
// set graph's UI
//
var cy = window.cy = cytoscape(
{
container: document.getElementById('cy'),
boxSelectionEnabled: false,
minZoom: 0.1,
maxZoom: 100,
wheelSensitivity: 0.1,
style:
[
{
selector: 'node',
css:
{
'content': 'data(label)',
'text-valign': 'center',
'text-halign': 'center',
'shape': 'roundrectangle',
'text-wrap': 'wrap',
'width': '300px',
'text-max-width' : '300px',
'height': '100px',
}
},
{
selector: 'edge',
css:
{
'curve-style': 'bezier',
'label': 'data(label)',
'target-arrow-shape': 'triangle'
}
}
]
});
// Load graph from database
//
base.LoadDataFromDatabase( function()
{
$( "#loginlogout" ).click(function()
{
if (base.isDynamicWebsite()==false)
{
alert ("Opps! You'd need this to run in a server with PHP for this in order to work.")
}
else if (bIsLoggedIn==true)
{
logOut();
cy.autolock( true );
}
else
{
$("#modal-login").prop("checked", true);
cy.autolock( false );
}
});
// if we are in a static website skipt all the login/editing code as it makes no sense
//
if (base.isDynamicWebsite())
{
SetUIForDynamicWebsite();
}
});
});
</script>
</head>
<body>
<!-- header -->
<header class="sticky">
<a href="#" class="logo">SE Map</a>
<button id="help" >Help</button>
<button class="admin" id="applyLayout" >Apply Layout</button>
<button class="admin" id="saveDatabase">Save</button>
<button id="loginlogout" style="float: right">Login</button>
</header>
<!-- graph area-->
<div class="container">
<div class="content">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<div id="cy"></div>
</div>
</div>
</div>
</div>
<!-- editing dialog -->
<input id="modal-edit" type="checkbox" class="modal">
<div>
<div class="wide" style="width:750px">
<label for="modal-edit"></label>
<form>
<h3 class="section">Edit field</h3>
<fieldset>
<div class="row responsive-label">
<div class="col-sm-12 col-md-3">
<label for="question" id="dialog_label">Question</label>
</div>
<div class="col-sm-12 col-md">
<textarea type="text" id="dialog_input" style="width:85%;" rows="3"></textarea>
</div>
</div>
<div class="input-group fluid">
<button type="button" class="primary" id="save">Save</button>
<button type="button" class="cancel" onclick='$("#modal-edit").prop("checked", false);'>Cancel</button>
</div>
</fieldset>
</form>
</div>
</div>
<!-- login dialog -->
<input id="modal-login" type="checkbox" class="modal">
<div>
<div class="wide">
<!-- Adding some flex properties to center the form and some height to the page, these can be omitted -->
<label for="modal-login"></label>
<form>
<fieldset>
<legend>Login form</legend>
<div class="input-group fluid">
<label for="username" style="width: 80px;">Username</label>
<input name="username" value="" id="username" placeholder="username" type="text" >
</div>
<div class="input-group fluid">
<label for="pwd" style="width: 80px;">Password</label>
<input name="password" value="" id="password" placeholder="password" type="password">
</div>
<div class="input-group fluid">
<button type="button" class="primary" id="login">Login</button>
<button type="button" class="cancel" onclick='$("#modal-login").prop("checked", false);'>Cancel</button>
</div>
</fieldset>
</form>
</div>
</div>
<!-- Basic Quick start dialog -->
<input id="modal-help" type="checkbox" class="modal">
<div class="wide">
<div class="card large">
<div class="section">
<h3>Quick start</h3>
<ul>
<li>Start in the <b>highlighted</b> box and go from there.</li>
<li><b>Controls</b>: same as Google Maps.</li>
<li><b>Help</b> brings back this dialog.</li>
</ul>
<div class="input-group fluid">
<button type="button" class="primary" onclick='$("#modal-help").prop("checked", false);' id="login">OK</button>
</div>
</div>
</div>
</div>
<!-- Editor help dialog -->
<input id="modal-admin-help" type="checkbox" class="modal">
<div class="wide">
<div class="card large">
<div class="section">
<h3>Quick start</h3>
<ul>
<li>The boxes are for the questions, the arrows for the answers</li>
<li><b>Long click</b> for editing text</li>
<li><b>Right click</b> creates a new box</li>
<li><b>Shift + clicking</b> in two boxes links them with an arrow</li>
<li><b>Del</b> key to erase an arrow/box</li>
<li><b>Help</b> brings back this dialog.</li>
</ul>
<div class="input-group fluid">
<button type="button" class="primary" onclick='$("#modal-admin-help").prop("checked", false);' id="login">OK</button>
</div>
</div>
</div>
</div>
</body>
</html>