Debian Codegraph @

src/imports/Components/Popups/1.2/internalPopupUtils.js

raw
1 /*
2 * Copyright 2012 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 .pragma library
18
19 .import Lomiri.Components 1.2 as Toolkit
20
21 // Simple positioning on the screen, not relative to a caller Item.
22 function SimplePositioning(foreground, area, edgeMargins) {
23 // all coordinate computation are relative inside "area".
24
25 // return the x-coordinate to center item horizontally in area
26 this.horizontalCenter = function(item) {
27 return area.width/2 - item.width/2;
28 }
29
30 // return the y-coordinate to center item vertically in area
31 this.verticalCenter = function(item) {
32 return area.height/2 - item.height/2;
33 }
34
35 // check whether item fits inside area, obeying the given margins
36 this.checkVerticalPosition = function(item, y, marginBothSides, marginOneSide) {
37 if (y < marginBothSides) return false;
38 if (y + item.height > area.height - marginBothSides) return false;
39 if (marginBothSides >= marginOneSide) return true;
40 if (y > marginOneSide) return true;
41 if (y + item.height < area.height - marginOneSide) return true;
42 return false;
43 }
44
45 // check whether item fits inside area, obeying the given margins
46 this.checkHorizontalPosition = function(item, x, marginBothSides, marginOneSide) {
47 if (x < marginBothSides) return false;
48 if (x + item.width > area.width - marginBothSides) return false;
49 if (marginBothSides >= marginOneSide) return true;
50 if (x > marginOneSide) return true;
51 if (x + item.width < area.width - marginOneSide) return true;
52 return false;
53 }
54
55 // position foreground at the top of the screen, horizontally centered
56 this.autoSmallScreenPortrait = function() {
57 foreground.x = this.horizontalCenter(foreground);
58 foreground.y = 0;
59 }
60
61 // position foreground at the left side of the screen, vertically centered
62 this.autoSmallScreenLandscape = function() {
63 foreground.x = 0;
64 foreground.y = this.verticalCenter(foreground);
65 }
66
67 // position foreground centered on a large screen
68 this.autoLargeScreen = function() {
69 foreground.x = this.horizontalCenter(foreground);
70 foreground.y = this.verticalCenter(foreground);
71 }
72
73 // automatically position foreground on the screen
74 this.auto = function(margin) {
75 if (foreground.width >= area.width - 2*edgeMargins) {
76 // the popover uses (almost) the full width of the screen
77 this.autoSmallScreenPortrait();
78 return;
79 }
80 if (foreground.height >= area.height - 2*edgeMargins) {
81 // the popover uses (almost) the full height of the screen
82 this.autoSmallScreenLandscape();
83 return;
84 }
85 this.autoLargeScreen();
86 }
87 }
88
89 // caller is optional.
90 // if caller is given, pointer and callerMargins must be specified, otherwise they are ignored.
91 function CallerPositioning(foreground, pointer, area, caller, pointerTarget, edgeMargins, callerMargins) {
92 var simplePos = new SimplePositioning(foreground, area, edgeMargins);
93 // -1 values are not relevant.
94
95 // return y-coordinate to position item a distance of margin above caller
96 this.above = function(item, margin, anchorItem) {
97 return area.mapFromItem(anchorItem, -1, 0).y - (item ? item.height : 0) - margin;
98 }
99
100 // return y-coordinate to position item a distance of margin below caller
101 this.below = function(item, margin, anchorItem) {
102 return area.mapFromItem(anchorItem, -1, anchorItem.height).y + margin;
103 }
104
105 // return x-coordinate to position item a distance of margin left of caller
106 this.left = function(item, margin, anchorItem) {
107 return area.mapFromItem(anchorItem, 0, -1).x - (item ? item.width : 0) - margin;
108 }
109
110 // return x-coodinate to position item a distance of margin right of caller
111 this.right = function(item, margin, anchorItem) {
112 return area.mapFromItem(anchorItem, anchorItem.width, -1).x + margin;
113 }
114
115 // return x-coordinate to align center of item horizontally with center of caller
116 this.horizontalAlign = function(item, anchorItem) {
117 var x = area.mapFromItem(anchorItem, anchorItem.width/2, -1).x - item.width/2;
118 return Toolkit.MathUtils.clamp(x, edgeMargins, area.width - item.width - edgeMargins);
119 }
120
121 // return y-coordinate to align center of item vertically with center of caller
122 this.verticalAlign = function(item, anchorItem) {
123 var y = area.mapFromItem(anchorItem, -1, anchorItem.height/2).y - item.height/2;
124 return Toolkit.MathUtils.clamp(y, edgeMargins, area.height - item.height - edgeMargins);
125 }
126
127 this.closestToHorizontalCenter = function(anchorItem, margin) {
128 var center = area.mapFromItem(anchorItem, anchorItem.width/2, -1).x;
129 return Toolkit.MathUtils.clamp(center, edgeMargins + margin, area.width - (edgeMargins + margin));
130 }
131
132 this.closestToVerticalCenter = function(anchorItem, margin) {
133 var center = area.mapFromItem(anchorItem, -1, anchorItem.height/2).y;
134 return Toolkit.MathUtils.clamp(center, edgeMargins + margin, area.height - (edgeMargins + margin));
135 }
136
137 // position foreground and pointer automatically on a small screen in portrait mode
138 this.autoSmallScreenPortrait = function() {
139 if (!caller) {
140 simplePos.autoSmallScreenPortrait();
141 pointer.direction = "none";
142 return;
143 }
144 foreground.x = simplePos.horizontalCenter(foreground);
145 var ycoord = this.above(foreground, callerMargins + pointer.size, caller);
146 if (simplePos.checkVerticalPosition(foreground, ycoord, 0, area.height/4)) {
147 foreground.y = ycoord;
148 pointer.direction = "down";
149 pointer.y = this.above(null, callerMargins, caller);
150 pointer.x = this.closestToHorizontalCenter(pointerTarget, pointer.horizontalMargin);
151 return;
152 }
153 ycoord = this.below(foreground, callerMargins + pointer.size, caller);
154 if (simplePos.checkVerticalPosition(foreground, ycoord, 0, area.height/4)) {
155 foreground.y = ycoord;
156 pointer.direction = "up";
157 pointer.y = this.below(null, callerMargins, caller);
158 pointer.x = this.closestToHorizontalCenter(pointerTarget, pointer.horizontalMargin);
159 return;
160 }
161 simplePos.autoSmallScreenPortrait();
162 pointer.direction = "none";
163 }
164
165 // position foreground and pointer automatically on a small screen in landscape mode.
166 this.autoSmallScreenLandscape = function() {
167 if (!caller) {
168 simplePos.autoSmallScreenLandscape();
169 pointer.direction = "none";
170 return;
171 }
172 foreground.y = simplePos.verticalCenter(foreground);
173 var xcoord = this.left(foreground, callerMargins + pointer.size, caller);
174 if (simplePos.checkHorizontalPosition(foreground, xcoord, 0, area.width/4)) {
175 foreground.x = xcoord;
176 pointer.direction = "right";
177 pointer.x = this.left(null, callerMargins, caller);
178 pointer.y = this.closestToVerticalCenter(pointerTarget, pointer.verticalMargin);
179 return;
180 }
181 xcoord = this.right(foreground, callerMargins + pointer.size, caller);
182 if (simplePos.checkHorizontalPosition(foreground, xcoord, 0, area.width/4)) {
183 foreground.x = xcoord;
184 pointer.direction = "left";
185 pointer.x = this.right(null, callerMargins, caller);
186 pointer.y = this.closestToVerticalCenter(pointerTarget, pointer.verticalMargin);
187 return;
188 }
189 // position at the left of the screen
190 simplePos.autoSmallScreenLandscape();
191 pointer.direction = "none";
192 }
193
194 // position foreground and pointer above caller; the pointer's y will be aligned
195 // to the caller, and x to the pointerTarget
196 this.positionAbove = function() {
197 var coord = this.above(foreground, callerMargins + pointer.size, caller);
198 if (simplePos.checkVerticalPosition(foreground, coord, edgeMargins, 0)) {
199 foreground.y = coord;
200 foreground.x = this.horizontalAlign(foreground, caller);
201 pointer.direction = "down";
202 pointer.y = this.above(null, callerMargins, caller);
203 pointer.x = this.closestToHorizontalCenter(pointerTarget, pointer.horizontalMargin);
204 return true;
205 }
206 return false;
207 }
208
209 // position foreground and pointer below caller; the pointer's y will be aligned
210 // to the caller, and x to the pointerTarget
211 this.positionBelow = function() {
212 var coord = this.below(foreground, callerMargins + pointer.size, caller);
213 if (simplePos.checkVerticalPosition(foreground, coord, edgeMargins, 0)) {
214 foreground.y = coord;
215 foreground.x = this.horizontalAlign(foreground, caller);
216 pointer.direction = "up";
217 pointer.y = this.below(null, callerMargins, caller);
218 pointer.x = this.closestToHorizontalCenter(pointerTarget, pointer.horizontalMargin);
219 return true;
220 }
221 return false;
222 }
223
224 // position foreground and pointer in front of caller; the pointer's x will be aligned
225 // to the caller, and y to the pointerTarget
226 this.positionInFront = function() {
227 var coord = this.left(foreground, callerMargins + pointer.size, caller);
228 if (simplePos.checkHorizontalPosition(foreground, coord, edgeMargins, 0)) {
229 foreground.x = coord;
230 foreground.y = this.verticalAlign(foreground, caller);
231 pointer.direction = "right";
232 pointer.x = this.left(null, callerMargins, caller);
233 pointer.y = this.closestToVerticalCenter(pointerTarget, pointer.verticalMargin);
234 return true;
235 }
236 return false;
237 }
238
239 // position foreground and pointer behind caller; the pointer's x will be aligned
240 // to the caller, and y to the pointerTarget
241 this.positionBehind = function() {
242 var coord = this.right(foreground, callerMargins + pointer.size, caller)
243 if (simplePos.checkHorizontalPosition(foreground, coord, edgeMargins, 0)) {
244 foreground.x = coord;
245 foreground.y = this.verticalAlign(foreground, caller);
246 pointer.direction = "left";
247 pointer.x = this.right(null, callerMargins, caller);
248 pointer.y = this.closestToVerticalCenter(pointerTarget, pointer.verticalMargin);
249 return true;
250 }
251 return false;
252 }
253
254 // position foreground and pointer automatically on a large screen.
255 this.autoLargeScreen = function() {
256 if (!caller) {
257 simplePos.autoLargeScreen();
258 pointer.direction = "none";
259 return;
260 }
261 // position with the following priorities: above, below, right, left.
262 var order = ["positionAbove", "positionBelow", "positionBehind", "positionInFront"];
263 for (var i = 0; i < order.length; i++) {
264 if (this[order[i]]())
265 return;
266 }
267 // not enough space on any of the sides to fit within the margins.
268 simplePos.autoLargeScreen();
269 pointer.direction = "none";
270 }
271
272 this.auto = function() {
273 // area may be null some times...
274 if (!area)
275 return;
276 if (!pointerTarget)
277 pointerTarget = caller;
278 if (foreground.width >= area.width - 2*edgeMargins) {
279 // the popover uses (almost) the full width of the screen
280 this.autoSmallScreenPortrait();
281 return;
282 }
283 if (foreground.height >= area.height - 2*edgeMargins) {
284 // the popover uses (almost) the full height of the screen
285 this.autoSmallScreenLandscape();
286 return;
287 }
288 this.autoLargeScreen();
289 }
290 }