| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| |
| |
| |
| |
| |
| |
|
|
| 'use strict';
|
|
|
| goog.provide('Blockly.FieldIconMenu');
|
|
|
| goog.require('Blockly.DropDownDiv');
|
|
|
| |
| |
| |
| |
| |
|
|
| Blockly.FieldIconMenu = function(icons) {
|
|
|
| this.icons_ = icons;
|
|
|
|
|
|
|
| var defaultValue = icons[0].value;
|
| Blockly.FieldIconMenu.superClass_.constructor.call(this, defaultValue);
|
| this.addArgType('iconmenu');
|
| };
|
| goog.inherits(Blockly.FieldIconMenu, Blockly.Field);
|
|
|
| |
| |
| |
| |
| |
| |
|
|
| Blockly.FieldIconMenu.fromJson = function(element) {
|
| return new Blockly.FieldIconMenu(element['options']);
|
| };
|
|
|
| |
| |
| |
| |
|
|
| Blockly.FieldIconMenu.DROPDOWN_WIDTH = 168;
|
|
|
| |
| |
| |
| |
|
|
| Blockly.FieldIconMenu.savedPrimary_ = null;
|
|
|
| |
| |
| |
|
|
| Blockly.FieldIconMenu.prototype.init = function(block) {
|
| if (this.fieldGroup_) {
|
|
|
| return;
|
| }
|
|
|
|
|
| var arrowSize = 12;
|
|
|
| this.arrowX_ = 18;
|
|
|
| this.arrowY_ = 10;
|
| if (block.RTL) {
|
|
|
| this.arrowX_ = -this.arrowX_ - arrowSize;
|
| }
|
|
|
| this.arrowIcon_ = Blockly.utils.createSvgElement('image', {
|
| 'height': arrowSize + 'px',
|
| 'width': arrowSize + 'px',
|
| 'transform': 'translate(' + this.arrowX_ + ',' + this.arrowY_ + ')'
|
| });
|
| this.arrowIcon_.setAttributeNS('http://www.w3.org/1999/xlink',
|
| 'xlink:href', Blockly.mainWorkspace.options.pathToMedia + 'dropdown-arrow.svg');
|
| block.getSvgRoot().appendChild(this.arrowIcon_);
|
| Blockly.FieldIconMenu.superClass_.init.call(this, block);
|
| };
|
|
|
| |
| |
| |
|
|
| Blockly.FieldIconMenu.prototype.CURSOR = 'default';
|
|
|
| |
| |
| |
| |
|
|
| Blockly.FieldIconMenu.prototype.setValue = function(newValue) {
|
| if (newValue === null || newValue === this.value_) {
|
| return;
|
| }
|
| if (this.sourceBlock_ && Blockly.Events.isEnabled()) {
|
| Blockly.Events.fire(new Blockly.Events.Change(
|
| this.sourceBlock_, 'field', this.name, this.value_, newValue));
|
| }
|
| this.value_ = newValue;
|
|
|
| this.setParentFieldImage(this.getSrcForValue(this.value_));
|
| };
|
|
|
| |
| |
| |
| |
|
|
| Blockly.FieldIconMenu.prototype.setParentFieldImage = function(src) {
|
|
|
|
|
|
|
| if (this.sourceBlock_ && this.sourceBlock_.parentBlock_) {
|
| var parentBlock = this.sourceBlock_.parentBlock_;
|
|
|
| for (var i = 0, input; input = parentBlock.inputList[i]; i++) {
|
| for (var j = 0, field; field = input.fieldRow[j]; j++) {
|
| if (field instanceof Blockly.FieldImage) {
|
|
|
| field.setValue(src);
|
| return;
|
| }
|
| }
|
| }
|
| }
|
| };
|
|
|
| |
| |
| |
|
|
| Blockly.FieldIconMenu.prototype.getValue = function() {
|
| return this.value_;
|
| };
|
|
|
| |
| |
| |
| |
|
|
| Blockly.FieldIconMenu.prototype.getSrcForValue = function(value) {
|
| for (var i = 0, icon; icon = this.icons_[i]; i++) {
|
| if (icon.value === value) {
|
| return icon.src;
|
| }
|
| }
|
| };
|
|
|
| |
| |
| |
|
|
| Blockly.FieldIconMenu.prototype.showEditor_ = function() {
|
|
|
| if (Blockly.DropDownDiv.hideIfOwner(this)) {
|
| return;
|
| }
|
|
|
| Blockly.DropDownDiv.hideWithoutAnimation();
|
| Blockly.DropDownDiv.clearContent();
|
|
|
| var contentDiv = Blockly.DropDownDiv.getContentDiv();
|
|
|
| contentDiv.setAttribute('role', 'menu');
|
| contentDiv.setAttribute('aria-haspopup', 'true');
|
| for (var i = 0, icon; icon = this.icons_[i]; i++) {
|
|
|
|
|
| if (icon.type == 'placeholder') {
|
| var placeholder = document.createElement('span');
|
| placeholder.setAttribute('class', 'blocklyDropDownPlaceholder');
|
| placeholder.style.width = icon.width + 'px';
|
| placeholder.style.height = icon.height + 'px';
|
| contentDiv.appendChild(placeholder);
|
| continue;
|
| }
|
| var button = document.createElement('button');
|
| button.setAttribute('id', ':' + i);
|
| button.setAttribute('role', 'menuitem');
|
| button.setAttribute('class', 'blocklyDropDownButton');
|
| button.title = icon.alt;
|
| button.style.width = icon.width + 'px';
|
| button.style.height = icon.height + 'px';
|
| var backgroundColor = this.sourceBlock_.getColour();
|
| if (icon.value == this.getValue()) {
|
|
|
| backgroundColor = this.sourceBlock_.getColourTertiary();
|
| button.setAttribute('aria-selected', 'true');
|
| }
|
| button.style.backgroundColor = backgroundColor;
|
| button.style.borderColor = this.sourceBlock_.getColourTertiary();
|
| Blockly.bindEvent_(button, 'click', this, this.buttonClick_);
|
| Blockly.bindEvent_(button, 'mouseup', this, this.buttonClick_);
|
|
|
|
|
|
|
| Blockly.bindEvent_(button, 'mousedown', button, function(e) {
|
| this.setAttribute('class', 'blocklyDropDownButton blocklyDropDownButtonHover');
|
| e.preventDefault();
|
| });
|
| Blockly.bindEvent_(button, 'mouseover', button, function() {
|
| this.setAttribute('class', 'blocklyDropDownButton blocklyDropDownButtonHover');
|
| contentDiv.setAttribute('aria-activedescendant', this.id);
|
| });
|
| Blockly.bindEvent_(button, 'mouseout', button, function() {
|
| this.setAttribute('class', 'blocklyDropDownButton');
|
| contentDiv.removeAttribute('aria-activedescendant');
|
| });
|
| var buttonImg = document.createElement('img');
|
| buttonImg.src = icon.src;
|
|
|
|
|
|
|
| button.setAttribute('data-value', icon.value);
|
| buttonImg.setAttribute('data-value', icon.value);
|
| button.appendChild(buttonImg);
|
| contentDiv.appendChild(button);
|
| }
|
| contentDiv.style.width = Blockly.FieldIconMenu.DROPDOWN_WIDTH + 'px';
|
|
|
| Blockly.DropDownDiv.setColour(this.sourceBlock_.getColour(), this.sourceBlock_.getColourTertiary());
|
| Blockly.DropDownDiv.setCategory(this.sourceBlock_.parentBlock_.getCategory());
|
|
|
|
|
| this.savedPrimary_ = this.sourceBlock_.getColour();
|
| this.sourceBlock_.setColour(this.sourceBlock_.getColourSecondary(),
|
| this.sourceBlock_.getColourSecondary(),
|
| this.sourceBlock_.getColourTertiary());
|
|
|
| var scale = this.sourceBlock_.workspace.scale;
|
|
|
| var secondaryYOffset = (
|
| -(Blockly.BlockSvg.MIN_BLOCK_Y * scale) - (Blockly.BlockSvg.FIELD_Y_OFFSET * scale)
|
| );
|
| var renderedPrimary = Blockly.DropDownDiv.showPositionedByBlock(
|
| this, this.sourceBlock_, this.onHide_.bind(this), secondaryYOffset);
|
| if (!renderedPrimary) {
|
|
|
| var arrowX = this.arrowX_ + Blockly.DropDownDiv.ARROW_SIZE / 1.5 + 1;
|
| var arrowY = this.arrowY_ + Blockly.DropDownDiv.ARROW_SIZE / 1.5;
|
|
|
| this.arrowIcon_.setAttribute('transform',
|
| 'translate(' + arrowX + ',' + arrowY + ') rotate(180)');}
|
| };
|
|
|
| |
| |
| |
| |
| |
|
|
| Blockly.FieldIconMenu.prototype.buttonClick_ = function(e) {
|
| var value = e.target.getAttribute('data-value');
|
| this.setValue(value);
|
| Blockly.DropDownDiv.hide();
|
| };
|
|
|
| |
| |
|
|
| Blockly.FieldIconMenu.prototype.onHide_ = function() {
|
|
|
|
|
|
|
|
|
| if (this.sourceBlock_) {
|
| this.sourceBlock_.setColour(this.savedPrimary_,
|
| this.sourceBlock_.getColourSecondary(),
|
| this.sourceBlock_.getColourTertiary());
|
| }
|
| Blockly.DropDownDiv.content_.removeAttribute('role');
|
| Blockly.DropDownDiv.content_.removeAttribute('aria-haspopup');
|
| Blockly.DropDownDiv.content_.removeAttribute('aria-activedescendant');
|
|
|
| this.arrowIcon_.setAttribute('transform', 'translate(' + this.arrowX_ + ',' + this.arrowY_ + ')');
|
| };
|
|
|
| Blockly.Field.register('field_iconmenu', Blockly.FieldIconMenu);
|
|
|