| 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 |
/** |
| 17 |
* @fileoverview A thicker wrapper around graphics groups. |
| 18 |
* @author [email protected] (Robby Walker) |
| 19 |
*/ |
| 20 |
|
| 21 |
|
| 22 |
goog.provide('goog.graphics.ext.Group'); |
| 23 |
|
| 24 |
goog.require('goog.array'); |
| 25 |
goog.require('goog.graphics.ext.Element'); |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
/** |
| 30 |
* Wrapper for a graphics group. |
| 31 |
* @param {goog.graphics.ext.Group} group Parent for this element. Can |
| 32 |
* be null if this is a Graphics instance. |
| 33 |
* @param {goog.graphics.GroupElement=} opt_wrapper The thin wrapper |
| 34 |
* to wrap. If omitted, a new group will be created. Must be included |
| 35 |
* when group is null. |
| 36 |
* @constructor |
| 37 |
* @extends {goog.graphics.ext.Element} |
| 38 |
*/ |
| 39 |
goog.graphics.ext.Group = function(group, opt_wrapper) { |
| 40 |
opt_wrapper = opt_wrapper || |
| 41 |
group.getGraphicsImplementation().createGroup(group.getWrapper()); |
| 42 |
goog.graphics.ext.Element.call(this, group, opt_wrapper); |
| 43 |
|
| 44 |
/** |
| 45 |
* Array of child elements this group contains. |
| 46 |
* @type {Array<goog.graphics.ext.Element>} |
| 47 |
* @private |
| 48 |
*/ |
| 49 |
this.children_ = []; |
| 50 |
}; |
| 51 |
goog.inherits(goog.graphics.ext.Group, goog.graphics.ext.Element); |
| 52 |
|
| 53 |
|
| 54 |
/** |
| 55 |
* Add an element to the group. This should be treated as package local, as |
| 56 |
* it is called by the draw* methods. |
| 57 |
* @param {!goog.graphics.ext.Element} element The element to add. |
| 58 |
* @param {boolean=} opt_chain Whether this addition is part of a longer set |
| 59 |
* of element additions. |
| 60 |
*/ |
| 61 |
goog.graphics.ext.Group.prototype.addChild = function(element, opt_chain) { |
| 62 |
if (!goog.array.contains(this.children_, element)) { |
| 63 |
this.children_.push(element); |
| 64 |
} |
| 65 |
|
| 66 |
var transformed = this.growToFit_(element); |
| 67 |
|
| 68 |
if (element.isParentDependent()) { |
| 69 |
element.parentTransform(); |
| 70 |
} |
| 71 |
|
| 72 |
if (!opt_chain && element.isPendingTransform()) { |
| 73 |
element.reset(); |
| 74 |
} |
| 75 |
|
| 76 |
if (transformed) { |
| 77 |
this.reset(); |
| 78 |
} |
| 79 |
}; |
| 80 |
|
| 81 |
|
| 82 |
/** |
| 83 |
* Remove an element from the group. |
| 84 |
* @param {goog.graphics.ext.Element} element The element to remove. |
| 85 |
*/ |
| 86 |
goog.graphics.ext.Group.prototype.removeChild = function(element) { |
| 87 |
goog.array.remove(this.children_, element); |
| 88 |
|
| 89 |
// TODO(robbyw): shape.fireEvent('delete') |
| 90 |
|
| 91 |
this.getGraphicsImplementation().removeElement(element.getWrapper()); |
| 92 |
}; |
| 93 |
|
| 94 |
|
| 95 |
/** |
| 96 |
* Calls the given function on each of this component's children in order. If |
| 97 |
* {@code opt_obj} is provided, it will be used as the 'this' object in the |
| 98 |
* function when called. The function should take two arguments: the child |
| 99 |
* component and its 0-based index. The return value is ignored. |
| 100 |
* @param {Function} f The function to call for every child component; should |
| 101 |
* take 2 arguments (the child and its index). |
| 102 |
* @param {Object=} opt_obj Used as the 'this' object in f when called. |
| 103 |
*/ |
| 104 |
goog.graphics.ext.Group.prototype.forEachChild = function(f, opt_obj) { |
| 105 |
if (this.children_) { |
| 106 |
goog.array.forEach(this.children_, f, opt_obj); |
| 107 |
} |
| 108 |
}; |
| 109 |
|
| 110 |
|
| 111 |
/** |
| 112 |
* @return {goog.graphics.GroupElement} The underlying thin wrapper. |
| 113 |
* @override |
| 114 |
*/ |
| 115 |
goog.graphics.ext.Group.prototype.getWrapper; |
| 116 |
|
| 117 |
|
| 118 |
/** |
| 119 |
* Reset the element. |
| 120 |
* @override |
| 121 |
*/ |
| 122 |
goog.graphics.ext.Group.prototype.reset = function() { |
| 123 |
goog.graphics.ext.Group.superClass_.reset.call(this); |
| 124 |
|
| 125 |
this.updateChildren(); |
| 126 |
}; |
| 127 |
|
| 128 |
|
| 129 |
/** |
| 130 |
* Called from the parent class, this method resets any pre-computed positions |
| 131 |
* and sizes. |
| 132 |
* @protected |
| 133 |
* @override |
| 134 |
*/ |
| 135 |
goog.graphics.ext.Group.prototype.redraw = function() { |
| 136 |
this.getWrapper().setSize(this.getWidth(), this.getHeight()); |
| 137 |
this.transformChildren(); |
| 138 |
}; |
| 139 |
|
| 140 |
|
| 141 |
/** |
| 142 |
* Transform the children that need to be transformed. |
| 143 |
* @protected |
| 144 |
*/ |
| 145 |
goog.graphics.ext.Group.prototype.transformChildren = function() { |
| 146 |
this.forEachChild(function(child) { |
| 147 |
if (child.isParentDependent()) { |
| 148 |
child.parentTransform(); |
| 149 |
} |
| 150 |
}); |
| 151 |
}; |
| 152 |
|
| 153 |
|
| 154 |
/** |
| 155 |
* As part of the reset process, update child elements. |
| 156 |
*/ |
| 157 |
goog.graphics.ext.Group.prototype.updateChildren = function() { |
| 158 |
this.forEachChild(function(child) { |
| 159 |
if (child.isParentDependent() || child.isPendingTransform()) { |
| 160 |
child.reset(); |
| 161 |
} else if (child.updateChildren) { |
| 162 |
child.updateChildren(); |
| 163 |
} |
| 164 |
}); |
| 165 |
}; |
| 166 |
|
| 167 |
|
| 168 |
/** |
| 169 |
* When adding an element, grow this group's bounds to fit it. |
| 170 |
* @param {!goog.graphics.ext.Element} element The added element. |
| 171 |
* @return {boolean} Whether the size of this group changed. |
| 172 |
* @private |
| 173 |
*/ |
| 174 |
goog.graphics.ext.Group.prototype.growToFit_ = function(element) { |
| 175 |
var transformed = false; |
| 176 |
|
| 177 |
var x = element.getMaxX(); |
| 178 |
if (x > this.getWidth()) { |
| 179 |
this.setMinWidth(x); |
| 180 |
transformed = true; |
| 181 |
} |
| 182 |
|
| 183 |
var y = element.getMaxY(); |
| 184 |
if (y > this.getHeight()) { |
| 185 |
this.setMinHeight(y); |
| 186 |
transformed = true; |
| 187 |
} |
| 188 |
|
| 189 |
return transformed; |
| 190 |
}; |
| 191 |
|
| 192 |
|
| 193 |
/** |
| 194 |
* @return {number} The width of the element's coordinate space. |
| 195 |
*/ |
| 196 |
goog.graphics.ext.Group.prototype.getCoordinateWidth = function() { |
| 197 |
return this.getWidth(); |
| 198 |
}; |
| 199 |
|
| 200 |
|
| 201 |
/** |
| 202 |
* @return {number} The height of the element's coordinate space. |
| 203 |
*/ |
| 204 |
goog.graphics.ext.Group.prototype.getCoordinateHeight = function() { |
| 205 |
return this.getHeight(); |
| 206 |
}; |
| 207 |
|
| 208 |
|
| 209 |
/** |
| 210 |
* Remove all drawing elements from the group. |
| 211 |
*/ |
| 212 |
goog.graphics.ext.Group.prototype.clear = function() { |
| 213 |
while (this.children_.length) { |
| 214 |
this.removeChild(this.children_[0]); |
| 215 |
} |
| 216 |
}; |