-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy patherrorIcon.js
More file actions
279 lines (256 loc) · 7.79 KB
/
Copy patherrorIcon.js
File metadata and controls
279 lines (256 loc) · 7.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/**
* Visual Blocks Editor
*
* Copyright 2012 Google Inc.
* http://blockly.googlecode.com/
*
* 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 a warning.
* @author fraser@google.com (Neil Fraser)
*/
'use strict';
goog.provide('Blockly.ErrorIcon');
/**
* Class for a warning.
* @param {!Blockly.Block} block The block associated with this warning.
* @constructor
*/
Blockly.ErrorIcon = function(block) {
this.block_ = block;
this.createIcon_();
};
/**
* Radius of the warning icon.
*/
Blockly.ErrorIcon.ICON_RADIUS = 8;
/**
* Bubble UI (if visible).
* @type {Blockly.Bubble}
* @private
*/
Blockly.ErrorIcon.prototype.bubble_ = null;
/**
* Warning text (if bubble is not visible).
* @private
*/
Blockly.ErrorIcon.prototype.text_ = '';
/**
* Absolute X coordinate of icon's center.
* @type {number}
* @private
*/
Blockly.ErrorIcon.prototype.iconX_ = 0;
/**
* Absolute Y coordinate of icon's centre.
* @type {number}
* @private
*/
Blockly.ErrorIcon.prototype.iconY_ = 0;
/**
* Create the icon on the block.
* @private
*/
Blockly.ErrorIcon.prototype.createIcon_ = function() {
/* Here's the markup that will be generated:
<g class="blocklyIconGroup">
<path class="blocklyIconShield" d="..."/>
<text class="blocklyIconMark" x="8" y="13">!</text>
</g>
*/
this.iconGroup_ = Blockly.createSvgElement('g',
{'class': 'blocklyIconGroup'}, null);
var iconShield = Blockly.createSvgElement('path',
{'class': 'blocklyErrorIconShield',
'd': 'M 2,15 Q -1,15 0.5,12 L 6.5,1.7 Q 8,-1 9.5,1.7 L 15.5,12 ' +
'Q 17,15 14,15 z'},
this.iconGroup_);
this.iconMark_ = Blockly.createSvgElement('text',
{'class': 'blocklyIconMark',
'x': Blockly.ErrorIcon.ICON_RADIUS,
'y': 2 * Blockly.ErrorIcon.ICON_RADIUS - 3}, this.iconGroup_);
this.iconMark_.appendChild(document.createTextNode('!'));
this.block_.getSvgRoot().appendChild(this.iconGroup_);
Blockly.bindEvent_(this.iconGroup_, 'mouseup', this, this.iconClick_);
};
/**
* Create the text for the warning's bubble.
* @param {string} text The text to display.
* @return {!Element} The top-level node of the text.
* @private
*/
Blockly.ErrorIcon.prototype.textToDom_ = function(text) {
var paragraph = Blockly.createSvgElement('text',
{'class': 'blocklyText', 'y': Blockly.Bubble.BORDER_WIDTH}, null);
var lines = text.split('\n');
for (var i = 0; i < lines.length; i++) {
var tspanElement = Blockly.createSvgElement('tspan',
{'dy': '1em', 'x': Blockly.Bubble.BORDER_WIDTH}, paragraph);
var textNode = document.createTextNode(lines[i]);
tspanElement.appendChild(textNode);
}
return paragraph;
};
/**
* Is the warning bubble visible?
* @return {boolean} True if the bubble is visible.
*/
Blockly.ErrorIcon.prototype.isVisible = function() {
return !!this.bubble_;
};
/**
* Show or hide the warning bubble.
* @param {boolean} visible True if the bubble should be visible.
*/
Blockly.ErrorIcon.prototype.setVisible = function(visible) {
if (visible == this.isVisible()) {
// No change.
return;
}
if (visible) {
// Create the bubble.
var paragraph = this.textToDom_(this.text_);
this.bubble_ = new Blockly.Bubble(
/** @type {!Blockly.Workspace} */ (this.block_.workspace),
paragraph, this.block_.svg_.svgGroup_,
this.iconX_, this.iconY_, null, null);
if (Blockly.RTL) {
// Right-align the paragraph.
// This cannot be done until the bubble is rendered on screen.
var maxWidth = paragraph.getBBox().width;
for (var x = 0, textElement; textElement = paragraph.childNodes[x]; x++) {
textElement.setAttribute('text-anchor', 'end');
textElement.setAttribute('x', maxWidth + Blockly.Bubble.BORDER_WIDTH);
}
}
this.updateColour();
// Bump the warning into the right location.
var size = this.bubble_.getBubbleSize();
this.bubble_.setBubbleSize(size.width, size.height);
} else {
// Dispose of the bubble.
this.bubble_.dispose();
this.bubble_ = null;
this.body_ = null;
this.foreignObject_ = null;
}
};
/**
* Clicking on the icon toggles if the bubble is visible.
* @param {!Event} e Mouse click event.
* @private
*/
Blockly.ErrorIcon.prototype.iconClick_ = function(e) {
this.setVisible(!this.isVisible());
};
/**
* Bring the warning to the top of the stack when clicked on.
* @param {!Event} e Mouse up event.
* @private
*/
Blockly.ErrorIcon.prototype.bodyFocus_ = function(e) {
this.bubble_.promote_();
};
/**
* Set this warning's text.
* @param {string} text Warning text.
*/
Blockly.ErrorIcon.prototype.setText = function(text) {
this.text_ = text;
if (this.isVisible()) {
this.setVisible(false);
this.setVisible(true);
}
};
/**
* Change the colour of a warning to match its block.
*/
Blockly.ErrorIcon.prototype.updateColour = function() {
if (this.isVisible()) {
var hexColour = Blockly.makeColour(this.block_.getColour());
this.bubble_.setColour(hexColour);
}
};
/**
* Dispose of this warning.
*/
Blockly.ErrorIcon.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);
// Disconnect links between the block and the warning.
this.block_.errorIcon = null;
this.block_ = null;
};
/**
* Render the icon for this warning.
* @param {number} cursorX Horizontal offset at which to position the icon.
* @return {number} Horizontal offset for next item to draw.
*/
Blockly.ErrorIcon.prototype.renderIcon = function(cursorX) {
if (this.block_.collapsed) {
this.iconGroup_.setAttribute('display', 'none');
return cursorX;
}
this.iconGroup_.setAttribute('display', 'block');
var TOP_MARGIN = 5;
var diameter = 2 * Blockly.ErrorIcon.ICON_RADIUS;
if (Blockly.RTL) {
cursorX -= diameter;
}
this.iconGroup_.setAttribute('transform',
'translate(' + cursorX + ', ' + TOP_MARGIN + ')');
this.computeIconLocation();
if (Blockly.RTL) {
cursorX -= Blockly.BlockSvg.SEP_SPACE_X;
} else {
cursorX += diameter + Blockly.BlockSvg.SEP_SPACE_X;
}
return cursorX;
};
/**
* Notification that the icon has moved. Update the arrow accordingly.
* @param {number} x Absolute horizontal location.
* @param {number} y Absolute vertical location.
*/
Blockly.ErrorIcon.prototype.setIconLocation = function(x, y) {
this.iconX_ = x;
this.iconY_ = y;
if (this.isVisible()) {
this.bubble_.setAnchorLocation(x, y);
}
};
/**
* Notification that the icon has moved, but we don't really know where.
* Recompute the icon's location from scratch.
*/
Blockly.ErrorIcon.prototype.computeIconLocation = function() {
// Find coordinates for the centre of the icon and update the arrow.
var blockXY = this.block_.getRelativeToSurfaceXY();
var iconXY = Blockly.getRelativeXY_(this.iconGroup_);
var newX = blockXY.x + iconXY.x + Blockly.ErrorIcon.ICON_RADIUS;
var newY = blockXY.y + iconXY.y + Blockly.ErrorIcon.ICON_RADIUS;
if (newX !== this.iconX_ || newY !== this.iconY_) {
this.setIconLocation(newX, newY);
}
};
/**
* Returns the center of the block's icon relative to the surface.
* @return {!Object} Object with x and y properties.
*/
Blockly.ErrorIcon.prototype.getIconLocation = function() {
return {x: this.iconX_, y: this.iconY_};
};