Add attribute for argument type (take 2) (#645)

* a more robust way to add an attribute to arguments

* always run superclass constructor before setting argtype
This commit is contained in:
Airhogs777 2016-10-02 11:36:32 -06:00 committed by Tim Mickel
parent 3538cb503e
commit bac07dc65a
11 changed files with 46 additions and 0 deletions

View file

@ -1242,19 +1242,23 @@ Blockly.BlockSvg.prototype.renderInputShape_ = function(input, x, y) {
var inputShapeX = 0, inputShapeY = 0;
// If the input connection is not connected, draw a hole shape.
var inputShapePath = null;
var inputShapeArgType = null;
switch (input.connection.getOutputShape()) {
case Blockly.OUTPUT_SHAPE_HEXAGONAL:
inputShapePath = Blockly.BlockSvg.INPUT_SHAPE_HEXAGONAL;
inputShapeWidth = Blockly.BlockSvg.INPUT_SHAPE_HEXAGONAL_WIDTH;
inputShapeArgType = 'boolean';
break;
case Blockly.OUTPUT_SHAPE_ROUND:
inputShapePath = Blockly.BlockSvg.INPUT_SHAPE_ROUND;
inputShapeWidth = Blockly.BlockSvg.INPUT_SHAPE_ROUND_WIDTH;
inputShapeArgType = 'round';
break;
case Blockly.OUTPUT_SHAPE_SQUARE:
default:
inputShapePath = Blockly.BlockSvg.INPUT_SHAPE_SQUARE;
inputShapeWidth = Blockly.BlockSvg.INPUT_SHAPE_SQUARE_WIDTH;
inputShapeArgType = 'square';
break;
}
if (this.RTL) {
@ -1267,6 +1271,7 @@ Blockly.BlockSvg.prototype.renderInputShape_ = function(input, x, y) {
inputShape.setAttribute('transform',
'translate(' + inputShapeX + ',' + inputShapeY + ')'
);
inputShape.setAttribute('data-argument-type', inputShapeArgType);
inputShape.setAttribute('style', 'visibility: visible');
}
};

View file

@ -102,6 +102,13 @@ Blockly.Field.prototype.sourceBlock_ = null;
*/
Blockly.Field.prototype.visible_ = true;
/**
* The field's argType (for styling).
* @type {Blockly.Block}
* @private
*/
Blockly.Field.prototype.argType_ = null;
/**
* Validation function called when user edits an editable field.
* @type {Function}
@ -142,6 +149,15 @@ Blockly.Field.prototype.init = function() {
if (!this.visible_) {
this.fieldGroup_.style.display = 'none';
}
// Add an attribute to cassify the type of field.
if (this.getArgType() !== null) {
if (this.sourceBlock_.isShadow()) {
this.sourceBlock_.svgGroup_.setAttribute('data-argument-type', this.getArgType());
} else {
// Fields without a shadow wrapper, like square dropdowns.
this.fieldGroup_.setAttribute('data-argument-type', this.getArgType());
}
}
// Adjust X to be flipped for RTL. Position is relative to horizontal start of source block.
var size = this.getSize();
var fieldX = (this.sourceBlock_.RTL) ? -size.width / 2 : size.width / 2;
@ -221,6 +237,22 @@ Blockly.Field.prototype.setVisible = function(visible) {
}
};
/**
* Sets the field's argType (used for styling).
* @param {string} argType New argType.
*/
Blockly.Field.prototype.setArgType = function(argType) {
this.argType_ = argType;
};
/**
* Gets the field's argType (used for styling).
* @return {string} argType string, or null.
*/
Blockly.Field.prototype.getArgType = function() {
return this.argType_;
};
/**
* Sets a new validation function for editable fields.
* @param {Function} handler New validation function, or null.

View file

@ -48,6 +48,7 @@ Blockly.FieldAngle = function(text, opt_validator) {
this.symbol_.appendChild(document.createTextNode('\u00B0'));
Blockly.FieldAngle.superClass_.constructor.call(this, text, opt_validator);
this.setArgType('angle');
};
goog.inherits(Blockly.FieldAngle, Blockly.FieldTextInput);

View file

@ -43,6 +43,7 @@ Blockly.FieldCheckbox = function(state, opt_validator) {
Blockly.FieldCheckbox.superClass_.constructor.call(this, '', opt_validator);
// Set the initial state.
this.setValue(state);
this.setArgType('checkbox');
};
goog.inherits(Blockly.FieldCheckbox, Blockly.Field);

View file

@ -46,6 +46,7 @@ goog.require('goog.ui.ColorPicker');
*/
Blockly.FieldColour = function(colour, opt_validator) {
Blockly.FieldColour.superClass_.constructor.call(this, colour, opt_validator);
this.setArgType('colour');
};
goog.inherits(Blockly.FieldColour, Blockly.Field);

View file

@ -53,6 +53,7 @@ Blockly.FieldDate = function(date, opt_validator) {
}
Blockly.FieldDate.superClass_.constructor.call(this, date, opt_validator);
this.setValue(date);
this.setArgType('date');
};
goog.inherits(Blockly.FieldDate, Blockly.Field);

View file

@ -58,6 +58,7 @@ Blockly.FieldDropdown = function(menuGenerator, opt_validator) {
// Call parent's constructor.
Blockly.FieldDropdown.superClass_.constructor.call(this, firstTuple[1],
opt_validator);
this.setArgType('dropdown');
};
goog.inherits(Blockly.FieldDropdown, Blockly.Field);

View file

@ -45,6 +45,7 @@ Blockly.FieldIconMenu = function(icons) {
// First icon provides the default values.
var defaultValue = icons[0].value;
Blockly.FieldIconMenu.superClass_.constructor.call(this, defaultValue);
this.setArgType('iconmenu');
};
goog.inherits(Blockly.FieldIconMenu, Blockly.Field);

View file

@ -73,6 +73,7 @@ Blockly.FieldNumber = function(value, opt_min, opt_max, opt_precision, opt_valid
this.negativeAllowed_ = (typeof opt_min == 'undefined') || isNaN(opt_min) || opt_min < 0;
var numRestrictor = getNumRestrictor(this.decimalAllowed_, this.negativeAllowed_);
Blockly.FieldNumber.superClass_.constructor.call(this, value, opt_validator, numRestrictor);
this.setArgType('number');
};
goog.inherits(Blockly.FieldNumber, Blockly.FieldTextInput);

View file

@ -54,6 +54,7 @@ Blockly.FieldTextInput = function(text, opt_validator, opt_restrictor) {
Blockly.FieldTextInput.superClass_.constructor.call(this, text,
opt_validator);
this.setRestrictor(opt_restrictor);
this.setArgType('text');
};
goog.inherits(Blockly.FieldTextInput, Blockly.Field);

View file

@ -45,6 +45,7 @@ Blockly.FieldVariable = function(varname, opt_validator) {
Blockly.FieldVariable.superClass_.constructor.call(this,
Blockly.FieldVariable.dropdownCreate, opt_validator);
this.setValue(varname || '');
this.setArgType('variable');
};
goog.inherits(Blockly.FieldVariable, Blockly.FieldDropdown);