| 1 |
// Copyright 2007 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview A thicker wrapper around the DOM element returned from |
| 17 |
* the different draw methods of the graphics implementation, and |
| 18 |
* all interfaces that the various element types support. |
| 19 |
* @author [email protected] (Robby Walker) |
| 20 |
*/ |
| 21 |
|
| 22 |
goog.provide('goog.graphics.ext.Element'); |
| 23 |
|
| 24 |
goog.require('goog.events.EventTarget'); |
| 25 |
goog.require('goog.functions'); |
| 26 |
goog.require('goog.graphics.ext.coordinates'); |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
/** |
| 31 |
* Base class for a wrapper around the goog.graphics wrapper that enables |
| 32 |
* more advanced functionality. |
| 33 |
* @param {goog.graphics.ext.Group?} group Parent for this element. |
| 34 |
* @param {goog.graphics.Element} wrapper The thin wrapper to wrap. |
| 35 |
* @constructor |
| 36 |
* @extends {goog.events.EventTarget} |
| 37 |
*/ |
| 38 |
goog.graphics.ext.Element = function(group, wrapper) { |
| 39 |
goog.events.EventTarget.call(this); |
| 40 |
this.wrapper_ = wrapper; |
| 41 |
this.graphics_ = group ? group.getGraphics() : this; |
| 42 |
|
| 43 |
this.xPosition_ = new goog.graphics.ext.Element.Position_(this, true); |
| 44 |
this.yPosition_ = new goog.graphics.ext.Element.Position_(this, false); |
| 45 |
|
| 46 |
// Handle parent / child relationships. |
| 47 |
if (group) { |
| 48 |
this.parent_ = group; |
| 49 |
this.parent_.addChild(this); |
| 50 |
} |
| 51 |
}; |
| 52 |
goog.inherits(goog.graphics.ext.Element, goog.events.EventTarget); |
| 53 |
|
| 54 |
|
| 55 |
/** |
| 56 |
* The graphics object that contains this element. |
| 57 |
* @type {goog.graphics.ext.Graphics|goog.graphics.ext.Element} |
| 58 |
* @private |
| 59 |
*/ |
| 60 |
goog.graphics.ext.Element.prototype.graphics_; |
| 61 |
|
| 62 |
|
| 63 |
/** |
| 64 |
* The goog.graphics wrapper this class wraps. |
| 65 |
* @type {goog.graphics.Element} |
| 66 |
* @private |
| 67 |
*/ |
| 68 |
goog.graphics.ext.Element.prototype.wrapper_; |
| 69 |
|
| 70 |
|
| 71 |
/** |
| 72 |
* The group or surface containing this element. |
| 73 |
* @type {goog.graphics.ext.Group|undefined} |
| 74 |
* @private |
| 75 |
*/ |
| 76 |
goog.graphics.ext.Element.prototype.parent_; |
| 77 |
|
| 78 |
|
| 79 |
/** |
| 80 |
* Whether or not computation of this element's position or size depends on its |
| 81 |
* parent's size. |
| 82 |
* @type {boolean} |
| 83 |
* @private |
| 84 |
*/ |
| 85 |
goog.graphics.ext.Element.prototype.parentDependent_ = false; |
| 86 |
|
| 87 |
|
| 88 |
/** |
| 89 |
* Whether the element has pending transformations. |
| 90 |
* @type {boolean} |
| 91 |
* @private |
| 92 |
*/ |
| 93 |
goog.graphics.ext.Element.prototype.needsTransform_ = false; |
| 94 |
|
| 95 |
|
| 96 |
/** |
| 97 |
* The current angle of rotation, expressed in degrees. |
| 98 |
* @type {number} |
| 99 |
* @private |
| 100 |
*/ |
| 101 |
goog.graphics.ext.Element.prototype.rotation_ = 0; |
| 102 |
|
| 103 |
|
| 104 |
/** |
| 105 |
* Object representing the x position and size of the element. |
| 106 |
* @type {goog.graphics.ext.Element.Position_} |
| 107 |
* @private |
| 108 |
*/ |
| 109 |
goog.graphics.ext.Element.prototype.xPosition_; |
| 110 |
|
| 111 |
|
| 112 |
/** |
| 113 |
* Object representing the y position and size of the element. |
| 114 |
* @type {goog.graphics.ext.Element.Position_} |
| 115 |
* @private |
| 116 |
*/ |
| 117 |
goog.graphics.ext.Element.prototype.yPosition_; |
| 118 |
|
| 119 |
|
| 120 |
/** @return {goog.graphics.Element} The underlying thin wrapper. */ |
| 121 |
goog.graphics.ext.Element.prototype.getWrapper = function() { |
| 122 |
return this.wrapper_; |
| 123 |
}; |
| 124 |
|
| 125 |
|
| 126 |
/** |
| 127 |
* @return {goog.graphics.ext.Element|goog.graphics.ext.Graphics} The graphics |
| 128 |
* surface the element is a part of. |
| 129 |
*/ |
| 130 |
goog.graphics.ext.Element.prototype.getGraphics = function() { |
| 131 |
return this.graphics_; |
| 132 |
}; |
| 133 |
|
| 134 |
|
| 135 |
/** |
| 136 |
* Returns the graphics implementation. |
| 137 |
* @return {goog.graphics.AbstractGraphics} The underlying graphics |
| 138 |
* implementation drawing this element's wrapper. |
| 139 |
* @protected |
| 140 |
*/ |
| 141 |
goog.graphics.ext.Element.prototype.getGraphicsImplementation = function() { |
| 142 |
return this.graphics_.getImplementation(); |
| 143 |
}; |
| 144 |
|
| 145 |
|
| 146 |
/** |
| 147 |
* @return {goog.graphics.ext.Group|undefined} The parent of this element. |
| 148 |
*/ |
| 149 |
goog.graphics.ext.Element.prototype.getParent = function() { |
| 150 |
return this.parent_; |
| 151 |
}; |
| 152 |
|
| 153 |
|
| 154 |
// GENERAL POSITIONING |
| 155 |
|
| 156 |
|
| 157 |
/** |
| 158 |
* Internal convenience method for setting position - either as a left/top, |
| 159 |
* center/middle, or right/bottom value. Only one should be specified. |
| 160 |
* @param {goog.graphics.ext.Element.Position_} position The position object to |
| 161 |
* set the value on. |
| 162 |
* @param {number|string} value The value of the coordinate. |
| 163 |
* @param {goog.graphics.ext.Element.PositionType_} type The type of the |
| 164 |
* coordinate. |
| 165 |
* @param {boolean=} opt_chain Optional flag to specify this function is part |
| 166 |
* of a chain of calls and therefore transformations should be set as |
| 167 |
* pending but not yet performed. |
| 168 |
* @private |
| 169 |
*/ |
| 170 |
goog.graphics.ext.Element.prototype.setPosition_ = function( |
| 171 |
position, value, type, opt_chain) { |
| 172 |
position.setPosition(value, type); |
| 173 |
this.computeIsParentDependent_(position); |
| 174 |
|
| 175 |
this.needsTransform_ = true; |
| 176 |
if (!opt_chain) { |
| 177 |
this.transform(); |
| 178 |
} |
| 179 |
}; |
| 180 |
|
| 181 |
|
| 182 |
/** |
| 183 |
* Sets the width/height of the element. |
| 184 |
* @param {goog.graphics.ext.Element.Position_} position The position object to |
| 185 |
* set the value on. |
| 186 |
* @param {string|number} size The new width/height value. |
| 187 |
* @param {boolean=} opt_chain Optional flag to specify this function is part |
| 188 |
* of a chain of calls and therefore transformations should be set as |
| 189 |
* pending but not yet performed. |
| 190 |
* @private |
| 191 |
*/ |
| 192 |
goog.graphics.ext.Element.prototype.setSize_ = function( |
| 193 |
position, size, opt_chain) { |
| 194 |
if (position.setSize(size)) { |
| 195 |
this.needsTransform_ = true; |
| 196 |
|
| 197 |
this.computeIsParentDependent_(position); |
| 198 |
|
| 199 |
if (!opt_chain) { |
| 200 |
this.reset(); |
| 201 |
} |
| 202 |
} else if (!opt_chain && this.isPendingTransform()) { |
| 203 |
this.reset(); |
| 204 |
} |
| 205 |
}; |
| 206 |
|
| 207 |
|
| 208 |
/** |
| 209 |
* Sets the minimum width/height of the element. |
| 210 |
* @param {goog.graphics.ext.Element.Position_} position The position object to |
| 211 |
* set the value on. |
| 212 |
* @param {string|number} minSize The minimum width/height of the element. |
| 213 |
* @private |
| 214 |
*/ |
| 215 |
goog.graphics.ext.Element.prototype.setMinSize_ = function(position, minSize) { |
| 216 |
position.setMinSize(minSize); |
| 217 |
this.needsTransform_ = true; |
| 218 |
this.computeIsParentDependent_(position); |
| 219 |
}; |
| 220 |
|
| 221 |
|
| 222 |
// HORIZONTAL POSITIONING |
| 223 |
|
| 224 |
|
| 225 |
/** |
| 226 |
* @return {number} The distance from the left edge of this element to the left |
| 227 |
* edge of its parent, specified in units of the parent's coordinate system. |
| 228 |
*/ |
| 229 |
goog.graphics.ext.Element.prototype.getLeft = function() { |
| 230 |
return this.xPosition_.getStart(); |
| 231 |
}; |
| 232 |
|
| 233 |
|
| 234 |
/** |
| 235 |
* Sets the left coordinate of the element. Overwrites any previous value of |
| 236 |
* left, center, or right for this element. |
| 237 |
* @param {string|number} left The left coordinate. |
| 238 |
* @param {boolean=} opt_chain Optional flag to specify this function is part |
| 239 |
* of a chain of calls and therefore transformations should be set as |
| 240 |
* pending but not yet performed. |
| 241 |
*/ |
| 242 |
goog.graphics.ext.Element.prototype.setLeft = function(left, opt_chain) { |
| 243 |
this.setPosition_( |
| 244 |
this.xPosition_, left, goog.graphics.ext.Element.PositionType_.START, |
| 245 |
opt_chain); |
| 246 |
}; |
| 247 |
|
| 248 |
|
| 249 |
/** |
| 250 |
* @return {number} The right coordinate of the element, in units of the |
| 251 |
* parent's coordinate system. |
| 252 |
*/ |
| 253 |
goog.graphics.ext.Element.prototype.getRight = function() { |
| 254 |
return this.xPosition_.getEnd(); |
| 255 |
}; |
| 256 |
|
| 257 |
|
| 258 |
/** |
| 259 |
* Sets the right coordinate of the element. Overwrites any previous value of |
| 260 |
* left, center, or right for this element. |
| 261 |
* @param {string|number} right The right coordinate. |
| 262 |
* @param {boolean=} opt_chain Optional flag to specify this function is part |
| 263 |
* of a chain of calls and therefore transformations should be set as |
| 264 |
* pending but not yet performed. |
| 265 |
*/ |
| 266 |
goog.graphics.ext.Element.prototype.setRight = function(right, opt_chain) { |
| 267 |
this.setPosition_( |
| 268 |
this.xPosition_, right, goog.graphics.ext.Element.PositionType_.END, |
| 269 |
opt_chain); |
| 270 |
}; |
| 271 |
|
| 272 |
|
| 273 |
/** |
| 274 |
* @return {number} The center coordinate of the element, in units of the |
| 275 |
* parent's coordinate system. |
| 276 |
*/ |
| 277 |
goog.graphics.ext.Element.prototype.getCenter = function() { |
| 278 |
return this.xPosition_.getMiddle(); |
| 279 |
}; |
| 280 |
|
| 281 |
|
| 282 |
/** |
| 283 |
* Sets the center coordinate of the element. Overwrites any previous value of |
| 284 |
* left, center, or right for this element. |
| 285 |
* @param {string|number} center The center coordinate. |
| 286 |
* @param {boolean=} opt_chain Optional flag to specify this function is part |
| 287 |
* of a chain of calls and therefore transformations should be set as |
| 288 |
* pending but not yet performed. |
| 289 |
*/ |
| 290 |
goog.graphics.ext.Element.prototype.setCenter = function(center, opt_chain) { |
| 291 |
this.setPosition_( |
| 292 |
this.xPosition_, center, goog.graphics.ext.Element.PositionType_.MIDDLE, |
| 293 |
opt_chain); |
| 294 |
}; |
| 295 |
|
| 296 |
|
| 297 |
// VERTICAL POSITIONING |
| 298 |
|
| 299 |
|
| 300 |
/** |
| 301 |
* @return {number} The distance from the top edge of this element to the top |
| 302 |
* edge of its parent, specified in units of the parent's coordinate system. |
| 303 |
*/ |
| 304 |
goog.graphics.ext.Element.prototype.getTop = function() { |
| 305 |
return this.yPosition_.getStart(); |
| 306 |
}; |
| 307 |
|
| 308 |
|
| 309 |
/** |
| 310 |
* Sets the top coordinate of the element. Overwrites any previous value of |
| 311 |
* top, middle, or bottom for this element. |
| 312 |
* @param {string|number} top The top coordinate. |
| 313 |
* @param {boolean=} opt_chain Optional flag to specify this function is part |
| 314 |
* of a chain of calls and therefore transformations should be set as |
| 315 |
* pending but not yet performed. |
| 316 |
*/ |
| 317 |
goog.graphics.ext.Element.prototype.setTop = function(top, opt_chain) { |
| 318 |
this.setPosition_( |
| 319 |
this.yPosition_, top, goog.graphics.ext.Element.PositionType_.START, |
| 320 |
opt_chain); |
| 321 |
}; |
| 322 |
|
| 323 |
|
| 324 |
/** |
| 325 |
* @return {number} The bottom coordinate of the element, in units of the |
| 326 |
* parent's coordinate system. |
| 327 |
*/ |
| 328 |
goog.graphics.ext.Element.prototype.getBottom = function() { |
| 329 |
return this.yPosition_.getEnd(); |
| 330 |
}; |
| 331 |
|
| 332 |
|
| 333 |
/** |
| 334 |
* Sets the bottom coordinate of the element. Overwrites any previous value of |
| 335 |
* top, middle, or bottom for this element. |
| 336 |
* @param {string|number} bottom The bottom coordinate. |
| 337 |
* @param {boolean=} opt_chain Optional flag to specify this function is part |
| 338 |
* of a chain of calls and therefore transformations should be set as |
| 339 |
* pending but not yet performed. |
| 340 |
*/ |
| 341 |
goog.graphics.ext.Element.prototype.setBottom = function(bottom, opt_chain) { |
| 342 |
this.setPosition_( |
| 343 |
this.yPosition_, bottom, goog.graphics.ext.Element.PositionType_.END, |
| 344 |
opt_chain); |
| 345 |
}; |
| 346 |
|
| 347 |
|
| 348 |
/** |
| 349 |
* @return {number} The middle coordinate of the element, in units of the |
| 350 |
* parent's coordinate system. |
| 351 |
*/ |
| 352 |
goog.graphics.ext.Element.prototype.getMiddle = function() { |
| 353 |
return this.yPosition_.getMiddle(); |
| 354 |
}; |
| 355 |
|
| 356 |
|
| 357 |
/** |
| 358 |
* Sets the middle coordinate of the element. Overwrites any previous value of |
| 359 |
* top, middle, or bottom for this element |
| 360 |
* @param {string|number} middle The middle coordinate. |
| 361 |
* @param {boolean=} opt_chain Optional flag to specify this function is part |
| 362 |
* of a chain of calls and therefore transformations should be set as |
| 363 |
* pending but not yet performed. |
| 364 |
*/ |
| 365 |
goog.graphics.ext.Element.prototype.setMiddle = function(middle, opt_chain) { |
| 366 |
this.setPosition_( |
| 367 |
this.yPosition_, middle, goog.graphics.ext.Element.PositionType_.MIDDLE, |
| 368 |
opt_chain); |
| 369 |
}; |
| 370 |
|
| 371 |
|
| 372 |
// DIMENSIONS |
| 373 |
|
| 374 |
|
| 375 |
/** |
| 376 |
* @return {number} The width of the element, in units of the parent's |
| 377 |
* coordinate system. |
| 378 |
*/ |
| 379 |
goog.graphics.ext.Element.prototype.getWidth = function() { |
| 380 |
return this.xPosition_.getSize(); |
| 381 |
}; |
| 382 |
|
| 383 |
|
| 384 |
/** |
| 385 |
* Sets the width of the element. |
| 386 |
* @param {string|number} width The new width value. |
| 387 |
* @param {boolean=} opt_chain Optional flag to specify this function is part |
| 388 |
* of a chain of calls and therefore transformations should be set as |
| 389 |
* pending but not yet performed. |
| 390 |
*/ |
| 391 |
goog.graphics.ext.Element.prototype.setWidth = function(width, opt_chain) { |
| 392 |
this.setSize_(this.xPosition_, width, opt_chain); |
| 393 |
}; |
| 394 |
|
| 395 |
|
| 396 |
/** |
| 397 |
* @return {number} The minimum width of the element, in units of the parent's |
| 398 |
* coordinate system. |
| 399 |
*/ |
| 400 |
goog.graphics.ext.Element.prototype.getMinWidth = function() { |
| 401 |
return this.xPosition_.getMinSize(); |
| 402 |
}; |
| 403 |
|
| 404 |
|
| 405 |
/** |
| 406 |
* Sets the minimum width of the element. |
| 407 |
* @param {string|number} minWidth The minimum width of the element. |
| 408 |
*/ |
| 409 |
goog.graphics.ext.Element.prototype.setMinWidth = function(minWidth) { |
| 410 |
this.setMinSize_(this.xPosition_, minWidth); |
| 411 |
}; |
| 412 |
|
| 413 |
|
| 414 |
/** |
| 415 |
* @return {number} The height of the element, in units of the parent's |
| 416 |
* coordinate system. |
| 417 |
*/ |
| 418 |
goog.graphics.ext.Element.prototype.getHeight = function() { |
| 419 |
return this.yPosition_.getSize(); |
| 420 |
}; |
| 421 |
|
| 422 |
|
| 423 |
/** |
| 424 |
* Sets the height of the element. |
| 425 |
* @param {string|number} height The new height value. |
| 426 |
* @param {boolean=} opt_chain Optional flag to specify this function is part |
| 427 |
* of a chain of calls and therefore transformations should be set as |
| 428 |
* pending but not yet performed. |
| 429 |
*/ |
| 430 |
goog.graphics.ext.Element.prototype.setHeight = function(height, opt_chain) { |
| 431 |
this.setSize_(this.yPosition_, height, opt_chain); |
| 432 |
}; |
| 433 |
|
| 434 |
|
| 435 |
/** |
| 436 |
* @return {number} The minimum height of the element, in units of the parent's |
| 437 |
* coordinate system. |
| 438 |
*/ |
| 439 |
goog.graphics.ext.Element.prototype.getMinHeight = function() { |
| 440 |
return this.yPosition_.getMinSize(); |
| 441 |
}; |
| 442 |
|
| 443 |
|
| 444 |
/** |
| 445 |
* Sets the minimum height of the element. |
| 446 |
* @param {string|number} minHeight The minimum height of the element. |
| 447 |
*/ |
| 448 |
goog.graphics.ext.Element.prototype.setMinHeight = function(minHeight) { |
| 449 |
this.setMinSize_(this.yPosition_, minHeight); |
| 450 |
}; |
| 451 |
|
| 452 |
|
| 453 |
// BOUNDS SHORTCUTS |
| 454 |
|
| 455 |
|
| 456 |
/** |
| 457 |
* Shortcut for setting the left and top position. |
| 458 |
* @param {string|number} left The left coordinate. |
| 459 |
* @param {string|number} top The top coordinate. |
| 460 |
* @param {boolean=} opt_chain Optional flag to specify this function is part |
| 461 |
* of a chain of calls and therefore transformations should be set as |
| 462 |
* pending but not yet performed. |
| 463 |
*/ |
| 464 |
goog.graphics.ext.Element.prototype.setPosition = function( |
| 465 |
left, top, opt_chain) { |
| 466 |
this.setLeft(left, true); |
| 467 |
this.setTop(top, opt_chain); |
| 468 |
}; |
| 469 |
|
| 470 |
|
| 471 |
/** |
| 472 |
* Shortcut for setting the width and height. |
| 473 |
* @param {string|number} width The new width value. |
| 474 |
* @param {string|number} height The new height value. |
| 475 |
* @param {boolean=} opt_chain Optional flag to specify this function is part |
| 476 |
* of a chain of calls and therefore transformations should be set as |
| 477 |
* pending but not yet performed. |
| 478 |
*/ |
| 479 |
goog.graphics.ext.Element.prototype.setSize = function( |
| 480 |
width, height, opt_chain) { |
| 481 |
this.setWidth(width, true); |
| 482 |
this.setHeight(height, opt_chain); |
| 483 |
}; |
| 484 |
|
| 485 |
|
| 486 |
/** |
| 487 |
* Shortcut for setting the left, top, width, and height. |
| 488 |
* @param {string|number} left The left coordinate. |
| 489 |
* @param {string|number} top The top coordinate. |
| 490 |
* @param {string|number} width The new width value. |
| 491 |
* @param {string|number} height The new height value. |
| 492 |
* @param {boolean=} opt_chain Optional flag to specify this function is part |
| 493 |
* of a chain of calls and therefore transformations should be set as |
| 494 |
* pending but not yet performed. |
| 495 |
*/ |
| 496 |
goog.graphics.ext.Element.prototype.setBounds = function( |
| 497 |
left, top, width, height, opt_chain) { |
| 498 |
this.setLeft(left, true); |
| 499 |
this.setTop(top, true); |
| 500 |
this.setWidth(width, true); |
| 501 |
this.setHeight(height, opt_chain); |
| 502 |
}; |
| 503 |
|
| 504 |
|
| 505 |
// MAXIMUM BOUNDS |
| 506 |
|
| 507 |
|
| 508 |
/** |
| 509 |
* @return {number} An estimate of the maximum x extent this element would have |
| 510 |
* in a parent of no width. |
| 511 |
*/ |
| 512 |
goog.graphics.ext.Element.prototype.getMaxX = function() { |
| 513 |
return this.xPosition_.getMaxPosition(); |
| 514 |
}; |
| 515 |
|
| 516 |
|
| 517 |
/** |
| 518 |
* @return {number} An estimate of the maximum y extent this element would have |
| 519 |
* in a parent of no height. |
| 520 |
*/ |
| 521 |
goog.graphics.ext.Element.prototype.getMaxY = function() { |
| 522 |
return this.yPosition_.getMaxPosition(); |
| 523 |
}; |
| 524 |
|
| 525 |
|
| 526 |
// RESET |
| 527 |
|
| 528 |
|
| 529 |
/** |
| 530 |
* Reset the element. This is called when the element changes size, or when |
| 531 |
* the coordinate system changes in a way that would affect pixel based |
| 532 |
* rendering |
| 533 |
*/ |
| 534 |
goog.graphics.ext.Element.prototype.reset = function() { |
| 535 |
this.xPosition_.resetCache(); |
| 536 |
this.yPosition_.resetCache(); |
| 537 |
|
| 538 |
this.redraw(); |
| 539 |
|
| 540 |
this.needsTransform_ = true; |
| 541 |
this.transform(); |
| 542 |
}; |
| 543 |
|
| 544 |
|
| 545 |
/** |
| 546 |
* Overridable function for subclass specific reset. |
| 547 |
* @protected |
| 548 |
*/ |
| 549 |
goog.graphics.ext.Element.prototype.redraw = goog.nullFunction; |
| 550 |
|
| 551 |
|
| 552 |
// PARENT DEPENDENCY |
| 553 |
|
| 554 |
|
| 555 |
/** |
| 556 |
* Computes whether the element is still parent dependent. |
| 557 |
* @param {goog.graphics.ext.Element.Position_} position The recently changed |
| 558 |
* position object. |
| 559 |
* @private |
| 560 |
*/ |
| 561 |
goog.graphics.ext.Element.prototype.computeIsParentDependent_ = function( |
| 562 |
position) { |
| 563 |
this.parentDependent_ = position.isParentDependent() || |
| 564 |
this.xPosition_.isParentDependent() || |
| 565 |
this.yPosition_.isParentDependent() || this.checkParentDependent(); |
| 566 |
}; |
| 567 |
|
| 568 |
|
| 569 |
/** |
| 570 |
* Returns whether this element's bounds depend on its parents. |
| 571 |
* |
| 572 |
* This function should be treated as if it has package scope. |
| 573 |
* @return {boolean} Whether this element's bounds depend on its parents. |
| 574 |
*/ |
| 575 |
goog.graphics.ext.Element.prototype.isParentDependent = function() { |
| 576 |
return this.parentDependent_; |
| 577 |
}; |
| 578 |
|
| 579 |
|
| 580 |
/** |
| 581 |
* Overridable function for subclass specific parent dependency. |
| 582 |
* @return {boolean} Whether this shape's bounds depends on its parent's. |
| 583 |
* @protected |
| 584 |
*/ |
| 585 |
goog.graphics.ext.Element.prototype.checkParentDependent = goog.functions.FALSE; |
| 586 |
|
| 587 |
|
| 588 |
// ROTATION |
| 589 |
|
| 590 |
|
| 591 |
/** |
| 592 |
* Set the rotation of this element. |
| 593 |
* @param {number} angle The angle of rotation, in degrees. |
| 594 |
*/ |
| 595 |
goog.graphics.ext.Element.prototype.setRotation = function(angle) { |
| 596 |
if (this.rotation_ != angle) { |
| 597 |
this.rotation_ = angle; |
| 598 |
|
| 599 |
this.needsTransform_ = true; |
| 600 |
this.transform(); |
| 601 |
} |
| 602 |
}; |
| 603 |
|
| 604 |
|
| 605 |
/** |
| 606 |
* @return {number} The angle of rotation of this element, in degrees. |
| 607 |
*/ |
| 608 |
goog.graphics.ext.Element.prototype.getRotation = function() { |
| 609 |
return this.rotation_; |
| 610 |
}; |
| 611 |
|
| 612 |
|
| 613 |
// TRANSFORMS |
| 614 |
|
| 615 |
|
| 616 |
/** |
| 617 |
* Called by the parent when the parent has transformed. |
| 618 |
* |
| 619 |
* Should be treated as package scope. |
| 620 |
*/ |
| 621 |
goog.graphics.ext.Element.prototype.parentTransform = function() { |
| 622 |
this.needsTransform_ = this.needsTransform_ || this.parentDependent_; |
| 623 |
}; |
| 624 |
|
| 625 |
|
| 626 |
/** |
| 627 |
* @return {boolean} Whether this element has pending transforms. |
| 628 |
*/ |
| 629 |
goog.graphics.ext.Element.prototype.isPendingTransform = function() { |
| 630 |
return this.needsTransform_; |
| 631 |
}; |
| 632 |
|
| 633 |
|
| 634 |
/** |
| 635 |
* Performs a pending transform. |
| 636 |
* @protected |
| 637 |
*/ |
| 638 |
goog.graphics.ext.Element.prototype.transform = function() { |
| 639 |
if (this.isPendingTransform()) { |
| 640 |
this.needsTransform_ = false; |
| 641 |
|
| 642 |
this.wrapper_.setTransformation( |
| 643 |
this.getLeft(), this.getTop(), this.rotation_, |
| 644 |
(this.getWidth() || 1) / 2, (this.getHeight() || 1) / 2); |
| 645 |
|
| 646 |
// TODO(robbyw): this._fireEvent('transform', [ this ]); |
| 647 |
} |
| 648 |
}; |
| 649 |
|
| 650 |
|
| 651 |
// PIXEL SCALE |
| 652 |
|
| 653 |
|
| 654 |
/** |
| 655 |
* @return {number} Returns the number of pixels per unit in the x direction. |
| 656 |
*/ |
| 657 |
goog.graphics.ext.Element.prototype.getPixelScaleX = function() { |
| 658 |
return this.getGraphics().getPixelScaleX(); |
| 659 |
}; |
| 660 |
|
| 661 |
|
| 662 |
/** |
| 663 |
* @return {number} Returns the number of pixels per unit in the y direction. |
| 664 |
*/ |
| 665 |
goog.graphics.ext.Element.prototype.getPixelScaleY = function() { |
| 666 |
return this.getGraphics().getPixelScaleY(); |
| 667 |
}; |
| 668 |
|
| 669 |
|
| 670 |
// EVENT HANDLING |
| 671 |
|
| 672 |
|
| 673 |
/** @override */ |
| 674 |
goog.graphics.ext.Element.prototype.disposeInternal = function() { |
| 675 |
goog.graphics.ext.Element.superClass_.disposeInternal.call(this); |
| 676 |
this.wrapper_.dispose(); |
| 677 |
}; |
| 678 |
|
| 679 |
|
| 680 |
// INTERNAL POSITION OBJECT |
| 681 |
|
| 682 |
|
| 683 |
/** |
| 684 |
* Position specification types. Start corresponds to left/top, middle to |
| 685 |
* center/middle, and end to right/bottom. |
| 686 |
* @enum {number} |
| 687 |
* @private |
| 688 |
*/ |
| 689 |
goog.graphics.ext.Element.PositionType_ = { |
| 690 |
START: 0, |
| 691 |
MIDDLE: 1, |
| 692 |
END: 2 |
| 693 |
}; |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
/** |
| 698 |
* Manages a position and size, either horizontal or vertical. |
| 699 |
* @param {goog.graphics.ext.Element} element The element the position applies |
| 700 |
* to. |
| 701 |
* @param {boolean} horizontal Whether the position is horizontal or vertical. |
| 702 |
* @constructor |
| 703 |
* @private |
| 704 |
*/ |
| 705 |
goog.graphics.ext.Element.Position_ = function(element, horizontal) { |
| 706 |
this.element_ = element; |
| 707 |
this.horizontal_ = horizontal; |
| 708 |
}; |
| 709 |
|
| 710 |
|
| 711 |
/** |
| 712 |
* @return {!Object} The coordinate value computation cache. |
| 713 |
* @private |
| 714 |
*/ |
| 715 |
goog.graphics.ext.Element.Position_.prototype.getCoordinateCache_ = function() { |
| 716 |
return this.coordinateCache_ || (this.coordinateCache_ = {}); |
| 717 |
}; |
| 718 |
|
| 719 |
|
| 720 |
/** |
| 721 |
* @return {number} The size of the parent's coordinate space. |
| 722 |
* @private |
| 723 |
*/ |
| 724 |
goog.graphics.ext.Element.Position_.prototype.getParentSize_ = function() { |
| 725 |
var parent = this.element_.getParent(); |
| 726 |
return this.horizontal_ ? parent.getCoordinateWidth() : |
| 727 |
parent.getCoordinateHeight(); |
| 728 |
}; |
| 729 |
|
| 730 |
|
| 731 |
/** |
| 732 |
* @return {number} The minimum width/height of the element. |
| 733 |
*/ |
| 734 |
goog.graphics.ext.Element.Position_.prototype.getMinSize = function() { |
| 735 |
return this.getValue_(this.minSize_); |
| 736 |
}; |
| 737 |
|
| 738 |
|
| 739 |
/** |
| 740 |
* Sets the minimum width/height of the element. |
| 741 |
* @param {string|number} minSize The minimum width/height of the element. |
| 742 |
*/ |
| 743 |
goog.graphics.ext.Element.Position_.prototype.setMinSize = function(minSize) { |
| 744 |
this.minSize_ = minSize; |
| 745 |
this.resetCache(); |
| 746 |
}; |
| 747 |
|
| 748 |
|
| 749 |
/** |
| 750 |
* @return {number} The width/height of the element. |
| 751 |
*/ |
| 752 |
goog.graphics.ext.Element.Position_.prototype.getSize = function() { |
| 753 |
return Math.max(this.getValue_(this.size_), this.getMinSize()); |
| 754 |
}; |
| 755 |
|
| 756 |
|
| 757 |
/** |
| 758 |
* Sets the width/height of the element. |
| 759 |
* @param {string|number} size The width/height of the element. |
| 760 |
* @return {boolean} Whether the value was changed. |
| 761 |
*/ |
| 762 |
goog.graphics.ext.Element.Position_.prototype.setSize = function(size) { |
| 763 |
if (size != this.size_) { |
| 764 |
this.size_ = size; |
| 765 |
this.resetCache(); |
| 766 |
return true; |
| 767 |
} |
| 768 |
return false; |
| 769 |
}; |
| 770 |
|
| 771 |
|
| 772 |
/** |
| 773 |
* Converts the given x coordinate to a number value in units. |
| 774 |
* @param {string|number} v The coordinate to retrieve the value for. |
| 775 |
* @param {boolean=} opt_forMaximum Whether we are computing the largest value |
| 776 |
* this coordinate would be in a parent of no size. |
| 777 |
* @return {number} The correct number of coordinate space units. |
| 778 |
* @private |
| 779 |
*/ |
| 780 |
goog.graphics.ext.Element.Position_.prototype.getValue_ = function( |
| 781 |
v, opt_forMaximum) { |
| 782 |
if (!goog.graphics.ext.coordinates.isSpecial(v)) { |
| 783 |
return parseFloat(String(v)); |
| 784 |
} |
| 785 |
|
| 786 |
var cache = this.getCoordinateCache_(); |
| 787 |
var scale = this.horizontal_ ? this.element_.getPixelScaleX() : |
| 788 |
this.element_.getPixelScaleY(); |
| 789 |
|
| 790 |
var containerSize; |
| 791 |
if (opt_forMaximum) { |
| 792 |
containerSize = |
| 793 |
goog.graphics.ext.coordinates.computeValue(this.size_ || 0, 0, scale); |
| 794 |
} else { |
| 795 |
var parent = this.element_.getParent(); |
| 796 |
containerSize = this.horizontal_ ? parent.getWidth() : parent.getHeight(); |
| 797 |
} |
| 798 |
|
| 799 |
return goog.graphics.ext.coordinates.getValue( |
| 800 |
v, opt_forMaximum, containerSize, scale, cache); |
| 801 |
}; |
| 802 |
|
| 803 |
|
| 804 |
/** |
| 805 |
* @return {number} The distance from the left/top edge of this element to the |
| 806 |
* left/top edge of its parent, specified in units of the parent's |
| 807 |
* coordinate system. |
| 808 |
*/ |
| 809 |
goog.graphics.ext.Element.Position_.prototype.getStart = function() { |
| 810 |
if (this.cachedValue_ == null) { |
| 811 |
var value = this.getValue_(this.distance_); |
| 812 |
if (this.distanceType_ == goog.graphics.ext.Element.PositionType_.START) { |
| 813 |
this.cachedValue_ = value; |
| 814 |
} else if ( |
| 815 |
this.distanceType_ == goog.graphics.ext.Element.PositionType_.MIDDLE) { |
| 816 |
this.cachedValue_ = value + (this.getParentSize_() - this.getSize()) / 2; |
| 817 |
} else { |
| 818 |
this.cachedValue_ = this.getParentSize_() - value - this.getSize(); |
| 819 |
} |
| 820 |
} |
| 821 |
|
| 822 |
return this.cachedValue_; |
| 823 |
}; |
| 824 |
|
| 825 |
|
| 826 |
/** |
| 827 |
* @return {number} The middle coordinate of the element, in units of the |
| 828 |
* parent's coordinate system. |
| 829 |
*/ |
| 830 |
goog.graphics.ext.Element.Position_.prototype.getMiddle = function() { |
| 831 |
return this.distanceType_ == goog.graphics.ext.Element.PositionType_.MIDDLE ? |
| 832 |
this.getValue_(this.distance_) : |
| 833 |
(this.getParentSize_() - this.getSize()) / 2 - this.getStart(); |
| 834 |
}; |
| 835 |
|
| 836 |
|
| 837 |
/** |
| 838 |
* @return {number} The end coordinate of the element, in units of the |
| 839 |
* parent's coordinate system. |
| 840 |
*/ |
| 841 |
goog.graphics.ext.Element.Position_.prototype.getEnd = function() { |
| 842 |
return this.distanceType_ == goog.graphics.ext.Element.PositionType_.END ? |
| 843 |
this.getValue_(this.distance_) : |
| 844 |
this.getParentSize_() - this.getStart() - this.getSize(); |
| 845 |
}; |
| 846 |
|
| 847 |
|
| 848 |
/** |
| 849 |
* Sets the position, either as a left/top, center/middle, or right/bottom |
| 850 |
* value. |
| 851 |
* @param {number|string} value The value of the coordinate. |
| 852 |
* @param {goog.graphics.ext.Element.PositionType_} type The type of the |
| 853 |
* coordinate. |
| 854 |
*/ |
| 855 |
goog.graphics.ext.Element.Position_.prototype.setPosition = function( |
| 856 |
value, type) { |
| 857 |
this.distance_ = value; |
| 858 |
this.distanceType_ = type; |
| 859 |
|
| 860 |
// Clear cached value. |
| 861 |
this.cachedValue_ = null; |
| 862 |
}; |
| 863 |
|
| 864 |
|
| 865 |
/** |
| 866 |
* @return {number} An estimate of the maximum x/y extent this element would |
| 867 |
* have in a parent of no width/height. |
| 868 |
*/ |
| 869 |
goog.graphics.ext.Element.Position_.prototype.getMaxPosition = function() { |
| 870 |
// TODO(robbyw): Handle transformed or rotated coordinates |
| 871 |
// TODO(robbyw): Handle pixel based sizes? |
| 872 |
|
| 873 |
return this.getValue_(this.distance_ || 0) + |
| 874 |
(goog.graphics.ext.coordinates.isSpecial(this.size_) ? 0 : |
| 875 |
this.getSize()); |
| 876 |
}; |
| 877 |
|
| 878 |
|
| 879 |
/** |
| 880 |
* Resets the caches of position values and coordinate values. |
| 881 |
*/ |
| 882 |
goog.graphics.ext.Element.Position_.prototype.resetCache = function() { |
| 883 |
this.coordinateCache_ = null; |
| 884 |
this.cachedValue_ = null; |
| 885 |
}; |
| 886 |
|
| 887 |
|
| 888 |
/** |
| 889 |
* @return {boolean} Whether the size or position of this element depends on |
| 890 |
* the size of the parent element. |
| 891 |
*/ |
| 892 |
goog.graphics.ext.Element.Position_.prototype.isParentDependent = function() { |
| 893 |
return this.distanceType_ != goog.graphics.ext.Element.PositionType_.START || |
| 894 |
goog.graphics.ext.coordinates.isSpecial(this.size_) || |
| 895 |
goog.graphics.ext.coordinates.isSpecial(this.minSize_) || |
| 896 |
goog.graphics.ext.coordinates.isSpecial(this.distance_); |
| 897 |
}; |
| 898 |
|
| 899 |
|
| 900 |
/** |
| 901 |
* The lazy loaded distance from the parent's top/left edge to this element's |
| 902 |
* top/left edge expressed in the parent's coordinate system. We cache this |
| 903 |
* because it is most freqeuently requested by the element and it is easy to |
| 904 |
* compute middle and end values from it. |
| 905 |
* @type {?number} |
| 906 |
* @private |
| 907 |
*/ |
| 908 |
goog.graphics.ext.Element.Position_.prototype.cachedValue_ = null; |
| 909 |
|
| 910 |
|
| 911 |
/** |
| 912 |
* A cache of computed x coordinates. |
| 913 |
* @type {Object} |
| 914 |
* @private |
| 915 |
*/ |
| 916 |
goog.graphics.ext.Element.Position_.prototype.coordinateCache_ = null; |
| 917 |
|
| 918 |
|
| 919 |
/** |
| 920 |
* The minimum width/height of this element, as specified by the caller. |
| 921 |
* @type {string|number} |
| 922 |
* @private |
| 923 |
*/ |
| 924 |
goog.graphics.ext.Element.Position_.prototype.minSize_ = 0; |
| 925 |
|
| 926 |
|
| 927 |
/** |
| 928 |
* The width/height of this object, as specified by the caller. |
| 929 |
* @type {string|number} |
| 930 |
* @private |
| 931 |
*/ |
| 932 |
goog.graphics.ext.Element.Position_.prototype.size_ = 0; |
| 933 |
|
| 934 |
|
| 935 |
/** |
| 936 |
* The coordinate of this object, as specified by the caller. The type of |
| 937 |
* coordinate is specified by distanceType_. |
| 938 |
* @type {string|number} |
| 939 |
* @private |
| 940 |
*/ |
| 941 |
goog.graphics.ext.Element.Position_.prototype.distance_ = 0; |
| 942 |
|
| 943 |
|
| 944 |
/** |
| 945 |
* The coordinate type specified by distance_. |
| 946 |
* @type {goog.graphics.ext.Element.PositionType_} |
| 947 |
* @private |
| 948 |
*/ |
| 949 |
goog.graphics.ext.Element.Position_.prototype.distanceType_ = |
| 950 |
goog.graphics.ext.Element.PositionType_.START; |