|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
goog.provide('Blockly.FieldDropdown');
|
|
|
|
goog.require('Blockly.Field');
|
|
goog.require('Blockly.DropDownDiv');
|
|
goog.require('goog.dom');
|
|
goog.require('goog.events');
|
|
goog.require('goog.style');
|
|
goog.require('goog.ui.Menu');
|
|
goog.require('goog.ui.MenuItem');
|
|
goog.require('goog.userAgent');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldDropdown = function(menuGenerator, opt_validator) {
|
|
this.menuGenerator_ = menuGenerator;
|
|
this.trimOptions_();
|
|
var firstTuple = this.getOptions()[0];
|
|
|
|
|
|
Blockly.FieldDropdown.superClass_.constructor.call(this, firstTuple[1],
|
|
opt_validator);
|
|
this.addArgType('dropdown');
|
|
};
|
|
goog.inherits(Blockly.FieldDropdown, Blockly.Field);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldDropdown.fromJson = function(element) {
|
|
return new Blockly.FieldDropdown(element['options']);
|
|
};
|
|
|
|
|
|
|
|
|
|
Blockly.FieldDropdown.CHECKMARK_OVERHANG = 25;
|
|
|
|
|
|
|
|
|
|
Blockly.FieldDropdown.prototype.CURSOR = 'default';
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldDropdown.prototype.selectedItem = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldDropdown.prototype.value_ = '';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldDropdown.prototype.imageElement_ = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldDropdown.prototype.imageJson_ = null;
|
|
|
|
|
|
|
|
|
|
Blockly.FieldDropdown.prototype.init = function() {
|
|
if (this.fieldGroup_) {
|
|
|
|
return;
|
|
}
|
|
|
|
|
|
|
|
this.arrowSize_ = 12;
|
|
|
|
this.arrowX_ = 0;
|
|
|
|
this.arrowY_ = 11;
|
|
this.arrow_ = Blockly.utils.createSvgElement('image', {
|
|
'height': this.arrowSize_ + 'px',
|
|
'width': this.arrowSize_ + 'px'
|
|
});
|
|
this.arrow_.setAttributeNS('http://www.w3.org/1999/xlink',
|
|
'xlink:href', Blockly.mainWorkspace.options.pathToMedia + 'dropdown-arrow.svg');
|
|
this.className_ += ' blocklyDropdownText';
|
|
|
|
Blockly.FieldDropdown.superClass_.init.call(this);
|
|
|
|
if (!this.sourceBlock_.isShadow()) {
|
|
this.box_ = Blockly.utils.createSvgElement('rect', {
|
|
'rx': Blockly.BlockSvg.CORNER_RADIUS,
|
|
'ry': Blockly.BlockSvg.CORNER_RADIUS,
|
|
'x': 0,
|
|
'y': 0,
|
|
'width': this.size_.width,
|
|
'height': this.size_.height,
|
|
'stroke': this.sourceBlock_.getColourTertiary(),
|
|
'fill': this.sourceBlock_.getColour(),
|
|
'class': 'blocklyBlockBackground',
|
|
'fill-opacity': 1
|
|
}, null);
|
|
this.fieldGroup_.insertBefore(this.box_, this.textElement_);
|
|
}
|
|
|
|
var text = this.text_;
|
|
this.text_ = null;
|
|
this.setText(text);
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldDropdown.prototype.showEditor_ = function() {
|
|
var options = this.getOptions();
|
|
if (options.length == 0) return;
|
|
|
|
this.dropDownOpen_ = true;
|
|
|
|
Blockly.DropDownDiv.hideWithoutAnimation();
|
|
Blockly.DropDownDiv.clearContent();
|
|
|
|
var contentDiv = Blockly.DropDownDiv.getContentDiv();
|
|
|
|
var thisField = this;
|
|
|
|
function callback(e) {
|
|
var menu = this;
|
|
var menuItem = e.target;
|
|
if (menuItem) {
|
|
thisField.onItemSelected(menu, menuItem);
|
|
}
|
|
Blockly.DropDownDiv.hide();
|
|
Blockly.Events.setGroup(false);
|
|
}
|
|
|
|
var menu = new goog.ui.Menu();
|
|
menu.setRightToLeft(this.sourceBlock_.RTL);
|
|
for (var i = 0; i < options.length; i++) {
|
|
var content = options[i][0];
|
|
var value = options[i][1];
|
|
if (typeof content == 'object') {
|
|
|
|
var image = new Image(content['width'], content['height']);
|
|
image.src = content['src'];
|
|
image.alt = content['alt'] || '';
|
|
content = image;
|
|
}
|
|
var menuItem = new goog.ui.MenuItem(content);
|
|
menuItem.setRightToLeft(this.sourceBlock_.RTL);
|
|
menuItem.setValue(value);
|
|
menuItem.setCheckable(true);
|
|
menu.addChild(menuItem, true);
|
|
var checked = (value == this.value_);
|
|
menuItem.setChecked(checked);
|
|
if (checked) {
|
|
this.selectedItem = menuItem;
|
|
}
|
|
}
|
|
|
|
goog.events.listen(menu, goog.ui.Component.EventType.ACTION, callback);
|
|
|
|
|
|
menu.render(contentDiv);
|
|
var menuDom = menu.getElement();
|
|
Blockly.utils.addClass(menuDom, 'blocklyDropdownMenu');
|
|
|
|
var menuSize = goog.style.getSize(menuDom);
|
|
|
|
menuSize.height = menuDom.scrollHeight;
|
|
|
|
var primaryColour = (this.sourceBlock_.isShadow()) ?
|
|
this.sourceBlock_.parentBlock_.getColour() : this.sourceBlock_.getColour();
|
|
|
|
Blockly.DropDownDiv.setColour(primaryColour, this.sourceBlock_.getColourTertiary());
|
|
|
|
var category = (this.sourceBlock_.isShadow()) ?
|
|
this.sourceBlock_.parentBlock_.getCategory() : this.sourceBlock_.getCategory();
|
|
Blockly.DropDownDiv.setCategory(category);
|
|
|
|
|
|
var scale = this.sourceBlock_.workspace.scale;
|
|
var bBox = {width: this.size_.width, height: this.size_.height};
|
|
bBox.width *= scale;
|
|
bBox.height *= scale;
|
|
var position = this.fieldGroup_.getBoundingClientRect();
|
|
var primaryX = position.left + bBox.width / 2;
|
|
var primaryY = position.top + bBox.height;
|
|
var secondaryX = primaryX;
|
|
var secondaryY = position.top;
|
|
|
|
Blockly.DropDownDiv.setBoundsElement(this.sourceBlock_.workspace.getParentSvg().parentNode);
|
|
Blockly.DropDownDiv.show(
|
|
this, primaryX, primaryY, secondaryX, secondaryY, this.onHide.bind(this));
|
|
|
|
menu.setAllowAutoFocus(true);
|
|
menuDom.focus();
|
|
|
|
|
|
if (!this.disableColourChange_) {
|
|
if (this.sourceBlock_.isShadow()) {
|
|
this.sourceBlock_.setShadowColour(this.sourceBlock_.getColourTertiary());
|
|
} else if (this.box_) {
|
|
this.box_.setAttribute('fill', this.sourceBlock_.getColourTertiary());
|
|
}
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
Blockly.FieldDropdown.prototype.onHide = function() {
|
|
this.dropDownOpen_ = false;
|
|
|
|
if (!this.disableColourChange_ && this.sourceBlock_) {
|
|
if (this.sourceBlock_.isShadow()) {
|
|
this.sourceBlock_.clearShadowColour();
|
|
} else if (this.box_) {
|
|
this.box_.setAttribute('fill', this.sourceBlock_.getColour());
|
|
}
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldDropdown.prototype.onItemSelected = function(menu, menuItem) {
|
|
var value = menuItem.getValue();
|
|
if (this.sourceBlock_) {
|
|
|
|
value = this.callValidator(value);
|
|
}
|
|
|
|
if (typeof value == 'function') {
|
|
value();
|
|
return;
|
|
}
|
|
if (value !== null) {
|
|
this.setValue(value);
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldDropdown.prototype.trimOptions_ = function() {
|
|
this.prefixField = null;
|
|
this.suffixField = null;
|
|
var options = this.menuGenerator_;
|
|
if (!goog.isArray(options)) {
|
|
return;
|
|
}
|
|
var hasImages = false;
|
|
|
|
|
|
for (var i = 0; i < options.length; i++) {
|
|
var label = options[i][0];
|
|
if (typeof label == 'string') {
|
|
options[i][0] = Blockly.utils.replaceMessageReferences(label);
|
|
} else {
|
|
if (label.alt != null) {
|
|
options[i][0].alt = Blockly.utils.replaceMessageReferences(label.alt);
|
|
}
|
|
hasImages = true;
|
|
}
|
|
}
|
|
if (hasImages || options.length < 2) {
|
|
return;
|
|
}
|
|
var strings = [];
|
|
for (var i = 0; i < options.length; i++) {
|
|
strings.push(options[i][0]);
|
|
}
|
|
var shortest = Blockly.utils.shortestStringLength(strings);
|
|
var prefixLength = Blockly.utils.commonWordPrefix(strings, shortest);
|
|
var suffixLength = Blockly.utils.commonWordSuffix(strings, shortest);
|
|
if (!prefixLength && !suffixLength) {
|
|
return;
|
|
}
|
|
if (shortest <= prefixLength + suffixLength) {
|
|
|
|
return;
|
|
}
|
|
if (prefixLength) {
|
|
this.prefixField = strings[0].substring(0, prefixLength - 1);
|
|
}
|
|
if (suffixLength) {
|
|
this.suffixField = strings[0].substr(1 - suffixLength);
|
|
}
|
|
|
|
var newOptions = [];
|
|
for (var i = 0; i < options.length; i++) {
|
|
var text = options[i][0];
|
|
var value = options[i][1];
|
|
text = text.substring(prefixLength, text.length - suffixLength);
|
|
newOptions[i] = [text, value];
|
|
}
|
|
this.menuGenerator_ = newOptions;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldDropdown.prototype.isOptionListDynamic = function() {
|
|
return goog.isFunction(this.menuGenerator_);
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldDropdown.prototype.getOptions = function() {
|
|
|
|
var list = goog.isFunction(this.menuGenerator_)
|
|
? this.menuGenerator_.call(this)
|
|
: this.menuGenerator_;
|
|
|
|
return list.length ? list : [['', '']];
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldDropdown.prototype.getValue = function() {
|
|
return this.value_;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldDropdown.prototype.setValue = function(newValue) {
|
|
if (newValue === null || newValue === this.value_) {
|
|
return;
|
|
}
|
|
if (this.sourceBlock_ && Blockly.Events.isEnabled()) {
|
|
Blockly.Events.fire(new Blockly.Events.BlockChange(
|
|
this.sourceBlock_, 'field', this.name, this.value_, newValue));
|
|
}
|
|
|
|
if (this.selectedItem) {
|
|
this.selectedItem.setChecked(false);
|
|
this.selectedItem = null;
|
|
}
|
|
this.value_ = newValue;
|
|
|
|
var options = this.getOptions();
|
|
for (var i = 0; i < options.length; i++) {
|
|
|
|
if (options[i][1] == newValue) {
|
|
var content = options[i][0];
|
|
if (typeof content == 'object') {
|
|
this.imageJson_ = content;
|
|
this.text_ = content.alt;
|
|
} else {
|
|
this.imageJson_ = null;
|
|
this.text_ = content;
|
|
}
|
|
|
|
this.forceRerender();
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
this.text_ = newValue;
|
|
this.forceRerender();
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldDropdown.prototype.setText = function(text) {
|
|
if (text === null || text === this.text_) {
|
|
|
|
return;
|
|
}
|
|
this.text_ = text;
|
|
this.updateTextNode_();
|
|
|
|
if (this.textElement_) {
|
|
this.textElement_.parentNode.appendChild(this.arrow_);
|
|
}
|
|
if (this.sourceBlock_ && this.sourceBlock_.rendered) {
|
|
this.sourceBlock_.render();
|
|
this.sourceBlock_.bumpNeighbours_();
|
|
}
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldDropdown.prototype.positionArrow = function(x) {
|
|
if (!this.arrow_) {
|
|
return 0;
|
|
}
|
|
|
|
var addedWidth = 0;
|
|
if (this.sourceBlock_.RTL) {
|
|
this.arrowX_ = this.arrowSize_ - Blockly.BlockSvg.DROPDOWN_ARROW_PADDING;
|
|
addedWidth = this.arrowSize_ + Blockly.BlockSvg.DROPDOWN_ARROW_PADDING;
|
|
} else {
|
|
this.arrowX_ = x + Blockly.BlockSvg.DROPDOWN_ARROW_PADDING / 2;
|
|
addedWidth = this.arrowSize_ + Blockly.BlockSvg.DROPDOWN_ARROW_PADDING;
|
|
}
|
|
if (this.box_) {
|
|
|
|
this.arrowX_ += Blockly.BlockSvg.BOX_FIELD_PADDING;
|
|
}
|
|
this.arrow_.setAttribute('transform',
|
|
'translate(' + this.arrowX_ + ',' + this.arrowY_ + ')');
|
|
return addedWidth;
|
|
};
|
|
|
|
|
|
|
|
|
|
Blockly.FieldDropdown.prototype.dispose = function() {
|
|
this.selectedItem = null;
|
|
Blockly.WidgetDiv.hideIfOwner(this);
|
|
Blockly.FieldDropdown.superClass_.dispose.call(this);
|
|
};
|
|
|
|
Blockly.Field.register('field_dropdown', Blockly.FieldDropdown);
|
|
|