OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * The class LayoutManager implements a vertically split layout that takes up | 6 * This view implements a vertically split display with a draggable divider. |
7 * the whole window, and provides a draggable bar to resize the left and right | |
8 * panes. Its elements are layed out as follows: | |
9 * | |
10 * | 7 * |
11 * <<-- sizer -->> | 8 * <<-- sizer -->> |
12 * | 9 * |
13 * +----------------------++----------------+ | 10 * +----------------------++----------------+ |
14 * | topbar || | | |
15 * +----------------------+| | | |
16 * | || | | 11 * | || | |
17 * | || | | 12 * | || | |
18 * | || | | 13 * | || | |
19 * | || | | 14 * | || | |
20 * | middlebox || rightbox | | 15 * | leftView || rightView | |
21 * | || | | 16 * | || | |
22 * | || | | 17 * | || | |
23 * | || | | 18 * | || | |
24 * | || | | 19 * | || | |
25 * | || | | 20 * | || | |
26 * +----------------------++ | | |
27 * | bottombar || | | |
28 * +----------------------++----------------+ | 21 * +----------------------++----------------+ |
29 * | 22 * |
30 * The "topbar" and "bottombar" have a fixed height which is determined | 23 * @param {!View} leftView The widget to position on the left. |
31 * by their initial content height. The rest of the boxes fill to occupy the | 24 * @param {!View} rightView The widget to position on the right. |
32 * remaining space. | 25 * @param {!DivView} sizerView The widget that will serve as draggable divider. |
33 * | |
34 * The consumer provides DIVs for each of these regions, and the LayoutManager | |
35 * positions them correctly in the window. | |
36 * | |
37 * @constructor | 26 * @constructor |
38 */ | 27 */ |
39 function LayoutManager(topbarId, | 28 function ResizableVerticalSplitView(leftView, rightView, sizerView) { |
40 middleboxId, | 29 View.call(this); |
41 bottombarId, | |
42 sizerId, | |
43 rightboxId) { | |
44 // Lookup the elements. | |
45 this.topbar_ = document.getElementById(topbarId); | |
46 this.middlebox_ = document.getElementById(middleboxId); | |
47 this.bottombar_ = document.getElementById(bottombarId); | |
48 this.sizer_ = document.getElementById(sizerId); | |
49 this.rightbox_ = document.getElementById(rightboxId); | |
50 | 30 |
51 // Make all the elements absolutely positioned. | 31 this.leftView_ = leftView; |
52 this.topbar_.style.position = "absolute"; | 32 this.rightView_ = rightView; |
53 this.middlebox_.style.position = "absolute"; | 33 this.sizerView_ = sizerView; |
54 this.bottombar_.style.position = "absolute"; | |
55 this.sizer_.style.position = "absolute"; | |
56 this.rightbox_.style.position = "absolute"; | |
57 | |
58 // Set the initial split position at 50%. | |
59 setNodeWidth(this.rightbox_, window.innerWidth / 2); | |
60 | 34 |
61 // Setup the "sizer" so it can be dragged left/right to reposition the | 35 // Setup the "sizer" so it can be dragged left/right to reposition the |
62 // vertical split. | 36 // vertical split. |
63 this.sizer_.addEventListener("mousedown", this.onDragSizerStart_.bind(this), | 37 sizerView.getNode().addEventListener( |
64 true); | 38 "mousedown", this.onDragSizerStart_.bind(this), true); |
65 | |
66 // Recalculate the layout whenever the window size changes. | |
67 window.addEventListener("resize", this.recalculateLayout_.bind(this), true); | |
68 | |
69 // Do the initial layout . | |
70 this.recalculateLayout_(); | |
71 } | 39 } |
72 | 40 |
| 41 inherits(ResizableVerticalSplitView, View); |
| 42 |
73 // Minimum width to size panels to, in pixels. | 43 // Minimum width to size panels to, in pixels. |
74 LayoutManager.MIN_PANEL_WIDTH = 50; | 44 ResizableVerticalSplitView.MIN_PANEL_WIDTH = 50; |
75 | 45 |
76 /** | 46 /** |
77 * Repositions all of the elements to fit the window. | 47 * Repositions all of the elements to fit the window. |
78 */ | 48 */ |
79 LayoutManager.prototype.recalculateLayout_ = function() { | 49 ResizableVerticalSplitView.prototype.setGeometry = function( |
| 50 left, top, width, height) { |
| 51 ResizableVerticalSplitView.superClass_.setGeometry.call( |
| 52 this, left, top, width, height); |
| 53 |
| 54 // If this is the first setGeometry(), initialize the split point at 50%. |
| 55 if (!this.rightSplit_) |
| 56 this.rightSplit_ = parseInt((width / 2).toFixed(0)); |
| 57 |
80 // Calculate the horizontal split points. | 58 // Calculate the horizontal split points. |
81 var totalWidth = window.innerWidth; | 59 var rightboxWidth = this.rightSplit_; |
82 var rightboxWidth = this.rightbox_.offsetWidth; | 60 var sizerWidth = this.sizerView_.getWidth(); |
83 var sizerWidth = this.sizer_.offsetWidth; | 61 var leftboxWidth = width - (rightboxWidth + sizerWidth); |
84 var leftPaneWidth = totalWidth - (rightboxWidth + sizerWidth); | |
85 | 62 |
86 // Don't let the left pane get too small. | 63 // Don't let the left pane get too small. |
87 if (leftPaneWidth < LayoutManager.MIN_PANEL_WIDTH) { | 64 if (leftboxWidth < ResizableVerticalSplitView.MIN_PANEL_WIDTH) { |
88 leftPaneWidth = LayoutManager.MIN_PANEL_WIDTH; | 65 leftboxWidth = ResizableVerticalSplitView.MIN_PANEL_WIDTH; |
89 rightboxWidth = totalWidth - (sizerWidth + leftPaneWidth); | 66 rightboxWidth = width - (sizerWidth + leftboxWidth); |
90 } | 67 } |
91 | 68 |
92 // Calculate the vertical split points. | 69 // Position the boxes using calculated split points. |
93 var totalHeight = window.innerHeight; | 70 this.leftView_.setGeometry(left, top, leftboxWidth, height); |
94 var topbarHeight = this.topbar_.offsetHeight; | 71 this.sizerView_.setGeometry(this.leftView_.getRight(), top, |
95 var bottombarHeight = this.bottombar_.offsetHeight; | 72 sizerWidth, height); |
96 var middleboxHeight = totalHeight - (topbarHeight + bottombarHeight); | 73 this.rightView_.setGeometry(this.sizerView_.getRight(), top, |
| 74 rightboxWidth, height); |
| 75 }; |
97 | 76 |
98 // Position the boxes using calculated split points. | 77 ResizableVerticalSplitView.prototype.show = function(isVisible) { |
99 setNodePosition(this.topbar_, 0, 0, | 78 ResizableVerticalSplitView.superClass_.show.call(this, isVisible); |
100 leftPaneWidth, topbarHeight); | 79 this.leftView_.show(isVisible); |
101 setNodePosition(this.middlebox_, 0, topbarHeight, | 80 this.sizerView_.show(isVisible); |
102 leftPaneWidth, | 81 this.rightView_.show(isVisible); |
103 middleboxHeight); | |
104 setNodePosition(this.bottombar_, 0, (topbarHeight + middleboxHeight), | |
105 leftPaneWidth, bottombarHeight); | |
106 | |
107 setNodePosition(this.sizer_, leftPaneWidth, 0, | |
108 sizerWidth, totalHeight); | |
109 setNodePosition(this.rightbox_, leftPaneWidth + sizerWidth, 0, | |
110 rightboxWidth, totalHeight); | |
111 }; | 82 }; |
112 | 83 |
113 /** | 84 /** |
114 * Called once we have clicked into the sizer. Starts capturing the mouse | 85 * Called once we have clicked into the sizer. Starts capturing the mouse |
115 * position to implement dragging. | 86 * position to implement dragging. |
116 */ | 87 */ |
117 LayoutManager.prototype.onDragSizerStart_ = function(event) { | 88 ResizableVerticalSplitView.prototype.onDragSizerStart_ = function(event) { |
118 this.sizerMouseMoveListener_ = this.onDragSizer.bind(this); | 89 this.sizerMouseMoveListener_ = this.onDragSizer.bind(this); |
119 this.sizerMouseUpListener_ = this.onDragSizerEnd.bind(this); | 90 this.sizerMouseUpListener_ = this.onDragSizerEnd.bind(this); |
120 | 91 |
121 window.addEventListener("mousemove", this.sizerMouseMoveListener_, true); | 92 window.addEventListener("mousemove", this.sizerMouseMoveListener_, true); |
122 window.addEventListener("mouseup", this.sizerMouseUpListener_, true); | 93 window.addEventListener("mouseup", this.sizerMouseUpListener_, true); |
123 | 94 |
124 event.preventDefault(); | 95 event.preventDefault(); |
125 }; | 96 }; |
126 | 97 |
127 /** | 98 /** |
128 * Called when the mouse has moved after dragging started. | 99 * Called when the mouse has moved after dragging started. |
129 */ | 100 */ |
130 LayoutManager.prototype.onDragSizer = function(event) { | 101 ResizableVerticalSplitView.prototype.onDragSizer = function(event) { |
131 var newWidth = window.innerWidth - event.pageX; | 102 // Convert from page coordinates, to view coordinates. |
| 103 this.rightSplit_ = this.getWidth() - (event.pageX - this.getLeft()); |
132 | 104 |
133 // Avoid shrinking the right box too much. | 105 // Avoid shrinking the right box too much. |
134 newWidth = Math.max(newWidth, LayoutManager.MIN_PANEL_WIDTH); | 106 this.rightSplit_ = Math.max( |
| 107 this.rightSplit_, ResizableVerticalSplitView.MIN_PANEL_WIDTH); |
135 | 108 |
136 setNodeWidth(this.rightbox_, newWidth); | 109 // Force a layout with the new |rightSplit_|. |
137 this.recalculateLayout_(); | 110 this.setGeometry( |
| 111 this.getLeft(), this.getTop(), this.getWidth(), this.getHeight()); |
138 }; | 112 }; |
139 | 113 |
140 /** | 114 /** |
141 * Called once the mouse has been released, and the dragging is over. | 115 * Called once the mouse has been released, and the dragging is over. |
142 */ | 116 */ |
143 LayoutManager.prototype.onDragSizerEnd = function(event) { | 117 ResizableVerticalSplitView.prototype.onDragSizerEnd = function(event) { |
144 window.removeEventListener("mousemove", this.sizerMouseMoveListener_, true); | 118 window.removeEventListener("mousemove", this.sizerMouseMoveListener_, true); |
145 window.removeEventListener("mouseup", this.sizerMouseUpListener_, true); | 119 window.removeEventListener("mouseup", this.sizerMouseUpListener_, true); |
146 | 120 |
147 this.sizerMouseMoveListener_ = null; | 121 this.sizerMouseMoveListener_ = null; |
148 this.sizerMouseUpListener_ = null; | 122 this.sizerMouseUpListener_ = null; |
149 | 123 |
150 event.preventDefault(); | 124 event.preventDefault(); |
151 }; | 125 }; |
OLD | NEW |