-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
324 lines (288 loc) · 11.6 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>SchemaBlocks</title>
<link rel="icon" href="favicon.ico" type="image/x-icon"/>
<!--Bootstrap stylesheets-->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.0/css/all.css" integrity="sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ" crossorigin="anonymous">
<!--Bootstrap scripts-->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<!--Blockly scripts-->
<script src="blockly/blockly_uncompressed.js"></script>
<script src="blockly/msg/messages.js"></script>
<script src="blockly/blocks/variables.js"></script>
<script src="blockly/tests/playgrounds/screenshot.js"></script>
<script>
'use strict';
var ontologies = {
'kairos': 'KAIROS',
'legal_prolog': 'Legal Prolog',
};
if (location.hash) {
var ont_name = location.hash.substr(1);
if (!(ont_name in ontologies)) {
ont_name = 'kairos';
}
} else {
var ont_name = 'kairos';
}
var ont_pretty_name = ontologies[ont_name];
var ont_did_init = false;
var ont_is_loaded = false;
var page_is_loaded = false;
var toolbox_is_loaded = false;
var loaded_schema_name = null;
function loadJS(path_to_js, callback) {
var script = document.createElement('script');
script.onload = callback;
script.src = path_to_js;
document.head.appendChild(script);
}
// Load toolbox
loadJS('ontologies/' + ont_name + '/' + ont_name + '_toolbox.js', function() {
toolbox_is_loaded = true;
pageInit();
});
// Load ontology-specific code
loadJS('ontologies/' + ont_name + '/' + ont_name + '.js', function() {
ont_is_loaded = true;
if (page_is_loaded && !ont_did_init) {
ont_did_init = true;
ontInit();
}
});
// Custom requires for the playground.
// Rendering.
// goog.require('Blockly.blockRendering.Debug');
// goog.require('Blockly.minimalist.Renderer');
// goog.require('Blockly.Themes.Classic');
// Other.
goog.require('Blockly.WorkspaceCommentSvg');
goog.require('Blockly.WorkspaceCommentSvg.render');
var workspace = null;
function changeOntology(new_ont_name) {
$('#navbar-ont-name-dropdown button').attr('disabled', true);
location.hash = '#' + new_ont_name;
location.reload(false);
return false;
}
function onload() {
page_is_loaded = true;
pageInit();
}
function pageInit() {
if (!page_is_loaded || !toolbox_is_loaded) {
return;
}
$('#navbar-ont-name-dropdown button').html(ont_pretty_name);
for (var key in ontologies) {
if (key !== ont_name) {
var value = ontologies[key];
var ont_menu_item = $('<a class="dropdown-item" onclick="changeOntology(\'' + key + '\')">' + value + '</a>');
$('#navbar-ont-name-dropdown div.dropdown-menu').append(ont_menu_item);
}
}
$('#navbar-ont-name-dropdown').show();
// Append toolbox XML to the HTML body
$("body").append($(toolbox_xml)).ready(function() {
// Create main workspace.
workspace = Blockly.inject('blocklyDiv',
{
comments: true,
collapse: true,
disable: true,
grid:
{
spacing: 25,
length: 3,
colour: '#ccc',
snap: true
},
horizontalLayout: false,
maxBlocks: Infinity,
maxInstances: {'test_basic_limit_instances': 3},
maxTrashcanContents: 256,
media: 'blockly/media/',
oneBasedIndex: true,
readOnly: false,
rtl: false,
move: {
scrollbars: true,
drag: true,
wheel: false,
},
toolbox: document.getElementById('toolbox-' + ont_name + '-blocks'),
toolboxPosition: 'start',
renderer: 'geras',
zoom:
{
controls: true,
wheel: true,
startScale: 1.0,
maxScale: 4,
minScale: 0.25,
scaleSpeed: 1.1
}
});
workspace.configureContextMenu = configureContextMenu;
// set onChangeListener on workspace
workspace.addChangeListener(workspaceOnChangeListener);
// register a callback for Create variable button
workspace.registerButtonCallback("CREATE_VARIABLE", createVariableHandler);
// Ontology-specific init
if (ont_is_loaded && !ont_did_init) {
ont_did_init = true;
ontInit();
}
});
}
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
function configureContextMenu(menuOptions, e) {
var screenshotOption = {
text: 'Download Screenshot',
enabled: workspace.getTopBlocks().length,
callback: function() {
Blockly.downloadScreenshot(workspace);
}
};
menuOptions.push(screenshotOption);
// Adds a default-sized workspace comment to the workspace.
menuOptions.push(Blockly.ContextMenu.workspaceCommentOption(workspace, e));
}
function createNamedVariable(var_name, is_unique_name) {
function incrName(varName) {
if (/\w+_\d+/.test(varName)) {
var res = varName.split('_');
return res[0] + '_' + (parseInt(res[1]) + 1).toString();
} else {
return varName + "_2";
}
}
if (is_unique_name) {
var all_used_var_models = workspace.getAllVariables();
var inv_used_vars_set = new Set();
all_used_var_models.forEach(function(used_var) {
inv_used_vars_set.add(used_var.name);
});
while (inv_used_vars_set.has(var_name)) {
var_name = incrName(var_name);
}
}
var xml = '<block type="variables_get"><field name="VAR">' + var_name + '</field></block>';
xml = '<xml xmlns="https://developers.google.com/blockly/xml">' + xml + '</xml>';
var dom = Blockly.Xml.textToDom(xml);
var new_block_id = Blockly.Xml.domToWorkspace(dom, workspace)[0];
return workspace.getBlockById(new_block_id);
}
function createVariableHandler(_) {
Blockly.Variables.promptName('Enter variable name:', '', function(var_name) {
if (!var_name) {
return;
}
var block = createNamedVariable(var_name, false);
var metrics = workspace.getMetrics();
var rnd_x = Math.random() * 0.5 + 0.25, rnd_y = Math.random() * 0.5 + 0.25;
block.moveBy((rnd_x * metrics.viewWidth + metrics.viewLeft) / workspace.scale, (rnd_y * metrics.viewHeight + metrics.viewTop) / workspace.scale);
Blockly.hideChaff();
})
}
function fromXml() {
var el = window._protected_reference = document.createElement('INPUT');
el.type = 'file';
el.accept = 'application/xml';
el.addEventListener('change', function (ev) {
if (el.files.length) {
var file = el.files[0];
var reader = new FileReader();
if (file.name) {
loaded_schema_name = file.name.replace(/\.[^/.]+$/, '');
}
reader.onload = (function (theFile) {
return function (e) {
var contents = e.target.result;
if (contents) {
// Clear current workspace
workspace.clear();
var xml = Blockly.Xml.textToDom(contents);
Blockly.Xml.domToWorkspace(xml, workspace);
}
el = window._protected_reference = undefined;
};
})(file);
// Read in the image file as a data URL.
reader.readAsText(file);
}
});
el.click();
}
</script>
<style>
html, body {
height: 100%;
}
#navbar {
height: 55px;
}
.navbar-nav > li{
padding-left:10px;
padding-right:10px;
}
#blocklyDiv {
display: block;
background: white;
box-sizing: border-box;
height: calc(100% - 60px);
width: 100%;
}
</style>
</head>
<body onload="onload()">
<nav id="navbar" class="navbar navbar-expand" style="background-color: #e3f2fd">
<span class="navbar-brand"><span style="color: #6CA362"><b>S</b></span>chema<span style="color: #637FA2"><b>B</b></span>locks</span>
<div class="dropdown" style="display: none" id="navbar-ont-name-dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" data-toggle="dropdown"></button>
<div class="dropdown-menu"></div>
</div>
<ul class="navbar-nav mr-auto">
<li class="nav-item"><button class="btn btn-outline-success my-2 my-sm-0" onclick="toXml()"><span class="fas fa-download"></span> Save schema to file...</button></li>
<li class="nav-item"><button class="btn btn-outline-success my-2 my-sm-0" onclick="fromXml()"><span class="fas fa-upload"></span> Load schema from file...</button></li>
</ul>
<ul class="navbar-nav ml-auto">
<li class="nav-item"><button id="show-help" class="btn btn-outline-info my-2 my-sm-0" style="display: none" data-toggle="modal" data-target="#helpModal"><span class="fas fa-question-circle"></span> Help</button></li>
</ul>
</nav>
<div class="container-fluid" id="blocklyDiv"></div>
<div class="modal fade" id="helpModal" tabindex="-1" role="dialog" aria-labelledby="helpModalTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Welcome to <span style="color: #6CA362"><b>S</b></span>chema<span style="color: #637FA2"><b>B</b></span>locks!</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Here are helpful tips on how to get started.<br>
<input type="checkbox" id="dont-show-help-on-startup">
<label class="form-check-label" for="dont-show-help-on-startup">Don't show this again.</label>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">Got it</button>
</div>
</div>
</div>
</div>
</body>
</html>