Set checkbox state after creating a variable through the prompt

This commit is contained in:
Paul Kaplan 2017-06-02 11:30:45 -04:00
parent 146f2822e7
commit 412c971d28
2 changed files with 39 additions and 14 deletions

View file

@ -546,26 +546,44 @@ Blockly.VerticalFlyout.prototype.createCheckbox_ = function(block, cursorX,
* @private
*/
Blockly.VerticalFlyout.prototype.checkboxClicked_ = function(checkboxObj) {
var context = this;
return function(e) {
var oldValue = checkboxObj.clicked;
var newValue = !oldValue;
checkboxObj.clicked = newValue;
if (checkboxObj.clicked) {
Blockly.utils.addClass((checkboxObj.svgRoot), 'checked');
} else {
Blockly.utils.removeClass((checkboxObj.svgRoot), 'checked');
}
Blockly.Events.fire(new Blockly.Events.Change(
checkboxObj.block, 'checkbox', null, oldValue, newValue));
context.setCheckboxState(checkboxObj.block.id, !checkboxObj.clicked);
// This event has been handled. No need to bubble up to the document.
e.stopPropagation();
e.preventDefault();
};
};
/**
* Set the state of a checkbox by block ID.
* @param {string} blockId ID of the block whose checkbox should be set
* @param {boolean} value Value to set the checkbox to.
* @public
*/
Blockly.VerticalFlyout.prototype.setCheckboxState = function(blockId, value) {
for (var i = 0; i < this.checkboxes_.length; i++) {
var checkboxObj = this.checkboxes_[i];
if (checkboxObj.block.id === blockId) {
if (checkboxObj.clicked === value) return;
var oldValue = checkboxObj.clicked;
checkboxObj.clicked = value;
if (checkboxObj.clicked) {
Blockly.utils.addClass((checkboxObj.svgRoot), 'checked');
} else {
Blockly.utils.removeClass((checkboxObj.svgRoot), 'checked');
}
Blockly.Events.fire(new Blockly.Events.Change(
checkboxObj.block, 'checkbox', null, oldValue, value));
return;
}
}
};
/**
* Determine if a drag delta is toward the workspace, based on the position
* and orientation of the flyout. This to decide if a new block should be

View file

@ -381,7 +381,14 @@ Blockly.Variables.createVariable = function(workspace, opt_callback) {
});
}
else {
workspace.createVariable(text);
var variable = workspace.createVariable(text);
var fl = workspace.getFlyout();
var variableBlockId = 'VAR_' + variable.name;
if (fl.setCheckboxState) {
fl.setCheckboxState(variableBlockId, true);
}
if (opt_callback) {
opt_callback(text);
}