forked from scratchfoundation/scratch-blocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_category.js
490 lines (455 loc) · 17.8 KB
/
data_category.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
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
/**
* @license
* Visual Blocks Editor
*
* Copyright 2017 Google Inc.
* https://developers.google.com/blockly/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview Data Flyout components including variable and list blocks.
* @author marisaleung@google.com (Marisa Leung)
*/
'use strict';
/**
* @name Blockly.DataCategory
* @namespace
**/
goog.provide('Blockly.DataCategory');
goog.require('Blockly.Blocks');
goog.require('Blockly.VariableModel');
goog.require('Blockly.Variables');
goog.require('Blockly.Workspace');
/**
* Construct the blocks required by the flyout for the variable category.
* @param {!Blockly.Workspace} workspace The workspace containing variables.
* @return {!Array.<!Element>} Array of XML block elements.
*/
Blockly.DataCategory = function(workspace) {
var variableModelList = workspace.getVariablesOfType('');
variableModelList.sort(Blockly.VariableModel.compareByName);
var xmlList = [];
Blockly.DataCategory.addCreateButton(xmlList, workspace, 'VARIABLE');
for (var i = 0; i < variableModelList.length; i++) {
Blockly.DataCategory.addDataVariable(xmlList, variableModelList[i]);
}
if (variableModelList.length > 0) {
xmlList[xmlList.length - 1].setAttribute('gap', 24);
var firstVariable = variableModelList[0];
Blockly.DataCategory.addSetVariableTo(xmlList, firstVariable);
Blockly.DataCategory.addChangeVariableBy(xmlList, firstVariable);
Blockly.DataCategory.addShowVariable(xmlList, firstVariable);
Blockly.DataCategory.addHideVariable(xmlList, firstVariable);
}
// Now add list variables to the flyout
Blockly.DataCategory.addCreateButton(xmlList, workspace, 'LIST');
variableModelList = workspace.getVariablesOfType(Blockly.LIST_VARIABLE_TYPE);
variableModelList.sort(Blockly.VariableModel.compareByName);
for (var i = 0; i < variableModelList.length; i++) {
Blockly.DataCategory.addDataList(xmlList, variableModelList[i]);
}
if (variableModelList.length > 0) {
xmlList[xmlList.length - 1].setAttribute('gap', 24);
var firstVariable = variableModelList[0];
Blockly.DataCategory.addAddToList(xmlList, firstVariable);
Blockly.DataCategory.addSep(xmlList);
Blockly.DataCategory.addDeleteOfList(xmlList, firstVariable);
Blockly.DataCategory.addDeleteAllOfList(xmlList, firstVariable);
Blockly.DataCategory.addInsertAtList(xmlList, firstVariable);
Blockly.DataCategory.addReplaceItemOfList(xmlList, firstVariable);
Blockly.DataCategory.addSep(xmlList);
Blockly.DataCategory.addItemOfList(xmlList, firstVariable);
Blockly.DataCategory.addItemNumberOfList(xmlList, firstVariable);
Blockly.DataCategory.addLengthOfList(xmlList, firstVariable);
Blockly.DataCategory.addListContainsItem(xmlList, firstVariable);
Blockly.DataCategory.addSep(xmlList);
Blockly.DataCategory.addShowList(xmlList, firstVariable);
Blockly.DataCategory.addHideList(xmlList, firstVariable);
}
return xmlList;
};
/**
* Construct and add a data_variable block to xmlList.
* @param {!Array.<!Element>} xmlList Array of XML block elements.
* @param {?Blockly.VariableModel} variable Variable to select in the field.
*/
Blockly.DataCategory.addDataVariable = function(xmlList, variable) {
// <block id="variableId" type="data_variable">
// <field name="VARIABLE">variablename</field>
// </block>
Blockly.DataCategory.addBlock(xmlList, variable, 'data_variable', 'VARIABLE');
// In the flyout, this ID must match variable ID for monitor syncing reasons
xmlList[xmlList.length - 1].setAttribute('id', variable.getId());
};
/**
* Construct and add a data_setvariableto block to xmlList.
* @param {!Array.<!Element>} xmlList Array of XML block elements.
* @param {?Blockly.VariableModel} variable Variable to select in the field.
*/
Blockly.DataCategory.addSetVariableTo = function(xmlList, variable) {
// <block type="data_setvariableto" gap="20">
// <value name="VARIABLE">
// <shadow type="data_variablemenu"></shadow>
// </value>
// <value name="VALUE">
// <shadow type="text">
// <field name="TEXT">0</field>
// </shadow>
// </value>
// </block>
Blockly.DataCategory.addBlock(xmlList, variable, 'data_setvariableto',
'VARIABLE', ['VALUE', 'text', 0]);
};
/**
* Construct and add a data_changevariableby block to xmlList.
* @param {!Array.<!Element>} xmlList Array of XML block elements.
* @param {?Blockly.VariableModel} variable Variable to select in the field.
*/
Blockly.DataCategory.addChangeVariableBy = function(xmlList, variable) {
// <block type="data_changevariableby">
// <value name="VARIABLE">
// <shadow type="data_variablemenu"></shadow>
// </value>
// <value name="VALUE">
// <shadow type="math_number">
// <field name="NUM">1</field>
// </shadow>
// </value>
// </block>
Blockly.DataCategory.addBlock(xmlList, variable, 'data_changevariableby',
'VARIABLE', ['VALUE', 'math_number', 1]);
};
/**
* Construct and add a data_showVariable block to xmlList.
* @param {!Array.<!Element>} xmlList Array of XML block elements.
* @param {?Blockly.VariableModel} variable Variable to select in the field.
*/
Blockly.DataCategory.addShowVariable = function(xmlList, variable) {
// <block type="data_showvariable">
// <value name="VARIABLE">
// <shadow type="data_variablemenu"></shadow>
// </value>
// </block>
Blockly.DataCategory.addBlock(xmlList, variable, 'data_showvariable',
'VARIABLE');
};
/**
* Construct and add a data_hideVariable block to xmlList.
* @param {!Array.<!Element>} xmlList Array of XML block elements.
* @param {?Blockly.VariableModel} variable Variable to select in the field.
*/
Blockly.DataCategory.addHideVariable = function(xmlList, variable) {
// <block type="data_hidevariable">
// <value name="VARIABLE">
// <shadow type="data_variablemenu"></shadow>
// </value>
// </block>
Blockly.DataCategory.addBlock(xmlList, variable, 'data_hidevariable',
'VARIABLE');
};
/**
* Construct and add a data_listcontents block to xmlList.
* @param {!Array.<!Element>} xmlList Array of XML block elements.
* @param {?Blockly.VariableModel} variable Variable to select in the field.
*/
Blockly.DataCategory.addDataList = function(xmlList, variable) {
// <block id="variableId" type="data_listcontents">
// <field name="LIST">variablename</field>
// </block>
Blockly.DataCategory.addBlock(xmlList, variable, 'data_listcontents', 'LIST');
// In the flyout, this ID must match variable ID for monitor syncing reasons
xmlList[xmlList.length - 1].setAttribute('id', variable.getId());
};
/**
* Construct and add a data_addtolist block to xmlList.
* @param {!Array.<!Element>} xmlList Array of XML block elements.
* @param {?Blockly.VariableModel} variable Variable to select in the field.
*/
Blockly.DataCategory.addAddToList = function(xmlList, variable) {
// <block type="data_addtolist">
// <field name="LIST" variabletype="list" id="">variablename</field>
// <value name="ITEM">
// <shadow type="text">
// <field name="TEXT">thing</field>
// </shadow>
// </value>
// </block>
Blockly.DataCategory.addBlock(xmlList, variable, 'data_addtolist', 'LIST',
['ITEM', 'text', Blockly.Msg.DEFAULT_LIST_ITEM]);
};
/**
* Construct and add a data_deleteoflist block to xmlList.
* @param {!Array.<!Element>} xmlList Array of XML block elements.
* @param {?Blockly.VariableModel} variable Variable to select in the field.
*/
Blockly.DataCategory.addDeleteOfList = function(xmlList, variable) {
// <block type="data_deleteoflist">
// <field name="LIST" variabletype="list" id="">variablename</field>
// <value name="INDEX">
// <shadow type="math_integer">
// <field name="NUM">1</field>
// </shadow>
// </value>
// </block>
Blockly.DataCategory.addBlock(xmlList, variable, 'data_deleteoflist', 'LIST',
['INDEX', 'math_integer', 1]);
};
/**
* Construct and add a data_deleteoflist block to xmlList.
* @param {!Array.<!Element>} xmlList Array of XML block elements.
* @param {?Blockly.VariableModel} variable Variable to select in the field.
*/
Blockly.DataCategory.addDeleteAllOfList = function(xmlList, variable) {
// <block type="data_deletealloflist">
// <field name="LIST" variabletype="list" id="">variablename</field>
// </block>
Blockly.DataCategory.addBlock(xmlList, variable, 'data_deletealloflist',
'LIST');
};
/**
* Construct and add a data_insertatlist block to xmlList.
* @param {!Array.<!Element>} xmlList Array of XML block elements.
* @param {?Blockly.VariableModel} variable Variable to select in the field.
*/
Blockly.DataCategory.addInsertAtList = function(xmlList, variable) {
// <block type="data_insertatlist">
// <field name="LIST" variabletype="list" id="">variablename</field>
// <value name="INDEX">
// <shadow type="math_integer">
// <field name="NUM">1</field>
// </shadow>
// </value>
// <value name="ITEM">
// <shadow type="text">
// <field name="TEXT">thing</field>
// </shadow>
// </value>
// </block>
Blockly.DataCategory.addBlock(xmlList, variable, 'data_insertatlist', 'LIST',
['INDEX', 'math_integer', 1], ['ITEM', 'text', Blockly.Msg.DEFAULT_LIST_ITEM]);
};
/**
* Construct and add a data_replaceitemoflist block to xmlList.
* @param {!Array.<!Element>} xmlList Array of XML block elements.
* @param {?Blockly.VariableModel} variable Variable to select in the field.
*/
Blockly.DataCategory.addReplaceItemOfList = function(xmlList, variable) {
// <block type="data_replaceitemoflist">
// <field name="LIST" variabletype="list" id="">variablename</field>
// <value name="INDEX">
// <shadow type="math_integer">
// <field name="NUM">1</field>
// </shadow>
// </value>
// <value name="ITEM">
// <shadow type="text">
// <field name="TEXT">thing</field>
// </shadow>
// </value>
// </block>
Blockly.DataCategory.addBlock(xmlList, variable, 'data_replaceitemoflist',
'LIST', ['INDEX', 'math_integer', 1], ['ITEM', 'text', Blockly.Msg.DEFAULT_LIST_ITEM]);
};
/**
* Construct and add a data_itemoflist block to xmlList.
* @param {!Array.<!Element>} xmlList Array of XML block elements.
* @param {?Blockly.VariableModel} variable Variable to select in the field.
*/
Blockly.DataCategory.addItemOfList = function(xmlList, variable) {
// <block type="data_itemoflist">
// <field name="LIST" variabletype="list" id="">variablename</field>
// <value name="INDEX">
// <shadow type="math_integer">
// <field name="NUM">1</field>
// </shadow>
// </value>
// </block>
Blockly.DataCategory.addBlock(xmlList, variable, 'data_itemoflist', 'LIST',
['INDEX', 'math_integer', 1]);
};
/** Construct and add a data_itemnumoflist block to xmlList.
* @param {!Array.<!Element>} xmlList Array of XML block elements.
* @param {?Blockly.VariableModel} variable Variable to select in the field.
*/
Blockly.DataCategory.addItemNumberOfList = function(xmlList, variable) {
// <block type="data_itemnumoflist">
// <value name="ITEM">
// <shadow type="text">
// <field name="TEXT">thing</field>
// </shadow>
// </value>
// <field name="LIST" variabletype="list" id="">variablename</field>
// </block>
Blockly.DataCategory.addBlock(xmlList, variable, 'data_itemnumoflist',
'LIST', ['ITEM', 'text', Blockly.Msg.DEFAULT_LIST_ITEM]);
};
/**
* Construct and add a data_lengthoflist block to xmlList.
* @param {!Array.<!Element>} xmlList Array of XML block elements.
* @param {?Blockly.VariableModel} variable Variable to select in the field.
*/
Blockly.DataCategory.addLengthOfList = function(xmlList, variable) {
// <block type="data_lengthoflist">
// <field name="LIST" variabletype="list" id="">variablename</field>
// </block>
Blockly.DataCategory.addBlock(xmlList, variable, 'data_lengthoflist', 'LIST');
};
/**
* Construct and add a data_listcontainsitem block to xmlList.
* @param {!Array.<!Element>} xmlList Array of XML block elements.
* @param {?Blockly.VariableModel} variable Variable to select in the field.
*/
Blockly.DataCategory.addListContainsItem = function(xmlList, variable) {
// <block type="data_listcontainsitem">
// <field name="LIST" variabletype="list" id="">variablename</field>
// <value name="ITEM">
// <shadow type="text">
// <field name="TEXT">thing</field>
// </shadow>
// </value>
// </block>
Blockly.DataCategory.addBlock(xmlList, variable, 'data_listcontainsitem',
'LIST', ['ITEM', 'text', Blockly.Msg.DEFAULT_LIST_ITEM]);
};
/**
* Construct and add a data_showlist block to xmlList.
* @param {!Array.<!Element>} xmlList Array of XML block elements.
* @param {?Blockly.VariableModel} variable Variable to select in the field.
*/
Blockly.DataCategory.addShowList = function(xmlList, variable) {
// <block type="data_showlist">
// <field name="LIST" variabletype="list" id="">variablename</field>
// </block>
Blockly.DataCategory.addBlock(xmlList, variable, 'data_showlist', 'LIST');
};
/**
* Construct and add a data_hidelist block to xmlList.
* @param {!Array.<!Element>} xmlList Array of XML block elements.
* @param {?Blockly.VariableModel} variable Variable to select in the field.
*/
Blockly.DataCategory.addHideList = function(xmlList, variable) {
// <block type="data_hidelist">
// <field name="LIST" variabletype="list" id="">variablename</field>
// </block>
Blockly.DataCategory.addBlock(xmlList, variable, 'data_hidelist', 'LIST');
};
/**
* Construct a create variable button and push it to the xmlList.
* @param {!Array.<!Element>} xmlList Array of XML block elements.
* @param {Blockly.Workspace} workspace Workspace to register callback to.
* @param {string} type Type of variable this is for. For example, 'LIST' or
* 'VARIABLE'.
*/
Blockly.DataCategory.addCreateButton = function(xmlList, workspace, type) {
var button = goog.dom.createDom('button');
// Set default msg, callbackKey, and callback values for type 'VARIABLE'
var msg = Blockly.Msg.NEW_VARIABLE;
var callbackKey = 'CREATE_VARIABLE';
var callback = function(button) {
Blockly.Variables.createVariable(button.getTargetWorkspace(), null, '');};
if (type === 'LIST') {
msg = Blockly.Msg.NEW_LIST;
callbackKey = 'CREATE_LIST';
callback = function(button) {
Blockly.Variables.createVariable(button.getTargetWorkspace(), null,
Blockly.LIST_VARIABLE_TYPE);};
}
button.setAttribute('text', msg);
button.setAttribute('callbackKey', callbackKey);
workspace.registerButtonCallback(callbackKey, callback);
xmlList.push(button);
};
/**
* Construct a variable block with the given variable, blockType, and optional
* value tags. Add the variable block to the given xmlList.
* @param {!Array.<!Element>} xmlList Array of XML block elements.
* @param {?Blockly.VariableModel} variable Variable to select in the field.
* @param {string} blockType Type of block. For example, 'data_hidelist' or
* data_showlist'.
* @param {string} fieldName Name of field in block. For example: 'VARIABLE' or
* 'LIST'.
* @param {?Array.<string>} opt_value Optional array containing the value name
* and shadow type of value tags.
* @param {?Array.<string>} opt_secondValue Optional array containing the value
* name and shadow type of a second pair of value tags.
*/
Blockly.DataCategory.addBlock = function(xmlList, variable, blockType,
fieldName, opt_value, opt_secondValue) {
if (Blockly.Blocks[blockType]) {
var firstValueField;
var secondValueField;
if (opt_value) {
firstValueField = Blockly.DataCategory.createValue(opt_value[0],
opt_value[1], opt_value[2]);
}
if (opt_secondValue) {
secondValueField = Blockly.DataCategory.createValue(opt_secondValue[0],
opt_secondValue[1], opt_secondValue[2]);
}
var gap = 8;
var blockText = '<xml>' +
'<block type="' + blockType + '" gap="' + gap + '">' +
Blockly.Variables.generateVariableFieldXml_(variable, fieldName) +
firstValueField + secondValueField +
'</block>' +
'</xml>';
var block = Blockly.Xml.textToDom(blockText).firstChild;
xmlList.push(block);
}
};
/**
* Create the text representation of a value dom element with a shadow of the
* indicated type inside.
* @param {string} valueName Name of the value tags.
* @param {string} type The type of the shadow tags.
* @param {string|number} value The default shadow value.
* @return {string} The generated dom element in text.
*/
Blockly.DataCategory.createValue = function(valueName, type, value) {
var fieldName;
switch (valueName) {
case 'ITEM':
fieldName = 'TEXT';
break;
case 'INDEX':
fieldName = 'NUM';
break;
case 'VALUE':
if (type === 'math_number') {
fieldName = 'NUM';
} else {
fieldName = 'TEXT';
}
break;
}
var valueField =
'<value name="' + valueName + '">' +
'<shadow type="' + type + '">' +
'<field name="' + fieldName + '">' + value + '</field>' +
'</shadow>' +
'</value>';
return valueField;
};
/**
* Construct a block separator. Add the separator to the given xmlList.
* @param {!Array.<!Element>} xmlList Array of XML block elements.
*/
Blockly.DataCategory.addSep = function(xmlList) {
var gap = 36;
var sepText = '<xml>' +
'<sep gap="' + gap + '"/>' +
'</xml>';
var sep = Blockly.Xml.textToDom(sepText).firstChild;
xmlList.push(sep);
};