123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- 'use strict';
- goog.provide('Blockly.Icon');
- goog.require('goog.dom');
- goog.require('goog.math.Coordinate');
- Blockly.Icon = function(block) {
- this.block_ = block;
- };
- Blockly.Icon.prototype.collapseHidden = true;
- Blockly.Icon.prototype.SIZE = 17;
- Blockly.Icon.prototype.bubble_ = null;
- Blockly.Icon.prototype.iconXY_ = null;
- Blockly.Icon.prototype.createIcon = function() {
- if (this.iconGroup_) {
-
- return;
- }
-
- this.iconGroup_ = Blockly.createSvgElement('g',
- {'class': 'blocklyIconGroup'}, null);
- if (this.block_.isInFlyout) {
- Blockly.addClass_( (this.iconGroup_),
- 'blocklyIconGroupReadonly');
- }
- this.drawIcon_(this.iconGroup_);
- this.block_.getSvgRoot().appendChild(this.iconGroup_);
- Blockly.bindEventWithChecks_(this.iconGroup_, 'mouseup', this,
- this.iconClick_);
- this.updateEditable();
- };
- Blockly.Icon.prototype.dispose = function() {
-
- goog.dom.removeNode(this.iconGroup_);
- this.iconGroup_ = null;
-
- this.setVisible(false);
- this.block_ = null;
- };
- Blockly.Icon.prototype.updateEditable = function() {
- };
- Blockly.Icon.prototype.isVisible = function() {
- return !!this.bubble_;
- };
- Blockly.Icon.prototype.iconClick_ = function(e) {
- if (this.block_.workspace.isDragging()) {
-
- return;
- }
- if (!this.block_.isInFlyout && !Blockly.isRightButton(e)) {
- this.setVisible(!this.isVisible());
- }
- };
- Blockly.Icon.prototype.updateColour = function() {
- if (this.isVisible()) {
- this.bubble_.setColour(this.block_.getColour());
- }
- };
- 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;
- };
- Blockly.Icon.prototype.setIconLocation = function(xy) {
- this.iconXY_ = xy;
- if (this.isVisible()) {
- this.bubble_.setAnchorLocation(xy);
- }
- };
- Blockly.Icon.prototype.computeIconLocation = function() {
-
- var blockXY = this.block_.getRelativeToSurfaceXY();
- var iconXY = Blockly.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);
- }
- };
- Blockly.Icon.prototype.getIconLocation = function() {
- return this.iconXY_;
- };
|