aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/graphics/ext/element.js
blob: 21ded29cd2d1dd3e44f09af702ad5f3a1b85881d (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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
// 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 the DOM element returned from
 * the different draw methods of the graphics implementation, and
 * all interfaces that the various element types support.
 * @author robbyw@google.com (Robby Walker)
 */

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

goog.require('goog.events');
goog.require('goog.events.EventTarget');
goog.require('goog.functions');
goog.require('goog.graphics');
goog.require('goog.graphics.ext.coordinates');



/**
 * Base class for a wrapper around the goog.graphics wrapper that enables
 * more advanced functionality.
 * @param {goog.graphics.ext.Group?} group Parent for this element.
 * @param {goog.graphics.Element} wrapper The thin wrapper to wrap.
 * @constructor
 * @extends {goog.events.EventTarget}
 */
goog.graphics.ext.Element = function(group, wrapper) {
  goog.events.EventTarget.call(this);
  this.wrapper_ = wrapper;
  this.graphics_ = group ? group.getGraphics() : this;

  this.xPosition_ = new goog.graphics.ext.Element.Position_(this, true);
  this.yPosition_ = new goog.graphics.ext.Element.Position_(this, false);

  // Handle parent / child relationships.
  if (group) {
    this.parent_ = group;
    this.parent_.addChild(this);
  }
};
goog.inherits(goog.graphics.ext.Element, goog.events.EventTarget);


/**
 * The graphics object that contains this element.
 * @type {goog.graphics.ext.Graphics|goog.graphics.ext.Element}
 * @private
 */
goog.graphics.ext.Element.prototype.graphics_;


/**
 * The goog.graphics wrapper this class wraps.
 * @type {goog.graphics.Element}
 * @private
 */
goog.graphics.ext.Element.prototype.wrapper_;


/**
 * The group or surface containing this element.
 * @type {goog.graphics.ext.Group|undefined}
 * @private
 */
goog.graphics.ext.Element.prototype.parent_;


/**
 * Whether or not computation of this element's position or size depends on its
 * parent's size.
 * @type {boolean}
 * @private
 */
goog.graphics.ext.Element.prototype.parentDependent_ = false;


/**
 * Whether the element has pending transformations.
 * @type {boolean}
 * @private
 */
goog.graphics.ext.Element.prototype.needsTransform_ = false;


/**
 * The current angle of rotation, expressed in degrees.
 * @type {number}
 * @private
 */
goog.graphics.ext.Element.prototype.rotation_ = 0;


/**
 * Object representing the x position and size of the element.
 * @type {goog.graphics.ext.Element.Position_}
 * @private
 */
goog.graphics.ext.Element.prototype.xPosition_;


/**
 * Object representing the y position and size of the element.
 * @type {goog.graphics.ext.Element.Position_}
 * @private
 */
goog.graphics.ext.Element.prototype.yPosition_;


/**
 * @return {goog.graphics.Element} The underlying thin wrapper.
 * @protected
 */
goog.graphics.ext.Element.prototype.getWrapper = function() {
  return this.wrapper_;
};


/**
 * @return {goog.graphics.ext.Element|goog.graphics.ext.Graphics} The graphics
 *     surface the element is a part of.
 */
goog.graphics.ext.Element.prototype.getGraphics = function() {
  return this.graphics_;
};


/**
 * Returns the graphics implementation.
 * @return {goog.graphics.AbstractGraphics} The underlying graphics
 *     implementation drawing this element's wrapper.
 * @protected
 */
goog.graphics.ext.Element.prototype.getGraphicsImplementation = function() {
  return this.graphics_.getImplementation();
};


/**
 * @return {goog.graphics.ext.Group|undefined} The parent of this element.
 */
goog.graphics.ext.Element.prototype.getParent = function() {
  return this.parent_;
};


// GENERAL POSITIONING


/**
 * Internal convenience method for setting position - either as a left/top,
 * center/middle, or right/bottom value.  Only one should be specified.
 * @param {goog.graphics.ext.Element.Position_} position The position object to
 *     set the value on.
 * @param {number|string} value The value of the coordinate.
 * @param {goog.graphics.ext.Element.PositionType_} type The type of the
 *     coordinate.
 * @param {boolean=} opt_chain Optional flag to specify this function is part
 *     of a chain of calls and therefore transformations should be set as
 *     pending but not yet performed.
 * @private
 */
goog.graphics.ext.Element.prototype.setPosition_ = function(position, value,
    type, opt_chain) {
  position.setPosition(value, type);
  this.computeIsParentDependent_(position);

  this.needsTransform_ = true;
  if (!opt_chain) {
    this.transform();
  }
};


/**
 * Sets the width/height of the element.
 * @param {goog.graphics.ext.Element.Position_} position The position object to
 *     set the value on.
 * @param {string|number} size The new width/height value.
 * @param {boolean=} opt_chain Optional flag to specify this function is part
 *     of a chain of calls and therefore transformations should be set as
 *     pending but not yet performed.
 * @private
 */
goog.graphics.ext.Element.prototype.setSize_ = function(position, size,
    opt_chain) {
  if (position.setSize(size)) {
    this.needsTransform_ = true;

    this.computeIsParentDependent_(position);

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


/**
 * Sets the minimum width/height of the element.
 * @param {goog.graphics.ext.Element.Position_} position The position object to
 *     set the value on.
 * @param {string|number} minSize The minimum width/height of the element.
 * @private
 */
goog.graphics.ext.Element.prototype.setMinSize_ = function(position, minSize) {
  position.setMinSize(minSize);
  this.needsTransform_ = true;
  this.computeIsParentDependent_(position);
};


// HORIZONTAL POSITIONING


/**
 * @return {number} The distance from the left edge of this element to the left
 *     edge of its parent, specified in units of the parent's coordinate system.
 */
goog.graphics.ext.Element.prototype.getLeft = function() {
  return this.xPosition_.getStart();
};


/**
 * Sets the left coordinate of the element.  Overwrites any previous value of
 * left, center, or right for this element.
 * @param {string|number} left The left coordinate.
 * @param {boolean=} opt_chain Optional flag to specify this function is part
 *     of a chain of calls and therefore transformations should be set as
 *     pending but not yet performed.
 */
goog.graphics.ext.Element.prototype.setLeft = function(left, opt_chain) {
  this.setPosition_(this.xPosition_,
      left,
      goog.graphics.ext.Element.PositionType_.START,
      opt_chain);
};


/**
 * @return {number} The right coordinate of the element, in units of the
 *     parent's coordinate system.
 */
goog.graphics.ext.Element.prototype.getRight = function() {
  return this.xPosition_.getEnd();
};


/**
 * Sets the right coordinate of the element.  Overwrites any previous value of
 * left, center, or right for this element.
 * @param {string|number} right The right coordinate.
 * @param {boolean=} opt_chain Optional flag to specify this function is part
 *     of a chain of calls and therefore transformations should be set as
 *     pending but not yet performed.
 */
goog.graphics.ext.Element.prototype.setRight = function(right, opt_chain) {
  this.setPosition_(this.xPosition_,
      right,
      goog.graphics.ext.Element.PositionType_.END,
      opt_chain);
};


/**
 * @return {number} The center coordinate of the element, in units of the
 * parent's coordinate system.
 */
goog.graphics.ext.Element.prototype.getCenter = function() {
  return this.xPosition_.getMiddle();
};


/**
 * Sets the center coordinate of the element.  Overwrites any previous value of
 * left, center, or right for this element.
 * @param {string|number} center The center coordinate.
 * @param {boolean=} opt_chain Optional flag to specify this function is part
 *     of a chain of calls and therefore transformations should be set as
 *     pending but not yet performed.
 */
goog.graphics.ext.Element.prototype.setCenter = function(center, opt_chain) {
  this.setPosition_(this.xPosition_,
      center,
      goog.graphics.ext.Element.PositionType_.MIDDLE,
      opt_chain);
};


// VERTICAL POSITIONING


/**
 * @return {number} The distance from the top edge of this element to the top
 *     edge of its parent, specified in units of the parent's coordinate system.
 */
goog.graphics.ext.Element.prototype.getTop = function() {
  return this.yPosition_.getStart();
};


/**
 * Sets the top coordinate of the element.  Overwrites any previous value of
 * top, middle, or bottom for this element.
 * @param {string|number} top The top coordinate.
 * @param {boolean=} opt_chain Optional flag to specify this function is part
 *     of a chain of calls and therefore transformations should be set as
 *     pending but not yet performed.
 */
goog.graphics.ext.Element.prototype.setTop = function(top, opt_chain) {
  this.setPosition_(this.yPosition_,
      top,
      goog.graphics.ext.Element.PositionType_.START,
      opt_chain);
};


/**
 * @return {number} The bottom coordinate of the element, in units of the
 *     parent's coordinate system.
 */
goog.graphics.ext.Element.prototype.getBottom = function() {
  return this.yPosition_.getEnd();
};


/**
 * Sets the bottom coordinate of the element.  Overwrites any previous value of
 * top, middle, or bottom for this element.
 * @param {string|number} bottom The bottom coordinate.
 * @param {boolean=} opt_chain Optional flag to specify this function is part
 *     of a chain of calls and therefore transformations should be set as
 *     pending but not yet performed.
 */
goog.graphics.ext.Element.prototype.setBottom = function(bottom, opt_chain) {
  this.setPosition_(this.yPosition_,
      bottom,
      goog.graphics.ext.Element.PositionType_.END,
      opt_chain);
};


/**
 * @return {number} The middle coordinate of the element, in units of the
 *     parent's coordinate system.
 */
goog.graphics.ext.Element.prototype.getMiddle = function() {
  return this.yPosition_.getMiddle();
};


/**
 * Sets the middle coordinate of the element.  Overwrites any previous value of
 * top, middle, or bottom for this element
 * @param {string|number} middle The middle coordinate.
 * @param {boolean=} opt_chain Optional flag to specify this function is part
 *     of a chain of calls and therefore transformations should be set as
 *     pending but not yet performed.
 */
goog.graphics.ext.Element.prototype.setMiddle = function(middle, opt_chain) {
  this.setPosition_(this.yPosition_,
      middle,
      goog.graphics.ext.Element.PositionType_.MIDDLE,
      opt_chain);
};


// DIMENSIONS


/**
 * @return {number} The width of the element, in units of the parent's
 *     coordinate system.
 */
goog.graphics.ext.Element.prototype.getWidth = function() {
  return this.xPosition_.getSize();
};


/**
 * Sets the width of the element.
 * @param {string|number} width The new width value.
 * @param {boolean=} opt_chain Optional flag to specify this function is part
 *     of a chain of calls and therefore transformations should be set as
 *     pending but not yet performed.
 */
goog.graphics.ext.Element.prototype.setWidth = function(width, opt_chain) {
  this.setSize_(this.xPosition_, width, opt_chain);
};


/**
 * @return {number} The minimum width of the element, in units of the parent's
 *     coordinate system.
 */
goog.graphics.ext.Element.prototype.getMinWidth = function() {
  return this.xPosition_.getMinSize();
};


/**
 * Sets the minimum width of the element.
 * @param {string|number} minWidth The minimum width of the element.
 */
goog.graphics.ext.Element.prototype.setMinWidth = function(minWidth) {
  this.setMinSize_(this.xPosition_, minWidth);
};


/**
 * @return {number} The height of the element, in units of the parent's
 *     coordinate system.
 */
goog.graphics.ext.Element.prototype.getHeight = function() {
  return this.yPosition_.getSize();
};


/**
 * Sets the height of the element.
 * @param {string|number} height The new height value.
 * @param {boolean=} opt_chain Optional flag to specify this function is part
 *     of a chain of calls and therefore transformations should be set as
 *     pending but not yet performed.
 */
goog.graphics.ext.Element.prototype.setHeight = function(height, opt_chain) {
  this.setSize_(this.yPosition_, height, opt_chain);
};


/**
 * @return {number} The minimum height of the element, in units of the parent's
 *     coordinate system.
 */
goog.graphics.ext.Element.prototype.getMinHeight = function() {
  return this.yPosition_.getMinSize();
};


/**
 * Sets the minimum height of the element.
 * @param {string|number} minHeight The minimum height of the element.
 */
goog.graphics.ext.Element.prototype.setMinHeight = function(minHeight) {
  this.setMinSize_(this.yPosition_, minHeight);
};


// BOUNDS SHORTCUTS


/**
 * Shortcut for setting the left and top position.
 * @param {string|number} left The left coordinate.
 * @param {string|number} top The top coordinate.
 * @param {boolean=} opt_chain Optional flag to specify this function is part
 *     of a chain of calls and therefore transformations should be set as
 *     pending but not yet performed.
 */
goog.graphics.ext.Element.prototype.setPosition = function(left, top,
                                                           opt_chain) {
  this.setLeft(left, true);
  this.setTop(top, opt_chain);
};


/**
 * Shortcut for setting the width and height.
 * @param {string|number} width The new width value.
 * @param {string|number} height The new height value.
 * @param {boolean=} opt_chain Optional flag to specify this function is part
 *     of a chain of calls and therefore transformations should be set as
 *     pending but not yet performed.
 */
goog.graphics.ext.Element.prototype.setSize = function(width, height,
                                                       opt_chain) {
  this.setWidth(width, true);
  this.setHeight(height, opt_chain);
};


/**
 * Shortcut for setting the left, top, width, and height.
 * @param {string|number} left The left coordinate.
 * @param {string|number} top The top coordinate.
 * @param {string|number} width The new width value.
 * @param {string|number} height The new height value.
 * @param {boolean=} opt_chain Optional flag to specify this function is part
 *     of a chain of calls and therefore transformations should be set as
 *     pending but not yet performed.
 */
goog.graphics.ext.Element.prototype.setBounds = function(left, top, width,
                                                         height, opt_chain) {
  this.setLeft(left, true);
  this.setTop(top, true);
  this.setWidth(width, true);
  this.setHeight(height, opt_chain);
};


// MAXIMUM BOUNDS


/**
 * @return {number} An estimate of the maximum x extent this element would have
 *     in a parent of no width.
 */
goog.graphics.ext.Element.prototype.getMaxX = function() {
  return this.xPosition_.getMaxPosition();
};


/**
 * @return {number} An estimate of the maximum y extent this element would have
 *     in a parent of no height.
 */
goog.graphics.ext.Element.prototype.getMaxY = function() {
  return this.yPosition_.getMaxPosition();
};


// RESET


/**
 * Reset the element.  This is called when the element changes size, or when
 * the coordinate system changes in a way that would affect pixel based
 * rendering
 */
goog.graphics.ext.Element.prototype.reset = function() {
  this.xPosition_.resetCache();
  this.yPosition_.resetCache();

  this.redraw();

  this.needsTransform_ = true;
  this.transform();
};


/**
 * Overridable function for subclass specific reset.
 * @protected
 */
goog.graphics.ext.Element.prototype.redraw = goog.nullFunction;


// PARENT DEPENDENCY


/**
 * Computes whether the element is still parent dependent.
 * @param {goog.graphics.ext.Element.Position_} position The recently changed
 *     position object.
 * @private
 */
goog.graphics.ext.Element.prototype.computeIsParentDependent_ = function(
    position) {
  this.parentDependent_ = position.isParentDependent() ||
      this.xPosition_.isParentDependent() ||
      this.yPosition_.isParentDependent() ||
      this.checkParentDependent();
};


/**
 * Returns whether this element's bounds depend on its parents.
 *
 * This function should be treated as if it has package scope.
 * @return {boolean} Whether this element's bounds depend on its parents.
 */
goog.graphics.ext.Element.prototype.isParentDependent = function() {
  return this.parentDependent_;
};


/**
 * Overridable function for subclass specific parent dependency.
 * @return {boolean} Whether this shape's bounds depends on its parent's.
 * @protected
 */
goog.graphics.ext.Element.prototype.checkParentDependent =
    goog.functions.FALSE;


// ROTATION


/**
 * Set the rotation of this element.
 * @param {number} angle The angle of rotation, in degrees.
 */
goog.graphics.ext.Element.prototype.setRotation = function(angle) {
  if (this.rotation_ != angle) {
    this.rotation_ = angle;

    this.needsTransform_ = true;
    this.transform();
  }
};


/**
 * @return {number} The angle of rotation of this element, in degrees.
 */
goog.graphics.ext.Element.prototype.getRotation = function() {
  return this.rotation_;
};


// TRANSFORMS


/**
 * Called by the parent when the parent has transformed.
 *
 * Should be treated as package scope.
 */
goog.graphics.ext.Element.prototype.parentTransform = function() {
  this.needsTransform_ = this.needsTransform_ || this.parentDependent_;
};


/**
 * @return {boolean} Whether this element has pending transforms.
 */
goog.graphics.ext.Element.prototype.isPendingTransform = function() {
  return this.needsTransform_;
};


/**
 * Performs a pending transform.
 * @protected
 */
goog.graphics.ext.Element.prototype.transform = function() {
  if (this.isPendingTransform()) {
    this.needsTransform_ = false;

    this.wrapper_.setTransformation(
        this.getLeft(),
        this.getTop(),
        this.rotation_,
        (this.getWidth() || 1) / 2,
        (this.getHeight() || 1) / 2);

    // TODO(robbyw): this._fireEvent('transform', [ this ]);
  }
};


// PIXEL SCALE


/**
 * @return {number} Returns the number of pixels per unit in the x direction.
 */
goog.graphics.ext.Element.prototype.getPixelScaleX = function() {
  return this.getGraphics().getPixelScaleX();
};


/**
 * @return {number} Returns the number of pixels per unit in the y direction.
 */
goog.graphics.ext.Element.prototype.getPixelScaleY = function() {
  return this.getGraphics().getPixelScaleY();
};


// EVENT HANDLING


/** @override */
goog.graphics.ext.Element.prototype.disposeInternal = function() {
  goog.graphics.ext.Element.superClass_.disposeInternal.call();
  this.wrapper_.dispose();
};


// INTERNAL POSITION OBJECT


/**
 * Position specification types.  Start corresponds to left/top, middle to
 * center/middle, and end to right/bottom.
 * @enum {number}
 * @private
 */
goog.graphics.ext.Element.PositionType_ = {
  START: 0,
  MIDDLE: 1,
  END: 2
};



/**
 * Manages a position and size, either horizontal or vertical.
 * @param {goog.graphics.ext.Element} element The element the position applies
 *     to.
 * @param {boolean} horizontal Whether the position is horizontal or vertical.
 * @constructor
 * @private
 */
goog.graphics.ext.Element.Position_ = function(element, horizontal) {
  this.element_ = element;
  this.horizontal_ = horizontal;
};


/**
 * @return {Object} The coordinate value computation cache.
 * @private
 */
goog.graphics.ext.Element.Position_.prototype.getCoordinateCache_ = function() {
  return this.coordinateCache_ || (this.coordinateCache_ = {});
};


/**
 * @return {number} The size of the parent's coordinate space.
 * @private
 */
goog.graphics.ext.Element.Position_.prototype.getParentSize_ = function() {
  var parent = this.element_.getParent();
  return this.horizontal_ ?
      parent.getCoordinateWidth() :
      parent.getCoordinateHeight();
};


/**
 * @return {number} The minimum width/height of the element.
 */
goog.graphics.ext.Element.Position_.prototype.getMinSize = function() {
  return this.getValue_(this.minSize_);
};


/**
 * Sets the minimum width/height of the element.
 * @param {string|number} minSize The minimum width/height of the element.
 */
goog.graphics.ext.Element.Position_.prototype.setMinSize = function(minSize) {
  this.minSize_ = minSize;
  this.resetCache();
};


/**
 * @return {number} The width/height of the element.
 */
goog.graphics.ext.Element.Position_.prototype.getSize = function() {
  return Math.max(this.getValue_(this.size_), this.getMinSize());
};


/**
 * Sets the width/height of the element.
 * @param {string|number} size The width/height of the element.
 * @return {boolean} Whether the value was changed.
 */
goog.graphics.ext.Element.Position_.prototype.setSize = function(size) {
  if (size != this.size_) {
    this.size_ = size;
    this.resetCache();
    return true;
  }
  return false;
};


/**
 * Converts the given x coordinate to a number value in units.
 * @param {string|number} v The coordinate to retrieve the value for.
 * @param {boolean=} opt_forMaximum Whether we are computing the largest value
 *     this coordinate would be in a parent of no size.
 * @return {number} The correct number of coordinate space units.
 * @private
 */
goog.graphics.ext.Element.Position_.prototype.getValue_ = function(v,
    opt_forMaximum) {
  if (!goog.graphics.ext.coordinates.isSpecial(v)) {
    return parseFloat(String(v));
  }

  var cache = this.getCoordinateCache_();
  var scale = this.horizontal_ ?
      this.element_.getPixelScaleX() :
      this.element_.getPixelScaleY();

  var containerSize;
  if (opt_forMaximum) {
    containerSize = goog.graphics.ext.coordinates.computeValue(
        this.size_ || 0, 0, scale);
  } else {
    var parent = this.element_.getParent();
    containerSize = this.horizontal_ ? parent.getWidth() : parent.getHeight();
  }

  return goog.graphics.ext.coordinates.getValue(v, opt_forMaximum,
      containerSize, scale, cache);
};


/**
 * @return {number} The distance from the left/top edge of this element to the
 *     left/top edge of its parent, specified in units of the parent's
 *     coordinate system.
 */
goog.graphics.ext.Element.Position_.prototype.getStart = function() {
  if (this.cachedValue_ == null) {
    var value = this.getValue_(this.distance_);
    if (this.distanceType_ == goog.graphics.ext.Element.PositionType_.START) {
      this.cachedValue_ = value;
    } else if (this.distanceType_ ==
               goog.graphics.ext.Element.PositionType_.MIDDLE) {
      this.cachedValue_ = value + (this.getParentSize_() - this.getSize()) / 2;
    } else {
      this.cachedValue_ = this.getParentSize_() - value - this.getSize();
    }
  }

  return this.cachedValue_;
};


/**
 * @return {number} The middle coordinate of the element, in units of the
 *     parent's coordinate system.
 */
goog.graphics.ext.Element.Position_.prototype.getMiddle = function() {
  return this.distanceType_ == goog.graphics.ext.Element.PositionType_.MIDDLE ?
      this.getValue_(this.distance_) :
      (this.getParentSize_() - this.getSize()) / 2 - this.getStart();
};


/**
 * @return {number} The end coordinate of the element, in units of the
 *     parent's coordinate system.
 */
goog.graphics.ext.Element.Position_.prototype.getEnd = function() {
  return this.distanceType_ == goog.graphics.ext.Element.PositionType_.END ?
      this.getValue_(this.distance_) :
      this.getParentSize_() - this.getStart() - this.getSize();
};


/**
 * Sets the position, either as a left/top, center/middle, or right/bottom
 * value.
 * @param {number|string} value The value of the coordinate.
 * @param {goog.graphics.ext.Element.PositionType_} type The type of the
 *     coordinate.
 */
goog.graphics.ext.Element.Position_.prototype.setPosition = function(value,
    type) {
  this.distance_ = value;
  this.distanceType_ = type;

  // Clear cached value.
  this.cachedValue_ = null;
};


/**
 * @return {number} An estimate of the maximum x/y extent this element would
 *     have in a parent of no width/height.
 */
goog.graphics.ext.Element.Position_.prototype.getMaxPosition = function() {
  // TODO(robbyw): Handle transformed or rotated coordinates
  // TODO(robbyw): Handle pixel based sizes?

  return this.getValue_(this.distance_ || 0) + (
      goog.graphics.ext.coordinates.isSpecial(this.size_) ? 0 : this.getSize());
};


/**
 * Resets the caches of position values and coordinate values.
 */
goog.graphics.ext.Element.Position_.prototype.resetCache = function() {
  this.coordinateCache_ = null;
  this.cachedValue_ = null;
};


/**
 * @return {boolean} Whether the size or position of this element depends on
 *     the size of the parent element.
 */
goog.graphics.ext.Element.Position_.prototype.isParentDependent = function() {
  return this.distanceType_ != goog.graphics.ext.Element.PositionType_.START ||
      goog.graphics.ext.coordinates.isSpecial(this.size_) ||
      goog.graphics.ext.coordinates.isSpecial(this.minSize_) ||
      goog.graphics.ext.coordinates.isSpecial(this.distance_);
};


/**
 * The lazy loaded distance from the parent's top/left edge to this element's
 * top/left edge expressed in the parent's coordinate system.  We cache this
 * because it is most freqeuently requested by the element and it is easy to
 * compute middle and end values from it.
 * @type {?number}
 * @private
 */
goog.graphics.ext.Element.Position_.prototype.cachedValue_ = null;


/**
 * A cache of computed x coordinates.
 * @type {Object}
 * @private
 */
goog.graphics.ext.Element.Position_.prototype.coordinateCache_ = null;


/**
 * The minimum width/height of this element, as specified by the caller.
 * @type {string|number}
 * @private
 */
goog.graphics.ext.Element.Position_.prototype.minSize_ = 0;


/**
 * The width/height of this object, as specified by the caller.
 * @type {string|number}
 * @private
 */
goog.graphics.ext.Element.Position_.prototype.size_ = 0;


/**
 * The coordinate of this object, as specified by the caller.  The type of
 * coordinate is specified by distanceType_.
 * @type {string|number}
 * @private
 */
goog.graphics.ext.Element.Position_.prototype.distance_ = 0;


/**
 * The coordinate type specified by distance_.
 * @type {goog.graphics.ext.Element.PositionType_}
 * @private
 */
goog.graphics.ext.Element.Position_.prototype.distanceType_ =
    goog.graphics.ext.Element.PositionType_.START;