forked from ens-lg4/MenulyJSON
-
Notifications
You must be signed in to change notification settings - Fork 2
/
menuly_2blocks.js
65 lines (51 loc) · 2.12 KB
/
menuly_2blocks.js
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
'use strict';
Blockly.JSON.toWorkspace = function(json_text, workspace) {
var json_structure = JSON.parse(json_text);
workspace.clear();
var startBlock = Blockly.Block.obtain(workspace, 'start');
startBlock.initSvg();
startBlock.render();
Blockly.JSON.buildAndConnect(json_structure, startBlock.getInput('json').connection);
};
Blockly.JSON.buildAndConnect = function(json_structure, parentConnection) {
if(json_structure === null) {
return;
} else {
var type = typeof(json_structure);
if(type == 'boolean') {
type = String(Boolean(json_structure));
} else if(type == 'object') {
type = (json_structure instanceof Array) ? 'array' : 'dictionary';
}
var targetBlock = Blockly.Block.obtain(parentConnection.sourceBlock_.workspace, type);
targetBlock.initSvg();
targetBlock.render();
var childConnection = targetBlock.outputConnection;
parentConnection.connect(childConnection);
switch(type) {
case 'string':
targetBlock.setFieldValue( String(json_structure), 'string_value' );
break;
case 'number':
targetBlock.setFieldValue( String(json_structure), 'number_value' );
break;
case 'dictionary':
var i=0;
for(var key in json_structure) {
targetBlock.appendKeyValuePairInput();
targetBlock.setFieldValue( key, 'key_field_'+i );
var elementConnection = targetBlock.getInput('element_'+i).connection;
Blockly.JSON.buildAndConnect(json_structure[key], elementConnection);
i++;
}
break;
case 'array':
for(var i in json_structure) {
targetBlock.appendArrayElementInput();
var elementConnection = targetBlock.getInput('element_'+i).connection;
Blockly.JSON.buildAndConnect(json_structure[i], elementConnection);
}
break;
}
}
};