Set field.sourceBlock in headless Blockly.

This commit is contained in:
Neil Fraser
2016-04-04 17:47:15 -07:00
parent 601ea15b75
commit 4ac1204550
8 changed files with 25 additions and 23 deletions

View File

@@ -118,15 +118,22 @@ Blockly.Field.NBSP = '\u00A0';
Blockly.Field.prototype.EDITABLE = true; Blockly.Field.prototype.EDITABLE = true;
/** /**
* Install this field on a block. * Attach this field to a block.
* @param {!Blockly.Block} block The block containing this field. * @param {!Blockly.Block} block The block containing this field.
*/ */
Blockly.Field.prototype.init = function(block) { Blockly.Field.prototype.setSourceBlock = function(block) {
if (this.sourceBlock_) { goog.asserts.assert(!this.sourceBlock_, 'Field already bound to a block.');
this.sourceBlock_ = block;
};
/**
* Install this field on a block.
*/
Blockly.Field.prototype.init = function() {
if (this.fieldGroup_) {
// Field has already been initialized once. // Field has already been initialized once.
return; return;
} }
this.sourceBlock_ = block;
// Build the DOM. // Build the DOM.
this.fieldGroup_ = Blockly.createSvgElement('g', {}, null); this.fieldGroup_ = Blockly.createSvgElement('g', {}, null);
if (!this.visible_) { if (!this.visible_) {
@@ -144,7 +151,7 @@ Blockly.Field.prototype.init = function(block) {
this.fieldGroup_); this.fieldGroup_);
this.updateEditable(); this.updateEditable();
block.getSvgRoot().appendChild(this.fieldGroup_); this.sourceBlock_.getSvgRoot().appendChild(this.fieldGroup_);
this.mouseUpWrapper_ = this.mouseUpWrapper_ =
Blockly.bindEvent_(this.fieldGroup_, 'mouseup', this, this.onMouseUp_); Blockly.bindEvent_(this.fieldGroup_, 'mouseup', this, this.onMouseUp_);
// Force a render. // Force a render.

View File

@@ -61,7 +61,7 @@ Blockly.FieldCheckbox.prototype.CURSOR = 'default';
* @param {!Blockly.Block} block The block containing this text. * @param {!Blockly.Block} block The block containing this text.
*/ */
Blockly.FieldCheckbox.prototype.init = function(block) { Blockly.FieldCheckbox.prototype.init = function(block) {
if (this.sourceBlock_) { if (this.fieldGroup_) {
// Checkbox has already been initialized once. // Checkbox has already been initialized once.
return; return;
} }

View File

@@ -80,11 +80,10 @@ Blockly.FieldDropdown.prototype.CURSOR = 'default';
* @param {!Blockly.Block} block The block containing this text. * @param {!Blockly.Block} block The block containing this text.
*/ */
Blockly.FieldDropdown.prototype.init = function(block) { Blockly.FieldDropdown.prototype.init = function(block) {
if (this.sourceBlock_) { if (this.fieldGroup_) {
// Dropdown has already been initialized once. // Dropdown has already been initialized once.
return; return;
} }
// Add dropdown arrow: "option ▾" (LTR) or "▾ אופציה" (RTL) // Add dropdown arrow: "option ▾" (LTR) or "▾ אופציה" (RTL)
this.arrow_ = Blockly.createSvgElement('tspan', {}, null); this.arrow_ = Blockly.createSvgElement('tspan', {}, null);
this.arrow_.appendChild(document.createTextNode( this.arrow_.appendChild(document.createTextNode(

View File

@@ -67,14 +67,12 @@ Blockly.FieldImage.prototype.EDITABLE = false;
/** /**
* Install this image on a block. * Install this image on a block.
* @param {!Blockly.Block} block The block containing this text.
*/ */
Blockly.FieldImage.prototype.init = function(block) { Blockly.FieldImage.prototype.init = function() {
if (this.sourceBlock_) { if (this.fieldGroup_) {
// Image has already been initialized once. // Image has already been initialized once.
return; return;
} }
this.sourceBlock_ = block;
// Build the DOM. // Build the DOM.
/** @type {SVGElement} */ /** @type {SVGElement} */
this.fieldGroup_ = Blockly.createSvgElement('g', {}, null); this.fieldGroup_ = Blockly.createSvgElement('g', {}, null);
@@ -97,7 +95,7 @@ Blockly.FieldImage.prototype.init = function(block) {
'width': this.width_ + 'px', 'width': this.width_ + 'px',
'fill-opacity': 0}, this.fieldGroup_); 'fill-opacity': 0}, this.fieldGroup_);
} }
block.getSvgRoot().appendChild(this.fieldGroup_); this.sourceBlock_.getSvgRoot().appendChild(this.fieldGroup_);
// Configure the field to be transparent with respect to tooltips. // Configure the field to be transparent with respect to tooltips.
var topElement = this.rectElement_ || this.imageElement_; var topElement = this.rectElement_ || this.imageElement_;

View File

@@ -53,15 +53,12 @@ Blockly.FieldLabel.prototype.EDITABLE = false;
/** /**
* Install this text on a block. * Install this text on a block.
* @param {!Blockly.Block} block The block containing this text.
*/ */
Blockly.FieldLabel.prototype.init = function(block) { Blockly.FieldLabel.prototype.init = function() {
if (this.sourceBlock_) { if (this.textElement_) {
// Text has already been initialized once. // Text has already been initialized once.
return; return;
} }
this.sourceBlock_ = block;
// Build the DOM. // Build the DOM.
this.textElement_ = Blockly.createSvgElement('text', this.textElement_ = Blockly.createSvgElement('text',
{'class': 'blocklyText', 'y': this.size_.height - 5}, null); {'class': 'blocklyText', 'y': this.size_.height - 5}, null);
@@ -71,7 +68,7 @@ Blockly.FieldLabel.prototype.init = function(block) {
if (!this.visible_) { if (!this.visible_) {
this.textElement_.style.display = 'none'; this.textElement_.style.display = 'none';
} }
block.getSvgRoot().appendChild(this.textElement_); this.sourceBlock_.getSvgRoot().appendChild(this.textElement_);
// Configure the field to be transparent with respect to tooltips. // Configure the field to be transparent with respect to tooltips.
this.textElement_.tooltip = this.sourceBlock_; this.textElement_.tooltip = this.sourceBlock_;

View File

@@ -82,7 +82,7 @@ Blockly.FieldVariable.prototype.setValidator = function(handler) {
* @param {!Blockly.Block} block The block containing this text. * @param {!Blockly.Block} block The block containing this text.
*/ */
Blockly.FieldVariable.prototype.init = function(block) { Blockly.FieldVariable.prototype.init = function(block) {
if (this.sourceBlock_) { if (this.fieldGroup_) {
// Dropdown has already been initialized once. // Dropdown has already been initialized once.
return; return;
} }

View File

@@ -326,8 +326,8 @@ Blockly.inject.bindDocumentEvents_ = function() {
/** /**
* Load sounds for the given workspace. * Load sounds for the given workspace.
* @param options {string} The path to the media directory. * @param {string} options The path to the media directory.
* @param workspace {!Blockly.Workspace} The workspace to load sounds for. * @param {!Blockly.Workspace} workspace The workspace to load sounds for.
* @private * @private
*/ */
Blockly.inject.loadSounds_ = function(pathToMedia, workspace) { Blockly.inject.loadSounds_ = function(pathToMedia, workspace) {

View File

@@ -84,8 +84,9 @@ Blockly.Input.prototype.appendField = function(field, opt_name) {
if (goog.isString(field)) { if (goog.isString(field)) {
field = new Blockly.FieldLabel(/** @type {string} */ (field)); field = new Blockly.FieldLabel(/** @type {string} */ (field));
} }
field.setSourceBlock(this.sourceBlock_);
if (this.sourceBlock_.rendered) { if (this.sourceBlock_.rendered) {
field.init(this.sourceBlock_); field.init();
} }
field.name = opt_name; field.name = opt_name;