| 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 The default renderer for a goog.dom.DimensionPicker. A |
| 17 |
* dimension picker allows the user to visually select a row and column count. |
| 18 |
* It looks like a palette but in order to minimize DOM load it is rendered. |
| 19 |
* using CSS background tiling instead of as a grid of nodes. |
| 20 |
* |
| 21 |
* @author [email protected] (Robby Walker) |
| 22 |
*/ |
| 23 |
|
| 24 |
goog.provide('goog.ui.DimensionPickerRenderer'); |
| 25 |
|
| 26 |
goog.require('goog.a11y.aria.Announcer'); |
| 27 |
goog.require('goog.a11y.aria.LivePriority'); |
| 28 |
goog.require('goog.dom'); |
| 29 |
goog.require('goog.dom.TagName'); |
| 30 |
goog.require('goog.i18n.bidi'); |
| 31 |
goog.require('goog.style'); |
| 32 |
goog.require('goog.ui.ControlRenderer'); |
| 33 |
goog.require('goog.userAgent'); |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
/** |
| 38 |
* Default renderer for {@link goog.ui.DimensionPicker}s. Renders the |
| 39 |
* palette as two divs, one with the un-highlighted background, and one with the |
| 40 |
* highlighted background. |
| 41 |
* |
| 42 |
* @constructor |
| 43 |
* @extends {goog.ui.ControlRenderer} |
| 44 |
*/ |
| 45 |
goog.ui.DimensionPickerRenderer = function() { |
| 46 |
goog.ui.ControlRenderer.call(this); |
| 47 |
|
| 48 |
/** @private {goog.a11y.aria.Announcer} */ |
| 49 |
this.announcer_ = new goog.a11y.aria.Announcer(); |
| 50 |
}; |
| 51 |
goog.inherits(goog.ui.DimensionPickerRenderer, goog.ui.ControlRenderer); |
| 52 |
goog.addSingletonGetter(goog.ui.DimensionPickerRenderer); |
| 53 |
|
| 54 |
|
| 55 |
/** |
| 56 |
* Default CSS class to be applied to the root element of components rendered |
| 57 |
* by this renderer. |
| 58 |
* @type {string} |
| 59 |
*/ |
| 60 |
goog.ui.DimensionPickerRenderer.CSS_CLASS = |
| 61 |
goog.getCssName('goog-dimension-picker'); |
| 62 |
|
| 63 |
|
| 64 |
/** |
| 65 |
* Return the underlying div for the given outer element. |
| 66 |
* @param {Element} element The root element. |
| 67 |
* @return {Element} The underlying div. |
| 68 |
* @private |
| 69 |
*/ |
| 70 |
goog.ui.DimensionPickerRenderer.prototype.getUnderlyingDiv_ = function( |
| 71 |
element) { |
| 72 |
return /** @type {Element} */ (element.firstChild.childNodes[1]); |
| 73 |
}; |
| 74 |
|
| 75 |
|
| 76 |
/** |
| 77 |
* Return the highlight div for the given outer element. |
| 78 |
* @param {Element} element The root element. |
| 79 |
* @return {Element} The highlight div. |
| 80 |
* @private |
| 81 |
*/ |
| 82 |
goog.ui.DimensionPickerRenderer.prototype.getHighlightDiv_ = function(element) { |
| 83 |
return /** @type {Element} */ (element.firstChild.lastChild); |
| 84 |
}; |
| 85 |
|
| 86 |
|
| 87 |
/** |
| 88 |
* Return the status message div for the given outer element. |
| 89 |
* @param {Element} element The root element. |
| 90 |
* @return {Element} The status message div. |
| 91 |
* @private |
| 92 |
*/ |
| 93 |
goog.ui.DimensionPickerRenderer.prototype.getStatusDiv_ = function(element) { |
| 94 |
return /** @type {Element} */ (element.lastChild); |
| 95 |
}; |
| 96 |
|
| 97 |
|
| 98 |
/** |
| 99 |
* Return the invisible mouse catching div for the given outer element. |
| 100 |
* @param {Element} element The root element. |
| 101 |
* @return {Element} The invisible mouse catching div. |
| 102 |
* @private |
| 103 |
*/ |
| 104 |
goog.ui.DimensionPickerRenderer.prototype.getMouseCatcher_ = function(element) { |
| 105 |
return /** @type {Element} */ (element.firstChild.firstChild); |
| 106 |
}; |
| 107 |
|
| 108 |
|
| 109 |
/** |
| 110 |
* Overrides {@link goog.ui.ControlRenderer#canDecorate} to allow decorating |
| 111 |
* empty DIVs only. |
| 112 |
* @param {Element} element The element to check. |
| 113 |
* @return {boolean} Whether if the element is an empty div. |
| 114 |
* @override |
| 115 |
*/ |
| 116 |
goog.ui.DimensionPickerRenderer.prototype.canDecorate = function(element) { |
| 117 |
return element.tagName == goog.dom.TagName.DIV && !element.firstChild; |
| 118 |
}; |
| 119 |
|
| 120 |
|
| 121 |
/** |
| 122 |
* Overrides {@link goog.ui.ControlRenderer#decorate} to decorate empty DIVs. |
| 123 |
* @param {goog.ui.Control} control goog.ui.DimensionPicker to decorate. |
| 124 |
* @param {Element} element The element to decorate. |
| 125 |
* @return {Element} The decorated element. |
| 126 |
* @override |
| 127 |
*/ |
| 128 |
goog.ui.DimensionPickerRenderer.prototype.decorate = function( |
| 129 |
control, element) { |
| 130 |
var palette = /** @type {goog.ui.DimensionPicker} */ (control); |
| 131 |
goog.ui.DimensionPickerRenderer.superClass_.decorate.call( |
| 132 |
this, palette, element); |
| 133 |
|
| 134 |
this.addElementContents_(palette, element); |
| 135 |
this.updateSize(palette, element); |
| 136 |
|
| 137 |
return element; |
| 138 |
}; |
| 139 |
|
| 140 |
|
| 141 |
/** |
| 142 |
* Scales various elements in order to update the palette's size. |
| 143 |
* @param {goog.ui.DimensionPicker} palette The palette object. |
| 144 |
* @param {Element} element The element to set the style of. |
| 145 |
*/ |
| 146 |
goog.ui.DimensionPickerRenderer.prototype.updateSize = function( |
| 147 |
palette, element) { |
| 148 |
var size = palette.getSize(); |
| 149 |
|
| 150 |
element.style.width = size.width + 'em'; |
| 151 |
|
| 152 |
var underlyingDiv = this.getUnderlyingDiv_(element); |
| 153 |
underlyingDiv.style.width = size.width + 'em'; |
| 154 |
underlyingDiv.style.height = size.height + 'em'; |
| 155 |
|
| 156 |
if (palette.isRightToLeft()) { |
| 157 |
this.adjustParentDirection_(palette, element); |
| 158 |
} |
| 159 |
}; |
| 160 |
|
| 161 |
|
| 162 |
/** |
| 163 |
* Adds the appropriate content elements to the given outer DIV. |
| 164 |
* @param {goog.ui.DimensionPicker} palette The palette object. |
| 165 |
* @param {Element} element The element to decorate. |
| 166 |
* @private |
| 167 |
*/ |
| 168 |
goog.ui.DimensionPickerRenderer.prototype.addElementContents_ = function( |
| 169 |
palette, element) { |
| 170 |
// First we create a single div containing three stacked divs. The bottom div |
| 171 |
// catches mouse events. We can't use document level mouse move detection as |
| 172 |
// we could lose events to iframes. This is especially important in Firefox 2 |
| 173 |
// in which TrogEdit creates iframes. The middle div uses a css tiled |
| 174 |
// background image to represent deselected tiles. The top div uses a |
| 175 |
// different css tiled background image to represent selected tiles. |
| 176 |
var mouseCatcherDiv = palette.getDomHelper().createDom( |
| 177 |
goog.dom.TagName.DIV, |
| 178 |
goog.getCssName(this.getCssClass(), 'mousecatcher')); |
| 179 |
var unhighlightedDiv = |
| 180 |
palette.getDomHelper().createDom(goog.dom.TagName.DIV, { |
| 181 |
'class': goog.getCssName(this.getCssClass(), 'unhighlighted'), |
| 182 |
'style': 'width:100%;height:100%' |
| 183 |
}); |
| 184 |
var highlightedDiv = palette.getDomHelper().createDom( |
| 185 |
goog.dom.TagName.DIV, goog.getCssName(this.getCssClass(), 'highlighted')); |
| 186 |
element.appendChild( |
| 187 |
palette.getDomHelper().createDom( |
| 188 |
goog.dom.TagName.DIV, {'style': 'width:100%;height:100%'}, |
| 189 |
mouseCatcherDiv, unhighlightedDiv, highlightedDiv)); |
| 190 |
|
| 191 |
// Lastly we add a div to store the text version of the current state. |
| 192 |
element.appendChild( |
| 193 |
palette.getDomHelper().createDom( |
| 194 |
goog.dom.TagName.DIV, goog.getCssName(this.getCssClass(), 'status'))); |
| 195 |
}; |
| 196 |
|
| 197 |
|
| 198 |
/** |
| 199 |
* Creates a div and adds the appropriate contents to it. |
| 200 |
* @param {goog.ui.Control} control Picker to render. |
| 201 |
* @return {!Element} Root element for the palette. |
| 202 |
* @override |
| 203 |
*/ |
| 204 |
goog.ui.DimensionPickerRenderer.prototype.createDom = function(control) { |
| 205 |
var palette = /** @type {goog.ui.DimensionPicker} */ (control); |
| 206 |
var classNames = this.getClassNames(palette); |
| 207 |
// Hide the element from screen readers so they don't announce "1 of 1" for |
| 208 |
// the perceived number of items in the palette. |
| 209 |
var element = palette.getDomHelper().createDom( |
| 210 |
goog.dom.TagName.DIV, |
| 211 |
{'class': classNames ? classNames.join(' ') : '', 'aria-hidden': 'true'}); |
| 212 |
this.addElementContents_(palette, element); |
| 213 |
this.updateSize(palette, element); |
| 214 |
return element; |
| 215 |
}; |
| 216 |
|
| 217 |
|
| 218 |
/** |
| 219 |
* Initializes the control's DOM when the control enters the document. Called |
| 220 |
* from {@link goog.ui.Control#enterDocument}. |
| 221 |
* @param {goog.ui.Control} control Palette whose DOM is to be |
| 222 |
* initialized as it enters the document. |
| 223 |
* @override |
| 224 |
*/ |
| 225 |
goog.ui.DimensionPickerRenderer.prototype.initializeDom = function(control) { |
| 226 |
var palette = /** @type {goog.ui.DimensionPicker} */ (control); |
| 227 |
goog.ui.DimensionPickerRenderer.superClass_.initializeDom.call(this, palette); |
| 228 |
|
| 229 |
// Make the displayed highlighted size match the dimension picker's value. |
| 230 |
var highlightedSize = palette.getValue(); |
| 231 |
this.setHighlightedSize( |
| 232 |
palette, highlightedSize.width, highlightedSize.height); |
| 233 |
|
| 234 |
this.positionMouseCatcher(palette); |
| 235 |
}; |
| 236 |
|
| 237 |
|
| 238 |
/** |
| 239 |
* Get the element to listen for mouse move events on. |
| 240 |
* @param {goog.ui.DimensionPicker} palette The palette to listen on. |
| 241 |
* @return {Element} The element to listen for mouse move events on. |
| 242 |
*/ |
| 243 |
goog.ui.DimensionPickerRenderer.prototype.getMouseMoveElement = function( |
| 244 |
palette) { |
| 245 |
return /** @type {Element} */ (palette.getElement().firstChild); |
| 246 |
}; |
| 247 |
|
| 248 |
|
| 249 |
/** |
| 250 |
* Returns the x offset in to the grid for the given mouse x position. |
| 251 |
* @param {goog.ui.DimensionPicker} palette The table size palette. |
| 252 |
* @param {number} x The mouse event x position. |
| 253 |
* @return {number} The x offset in to the grid. |
| 254 |
*/ |
| 255 |
goog.ui.DimensionPickerRenderer.prototype.getGridOffsetX = function( |
| 256 |
palette, x) { |
| 257 |
// TODO(robbyw): Don't rely on magic 18 - measure each palette's em size. |
| 258 |
return Math.min(palette.maxColumns, Math.ceil(x / 18)); |
| 259 |
}; |
| 260 |
|
| 261 |
|
| 262 |
/** |
| 263 |
* Returns the y offset in to the grid for the given mouse y position. |
| 264 |
* @param {goog.ui.DimensionPicker} palette The table size palette. |
| 265 |
* @param {number} y The mouse event y position. |
| 266 |
* @return {number} The y offset in to the grid. |
| 267 |
*/ |
| 268 |
goog.ui.DimensionPickerRenderer.prototype.getGridOffsetY = function( |
| 269 |
palette, y) { |
| 270 |
return Math.min(palette.maxRows, Math.ceil(y / 18)); |
| 271 |
}; |
| 272 |
|
| 273 |
|
| 274 |
/** |
| 275 |
* Sets the highlighted size. Does nothing if the palette hasn't been rendered. |
| 276 |
* @param {goog.ui.DimensionPicker} palette The table size palette. |
| 277 |
* @param {number} columns The number of columns to highlight. |
| 278 |
* @param {number} rows The number of rows to highlight. |
| 279 |
*/ |
| 280 |
goog.ui.DimensionPickerRenderer.prototype.setHighlightedSize = function( |
| 281 |
palette, columns, rows) { |
| 282 |
var element = palette.getElement(); |
| 283 |
// Can't update anything if DimensionPicker hasn't been rendered. |
| 284 |
if (!element) { |
| 285 |
return; |
| 286 |
} |
| 287 |
|
| 288 |
// Style the highlight div. |
| 289 |
var style = this.getHighlightDiv_(element).style; |
| 290 |
style.width = columns + 'em'; |
| 291 |
style.height = rows + 'em'; |
| 292 |
|
| 293 |
// Explicitly set style.right so the element grows to the left when increase |
| 294 |
// in width. |
| 295 |
if (palette.isRightToLeft()) { |
| 296 |
style.right = '0'; |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* @desc The dimension of the columns and rows currently selected in the |
| 301 |
* dimension picker, as text that can be spoken by a screen reader. |
| 302 |
*/ |
| 303 |
var MSG_DIMENSION_PICKER_HIGHLIGHTED_DIMENSIONS = goog.getMsg( |
| 304 |
'{$numCols} by {$numRows}', |
| 305 |
{'numCols': String(columns), 'numRows': String(rows)}); |
| 306 |
this.announcer_.say( |
| 307 |
MSG_DIMENSION_PICKER_HIGHLIGHTED_DIMENSIONS, |
| 308 |
goog.a11y.aria.LivePriority.ASSERTIVE); |
| 309 |
|
| 310 |
// Update the size text. |
| 311 |
goog.dom.setTextContent( |
| 312 |
this.getStatusDiv_(element), |
| 313 |
goog.i18n.bidi.enforceLtrInText(columns + ' x ' + rows)); |
| 314 |
}; |
| 315 |
|
| 316 |
|
| 317 |
/** |
| 318 |
* Position the mouse catcher such that it receives mouse events past the |
| 319 |
* selectedsize up to the maximum size. Takes care to not introduce scrollbars. |
| 320 |
* Should be called on enter document and when the window changes size. |
| 321 |
* @param {goog.ui.DimensionPicker} palette The table size palette. |
| 322 |
*/ |
| 323 |
goog.ui.DimensionPickerRenderer.prototype.positionMouseCatcher = function( |
| 324 |
palette) { |
| 325 |
var mouseCatcher = this.getMouseCatcher_(palette.getElement()); |
| 326 |
var doc = goog.dom.getOwnerDocument(mouseCatcher); |
| 327 |
var body = doc.body; |
| 328 |
|
| 329 |
var position = goog.style.getRelativePosition(mouseCatcher, body); |
| 330 |
|
| 331 |
// Hide the mouse catcher so it doesn't affect the body's scroll size. |
| 332 |
mouseCatcher.style.display = 'none'; |
| 333 |
|
| 334 |
// Compute the maximum size the catcher can be without introducing scrolling. |
| 335 |
var xAvailableEm = (palette.isRightToLeft() && position.x > 0) ? |
| 336 |
Math.floor(position.x / 18) : |
| 337 |
Math.floor((body.scrollWidth - position.x) / 18); |
| 338 |
|
| 339 |
// Computing available height is more complicated - we need to check the |
| 340 |
// window's inner height. |
| 341 |
var height; |
| 342 |
if (goog.userAgent.IE) { |
| 343 |
// Offset 20px to make up for scrollbar size. |
| 344 |
height = goog.style.getClientViewportElement(body).scrollHeight - 20; |
| 345 |
} else { |
| 346 |
var win = goog.dom.getWindow(doc); |
| 347 |
// Offset 20px to make up for scrollbar size. |
| 348 |
height = Math.max(win.innerHeight, body.scrollHeight) - 20; |
| 349 |
} |
| 350 |
var yAvailableEm = Math.floor((height - position.y) / 18); |
| 351 |
|
| 352 |
// Resize and display the mouse catcher. |
| 353 |
mouseCatcher.style.width = Math.min(palette.maxColumns, xAvailableEm) + 'em'; |
| 354 |
mouseCatcher.style.height = Math.min(palette.maxRows, yAvailableEm) + 'em'; |
| 355 |
mouseCatcher.style.display = ''; |
| 356 |
|
| 357 |
// Explicitly set style.right so the mouse catcher is positioned on the left |
| 358 |
// side instead of right. |
| 359 |
if (palette.isRightToLeft()) { |
| 360 |
mouseCatcher.style.right = '0'; |
| 361 |
} |
| 362 |
}; |
| 363 |
|
| 364 |
|
| 365 |
/** |
| 366 |
* Returns the CSS class to be applied to the root element of components |
| 367 |
* rendered using this renderer. |
| 368 |
* @return {string} Renderer-specific CSS class. |
| 369 |
* @override |
| 370 |
*/ |
| 371 |
goog.ui.DimensionPickerRenderer.prototype.getCssClass = function() { |
| 372 |
return goog.ui.DimensionPickerRenderer.CSS_CLASS; |
| 373 |
}; |
| 374 |
|
| 375 |
|
| 376 |
/** |
| 377 |
* This function adjusts the positioning from 'left' and 'top' to 'right' and |
| 378 |
* 'top' as appropriate for RTL control. This is so when the dimensionpicker |
| 379 |
* grow in width, the containing element grow to the left instead of right. |
| 380 |
* This won't be necessary if goog.ui.SubMenu rendering code would position RTL |
| 381 |
* control with 'right' and 'top'. |
| 382 |
* @private |
| 383 |
* |
| 384 |
* @param {goog.ui.DimensionPicker} palette The palette object. |
| 385 |
* @param {Element} element The palette's element. |
| 386 |
*/ |
| 387 |
goog.ui.DimensionPickerRenderer.prototype.adjustParentDirection_ = function( |
| 388 |
palette, element) { |
| 389 |
var parent = palette.getParent(); |
| 390 |
if (parent) { |
| 391 |
var parentElement = parent.getElement(); |
| 392 |
|
| 393 |
// Anchors the containing element to the right so it grows to the left |
| 394 |
// when it increase in width. |
| 395 |
var right = goog.style.getStyle(parentElement, 'right'); |
| 396 |
if (right == '') { |
| 397 |
var parentPos = goog.style.getPosition(parentElement); |
| 398 |
var parentSize = goog.style.getSize(parentElement); |
| 399 |
if (parentSize.width != 0 && parentPos.x != 0) { |
| 400 |
var visibleRect = |
| 401 |
goog.style.getBounds(goog.style.getClientViewportElement()); |
| 402 |
var visibleWidth = visibleRect.width; |
| 403 |
right = visibleWidth - parentPos.x - parentSize.width; |
| 404 |
goog.style.setStyle(parentElement, 'right', right + 'px'); |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
// When a table is inserted, the containing elemet's position is |
| 409 |
// recalculated the next time it shows, set left back to '' to prevent |
| 410 |
// extra white space on the left. |
| 411 |
var left = goog.style.getStyle(parentElement, 'left'); |
| 412 |
if (left != '') { |
| 413 |
goog.style.setStyle(parentElement, 'left', ''); |
| 414 |
} |
| 415 |
} else { |
| 416 |
goog.style.setStyle(element, 'right', '0px'); |
| 417 |
} |
| 418 |
}; |