From 1546f563084c223f55d73cd2f6db76583cb289a6 Mon Sep 17 00:00:00 2001 From: Rachel Fenichel Date: Tue, 26 Feb 2019 16:23:21 -0800 Subject: [PATCH] Add click target property to fields, with accessor --- core/field.js | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/core/field.js b/core/field.js index d6eacaf37..52bd66b23 100644 --- a/core/field.js +++ b/core/field.js @@ -151,6 +151,13 @@ Blockly.Field.prototype.visible_ = true; */ Blockly.Field.prototype.validator_ = null; +/** + * The element the click handler is bound to. + * @type {!Element} + * @private + */ +Blockly.Field.prototype.clickTarget_ = null; + /** * Non-breaking space. * @const @@ -200,10 +207,12 @@ Blockly.Field.prototype.init = function() { this.fieldGroup_); this.updateEditable(); + + this.clickTarget_ = this.getSvgRoot(); this.sourceBlock_.getSvgRoot().appendChild(this.fieldGroup_); this.mouseDownWrapper_ = Blockly.bindEventWithChecks_( - this.fieldGroup_, 'mousedown', this, this.onMouseDown_); + this.clickTarget_, 'mousedown', this, this.onMouseDown_); }; /** @@ -235,18 +244,18 @@ Blockly.Field.prototype.dispose = function() { * Add or remove the UI indicating if this field is editable or not. */ Blockly.Field.prototype.updateEditable = function() { - var group = this.fieldGroup_; + var group = this.getClickTarget_(); if (!this.EDITABLE || !group) { return; } if (this.sourceBlock_.isEditable()) { Blockly.utils.addClass(group, 'blocklyEditableText'); Blockly.utils.removeClass(group, 'blocklyNonEditableText'); - this.fieldGroup_.style.cursor = this.CURSOR; + group.style.cursor = this.CURSOR; } else { Blockly.utils.addClass(group, 'blocklyNonEditableText'); Blockly.utils.removeClass(group, 'blocklyEditableText'); - this.fieldGroup_.style.cursor = ''; + group.style.cursor = ''; } }; @@ -600,6 +609,17 @@ Blockly.Field.prototype.setTooltip = function(_newTip) { // Non-abstract sub-classes may wish to implement this. See FieldLabel. }; +/** + * The element to bind the click handler to. If not set explicitly, defaults + * to the SVG root of the field. When this element is + * clicked on an editable field, the editor will open. + * @return {!Element} Element to bind click handler to. + * @private + */ +Blockly.Field.prototype.getClickTarget_ = function() { + return this.clickTarget_ || this.getSvgRoot(); +}; + /** * Return the absolute coordinates of the top-left corner of this field. * The origin (0,0) is the top-left corner of the page body.