mirror of
https://github.com/google/blockly.git
synced 2026-01-10 02:17:09 +01:00
* Add block drag surface translateSurfaceBy * Add dragged connection manager * Add gesture.js * Add GestureHandler * Implemented gesture skeleton * Most basic workspace dragging * Add dragged connection manager * cleanup * doc * more cleanup * Add gesture handler * Add translateSurfaceInternal * core/block_dragger.js * cleanup * Pull in changes to dragged connection manager * Pull in changes to dragged connection manager * comments * more annotations * Add workspace dragger * Add coordinate annotations * Start on block dragging * Limit number of concurrent gestures * Add some TODOs * start using dragged connection manager * Set origin correctly for dragging blocks * Connect or delete at the end of a block drag. * cleanup * handle field clicks and block + workspace right-clicks * move code into BlockDragger class, but still reach into Gesture internals a lot * Clean up block dragger * Call blockDragger constructor with correct arguments * Enable block dragging in a mutator workspace * Add workspace dragger * click todos * Drag flyout with background * more dragging from flyout * nit * fix dragging from flyouts * Remove unused code and rename gestureHandler to gestureDB * Rename gesture handler * Added some jsdoc in gesture.js * Update some docs * Move some code to block_svg and clean up code * Lots of coordinate annotations * Fix block dragging when zoomed. * Remove built files from branch * More dragging work (#1026) -- Drag bubbles while dragging blocks -- Use bindEventWithChecks to work in touch on Android. Not tested anywhere else yet. -- Handle dragging blocks while zoomed -- Handle dragging blocks in mutators -- Handle right-clicks (I hope) -- Removed lots of unused code * More dragging work (#1048) - Removed gestureDB - Removing uses of terminateDrag - Cleaned up disposal code * Dragging bugfixes (#1058) - Get rid of flyout.dragMode_ and blockly.dragMode_ - Make drags from the flyout start from the top block in the group - Block tooltips from being scheduled or shown during gestures - Don't resize mutator bubbles mid-drag * Fix events in new dragging (#1060) * rebuild for testing * unbuild * Fix events * rebuild * Fix up cursors * Use language files from develop * Remove handled TODOS * attempt to fix IE rerendering bug, and recalculate workspace positions on scroll * Rebuild all the things * Comment cleanup; annotations; delete unused variables.
206 lines
5.3 KiB
JavaScript
206 lines
5.3 KiB
JavaScript
/**
|
|
* @license
|
|
* Visual Blocks Editor
|
|
*
|
|
* Copyright 2013 Google Inc.
|
|
* https://developers.google.com/blockly/
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
/**
|
|
* @fileoverview Object representing an icon on a block.
|
|
* @author fraser@google.com (Neil Fraser)
|
|
*/
|
|
'use strict';
|
|
|
|
goog.provide('Blockly.Icon');
|
|
|
|
goog.require('goog.dom');
|
|
goog.require('goog.math.Coordinate');
|
|
|
|
|
|
/**
|
|
* Class for an icon.
|
|
* @param {Blockly.Block} block The block associated with this icon.
|
|
* @constructor
|
|
*/
|
|
Blockly.Icon = function(block) {
|
|
this.block_ = block;
|
|
};
|
|
|
|
/**
|
|
* Does this icon get hidden when the block is collapsed.
|
|
*/
|
|
Blockly.Icon.prototype.collapseHidden = true;
|
|
|
|
/**
|
|
* Height and width of icons.
|
|
*/
|
|
Blockly.Icon.prototype.SIZE = 17;
|
|
|
|
/**
|
|
* Bubble UI (if visible).
|
|
* @type {Blockly.Bubble}
|
|
* @private
|
|
*/
|
|
Blockly.Icon.prototype.bubble_ = null;
|
|
|
|
/**
|
|
* Absolute coordinate of icon's center.
|
|
* @type {goog.math.Coordinate}
|
|
* @private
|
|
*/
|
|
Blockly.Icon.prototype.iconXY_ = null;
|
|
|
|
/**
|
|
* Create the icon on the block.
|
|
*/
|
|
Blockly.Icon.prototype.createIcon = function() {
|
|
if (this.iconGroup_) {
|
|
// Icon already exists.
|
|
return;
|
|
}
|
|
/* Here's the markup that will be generated:
|
|
<g class="blocklyIconGroup">
|
|
...
|
|
</g>
|
|
*/
|
|
this.iconGroup_ = Blockly.utils.createSvgElement('g',
|
|
{'class': 'blocklyIconGroup'}, null);
|
|
if (this.block_.isInFlyout) {
|
|
Blockly.utils.addClass(/** @type {!Element} */ (this.iconGroup_),
|
|
'blocklyIconGroupReadonly');
|
|
}
|
|
this.drawIcon_(this.iconGroup_);
|
|
|
|
this.block_.getSvgRoot().appendChild(this.iconGroup_);
|
|
Blockly.bindEventWithChecks_(this.iconGroup_, 'mouseup', this,
|
|
this.iconClick_);
|
|
this.updateEditable();
|
|
};
|
|
|
|
/**
|
|
* Dispose of this icon.
|
|
*/
|
|
Blockly.Icon.prototype.dispose = function() {
|
|
// Dispose of and unlink the icon.
|
|
goog.dom.removeNode(this.iconGroup_);
|
|
this.iconGroup_ = null;
|
|
// Dispose of and unlink the bubble.
|
|
this.setVisible(false);
|
|
this.block_ = null;
|
|
};
|
|
|
|
/**
|
|
* Add or remove the UI indicating if this icon may be clicked or not.
|
|
*/
|
|
Blockly.Icon.prototype.updateEditable = function() {
|
|
};
|
|
|
|
/**
|
|
* Is the associated bubble visible?
|
|
* @return {boolean} True if the bubble is visible.
|
|
*/
|
|
Blockly.Icon.prototype.isVisible = function() {
|
|
return !!this.bubble_;
|
|
};
|
|
|
|
/**
|
|
* Clicking on the icon toggles if the bubble is visible.
|
|
* @param {!Event} e Mouse click event.
|
|
* @private
|
|
*/
|
|
Blockly.Icon.prototype.iconClick_ = function(e) {
|
|
if (this.block_.workspace.isDragging()) {
|
|
// Drag operation is concluding. Don't open the editor.
|
|
return;
|
|
}
|
|
if (!this.block_.isInFlyout && !Blockly.utils.isRightButton(e)) {
|
|
this.setVisible(!this.isVisible());
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Change the colour of the associated bubble to match its block.
|
|
*/
|
|
Blockly.Icon.prototype.updateColour = function() {
|
|
if (this.isVisible()) {
|
|
this.bubble_.setColour(this.block_.getColour());
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Render the icon.
|
|
* @param {number} cursorX Horizontal offset at which to position the icon.
|
|
* @return {number} Horizontal offset for next item to draw.
|
|
*/
|
|
Blockly.Icon.prototype.renderIcon = function(cursorX) {
|
|
if (this.collapseHidden && this.block_.isCollapsed()) {
|
|
this.iconGroup_.setAttribute('display', 'none');
|
|
return cursorX;
|
|
}
|
|
this.iconGroup_.setAttribute('display', 'block');
|
|
|
|
var TOP_MARGIN = 5;
|
|
var width = this.SIZE;
|
|
if (this.block_.RTL) {
|
|
cursorX -= width;
|
|
}
|
|
this.iconGroup_.setAttribute('transform',
|
|
'translate(' + cursorX + ',' + TOP_MARGIN + ')');
|
|
this.computeIconLocation();
|
|
if (this.block_.RTL) {
|
|
cursorX -= Blockly.BlockSvg.SEP_SPACE_X;
|
|
} else {
|
|
cursorX += width + Blockly.BlockSvg.SEP_SPACE_X;
|
|
}
|
|
return cursorX;
|
|
};
|
|
|
|
/**
|
|
* Notification that the icon has moved. Update the arrow accordingly.
|
|
* @param {!goog.math.Coordinate} xy Absolute location in workspace coordinates.
|
|
*/
|
|
Blockly.Icon.prototype.setIconLocation = function(xy) {
|
|
this.iconXY_ = xy;
|
|
if (this.isVisible()) {
|
|
this.bubble_.setAnchorLocation(xy);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Notification that the icon has moved, but we don't really know where.
|
|
* Recompute the icon's location from scratch.
|
|
*/
|
|
Blockly.Icon.prototype.computeIconLocation = function() {
|
|
// Find coordinates for the centre of the icon and update the arrow.
|
|
var blockXY = this.block_.getRelativeToSurfaceXY();
|
|
var iconXY = Blockly.utils.getRelativeXY(this.iconGroup_);
|
|
var newXY = new goog.math.Coordinate(
|
|
blockXY.x + iconXY.x + this.SIZE / 2,
|
|
blockXY.y + iconXY.y + this.SIZE / 2);
|
|
if (!goog.math.Coordinate.equals(this.getIconLocation(), newXY)) {
|
|
this.setIconLocation(newXY);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Returns the center of the block's icon relative to the surface.
|
|
* @return {!goog.math.Coordinate} Object with x and y properties in workspace
|
|
* coordinates.
|
|
*/
|
|
Blockly.Icon.prototype.getIconLocation = function() {
|
|
return this.iconXY_;
|
|
};
|