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 stacks three boxes -- one at the top, one at the bottom, and |
7 * the whole window, and provides a draggable bar to resize the left and right | 7 * one that fills the remaining space between those two. |
8 * panes. Its elements are layed out as follows: | |
9 * | 8 * |
| 9 * +----------------------+ |
| 10 * | topbar | |
| 11 * +----------------------+ |
| 12 * | | |
| 13 * | | |
| 14 * | | |
| 15 * | | |
| 16 * | middlebox | |
| 17 * | | |
| 18 * | | |
| 19 * | | |
| 20 * | | |
| 21 * | | |
| 22 * +----------------------+ |
| 23 * | bottombar | |
| 24 * +----------------------+ |
10 * | 25 * |
11 * <<-- sizer -->> | 26 * @constructor |
12 * | |
13 * +----------------------++----------------+ | |
14 * | topbar || | | |
15 * +----------------------+| | | |
16 * | || | | |
17 * | || | | |
18 * | || | | |
19 * | || | | |
20 * | middlebox || rightbox | | |
21 * | || | | |
22 * | || | | |
23 * | || | | |
24 * | || | | |
25 * | || | | |
26 * +----------------------++ | | |
27 * | bottombar || | | |
28 * +----------------------++----------------+ | |
29 * | |
30 * The "topbar" and "bottombar" have a fixed height which is determined | |
31 * by their initial content height. The rest of the boxes fill to occupy the | |
32 * remaining space. | |
33 * | |
34 * The consumer provides DIVs for each of these regions, and the LayoutManager | |
35 * positions them correctly in the window. | |
36 * | |
37 * @constructor | |
38 */ | 27 */ |
39 function LayoutManager(topbarId, | 28 function TopMidBottomView(topView, midView, bottomView) { |
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.topView_ = topView; |
52 this.topbar_.style.position = "absolute"; | 32 this.midView_ = midView; |
53 this.middlebox_.style.position = "absolute"; | 33 this.bottomView_ = bottomView; |
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 | |
61 // Setup the "sizer" so it can be dragged left/right to reposition the | |
62 // vertical split. | |
63 this.sizer_.addEventListener("mousedown", this.onDragSizerStart_.bind(this), | |
64 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 } | 34 } |
72 | 35 |
73 // Minimum width to size panels to, in pixels. | 36 inherits(TopMidBottomView, View); |
74 LayoutManager.MIN_PANEL_WIDTH = 50; | |
75 | 37 |
76 /** | 38 TopMidBottomView.prototype.setGeometry = function(left, top, width, height) { |
77 * Repositions all of the elements to fit the window. | 39 TopMidBottomView.superClass_.setGeometry.call(this, left, top, width, height); |
78 */ | |
79 LayoutManager.prototype.recalculateLayout_ = function() { | |
80 // Calculate the horizontal split points. | |
81 var totalWidth = window.innerWidth; | |
82 var rightboxWidth = this.rightbox_.offsetWidth; | |
83 var sizerWidth = this.sizer_.offsetWidth; | |
84 var leftPaneWidth = totalWidth - (rightboxWidth + sizerWidth); | |
85 | |
86 // Don't let the left pane get too small. | |
87 if (leftPaneWidth < LayoutManager.MIN_PANEL_WIDTH) { | |
88 leftPaneWidth = LayoutManager.MIN_PANEL_WIDTH; | |
89 rightboxWidth = totalWidth - (sizerWidth + leftPaneWidth); | |
90 } | |
91 | 40 |
92 // Calculate the vertical split points. | 41 // Calculate the vertical split points. |
93 var totalHeight = window.innerHeight; | 42 var topbarHeight = this.topView_.getHeight(); |
94 var topbarHeight = this.topbar_.offsetHeight; | 43 var bottombarHeight = this.bottomView_.getHeight(); |
95 var bottombarHeight = this.bottombar_.offsetHeight; | 44 var middleboxHeight = height - (topbarHeight + bottombarHeight); |
96 var middleboxHeight = totalHeight - (topbarHeight + bottombarHeight); | |
97 | 45 |
98 // Position the boxes using calculated split points. | 46 // Position the boxes using calculated split points. |
99 setNodePosition(this.topbar_, 0, 0, | 47 this.topView_.setGeometry(left, top, width, topbarHeight); |
100 leftPaneWidth, topbarHeight); | 48 this.midView_.setGeometry(left, this.topView_.getBottom(), |
101 setNodePosition(this.middlebox_, 0, topbarHeight, | 49 width, middleboxHeight); |
102 leftPaneWidth, | 50 this.bottomView_.setGeometry(left, this.midView_.getBottom(), |
103 middleboxHeight); | 51 width, bottombarHeight); |
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 }; | 52 }; |
112 | 53 |
113 /** | 54 TopMidBottomView.prototype.show = function(isVisible) { |
114 * Called once we have clicked into the sizer. Starts capturing the mouse | 55 TopMidBottomView.superClass_.show.call(this, isVisible); |
115 * position to implement dragging. | 56 this.topView_.show(isVisible); |
116 */ | 57 this.midView_.show(isVisible); |
117 LayoutManager.prototype.onDragSizerStart_ = function(event) { | 58 this.bottomView_.show(isVisible); |
118 this.sizerMouseMoveListener_ = this.onDragSizer.bind(this); | |
119 this.sizerMouseUpListener_ = this.onDragSizerEnd.bind(this); | |
120 | |
121 window.addEventListener("mousemove", this.sizerMouseMoveListener_, true); | |
122 window.addEventListener("mouseup", this.sizerMouseUpListener_, true); | |
123 | |
124 event.preventDefault(); | |
125 }; | 59 }; |
126 | |
127 /** | |
128 * Called when the mouse has moved after dragging started. | |
129 */ | |
130 LayoutManager.prototype.onDragSizer = function(event) { | |
131 var newWidth = window.innerWidth - event.pageX; | |
132 | |
133 // Avoid shrinking the right box too much. | |
134 newWidth = Math.max(newWidth, LayoutManager.MIN_PANEL_WIDTH); | |
135 | |
136 setNodeWidth(this.rightbox_, newWidth); | |
137 this.recalculateLayout_(); | |
138 }; | |
139 | |
140 /** | |
141 * Called once the mouse has been released, and the dragging is over. | |
142 */ | |
143 LayoutManager.prototype.onDragSizerEnd = function(event) { | |
144 window.removeEventListener("mousemove", this.sizerMouseMoveListener_, true); | |
145 window.removeEventListener("mouseup", this.sizerMouseUpListener_, true); | |
146 | |
147 this.sizerMouseMoveListener_ = null; | |
148 this.sizerMouseUpListener_ = null; | |
149 | |
150 event.preventDefault(); | |
151 }; | |
OLD | NEW |