From 4ac12045508cde39db7e77ba5fbcdf85b657e2f0 Mon Sep 17 00:00:00 2001 From: Neil Fraser Date: Mon, 4 Apr 2016 17:47:15 -0700 Subject: [PATCH] Set field.sourceBlock in headless Blockly. --- core/field.js | 17 ++++++++++++----- core/field_checkbox.js | 2 +- core/field_dropdown.js | 3 +-- core/field_image.js | 8 +++----- core/field_label.js | 9 +++------ core/field_variable.js | 2 +- core/inject.js | 4 ++-- core/input.js | 3 ++- 8 files changed, 25 insertions(+), 23 deletions(-) diff --git a/core/field.js b/core/field.js index 6db2f0c97..30078e0f3 100644 --- a/core/field.js +++ b/core/field.js @@ -118,15 +118,22 @@ Blockly.Field.NBSP = '\u00A0'; 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. */ -Blockly.Field.prototype.init = function(block) { - if (this.sourceBlock_) { +Blockly.Field.prototype.setSourceBlock = function(block) { + 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. return; } - this.sourceBlock_ = block; // Build the DOM. this.fieldGroup_ = Blockly.createSvgElement('g', {}, null); if (!this.visible_) { @@ -144,7 +151,7 @@ Blockly.Field.prototype.init = function(block) { this.fieldGroup_); this.updateEditable(); - block.getSvgRoot().appendChild(this.fieldGroup_); + this.sourceBlock_.getSvgRoot().appendChild(this.fieldGroup_); this.mouseUpWrapper_ = Blockly.bindEvent_(this.fieldGroup_, 'mouseup', this, this.onMouseUp_); // Force a render. diff --git a/core/field_checkbox.js b/core/field_checkbox.js index cdf69b29a..183e226c9 100644 --- a/core/field_checkbox.js +++ b/core/field_checkbox.js @@ -61,7 +61,7 @@ Blockly.FieldCheckbox.prototype.CURSOR = 'default'; * @param {!Blockly.Block} block The block containing this text. */ Blockly.FieldCheckbox.prototype.init = function(block) { - if (this.sourceBlock_) { + if (this.fieldGroup_) { // Checkbox has already been initialized once. return; } diff --git a/core/field_dropdown.js b/core/field_dropdown.js index 332a7b787..d715c8c72 100644 --- a/core/field_dropdown.js +++ b/core/field_dropdown.js @@ -80,11 +80,10 @@ Blockly.FieldDropdown.prototype.CURSOR = 'default'; * @param {!Blockly.Block} block The block containing this text. */ Blockly.FieldDropdown.prototype.init = function(block) { - if (this.sourceBlock_) { + if (this.fieldGroup_) { // Dropdown has already been initialized once. return; } - // Add dropdown arrow: "option ▾" (LTR) or "▾ אופציה" (RTL) this.arrow_ = Blockly.createSvgElement('tspan', {}, null); this.arrow_.appendChild(document.createTextNode( diff --git a/core/field_image.js b/core/field_image.js index b24e75c54..71d8052f9 100644 --- a/core/field_image.js +++ b/core/field_image.js @@ -67,14 +67,12 @@ Blockly.FieldImage.prototype.EDITABLE = false; /** * Install this image on a block. - * @param {!Blockly.Block} block The block containing this text. */ -Blockly.FieldImage.prototype.init = function(block) { - if (this.sourceBlock_) { +Blockly.FieldImage.prototype.init = function() { + if (this.fieldGroup_) { // Image has already been initialized once. return; } - this.sourceBlock_ = block; // Build the DOM. /** @type {SVGElement} */ this.fieldGroup_ = Blockly.createSvgElement('g', {}, null); @@ -97,7 +95,7 @@ Blockly.FieldImage.prototype.init = function(block) { 'width': this.width_ + 'px', '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. var topElement = this.rectElement_ || this.imageElement_; diff --git a/core/field_label.js b/core/field_label.js index 5e7a6cbb9..cb5fa7d39 100644 --- a/core/field_label.js +++ b/core/field_label.js @@ -53,15 +53,12 @@ Blockly.FieldLabel.prototype.EDITABLE = false; /** * Install this text on a block. - * @param {!Blockly.Block} block The block containing this text. */ -Blockly.FieldLabel.prototype.init = function(block) { - if (this.sourceBlock_) { +Blockly.FieldLabel.prototype.init = function() { + if (this.textElement_) { // Text has already been initialized once. return; } - this.sourceBlock_ = block; - // Build the DOM. this.textElement_ = Blockly.createSvgElement('text', {'class': 'blocklyText', 'y': this.size_.height - 5}, null); @@ -71,7 +68,7 @@ Blockly.FieldLabel.prototype.init = function(block) { if (!this.visible_) { 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. this.textElement_.tooltip = this.sourceBlock_; diff --git a/core/field_variable.js b/core/field_variable.js index 3cd4c5deb..8e5bac966 100644 --- a/core/field_variable.js +++ b/core/field_variable.js @@ -82,7 +82,7 @@ Blockly.FieldVariable.prototype.setValidator = function(handler) { * @param {!Blockly.Block} block The block containing this text. */ Blockly.FieldVariable.prototype.init = function(block) { - if (this.sourceBlock_) { + if (this.fieldGroup_) { // Dropdown has already been initialized once. return; } diff --git a/core/inject.js b/core/inject.js index 2e8d93a5d..0db7594c1 100644 --- a/core/inject.js +++ b/core/inject.js @@ -326,8 +326,8 @@ Blockly.inject.bindDocumentEvents_ = function() { /** * Load sounds for the given workspace. - * @param options {string} The path to the media directory. - * @param workspace {!Blockly.Workspace} The workspace to load sounds for. + * @param {string} options The path to the media directory. + * @param {!Blockly.Workspace} workspace The workspace to load sounds for. * @private */ Blockly.inject.loadSounds_ = function(pathToMedia, workspace) { diff --git a/core/input.js b/core/input.js index b92433769..8ca265586 100644 --- a/core/input.js +++ b/core/input.js @@ -84,8 +84,9 @@ Blockly.Input.prototype.appendField = function(field, opt_name) { if (goog.isString(field)) { field = new Blockly.FieldLabel(/** @type {string} */ (field)); } + field.setSourceBlock(this.sourceBlock_); if (this.sourceBlock_.rendered) { - field.init(this.sourceBlock_); + field.init(); } field.name = opt_name;