|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
goog.provide('Blockly.Bubble');
|
|
|
|
|
|
goog.require('Blockly.Touch');
|
|
|
goog.require('Blockly.Workspace');
|
|
|
goog.require('goog.dom');
|
|
|
goog.require('goog.math.Coordinate');
|
|
|
goog.require('goog.userAgent');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble = function(workspace, content, shape, anchorXY,
|
|
|
bubbleWidth, bubbleHeight) {
|
|
|
this.workspace_ = workspace;
|
|
|
this.content_ = content;
|
|
|
this.shape_ = shape;
|
|
|
|
|
|
var angle = Blockly.Bubble.ARROW_ANGLE;
|
|
|
if (this.workspace_.RTL) {
|
|
|
angle = -angle;
|
|
|
}
|
|
|
this.arrow_radians_ = Blockly.utils.toRadians(angle);
|
|
|
|
|
|
var canvas = workspace.getBubbleCanvas();
|
|
|
canvas.appendChild(this.createDom_(content, !!(bubbleWidth && bubbleHeight)));
|
|
|
|
|
|
this.setAnchorLocation(anchorXY);
|
|
|
if (!bubbleWidth || !bubbleHeight) {
|
|
|
var bBox = (this.content_).getBBox();
|
|
|
bubbleWidth = bBox.width + 2 * Blockly.Bubble.BORDER_WIDTH;
|
|
|
bubbleHeight = bBox.height + 2 * Blockly.Bubble.BORDER_WIDTH;
|
|
|
}
|
|
|
this.setBubbleSize(bubbleWidth, bubbleHeight);
|
|
|
|
|
|
|
|
|
this.positionBubble_();
|
|
|
this.renderArrow_();
|
|
|
this.rendered_ = true;
|
|
|
|
|
|
if (!workspace.options.readOnly) {
|
|
|
Blockly.bindEventWithChecks_(
|
|
|
this.bubbleBack_, 'mousedown', this, this.bubbleMouseDown_);
|
|
|
if (this.resizeGroup_) {
|
|
|
Blockly.bindEventWithChecks_(
|
|
|
this.resizeGroup_, 'mousedown', this, this.resizeMouseDown_);
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.BORDER_WIDTH = 6;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.ARROW_THICKNESS = 5;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.ARROW_ANGLE = 20;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.ARROW_BEND = 4;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.ANCHOR_RADIUS = 8;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.onMouseUpWrapper_ = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.onMouseMoveWrapper_ = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.prototype.resizeCallback_ = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.unbindDragEvents_ = function() {
|
|
|
if (Blockly.Bubble.onMouseUpWrapper_) {
|
|
|
Blockly.unbindEvent_(Blockly.Bubble.onMouseUpWrapper_);
|
|
|
Blockly.Bubble.onMouseUpWrapper_ = null;
|
|
|
}
|
|
|
if (Blockly.Bubble.onMouseMoveWrapper_) {
|
|
|
Blockly.unbindEvent_(Blockly.Bubble.onMouseMoveWrapper_);
|
|
|
Blockly.Bubble.onMouseMoveWrapper_ = null;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.bubbleMouseUp_ = function() {
|
|
|
Blockly.Touch.clearTouchIdentifier();
|
|
|
Blockly.Bubble.unbindDragEvents_();
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.prototype.rendered_ = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.prototype.anchorXY_ = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.prototype.relativeLeft_ = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.prototype.relativeTop_ = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.prototype.width_ = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.prototype.height_ = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.prototype.autoLayout_ = true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.prototype.createDom_ = function(content, hasResize) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.bubbleGroup_ = Blockly.utils.createSvgElement('g', {}, null);
|
|
|
var filter =
|
|
|
{'filter': 'url(#' + this.workspace_.options.embossFilterId + ')'};
|
|
|
if (goog.userAgent.getUserAgentString().indexOf('JavaFX') != -1) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
filter = {};
|
|
|
}
|
|
|
var bubbleEmboss = Blockly.utils.createSvgElement('g',
|
|
|
filter, this.bubbleGroup_);
|
|
|
this.bubbleArrow_ = Blockly.utils.createSvgElement('path', {}, bubbleEmboss);
|
|
|
this.bubbleBack_ = Blockly.utils.createSvgElement('rect',
|
|
|
{
|
|
|
'class': 'blocklyDraggable',
|
|
|
'x': 0,
|
|
|
'y': 0,
|
|
|
'rx': Blockly.Bubble.BORDER_WIDTH,
|
|
|
'ry': Blockly.Bubble.BORDER_WIDTH
|
|
|
},
|
|
|
bubbleEmboss);
|
|
|
if (hasResize) {
|
|
|
this.resizeGroup_ = Blockly.utils.createSvgElement('g',
|
|
|
{'class': this.workspace_.RTL ?
|
|
|
'blocklyResizeSW' : 'blocklyResizeSE'},
|
|
|
this.bubbleGroup_);
|
|
|
var resizeSize = 2 * Blockly.Bubble.BORDER_WIDTH;
|
|
|
Blockly.utils.createSvgElement('polygon',
|
|
|
{'points': '0,x x,x x,0'.replace(/x/g, resizeSize.toString())},
|
|
|
this.resizeGroup_);
|
|
|
Blockly.utils.createSvgElement('line',
|
|
|
{
|
|
|
'class': 'blocklyResizeLine',
|
|
|
'x1': resizeSize / 3, 'y1': resizeSize - 1,
|
|
|
'x2': resizeSize - 1, 'y2': resizeSize / 3
|
|
|
}, this.resizeGroup_);
|
|
|
Blockly.utils.createSvgElement('line',
|
|
|
{
|
|
|
'class': 'blocklyResizeLine',
|
|
|
'x1': resizeSize * 2 / 3,
|
|
|
'y1': resizeSize - 1,
|
|
|
'x2': resizeSize - 1,
|
|
|
'y2': resizeSize * 2 / 3
|
|
|
}, this.resizeGroup_);
|
|
|
} else {
|
|
|
this.resizeGroup_ = null;
|
|
|
}
|
|
|
this.bubbleGroup_.appendChild(content);
|
|
|
return this.bubbleGroup_;
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.prototype.getSvgRoot = function() {
|
|
|
return this.bubbleGroup_;
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.prototype.setSvgId = function(id) {
|
|
|
if (this.bubbleGroup_.dataset) {
|
|
|
this.bubbleGroup_.dataset.blockId = id;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.prototype.bubbleMouseDown_ = function(e) {
|
|
|
var gesture = this.workspace_.getGesture(e);
|
|
|
if (gesture) {
|
|
|
gesture.handleBubbleStart(e, this);
|
|
|
}
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.prototype.showContextMenu_ = function(_e) {
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.prototype.isDeletable = function() {
|
|
|
return false;
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.prototype.resizeMouseDown_ = function(e) {
|
|
|
this.promote_();
|
|
|
Blockly.Bubble.unbindDragEvents_();
|
|
|
if (Blockly.utils.isRightButton(e)) {
|
|
|
|
|
|
e.stopPropagation();
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
this.workspace_.startDrag(e, new goog.math.Coordinate(
|
|
|
this.workspace_.RTL ? -this.width_ : this.width_, this.height_));
|
|
|
|
|
|
Blockly.Bubble.onMouseUpWrapper_ = Blockly.bindEventWithChecks_(document,
|
|
|
'mouseup', this, Blockly.Bubble.bubbleMouseUp_);
|
|
|
Blockly.Bubble.onMouseMoveWrapper_ = Blockly.bindEventWithChecks_(document,
|
|
|
'mousemove', this, this.resizeMouseMove_);
|
|
|
Blockly.hideChaff();
|
|
|
|
|
|
e.stopPropagation();
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.prototype.resizeMouseMove_ = function(e) {
|
|
|
this.autoLayout_ = false;
|
|
|
var newXY = this.workspace_.moveDrag(e);
|
|
|
this.setBubbleSize(this.workspace_.RTL ? -newXY.x : newXY.x, newXY.y);
|
|
|
if (this.workspace_.RTL) {
|
|
|
|
|
|
this.positionBubble_();
|
|
|
}
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.prototype.registerResizeEvent = function(callback) {
|
|
|
this.resizeCallback_ = callback;
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.prototype.promote_ = function() {
|
|
|
var svgGroup = this.bubbleGroup_.parentNode;
|
|
|
if (svgGroup.lastChild !== this.bubbleGroup_) {
|
|
|
svgGroup.appendChild(this.bubbleGroup_);
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.prototype.setAnchorLocation = function(xy) {
|
|
|
this.anchorXY_ = xy;
|
|
|
if (this.rendered_) {
|
|
|
this.positionBubble_();
|
|
|
}
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.prototype.layoutBubble_ = function() {
|
|
|
|
|
|
var relativeLeft = -this.width_ / 4;
|
|
|
var relativeTop = -this.height_ - Blockly.BlockSvg.MIN_BLOCK_Y;
|
|
|
|
|
|
var metrics = this.workspace_.getMetrics();
|
|
|
metrics.viewWidth /= this.workspace_.scale;
|
|
|
metrics.viewLeft /= this.workspace_.scale;
|
|
|
var anchorX = this.anchorXY_.x;
|
|
|
if (this.workspace_.RTL) {
|
|
|
if (anchorX - metrics.viewLeft - relativeLeft - this.width_ <
|
|
|
Blockly.Scrollbar.scrollbarThickness) {
|
|
|
|
|
|
relativeLeft = anchorX - metrics.viewLeft - this.width_ -
|
|
|
Blockly.Scrollbar.scrollbarThickness;
|
|
|
} else if (anchorX - metrics.viewLeft - relativeLeft >
|
|
|
metrics.viewWidth) {
|
|
|
|
|
|
relativeLeft = anchorX - metrics.viewLeft - metrics.viewWidth;
|
|
|
}
|
|
|
} else {
|
|
|
if (anchorX + relativeLeft < metrics.viewLeft) {
|
|
|
|
|
|
relativeLeft = metrics.viewLeft - anchorX;
|
|
|
} else if (metrics.viewLeft + metrics.viewWidth <
|
|
|
anchorX + relativeLeft + this.width_ +
|
|
|
Blockly.BlockSvg.SEP_SPACE_X +
|
|
|
Blockly.Scrollbar.scrollbarThickness) {
|
|
|
|
|
|
relativeLeft = metrics.viewLeft + metrics.viewWidth - anchorX -
|
|
|
this.width_ - Blockly.Scrollbar.scrollbarThickness;
|
|
|
}
|
|
|
}
|
|
|
if (this.anchorXY_.y + relativeTop < metrics.viewTop) {
|
|
|
|
|
|
var bBox = (this.shape_).getBBox();
|
|
|
relativeTop = bBox.height;
|
|
|
}
|
|
|
this.relativeLeft_ = relativeLeft;
|
|
|
this.relativeTop_ = relativeTop;
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.prototype.positionBubble_ = function() {
|
|
|
var left = this.anchorXY_.x;
|
|
|
if (this.workspace_.RTL) {
|
|
|
left -= this.relativeLeft_ ;
|
|
|
} else {
|
|
|
left += this.relativeLeft_;
|
|
|
}
|
|
|
var top = this.relativeTop_ + this.anchorXY_.y;
|
|
|
this.moveTo(left, top);
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.prototype.moveTo = function(x, y) {
|
|
|
this.bubbleGroup_.setAttribute('transform', 'translate(' + x + ',' + y + ')');
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.prototype.getBubbleSize = function() {
|
|
|
return {width: this.width_, height: this.height_};
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.prototype.setBubbleSize = function(width, height) {
|
|
|
var doubleBorderWidth = 2 * Blockly.Bubble.BORDER_WIDTH;
|
|
|
|
|
|
width = Math.max(width, doubleBorderWidth + 45);
|
|
|
height = Math.max(height, doubleBorderWidth + 20);
|
|
|
this.width_ = width;
|
|
|
this.height_ = height;
|
|
|
this.bubbleBack_.setAttribute('width', width);
|
|
|
this.bubbleBack_.setAttribute('height', height);
|
|
|
if (this.resizeGroup_) {
|
|
|
if (this.workspace_.RTL) {
|
|
|
|
|
|
var resizeSize = 2 * Blockly.Bubble.BORDER_WIDTH;
|
|
|
this.resizeGroup_.setAttribute('transform', 'translate(' +
|
|
|
resizeSize + ',' + (height - doubleBorderWidth) + ') scale(-1 1)');
|
|
|
} else {
|
|
|
this.resizeGroup_.setAttribute('transform', 'translate(' +
|
|
|
(width - doubleBorderWidth) + ',' +
|
|
|
(height - doubleBorderWidth) + ')');
|
|
|
}
|
|
|
}
|
|
|
if (this.rendered_) {
|
|
|
if (this.autoLayout_) {
|
|
|
this.layoutBubble_();
|
|
|
}
|
|
|
this.positionBubble_();
|
|
|
this.renderArrow_();
|
|
|
}
|
|
|
|
|
|
if (this.resizeCallback_) {
|
|
|
this.resizeCallback_();
|
|
|
}
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.prototype.renderArrow_ = function() {
|
|
|
var steps = [];
|
|
|
|
|
|
var relBubbleX = this.width_ / 2;
|
|
|
var relBubbleY = this.height_ / 2;
|
|
|
|
|
|
var relAnchorX = -this.relativeLeft_;
|
|
|
var relAnchorY = -this.relativeTop_;
|
|
|
if (relBubbleX == relAnchorX && relBubbleY == relAnchorY) {
|
|
|
|
|
|
|
|
|
steps.push('M ' + relBubbleX + ',' + relBubbleY);
|
|
|
} else {
|
|
|
|
|
|
var rise = relAnchorY - relBubbleY;
|
|
|
var run = relAnchorX - relBubbleX;
|
|
|
if (this.workspace_.RTL) {
|
|
|
run *= -1;
|
|
|
}
|
|
|
var hypotenuse = Math.sqrt(rise * rise + run * run);
|
|
|
var angle = Math.acos(run / hypotenuse);
|
|
|
if (rise < 0) {
|
|
|
angle = 2 * Math.PI - angle;
|
|
|
}
|
|
|
|
|
|
var rightAngle = angle + Math.PI / 2;
|
|
|
if (rightAngle > Math.PI * 2) {
|
|
|
rightAngle -= Math.PI * 2;
|
|
|
}
|
|
|
var rightRise = Math.sin(rightAngle);
|
|
|
var rightRun = Math.cos(rightAngle);
|
|
|
|
|
|
|
|
|
var bubbleSize = this.getBubbleSize();
|
|
|
var thickness = (bubbleSize.width + bubbleSize.height) /
|
|
|
Blockly.Bubble.ARROW_THICKNESS;
|
|
|
thickness = Math.min(thickness, bubbleSize.width, bubbleSize.height) / 4;
|
|
|
|
|
|
|
|
|
var backoffRatio = 1 - Blockly.Bubble.ANCHOR_RADIUS / hypotenuse;
|
|
|
relAnchorX = relBubbleX + backoffRatio * run;
|
|
|
relAnchorY = relBubbleY + backoffRatio * rise;
|
|
|
|
|
|
|
|
|
var baseX1 = relBubbleX + thickness * rightRun;
|
|
|
var baseY1 = relBubbleY + thickness * rightRise;
|
|
|
var baseX2 = relBubbleX - thickness * rightRun;
|
|
|
var baseY2 = relBubbleY - thickness * rightRise;
|
|
|
|
|
|
|
|
|
var swirlAngle = angle + this.arrow_radians_;
|
|
|
if (swirlAngle > Math.PI * 2) {
|
|
|
swirlAngle -= Math.PI * 2;
|
|
|
}
|
|
|
var swirlRise = Math.sin(swirlAngle) *
|
|
|
hypotenuse / Blockly.Bubble.ARROW_BEND;
|
|
|
var swirlRun = Math.cos(swirlAngle) *
|
|
|
hypotenuse / Blockly.Bubble.ARROW_BEND;
|
|
|
|
|
|
steps.push('M' + baseX1 + ',' + baseY1);
|
|
|
steps.push('C' + (baseX1 + swirlRun) + ',' + (baseY1 + swirlRise) +
|
|
|
' ' + relAnchorX + ',' + relAnchorY +
|
|
|
' ' + relAnchorX + ',' + relAnchorY);
|
|
|
steps.push('C' + relAnchorX + ',' + relAnchorY +
|
|
|
' ' + (baseX2 + swirlRun) + ',' + (baseY2 + swirlRise) +
|
|
|
' ' + baseX2 + ',' + baseY2);
|
|
|
}
|
|
|
steps.push('z');
|
|
|
this.bubbleArrow_.setAttribute('d', steps.join(' '));
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.prototype.setColour = function(hexColour) {
|
|
|
this.bubbleBack_.setAttribute('fill', hexColour);
|
|
|
this.bubbleArrow_.setAttribute('fill', hexColour);
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.prototype.dispose = function() {
|
|
|
Blockly.Bubble.unbindDragEvents_();
|
|
|
|
|
|
goog.dom.removeNode(this.bubbleGroup_);
|
|
|
this.bubbleGroup_ = null;
|
|
|
this.bubbleArrow_ = null;
|
|
|
this.bubbleBack_ = null;
|
|
|
this.resizeGroup_ = null;
|
|
|
this.workspace_ = null;
|
|
|
this.content_ = null;
|
|
|
this.shape_ = null;
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.prototype.moveDuringDrag = function(dragSurface, newLoc) {
|
|
|
if (dragSurface) {
|
|
|
dragSurface.translateSurface(newLoc.x, newLoc.y);
|
|
|
} else {
|
|
|
this.moveTo(newLoc.x, newLoc.y);
|
|
|
}
|
|
|
if (this.workspace_.RTL) {
|
|
|
this.relativeLeft_ = this.anchorXY_.x - newLoc.x - this.width_;
|
|
|
} else {
|
|
|
this.relativeLeft_ = newLoc.x - this.anchorXY_.x;
|
|
|
}
|
|
|
this.relativeTop_ = newLoc.y - this.anchorXY_.y;
|
|
|
this.renderArrow_();
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.prototype.getRelativeToSurfaceXY = function() {
|
|
|
return new goog.math.Coordinate(
|
|
|
this.workspace_.RTL ? this.anchorXY_.x - this.relativeLeft_ : this.anchorXY_.x + this.relativeLeft_,
|
|
|
this.anchorXY_.y + this.relativeTop_);
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.Bubble.prototype.setAutoLayout = function(enable) {
|
|
|
this.autoLayout_ = enable;
|
|
|
};
|
|
|
|