Debian Codegraph @

closure-library/closure/goog/ui/splitpane.js

raw
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 Class for splitting two areas with draggable control for
17 * changing size.
18 *
19 * The DOM that is created (or that can be decorated) looks like this:
20 * <div class='goog-splitpane'>
21 * <div class='goog-splitpane-first-container'></div>
22 * <div class='goog-splitpane-second-container'></div>
23 * <div class='goog-splitpane-handle'></div>
24 * </div>
25 *
26 * The content to be split goes in the first and second DIVs, the third one
27 * is for managing (and styling) the splitter handle.
28 *
29 * @see ../demos/splitpane.html
30 */
31
32
33 goog.provide('goog.ui.SplitPane');
34 goog.provide('goog.ui.SplitPane.Orientation');
35
36 goog.require('goog.asserts');
37 goog.require('goog.dom');
38 goog.require('goog.dom.TagName');
39 goog.require('goog.dom.classlist');
40 goog.require('goog.events.EventType');
41 goog.require('goog.fx.Dragger');
42 goog.require('goog.math.Rect');
43 goog.require('goog.math.Size');
44 goog.require('goog.style');
45 goog.require('goog.ui.Component');
46 goog.require('goog.userAgent');
47
48
49
50 /**
51 * A left/right up/down Container SplitPane.
52 * Create SplitPane with two goog.ui.Component opjects to split.
53 * TODO(user): Support minimum splitpane size.
54 * TODO(user): Allow component change/orientation after init.
55 * TODO(user): Support hiding either side of handle (plus handle).
56 * TODO(user): Look at setBorderBoxSize fixes and revist borderwidth code.
57 *
58 * @param {goog.ui.Component} firstComponent Left or Top component.
59 * @param {goog.ui.Component} secondComponent Right or Bottom component.
60 * @param {goog.ui.SplitPane.Orientation} orientation SplitPane orientation.
61 * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper.
62 * @extends {goog.ui.Component}
63 * @constructor
64 */
65 goog.ui.SplitPane = function(
66 firstComponent, secondComponent, orientation, opt_domHelper) {
67 goog.ui.SplitPane.base(this, 'constructor', opt_domHelper);
68
69 /**
70 * The orientation of the containers.
71 * @type {goog.ui.SplitPane.Orientation}
72 * @private
73 */
74 this.orientation_ = orientation;
75
76 /**
77 * The left/top component.
78 * @type {goog.ui.Component}
79 * @private
80 */
81 this.firstComponent_ = firstComponent;
82 this.addChild(firstComponent);
83
84 /**
85 * The right/bottom component.
86 * @type {goog.ui.Component}
87 * @private
88 */
89 this.secondComponent_ = secondComponent;
90 this.addChild(secondComponent);
91
92 /** @private {Element} */
93 this.splitpaneHandle_ = null;
94 };
95 goog.inherits(goog.ui.SplitPane, goog.ui.Component);
96 goog.tagUnsealableClass(goog.ui.SplitPane);
97
98
99 /**
100 * Events.
101 * @enum {string}
102 */
103 goog.ui.SplitPane.EventType = {
104
105 /**
106 * Dispatched after handle drag.
107 */
108 HANDLE_DRAG: 'handle_drag',
109
110 /**
111 * Dispatched after handle drag end.
112 */
113 HANDLE_DRAG_END: 'handle_drag_end',
114
115 /**
116 * Dispatched after handle snap (double-click splitter).
117 */
118 HANDLE_SNAP: 'handle_snap'
119 };
120
121
122 /**
123 * CSS class names for splitpane outer container.
124 * @type {string}
125 * @private
126 */
127 goog.ui.SplitPane.CLASS_NAME_ = goog.getCssName('goog-splitpane');
128
129
130 /**
131 * CSS class name for first splitpane container.
132 * @type {string}
133 * @private
134 */
135 goog.ui.SplitPane.FIRST_CONTAINER_CLASS_NAME_ =
136 goog.getCssName('goog-splitpane-first-container');
137
138
139 /**
140 * CSS class name for second splitpane container.
141 * @type {string}
142 * @private
143 */
144 goog.ui.SplitPane.SECOND_CONTAINER_CLASS_NAME_ =
145 goog.getCssName('goog-splitpane-second-container');
146
147
148 /**
149 * CSS class name for the splitpane handle.
150 * @type {string}
151 * @private
152 */
153 goog.ui.SplitPane.HANDLE_CLASS_NAME_ = goog.getCssName('goog-splitpane-handle');
154
155
156 /**
157 * CSS class name for the splitpane handle in horizontal orientation.
158 * @type {string}
159 * @private
160 */
161 goog.ui.SplitPane.HANDLE_CLASS_NAME_HORIZONTAL_ =
162 goog.getCssName('goog-splitpane-handle-horizontal');
163
164
165 /**
166 * CSS class name for the splitpane handle in horizontal orientation.
167 * @type {string}
168 * @private
169 */
170 goog.ui.SplitPane.HANDLE_CLASS_NAME_VERTICAL_ =
171 goog.getCssName('goog-splitpane-handle-vertical');
172
173
174 /**
175 * The dragger to move the drag handle.
176 * @type {goog.fx.Dragger?}
177 * @private
178 */
179 goog.ui.SplitPane.prototype.splitDragger_ = null;
180
181
182 /**
183 * The left/top component dom container.
184 * @type {Element}
185 * @private
186 */
187 goog.ui.SplitPane.prototype.firstComponentContainer_ = null;
188
189
190 /**
191 * The right/bottom component dom container.
192 * @type {Element}
193 * @private
194 */
195 goog.ui.SplitPane.prototype.secondComponentContainer_ = null;
196
197
198 /**
199 * The size (width or height) of the splitpane handle, default = 5.
200 * @type {number}
201 * @private
202 */
203 goog.ui.SplitPane.prototype.handleSize_ = 5;
204
205
206 /**
207 * The initial size (width or height) of the left or top component.
208 * @type {?number}
209 * @private
210 */
211 goog.ui.SplitPane.prototype.initialSize_ = null;
212
213
214 /**
215 * The saved size (width or height) of the left or top component on a
216 * double-click (snap).
217 * This needs to be saved so it can be restored after another double-click.
218 * @type {?number}
219 * @private
220 */
221 goog.ui.SplitPane.prototype.savedSnapSize_ = null;
222
223
224 /**
225 * The first component size, so we don't change it on a window resize.
226 * @type {?number}
227 * @private
228 */
229 goog.ui.SplitPane.prototype.firstComponentSize_ = null;
230
231
232 /**
233 * If we resize as they user moves the handle (default = true).
234 * @type {boolean}
235 * @private
236 */
237 goog.ui.SplitPane.prototype.continuousResize_ = true;
238
239
240 /**
241 * Iframe overlay to prevent iframes from grabbing events.
242 * @type {Element}
243 * @private
244 */
245 goog.ui.SplitPane.prototype.iframeOverlay_ = null;
246
247
248 /**
249 * Z indices for iframe overlay and splitter handle.
250 * @enum {number}
251 * @private
252 */
253 goog.ui.SplitPane.IframeOverlayIndex_ = {
254 HIDDEN: -1,
255 OVERLAY: 1,
256 SPLITTER_HANDLE: 2
257 };
258
259
260 /**
261 * Orientation values for the splitpane.
262 * @enum {string}
263 */
264 goog.ui.SplitPane.Orientation = {
265
266 /**
267 * Horizontal orientation means splitter moves right-left.
268 */
269 HORIZONTAL: 'horizontal',
270
271 /**
272 * Vertical orientation means splitter moves up-down.
273 */
274 VERTICAL: 'vertical'
275 };
276
277
278 /**
279 * Create the DOM node & text node needed for the splitpane.
280 * @override
281 */
282 goog.ui.SplitPane.prototype.createDom = function() {
283 var dom = this.getDomHelper();
284
285 // Create the components.
286 var firstContainer = dom.createDom(
287 goog.dom.TagName.DIV, goog.ui.SplitPane.FIRST_CONTAINER_CLASS_NAME_);
288 var secondContainer = dom.createDom(
289 goog.dom.TagName.DIV, goog.ui.SplitPane.SECOND_CONTAINER_CLASS_NAME_);
290 var splitterHandle =
291 dom.createDom(goog.dom.TagName.DIV, goog.ui.SplitPane.HANDLE_CLASS_NAME_);
292
293 // Create the primary element, a DIV that holds the two containers and handle.
294 this.setElementInternal(
295 dom.createDom(
296 goog.dom.TagName.DIV, goog.ui.SplitPane.CLASS_NAME_, firstContainer,
297 secondContainer, splitterHandle));
298
299 this.firstComponentContainer_ = firstContainer;
300 this.secondComponentContainer_ = secondContainer;
301 this.splitpaneHandle_ = splitterHandle;
302 this.setUpHandle_();
303
304 this.finishSetup_();
305 };
306
307
308 /**
309 * Determines if a given element can be decorated by this type of component.
310 * @param {Element} element Element to decorate.
311 * @return {boolean} True if the element can be decorated, false otherwise.
312 * @override
313 */
314 goog.ui.SplitPane.prototype.canDecorate = function(element) {
315 var className = goog.ui.SplitPane.FIRST_CONTAINER_CLASS_NAME_;
316 var firstContainer = this.getElementToDecorate_(element, className);
317 if (!firstContainer) {
318 return false;
319 }
320 // Since we have this component, save it so we don't have to get it
321 // again in decorateInternal. Same w/other components.
322 this.firstComponentContainer_ = firstContainer;
323
324 className = goog.ui.SplitPane.SECOND_CONTAINER_CLASS_NAME_;
325 var secondContainer = this.getElementToDecorate_(element, className);
326
327 if (!secondContainer) {
328 return false;
329 }
330 this.secondComponentContainer_ = secondContainer;
331
332 className = goog.ui.SplitPane.HANDLE_CLASS_NAME_;
333 var splitpaneHandle = this.getElementToDecorate_(element, className);
334 if (!splitpaneHandle) {
335 return false;
336 }
337 this.splitpaneHandle_ = splitpaneHandle;
338
339 // We found all the components we're looking for, so return true.
340 return true;
341 };
342
343
344 /**
345 * Obtains the element to be decorated by class name. If multiple such elements
346 * are found, preference is given to those directly attached to the specified
347 * root element.
348 * @param {Element} rootElement The root element from which to retrieve the
349 * element to be decorated.
350 * @param {string} className The target class name.
351 * @return {Element} The element to decorate.
352 * @private
353 */
354 goog.ui.SplitPane.prototype.getElementToDecorate_ = function(
355 rootElement, className) {
356
357 // Decorate the root element's children, if available.
358 var childElements = goog.dom.getChildren(rootElement);
359 for (var i = 0; i < childElements.length; i++) {
360 var childElement = goog.asserts.assertElement(childElements[i]);
361 if (goog.dom.classlist.contains(childElement, className)) {
362 return childElement;
363 }
364 }
365
366 // Default to the first descendent element with the correct class.
367 return goog.dom.getElementsByTagNameAndClass(null, className, rootElement)[0];
368 };
369
370
371 /**
372 * Decorates the given HTML element as a SplitPane. Overrides {@link
373 * goog.ui.Component#decorateInternal}. Considered protected.
374 * @param {Element} element Element (SplitPane div) to decorate.
375 * @protected
376 * @override
377 */
378 goog.ui.SplitPane.prototype.decorateInternal = function(element) {
379 goog.ui.SplitPane.base(this, 'decorateInternal', element);
380
381 this.setUpHandle_();
382
383 var elSize = goog.style.getBorderBoxSize(element);
384 this.setSize(new goog.math.Size(elSize.width, elSize.height));
385
386 this.finishSetup_();
387 };
388
389
390 /**
391 * Parent the passed in components to the split containers. Call their
392 * createDom methods if necessary.
393 * @private
394 */
395 goog.ui.SplitPane.prototype.finishSetup_ = function() {
396 var dom = this.getDomHelper();
397
398 if (!this.firstComponent_.getElement()) {
399 this.firstComponent_.createDom();
400 }
401
402 dom.appendChild(
403 this.firstComponentContainer_, this.firstComponent_.getElement());
404
405 if (!this.secondComponent_.getElement()) {
406 this.secondComponent_.createDom();
407 }
408
409 dom.appendChild(
410 this.secondComponentContainer_, this.secondComponent_.getElement());
411
412 this.splitDragger_ =
413 new goog.fx.Dragger(this.splitpaneHandle_, this.splitpaneHandle_);
414
415 this.firstComponentContainer_.style.position = 'absolute';
416 this.secondComponentContainer_.style.position = 'absolute';
417 var handleStyle = this.splitpaneHandle_.style;
418 handleStyle.position = 'absolute';
419 handleStyle.overflow = 'hidden';
420 handleStyle.zIndex = goog.ui.SplitPane.IframeOverlayIndex_.SPLITTER_HANDLE;
421 };
422
423
424 /**
425 * Setup all events and do an initial resize.
426 * @override
427 */
428 goog.ui.SplitPane.prototype.enterDocument = function() {
429 goog.ui.SplitPane.base(this, 'enterDocument');
430
431 // If position is not set in the inline style of the element, it is not
432 // possible to get the element's real CSS position until the element is in
433 // the document.
434 // When position:relative is set in the CSS and the element is not in the
435 // document, Safari, Chrome, and Opera always return the empty string; while
436 // IE always return "static".
437 // Do the final check to see if element's position is set as "relative",
438 // "absolute" or "fixed".
439 var element = this.getElement();
440 if (goog.style.getComputedPosition(element) == 'static') {
441 element.style.position = 'relative';
442 }
443
444 this.getHandler()
445 .listen(
446 this.splitpaneHandle_, goog.events.EventType.DBLCLICK,
447 this.handleDoubleClick_)
448 .listen(
449 this.splitDragger_, goog.fx.Dragger.EventType.START,
450 this.handleDragStart_)
451 .listen(
452 this.splitDragger_, goog.fx.Dragger.EventType.DRAG, this.handleDrag_)
453 .listen(
454 this.splitDragger_, goog.fx.Dragger.EventType.END,
455 this.handleDragEnd_);
456
457 this.setFirstComponentSize(this.initialSize_);
458 };
459
460
461 /**
462 * Sets the initial size of the left or top component.
463 * @param {number} size The size in Pixels of the container.
464 */
465 goog.ui.SplitPane.prototype.setInitialSize = function(size) {
466 this.initialSize_ = size;
467 };
468
469
470 /**
471 * Sets the SplitPane handle size.
472 * TODO(user): Make sure this works after initialization.
473 * @param {number} size The size of the handle in pixels.
474 */
475 goog.ui.SplitPane.prototype.setHandleSize = function(size) {
476 this.handleSize_ = size;
477 };
478
479
480 /**
481 * Sets whether we resize on handle drag.
482 * @param {boolean} continuous The continuous resize value.
483 */
484 goog.ui.SplitPane.prototype.setContinuousResize = function(continuous) {
485 this.continuousResize_ = continuous;
486 };
487
488
489 /**
490 * Returns whether the orientation for the split pane is vertical
491 * or not.
492 * @return {boolean} True if the orientation is vertical, false otherwise.
493 */
494 goog.ui.SplitPane.prototype.isVertical = function() {
495 return this.orientation_ == goog.ui.SplitPane.Orientation.VERTICAL;
496 };
497
498
499 /**
500 * Initializes the handle by assigning the correct height/width and adding
501 * the correct class as per the orientation.
502 * @private
503 */
504 goog.ui.SplitPane.prototype.setUpHandle_ = function() {
505 if (this.isVertical()) {
506 this.splitpaneHandle_.style.height = this.handleSize_ + 'px';
507 goog.dom.classlist.add(
508 this.splitpaneHandle_, goog.ui.SplitPane.HANDLE_CLASS_NAME_VERTICAL_);
509 } else {
510 this.splitpaneHandle_.style.width = this.handleSize_ + 'px';
511 goog.dom.classlist.add(
512 this.splitpaneHandle_, goog.ui.SplitPane.HANDLE_CLASS_NAME_HORIZONTAL_);
513 }
514 };
515
516
517 /**
518 * Sets the orientation class for the split pane handle.
519 * @protected
520 */
521 goog.ui.SplitPane.prototype.setOrientationClassForHandle = function() {
522 goog.asserts.assert(this.splitpaneHandle_);
523 if (this.isVertical()) {
524 goog.dom.classlist.swap(
525 this.splitpaneHandle_, goog.ui.SplitPane.HANDLE_CLASS_NAME_HORIZONTAL_,
526 goog.ui.SplitPane.HANDLE_CLASS_NAME_VERTICAL_);
527 } else {
528 goog.dom.classlist.swap(
529 this.splitpaneHandle_, goog.ui.SplitPane.HANDLE_CLASS_NAME_VERTICAL_,
530 goog.ui.SplitPane.HANDLE_CLASS_NAME_HORIZONTAL_);
531 }
532 };
533
534
535 /**
536 * Sets the orientation of the split pane.
537 * @param {goog.ui.SplitPane.Orientation} orientation SplitPane orientation.
538 */
539 goog.ui.SplitPane.prototype.setOrientation = function(orientation) {
540 if (this.orientation_ != orientation) {
541 this.orientation_ = orientation;
542 var isVertical = this.isVertical();
543
544 // If the split pane is already in document, then the positions and sizes
545 // need to be adjusted.
546 if (this.isInDocument()) {
547 this.setOrientationClassForHandle();
548 // TODO(user): Should handleSize_ and initialSize_ also be adjusted ?
549 if (goog.isNumber(this.firstComponentSize_)) {
550 var splitpaneSize = goog.style.getBorderBoxSize(this.getElement());
551 var ratio = isVertical ? splitpaneSize.height / splitpaneSize.width :
552 splitpaneSize.width / splitpaneSize.height;
553 // TODO(user): Fix the behaviour for the case when the handle is
554 // placed on either of the edges of the split pane. Also, similar
555 // behaviour is present in {@link #setSize}. Probably need to modify
556 // {@link #setFirstComponentSize}.
557 this.setFirstComponentSize(this.firstComponentSize_ * ratio);
558 } else {
559 this.setFirstComponentSize();
560 }
561 }
562 }
563 };
564
565
566 /**
567 * Gets the orientation of the split pane.
568 * @return {goog.ui.SplitPane.Orientation} The orientation.
569 */
570 goog.ui.SplitPane.prototype.getOrientation = function() {
571 return this.orientation_;
572 };
573
574
575 /**
576 * Move and resize a container. The sizing changes the BorderBoxSize.
577 * @param {Element} element The element to move and size.
578 * @param {goog.math.Rect} rect The top, left, width and height to change to.
579 * @private
580 */
581 goog.ui.SplitPane.prototype.moveAndSize_ = function(element, rect) {
582 goog.style.setPosition(element, rect.left, rect.top);
583 // TODO(user): Add a goog.math.Size.max call for below.
584 goog.style.setBorderBoxSize(
585 element,
586 new goog.math.Size(Math.max(rect.width, 0), Math.max(rect.height, 0)));
587 };
588
589
590 /**
591 * @return {?number} The size of the left/top component.
592 */
593 goog.ui.SplitPane.prototype.getFirstComponentSize = function() {
594 return this.firstComponentSize_;
595 };
596
597
598 /**
599 * Set the size of the left/top component, and resize the other component based
600 * on that size and handle size.
601 * @param {?number=} opt_size The size of the top or left, in pixels. If
602 * unspecified, leaves the size of the first component unchanged but adjusts
603 * the size of the second component to fit the split pane size.
604 */
605 goog.ui.SplitPane.prototype.setFirstComponentSize = function(opt_size) {
606 this.setFirstComponentSize_(
607 goog.style.getBorderBoxSize(this.getElement()), opt_size);
608 };
609
610
611 /**
612 * Set the size of the left/top component, and resize the other component based
613 * on that size and handle size. Unlike the public method, this takes the
614 * current pane size which avoids the expensive getBorderBoxSize() call
615 * when we have the size available.
616 *
617 * @param {!goog.math.Size} splitpaneSize The current size of the splitpane.
618 * @param {?number=} opt_size The size of the top or left, in pixels.
619 * @private
620 */
621 goog.ui.SplitPane.prototype.setFirstComponentSize_ = function(
622 splitpaneSize, opt_size) {
623 var top = 0, left = 0;
624
625 var isVertical = this.isVertical();
626 // Figure out first component size; it's either passed in, taken from the
627 // saved size, or is half of the total size.
628 var firstComponentSize = goog.isNumber(opt_size) ?
629 opt_size :
630 goog.isNumber(this.firstComponentSize_) ?
631 this.firstComponentSize_ :
632 Math.floor((isVertical ? splitpaneSize.height : splitpaneSize.width) / 2);
633 this.firstComponentSize_ = firstComponentSize;
634
635 var firstComponentWidth;
636 var firstComponentHeight;
637 var secondComponentWidth;
638 var secondComponentHeight;
639 var handleWidth;
640 var handleHeight;
641 var secondComponentLeft;
642 var secondComponentTop;
643 var handleLeft;
644 var handleTop;
645
646 if (isVertical) {
647 // Width for the handle and the first and second components will be the
648 // width of the split pane. The height for the first component will be
649 // the calculated first component size. The height for the second component
650 // will be the total height minus the heights of the first component and
651 // the handle.
652 firstComponentHeight = firstComponentSize;
653 firstComponentWidth = splitpaneSize.width;
654 handleWidth = splitpaneSize.width;
655 handleHeight = this.handleSize_;
656 secondComponentHeight =
657 splitpaneSize.height - firstComponentHeight - handleHeight;
658 secondComponentWidth = splitpaneSize.width;
659 handleTop = top + firstComponentHeight;
660 handleLeft = left;
661 secondComponentTop = handleTop + handleHeight;
662 secondComponentLeft = left;
663 } else {
664 // Height for the handle and the first and second components will be the
665 // height of the split pane. The width for the first component will be
666 // the calculated first component size. The width for the second component
667 // will be the total width minus the widths of the first component and
668 // the handle.
669 firstComponentWidth = firstComponentSize;
670 firstComponentHeight = splitpaneSize.height;
671 handleWidth = this.handleSize_;
672 handleHeight = splitpaneSize.height;
673 secondComponentWidth =
674 splitpaneSize.width - firstComponentWidth - handleWidth;
675 secondComponentHeight = splitpaneSize.height;
676 handleLeft = left + firstComponentWidth;
677 handleTop = top;
678 secondComponentLeft = handleLeft + handleWidth;
679 secondComponentTop = top;
680 }
681
682 // Now move and size the containers.
683 this.moveAndSize_(
684 this.firstComponentContainer_,
685 new goog.math.Rect(left, top, firstComponentWidth, firstComponentHeight));
686
687 if (typeof this.firstComponent_.resize == 'function') {
688 this.firstComponent_.resize(
689 new goog.math.Size(firstComponentWidth, firstComponentHeight));
690 }
691
692 this.moveAndSize_(
693 this.splitpaneHandle_,
694 new goog.math.Rect(handleLeft, handleTop, handleWidth, handleHeight));
695
696 this.moveAndSize_(
697 this.secondComponentContainer_,
698 new goog.math.Rect(
699 secondComponentLeft, secondComponentTop, secondComponentWidth,
700 secondComponentHeight));
701
702 if (typeof this.secondComponent_.resize == 'function') {
703 this.secondComponent_.resize(
704 new goog.math.Size(secondComponentWidth, secondComponentHeight));
705 }
706 // Fire a CHANGE event.
707 this.dispatchEvent(goog.ui.Component.EventType.CHANGE);
708 };
709
710
711 /**
712 * Set the size of the splitpane. This is usually called by the controlling
713 * application. This will set the SplitPane BorderBoxSize.
714 * @param {!goog.math.Size} size The size to set the splitpane.
715 * @param {?number=} opt_firstComponentSize The size of the top or left
716 * component, in pixels.
717 */
718 goog.ui.SplitPane.prototype.setSize = function(size, opt_firstComponentSize) {
719 goog.style.setBorderBoxSize(this.getElement(), size);
720 if (this.iframeOverlay_) {
721 goog.style.setBorderBoxSize(this.iframeOverlay_, size);
722 }
723 this.setFirstComponentSize_(size, opt_firstComponentSize);
724 };
725
726
727 /**
728 * Snap the container to the left or top on a Double-click.
729 * @private
730 */
731 goog.ui.SplitPane.prototype.snapIt_ = function() {
732 var handlePos = goog.style.getRelativePosition(
733 this.splitpaneHandle_, this.firstComponentContainer_);
734 var firstBorderBoxSize =
735 goog.style.getBorderBoxSize(this.firstComponentContainer_);
736 var firstContentBoxSize =
737 goog.style.getContentBoxSize(this.firstComponentContainer_);
738
739 var isVertical = this.isVertical();
740
741 // Where do we snap the handle (what size to make the component) and what
742 // is the current handle position.
743 var snapSize;
744 var handlePosition;
745 if (isVertical) {
746 snapSize = firstBorderBoxSize.height - firstContentBoxSize.height;
747 handlePosition = handlePos.y;
748 } else {
749 snapSize = firstBorderBoxSize.width - firstContentBoxSize.width;
750 handlePosition = handlePos.x;
751 }
752
753 if (snapSize == handlePosition) {
754 // This means we're 'unsnapping', set it back to where it was.
755 this.setFirstComponentSize(this.savedSnapSize_);
756 } else {
757 // This means we're 'snapping', set the size to snapSize, and hide the
758 // first component.
759 if (isVertical) {
760 this.savedSnapSize_ =
761 goog.style.getBorderBoxSize(this.firstComponentContainer_).height;
762 } else {
763 this.savedSnapSize_ =
764 goog.style.getBorderBoxSize(this.firstComponentContainer_).width;
765 }
766 this.setFirstComponentSize(snapSize);
767 }
768
769 // Fire a SNAP event.
770 this.dispatchEvent(goog.ui.SplitPane.EventType.HANDLE_SNAP);
771 };
772
773
774 /**
775 * Handle the start drag event - set up the dragger.
776 * @param {goog.events.Event} e The event.
777 * @private
778 */
779 goog.ui.SplitPane.prototype.handleDragStart_ = function(e) {
780
781 // Setup iframe overlay to prevent iframes from grabbing events.
782 if (!this.iframeOverlay_) {
783 // Create the overlay.
784 var cssStyles = 'position: relative';
785
786 if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('10')) {
787 // IE doesn't look at this div unless it has a background, so we'll
788 // put one on, but make it opaque.
789 cssStyles += ';background-color: #000;filter: Alpha(Opacity=0)';
790 }
791 this.iframeOverlay_ = this.getDomHelper().createDom(
792 goog.dom.TagName.DIV, {'style': cssStyles});
793
794 this.getDomHelper().appendChild(this.getElement(), this.iframeOverlay_);
795 }
796 this.iframeOverlay_.style.zIndex =
797 goog.ui.SplitPane.IframeOverlayIndex_.OVERLAY;
798
799 goog.style.setBorderBoxSize(
800 this.iframeOverlay_, goog.style.getBorderBoxSize(this.getElement()));
801
802 var pos = goog.style.getPosition(this.firstComponentContainer_);
803
804 // For the size of the limiting box, we add the container content box sizes
805 // so that if the handle is placed all the way to the end or the start, the
806 // border doesn't exceed the total size. For position, we add the difference
807 // between the border box and content box sizes of the first container to the
808 // position of the first container. The start position should be such that
809 // there is no overlap of borders.
810 var limitWidth = 0;
811 var limitHeight = 0;
812 var limitx = pos.x;
813 var limity = pos.y;
814 var firstBorderBoxSize =
815 goog.style.getBorderBoxSize(this.firstComponentContainer_);
816 var firstContentBoxSize =
817 goog.style.getContentBoxSize(this.firstComponentContainer_);
818 var secondContentBoxSize =
819 goog.style.getContentBoxSize(this.secondComponentContainer_);
820 if (this.isVertical()) {
821 limitHeight = firstContentBoxSize.height + secondContentBoxSize.height;
822 limity += firstBorderBoxSize.height - firstContentBoxSize.height;
823 } else {
824 limitWidth = firstContentBoxSize.width + secondContentBoxSize.width;
825 limitx += firstBorderBoxSize.width - firstContentBoxSize.width;
826 }
827 var limits = new goog.math.Rect(limitx, limity, limitWidth, limitHeight);
828 this.splitDragger_.setLimits(limits);
829 };
830
831
832 /**
833 * Find the location relative to the splitpane.
834 * @param {number} left The x location relative to the window.
835 * @return {number} The relative x location.
836 * @private
837 */
838 goog.ui.SplitPane.prototype.getRelativeLeft_ = function(left) {
839 return left - goog.style.getPosition(this.firstComponentContainer_).x;
840 };
841
842
843 /**
844 * Find the location relative to the splitpane.
845 * @param {number} top The y location relative to the window.
846 * @return {number} The relative y location.
847 * @private
848 */
849 goog.ui.SplitPane.prototype.getRelativeTop_ = function(top) {
850 return top - goog.style.getPosition(this.firstComponentContainer_).y;
851 };
852
853
854 /**
855 * Handle the drag event. Move the containers.
856 * @param {!goog.fx.DragEvent} e The event.
857 * @private
858 */
859 goog.ui.SplitPane.prototype.handleDrag_ = function(e) {
860 if (this.continuousResize_) {
861 if (this.isVertical()) {
862 var top = this.getRelativeTop_(e.top);
863 this.setFirstComponentSize(top);
864 } else {
865 var left = this.getRelativeLeft_(e.left);
866 this.setFirstComponentSize(left);
867 }
868 this.dispatchEvent(goog.ui.SplitPane.EventType.HANDLE_DRAG);
869 }
870 };
871
872
873 /**
874 * Handle the drag end event. If we're not doing continuous resize,
875 * resize the component. If we're doing continuous resize, the component
876 * is already the correct size.
877 * @param {!goog.fx.DragEvent} e The event.
878 * @private
879 */
880 goog.ui.SplitPane.prototype.handleDragEnd_ = function(e) {
881 // Push iframe overlay down.
882 this.iframeOverlay_.style.zIndex =
883 goog.ui.SplitPane.IframeOverlayIndex_.HIDDEN;
884 if (!this.continuousResize_) {
885 if (this.isVertical()) {
886 var top = this.getRelativeTop_(e.top);
887 this.setFirstComponentSize(top);
888 } else {
889 var left = this.getRelativeLeft_(e.left);
890 this.setFirstComponentSize(left);
891 }
892 }
893
894 this.dispatchEvent(goog.ui.SplitPane.EventType.HANDLE_DRAG_END);
895 };
896
897
898 /**
899 * Handle the Double-click. Call the snapIt method which snaps the container
900 * to the top or left.
901 * @param {goog.events.Event} e The event.
902 * @private
903 */
904 goog.ui.SplitPane.prototype.handleDoubleClick_ = function(e) {
905 this.snapIt_();
906 };
907
908
909 /** @override */
910 goog.ui.SplitPane.prototype.disposeInternal = function() {
911 goog.dispose(this.splitDragger_);
912 this.splitDragger_ = null;
913
914 goog.dom.removeNode(this.iframeOverlay_);
915 this.iframeOverlay_ = null;
916
917 goog.ui.SplitPane.base(this, 'disposeInternal');
918 };