forked from ens-lg4/MenulyJSON
-
Notifications
You must be signed in to change notification settings - Fork 2
/
menuly_blocks.js
180 lines (127 loc) · 5.02 KB
/
menuly_blocks.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
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
'use strict';
Blockly.selectionArrow = function() { return Blockly.RTL ? "←" : "→"; };
Blockly.keyValueArrow = function() { return Blockly.RTL ? "⇐" : "⇒"; };
Blockly.Blocks['start'] = {
init: function() {
this.setColour(250);
this.appendDummyInput()
.setAlign(Blockly.ALIGN_CENTRE)
.appendField("JSON structure");
this.appendValueInput('json')
.appendSelector(['dictionary', 'array', 'string', 'number', 'true', 'false'], Blockly.selectionArrow(), 'null');
this.setDeletable(false);
}
};
Blockly.Blocks['true'] = {
init: function() {
this.setColour(20);
this.setOutput(true, ["element"]);
this.appendDummyInput()
.setAlign(Blockly.ALIGN_CENTRE)
.appendField('true');
}
};
Blockly.Blocks['false'] = {
init: function() {
this.setColour(20);
this.setOutput(true, ["element"]);
this.appendDummyInput()
.setAlign(Blockly.ALIGN_CENTRE)
.appendField('false');
}
};
Blockly.Blocks['string'] = {
init: function() {
this.setColour(190);
this.setOutput(true, ["element"]);
this.appendDummyInput()
.setAlign(Blockly.ALIGN_CENTRE)
.appendField('"')
.appendField(new Blockly.FieldTextInput(''), 'string_value')
.appendField('"');
}
};
Blockly.Blocks['number'] = {
init: function() {
this.setColour(210);
this.setOutput(true, ["element"]);
this.appendDummyInput()
.setAlign(Blockly.ALIGN_CENTRE)
.appendField('№')
.appendField(new Blockly.FieldTextInput('0', Blockly.FieldTextInput.numberValidator), "number_value");
}
};
Blockly.Blocks['dictionary'] = {
length: 0,
init: function() {
this.setColour(120);
this.setOutput(true, ["element"]);
this.appendDummyInput('open_bracket')
.appendField(" { ")
.appendField(new Blockly.FieldTextbutton('+', function() { this.sourceBlock_.appendKeyValuePairInput(); }) );
this.appendDummyInput('close_bracket')
.appendField(" } ");
this.setInputsInline(false);
},
appendKeyValuePairInput: function() {
var lastIndex = this.length++;
var appended_input = this.appendValueInput('element_'+lastIndex);
appended_input.appendField(new Blockly.FieldTextbutton('–', function() { this.sourceBlock_.deleteKeyValuePairInput(appended_input); }) )
.appendField(new Blockly.FieldTextInput('key_'+lastIndex), 'key_field_'+lastIndex)
.appendField( Blockly.keyValueArrow() )
.appendSelector(['string', 'number', 'true', 'false', 'dictionary', 'array'], Blockly.selectionArrow(), 'null');
this.moveInputBefore('element_'+lastIndex, 'close_bracket');
return appended_input;
},
deleteKeyValuePairInput: function(inputToDelete) {
var inputNameToDelete = inputToDelete.name;
var substructure = this.getInputTargetBlock(inputNameToDelete);
if(substructure) {
substructure.dispose(true, true);
}
this.removeInput(inputNameToDelete);
var inputIndexToDelete = parseInt(inputToDelete.name.match(/\d+/)[0]);
var lastIndex = --this.length;
for(var i=inputIndexToDelete+1; i<=lastIndex; i++) { // rename all the subsequent element-inputs
var input = this.getInput( 'element_'+i );
input.name = 'element_'+(i-1);
var key_field = this.getField_( 'key_field_'+i );
key_field.name = 'key_field_'+(i-1);
}
}
};
Blockly.Blocks['array'] = {
length: 0,
init: function() {
this.setColour(350);
this.setOutput(true, ["element"]);
this.appendDummyInput('open_bracket')
.appendField(" [ ")
.appendField(new Blockly.FieldTextbutton('+', function() { this.sourceBlock_.appendArrayElementInput(); }) );
this.appendDummyInput('close_bracket')
.appendField(" ] ");
this.setInputsInline(false);
},
appendArrayElementInput: function() {
var lastIndex = this.length++;
var appended_input = this.appendValueInput('element_'+lastIndex);
appended_input.appendField(new Blockly.FieldTextbutton('–', function() { this.sourceBlock_.deleteArrayElementInput(appended_input); }) )
.appendSelector(['string', 'number', 'true', 'false', 'dictionary', 'array'], Blockly.selectionArrow(), 'null');
this.moveInputBefore('element_'+lastIndex, 'close_bracket');
return appended_input;
},
deleteArrayElementInput: function(inputToDelete) {
var inputNameToDelete = inputToDelete.name;
var substructure = this.getInputTargetBlock(inputNameToDelete);
if(substructure) {
substructure.dispose(true, true);
}
this.removeInput(inputNameToDelete);
var inputIndexToDelete = parseInt(inputToDelete.name.match(/\d+/)[0]);
var lastIndex = --this.length;
for(var i=inputIndexToDelete+1; i<=lastIndex; i++) { // rename all the subsequent element-inputs
var input = this.getInput( 'element_'+i );
input.name = 'element_'+(i-1);
}
}
};