aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/graphics/ext/group.js
blob: cc65a34e284beacbd323724cdcbb430ebe800d94 (plain)
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
// Copyright 2007 The Closure Library Authors. All Rights Reserved.
//
// 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 A thicker wrapper around graphics groups.
 * @author robbyw@google.com (Robby Walker)
 */


goog.provide('goog.graphics.ext.Group');

goog.require('goog.graphics.ext.Element');



/**
 * Wrapper for a graphics group.
 * @param {goog.graphics.ext.Group} group Parent for this element. Can
 *     be null if this is a Graphics instance.
 * @param {goog.graphics.GroupElement=} opt_wrapper The thin wrapper
 *     to wrap. If omitted, a new group will be created. Must be included
 *     when group is null.
 * @constructor
 * @extends {goog.graphics.ext.Element}
 */
goog.graphics.ext.Group = function(group, opt_wrapper) {
  opt_wrapper = opt_wrapper || group.getGraphicsImplementation().createGroup(
      group.getWrapper());
  goog.graphics.ext.Element.call(this, group, opt_wrapper);

  /**
   * Array of child elements this group contains.
   * @type {Array.<goog.graphics.ext.Element>}
   * @private
   */
  this.children_ = [];
};
goog.inherits(goog.graphics.ext.Group, goog.graphics.ext.Element);


/**
 * Add an element to the group.  This should be treated as package local, as
 * it is called by the draw* methods.
 * @param {!goog.graphics.ext.Element} element The element to add.
 * @param {boolean=} opt_chain Whether this addition is part of a longer set
 *     of element additions.
 */
goog.graphics.ext.Group.prototype.addChild = function(element, opt_chain) {
  if (!goog.array.contains(this.children_, element)) {
    this.children_.push(element);
  }

  var transformed = this.growToFit_(element);

  if (element.isParentDependent()) {
    element.parentTransform();
  }

  if (!opt_chain && element.isPendingTransform()) {
    element.reset();
  }

  if (transformed) {
    this.reset();
  }
};


/**
 * Remove an element from the group.
 * @param {goog.graphics.ext.Element} element The element to remove.
 */
goog.graphics.ext.Group.prototype.removeChild = function(element) {
  goog.array.remove(this.children_, element);

  // TODO(robbyw): shape.fireEvent('delete')

  this.getGraphicsImplementation().removeElement(element.getWrapper());
};


/**
 * Calls the given function on each of this component's children in order.  If
 * {@code opt_obj} is provided, it will be used as the 'this' object in the
 * function when called.  The function should take two arguments:  the child
 * component and its 0-based index.  The return value is ignored.
 * @param {Function} f The function to call for every child component; should
 *    take 2 arguments (the child and its index).
 * @param {Object=} opt_obj Used as the 'this' object in f when called.
 */
goog.graphics.ext.Group.prototype.forEachChild = function(f, opt_obj) {
  if (this.children_) {
    goog.array.forEach(this.children_, f, opt_obj);
  }
};


/**
 * @return {goog.graphics.GroupElement} The underlying thin wrapper.
 * @protected
 */
goog.graphics.ext.Group.prototype.getWrapper;


/**
 * Reset the element.
 * @override
 */
goog.graphics.ext.Group.prototype.reset = function() {
  goog.graphics.ext.Group.superClass_.reset.call(this);

  this.updateChildren();
};


/**
 * Called from the parent class, this method resets any pre-computed positions
 * and sizes.
 * @protected
 * @override
 */
goog.graphics.ext.Group.prototype.redraw = function() {
  this.getWrapper().setSize(this.getWidth(), this.getHeight());
  this.transformChildren();
};


/**
 * Transform the children that need to be transformed.
 * @protected
 */
goog.graphics.ext.Group.prototype.transformChildren = function() {
  this.forEachChild(function(child) {
    if (child.isParentDependent()) {
      child.parentTransform();
    }
  });
};


/**
 * As part of the reset process, update child elements.
 */
goog.graphics.ext.Group.prototype.updateChildren = function() {
  this.forEachChild(function(child) {
    if (child.isParentDependent() || child.isPendingTransform()) {
      child.reset();
    } else if (child.updateChildren) {
      child.updateChildren();
    }
  });
};


/**
 * When adding an element, grow this group's bounds to fit it.
 * @param {!goog.graphics.ext.Element} element The added element.
 * @return {boolean} Whether the size of this group changed.
 * @private
 */
goog.graphics.ext.Group.prototype.growToFit_ = function(element) {
  var transformed = false;

  var x = element.getMaxX();
  if (x > this.getWidth()) {
    this.setMinWidth(x);
    transformed = true;
  }

  var y = element.getMaxY();
  if (y > this.getHeight()) {
    this.setMinHeight(y);
    transformed = true;
  }

  return transformed;
};


/**
 * @return {number} The width of the element's coordinate space.
 */
goog.graphics.ext.Group.prototype.getCoordinateWidth = function() {
  return this.getWidth();
};


/**
 * @return {number} The height of the element's coordinate space.
 */
goog.graphics.ext.Group.prototype.getCoordinateHeight = function() {
  return this.getHeight();
};


/**
 * Remove all drawing elements from the group.
 */
goog.graphics.ext.Group.prototype.clear = function() {
  while (this.children_.length) {
    this.removeChild(this.children_[0]);
  }
};