-
Notifications
You must be signed in to change notification settings - Fork 9
/
shadowUniqueVars.js
108 lines (102 loc) · 3.39 KB
/
shadowUniqueVars.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
/**
* @fileoverview Shim on Blockly to permit breaking inline blocks using dummy inputs
* @author miguel.ceriani@gmail.com (Miguel Ceriani)
*/
'use strict';
var Blockly = require('blockly');
/**
* Return a new variable name that is not yet being used. This will try to
* generate variable names starting with the provided prefix and followed
* by a number.
* @param {!Blockly.Workspace} workspace The workspace to be unique in.
* @param {!prefix} Variable prefix.
* @return {string} New variable name.
*/
var generateUniqueName = function(workspace, prefix) {
var variableList = Blockly.Variables.allVariables(workspace);
var newName = '';
var nameSuffix = 0;
while (!newName) {
var potName = '' + prefix + (nameSuffix ? nameSuffix : '');
var inUse = false;
for (var i = 0; i < variableList.length; i++) {
if (variableList[i].toLowerCase() == potName) {
// This potential name is already used.
inUse = true;
break;
}
}
if (inUse) {
// Try the next potential name.
nameSuffix++;
} else {
// We can use the current potential name.
newName = potName;
}
}
return newName;
};
var justPrefix = function(varName) {
return varName.substr(0, varName.length - varName.match(/[0-9]*$/)[0].length);
};
var defaultVarNamesByWorkspace = {};
/**
* Install this dropdown on a block.
*/
Blockly.FieldVariable.prototype.init = function() {
if (this.fieldGroup_) {
// Dropdown has already been initialized once.
return;
}
Blockly.FieldVariable.superClass_.init.call(this);
// Variables without names (or in shadow blocks) get uniquely named for this workspace.
var workspace =
this.sourceBlock_.isInFlyout ?
this.sourceBlock_.workspace.targetWorkspace :
this.sourceBlock_.workspace;
var newValue = null;
var newValueGenerated = false;
if (!this.getValue()) {
newValue = Blockly.Variables.generateUniqueName(workspace);
newValueGenerated = true;
} else if (this.sourceBlock_.isShadow_ && !this.sourceBlock_.isInFlyout) {
newValue = generateUniqueName(workspace, justPrefix(this.getValue()));
newValueGenerated = true;
}
if (newValue) {
this.value_ = newValue;
this.setText(newValue);
}
// If the selected variable doesn't exist yet, create it.
// For instance, some blocks in the toolbox have variable dropdowns filled
// in by default.
if (!this.sourceBlock_.isInFlyout) {
this.sourceBlock_.workspace.createVariable(this.getValue());
if (newValueGenerated) {
var defaultVarNames = defaultVarNamesByWorkspace[this.sourceBlock_.workspace];
if (!defaultVarNames) {
defaultVarNames = [];
defaultVarNamesByWorkspace[this.sourceBlock_.workspace] = defaultVarNames;
}
defaultVarNames.push(this.getValue());
}
}
this.workspace_ = workspace;
};
/**
* Dispose of all DOM objects belonging to this editable field.
*/
Blockly.FieldVariable.prototype.dispose = function() {
var varName = this.getValue();
var block = this.sourceBlock_;
var workspace = this.workspace_;
Blockly.Field.prototype.dispose.call(this);
this.workspace_ = null;
var defaultVarNames = workspace && defaultVarNamesByWorkspace[workspace];
if (block && varName && workspace
&& defaultVarNames
&& defaultVarNames.indexOf(varName) != -1
&& workspace.getVariableUses(varName).length == 0) {
workspace.deleteVariable(varName);
}
};