Debian Codegraph @

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

raw
1 // Copyright 2008 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 A dimension picker control. A dimension picker allows the
17 * user to visually select a row and column count.
18 *
19 * @author [email protected] (Robby Walker)
20 * @see ../demos/dimensionpicker.html
21 * @see ../demos/dimensionpicker_rtl.html
22 */
23
24 goog.provide('goog.ui.DimensionPicker');
25
26 goog.require('goog.events.EventType');
27 goog.require('goog.events.KeyCodes');
28 goog.require('goog.math.Size');
29 goog.require('goog.ui.Component');
30 goog.require('goog.ui.Control');
31 goog.require('goog.ui.DimensionPickerRenderer');
32 goog.require('goog.ui.registry');
33
34
35
36 /**
37 * A dimension picker allows the user to visually select a row and column
38 * count using their mouse and keyboard.
39 *
40 * The currently selected dimension is controlled by an ACTION event. Event
41 * listeners may retrieve the selected item using the
42 * {@link #getValue} method.
43 *
44 * @param {goog.ui.DimensionPickerRenderer=} opt_renderer Renderer used to
45 * render or decorate the palette; defaults to
46 * {@link goog.ui.DimensionPickerRenderer}.
47 * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper, used for
48 * document interaction.
49 * @constructor
50 * @extends {goog.ui.Control}
51 * @final
52 */
53 goog.ui.DimensionPicker = function(opt_renderer, opt_domHelper) {
54 goog.ui.Control.call(
55 this, null, opt_renderer || goog.ui.DimensionPickerRenderer.getInstance(),
56 opt_domHelper);
57
58 this.size_ = new goog.math.Size(this.minColumns, this.minRows);
59 };
60 goog.inherits(goog.ui.DimensionPicker, goog.ui.Control);
61
62
63 /**
64 * Minimum number of columns to show in the grid.
65 * @type {number}
66 */
67 goog.ui.DimensionPicker.prototype.minColumns = 5;
68
69
70 /**
71 * Minimum number of rows to show in the grid.
72 * @type {number}
73 */
74 goog.ui.DimensionPicker.prototype.minRows = 5;
75
76
77 /**
78 * Maximum number of columns to show in the grid.
79 * @type {number}
80 */
81 goog.ui.DimensionPicker.prototype.maxColumns = 20;
82
83
84 /**
85 * Maximum number of rows to show in the grid.
86 * @type {number}
87 */
88 goog.ui.DimensionPicker.prototype.maxRows = 20;
89
90
91 /**
92 * Palette dimensions (columns x rows).
93 * @type {goog.math.Size}
94 * @private
95 */
96 goog.ui.DimensionPicker.prototype.size_;
97
98
99 /**
100 * Currently highlighted row count.
101 * @type {number}
102 * @private
103 */
104 goog.ui.DimensionPicker.prototype.highlightedRows_ = 1;
105
106
107 /**
108 * Currently highlighted column count.
109 * @type {number}
110 * @private
111 */
112 goog.ui.DimensionPicker.prototype.highlightedColumns_ = 1;
113
114
115 /** @override */
116 goog.ui.DimensionPicker.prototype.enterDocument = function() {
117 goog.ui.DimensionPicker.superClass_.enterDocument.call(this);
118
119 var handler = this.getHandler();
120 handler
121 .listen(
122 this.getRenderer().getMouseMoveElement(this),
123 goog.events.EventType.MOUSEMOVE, this.handleMouseMove)
124 .listen(
125 this.getDomHelper().getWindow(), goog.events.EventType.RESIZE,
126 this.handleWindowResize);
127
128 var parent = this.getParent();
129 if (parent) {
130 handler.listen(parent, goog.ui.Component.EventType.SHOW, this.handleShow_);
131 }
132 };
133
134
135 /** @override */
136 goog.ui.DimensionPicker.prototype.exitDocument = function() {
137 goog.ui.DimensionPicker.superClass_.exitDocument.call(this);
138
139 var handler = this.getHandler();
140 handler
141 .unlisten(
142 this.getRenderer().getMouseMoveElement(this),
143 goog.events.EventType.MOUSEMOVE, this.handleMouseMove)
144 .unlisten(
145 this.getDomHelper().getWindow(), goog.events.EventType.RESIZE,
146 this.handleWindowResize);
147
148 var parent = this.getParent();
149 if (parent) {
150 handler.unlisten(
151 parent, goog.ui.Component.EventType.SHOW, this.handleShow_);
152 }
153 };
154
155
156 /**
157 * Resets the highlighted size when the picker is shown.
158 * @private
159 */
160 goog.ui.DimensionPicker.prototype.handleShow_ = function() {
161 if (this.isVisible()) {
162 this.setValue(1, 1);
163 }
164 };
165
166
167 /** @override */
168 goog.ui.DimensionPicker.prototype.disposeInternal = function() {
169 goog.ui.DimensionPicker.superClass_.disposeInternal.call(this);
170 delete this.size_;
171 };
172
173
174 // Palette event handling.
175
176
177 /**
178 * Handles mousemove events. Determines which palette size was moused over and
179 * highlights it.
180 * @param {goog.events.BrowserEvent} e Mouse event to handle.
181 * @protected
182 */
183 goog.ui.DimensionPicker.prototype.handleMouseMove = function(e) {
184 var highlightedSizeX = this.getRenderer().getGridOffsetX(
185 this, this.isRightToLeft() ?
186 /** @type {!HTMLElement} */ (e.target).offsetWidth - e.offsetX :
187 e.offsetX);
188 var highlightedSizeY = this.getRenderer().getGridOffsetY(this, e.offsetY);
189
190 this.setValue(highlightedSizeX, highlightedSizeY);
191 };
192
193
194 /**
195 * Handles window resize events. Ensures no scrollbars are introduced by the
196 * renderer's mouse catcher.
197 * @param {goog.events.Event} e Resize event to handle.
198 * @protected
199 */
200 goog.ui.DimensionPicker.prototype.handleWindowResize = function(e) {
201 this.getRenderer().positionMouseCatcher(this);
202 };
203
204
205 /**
206 * Handle key events if supported, so the user can use the keyboard to
207 * manipulate the highlighted rows and columns.
208 * @param {goog.events.KeyEvent} e The key event object.
209 * @return {boolean} Whether the key event was handled.
210 * @override
211 */
212 goog.ui.DimensionPicker.prototype.handleKeyEvent = function(e) {
213 var rows = this.highlightedRows_;
214 var columns = this.highlightedColumns_;
215 switch (e.keyCode) {
216 case goog.events.KeyCodes.DOWN:
217 rows++;
218 break;
219 case goog.events.KeyCodes.UP:
220 rows--;
221 break;
222 case goog.events.KeyCodes.LEFT:
223 if (this.isRightToLeft()) {
224 columns++;
225 } else {
226 if (columns == 1) {
227 // Delegate to parent.
228 return false;
229 } else {
230 columns--;
231 }
232 }
233 break;
234 case goog.events.KeyCodes.RIGHT:
235 if (this.isRightToLeft()) {
236 if (columns == 1) {
237 // Delegate to parent.
238 return false;
239 } else {
240 columns--;
241 }
242 } else {
243 columns++;
244 }
245 break;
246 default:
247 return goog.ui.DimensionPicker.superClass_.handleKeyEvent.call(this, e);
248 }
249 this.setValue(columns, rows);
250 return true;
251 };
252
253
254 // Palette management.
255
256
257 /**
258 * @return {goog.math.Size} Current table size shown (columns x rows).
259 */
260 goog.ui.DimensionPicker.prototype.getSize = function() {
261 return this.size_;
262 };
263
264
265 /**
266 * @return {!goog.math.Size} size The currently highlighted dimensions.
267 */
268 goog.ui.DimensionPicker.prototype.getValue = function() {
269 return new goog.math.Size(this.highlightedColumns_, this.highlightedRows_);
270 };
271
272
273 /**
274 * Sets the currently highlighted dimensions. If the dimensions are not valid
275 * (not between 1 and the maximum number of columns/rows to show), they will
276 * be changed to the closest valid value.
277 * @param {(number|!goog.math.Size)} columns The number of columns to highlight,
278 * or a goog.math.Size object containing both.
279 * @param {number=} opt_rows The number of rows to highlight. Can be
280 * omitted when columns is a good.math.Size object.
281 */
282 goog.ui.DimensionPicker.prototype.setValue = function(columns, opt_rows) {
283 if (!goog.isDef(opt_rows)) {
284 columns = /** @type {!goog.math.Size} */ (columns);
285 opt_rows = columns.height;
286 columns = columns.width;
287 } else {
288 columns = /** @type {number} */ (columns);
289 }
290
291 // Ensure that the row and column values are within the minimum value (1) and
292 // maxmimum values.
293 columns = Math.max(1, columns);
294 opt_rows = Math.max(1, opt_rows);
295 columns = Math.min(this.maxColumns, columns);
296 opt_rows = Math.min(this.maxRows, opt_rows);
297
298 if (this.highlightedColumns_ != columns ||
299 this.highlightedRows_ != opt_rows) {
300 var renderer = this.getRenderer();
301 // Show one more row/column than highlighted so the user understands the
302 // palette can grow.
303 this.size_.width =
304 Math.max(Math.min(columns + 1, this.maxColumns), this.minColumns);
305 this.size_.height =
306 Math.max(Math.min(opt_rows + 1, this.maxRows), this.minRows);
307 renderer.updateSize(this, this.getElement());
308
309 this.highlightedColumns_ = columns;
310 this.highlightedRows_ = opt_rows;
311 renderer.setHighlightedSize(this, columns, opt_rows);
312 }
313 };
314
315
316 /**
317 * Register this control so it can be created from markup
318 */
319 goog.ui.registry.setDecoratorByClassName(
320 goog.ui.DimensionPickerRenderer.CSS_CLASS,
321 function() { return new goog.ui.DimensionPicker(); });