| 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 Renderer for {@link goog.ui.Palette}s. |
| 17 |
* |
| 18 |
* @author [email protected] (Attila Bodis) |
| 19 |
*/ |
| 20 |
|
| 21 |
goog.provide('goog.ui.PaletteRenderer'); |
| 22 |
|
| 23 |
goog.require('goog.a11y.aria'); |
| 24 |
goog.require('goog.a11y.aria.Role'); |
| 25 |
goog.require('goog.a11y.aria.State'); |
| 26 |
goog.require('goog.array'); |
| 27 |
goog.require('goog.asserts'); |
| 28 |
goog.require('goog.dom'); |
| 29 |
goog.require('goog.dom.NodeIterator'); |
| 30 |
goog.require('goog.dom.NodeType'); |
| 31 |
goog.require('goog.dom.TagName'); |
| 32 |
goog.require('goog.dom.classlist'); |
| 33 |
goog.require('goog.iter'); |
| 34 |
goog.require('goog.style'); |
| 35 |
goog.require('goog.ui.ControlRenderer'); |
| 36 |
goog.require('goog.userAgent'); |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
/** |
| 41 |
* Default renderer for {@link goog.ui.Palette}s. Renders the palette as an |
| 42 |
* HTML table wrapped in a DIV, with one palette item per cell: |
| 43 |
* |
| 44 |
* <div class="goog-palette"> |
| 45 |
* <table class="goog-palette-table"> |
| 46 |
* <tbody class="goog-palette-body"> |
| 47 |
* <tr class="goog-palette-row"> |
| 48 |
* <td class="goog-palette-cell">...Item 0...</td> |
| 49 |
* <td class="goog-palette-cell">...Item 1...</td> |
| 50 |
* ... |
| 51 |
* </tr> |
| 52 |
* <tr class="goog-palette-row"> |
| 53 |
* ... |
| 54 |
* </tr> |
| 55 |
* </tbody> |
| 56 |
* </table> |
| 57 |
* </div> |
| 58 |
* |
| 59 |
* @constructor |
| 60 |
* @extends {goog.ui.ControlRenderer} |
| 61 |
*/ |
| 62 |
goog.ui.PaletteRenderer = function() { |
| 63 |
goog.ui.ControlRenderer.call(this); |
| 64 |
}; |
| 65 |
goog.inherits(goog.ui.PaletteRenderer, goog.ui.ControlRenderer); |
| 66 |
goog.addSingletonGetter(goog.ui.PaletteRenderer); |
| 67 |
|
| 68 |
|
| 69 |
/** |
| 70 |
* Globally unique ID sequence for cells rendered by this renderer class. |
| 71 |
* @type {number} |
| 72 |
* @private |
| 73 |
*/ |
| 74 |
goog.ui.PaletteRenderer.cellId_ = 0; |
| 75 |
|
| 76 |
|
| 77 |
/** |
| 78 |
* Default CSS class to be applied to the root element of components rendered |
| 79 |
* by this renderer. |
| 80 |
* @type {string} |
| 81 |
*/ |
| 82 |
goog.ui.PaletteRenderer.CSS_CLASS = goog.getCssName('goog-palette'); |
| 83 |
|
| 84 |
|
| 85 |
/** |
| 86 |
* Returns the palette items arranged in a table wrapped in a DIV, with the |
| 87 |
* renderer's own CSS class and additional state-specific classes applied to |
| 88 |
* it. |
| 89 |
* @param {goog.ui.Control} palette goog.ui.Palette to render. |
| 90 |
* @return {!Element} Root element for the palette. |
| 91 |
* @override |
| 92 |
*/ |
| 93 |
goog.ui.PaletteRenderer.prototype.createDom = function(palette) { |
| 94 |
var classNames = this.getClassNames(palette); |
| 95 |
var element = palette.getDomHelper().createDom( |
| 96 |
goog.dom.TagName.DIV, classNames, |
| 97 |
this.createGrid( |
| 98 |
/** @type {Array<Node>} */ (palette.getContent()), palette.getSize(), |
| 99 |
palette.getDomHelper())); |
| 100 |
goog.a11y.aria.setRole(element, goog.a11y.aria.Role.GRID); |
| 101 |
return element; |
| 102 |
}; |
| 103 |
|
| 104 |
|
| 105 |
/** |
| 106 |
* Returns the given items in a table with {@code size.width} columns and |
| 107 |
* {@code size.height} rows. If the table is too big, empty cells will be |
| 108 |
* created as needed. If the table is too small, the items that don't fit |
| 109 |
* will not be rendered. |
| 110 |
* @param {Array<Node>} items Palette items. |
| 111 |
* @param {goog.math.Size} size Palette size (columns x rows); both dimensions |
| 112 |
* must be specified as numbers. |
| 113 |
* @param {goog.dom.DomHelper} dom DOM helper for document interaction. |
| 114 |
* @return {!Element} Palette table element. |
| 115 |
*/ |
| 116 |
goog.ui.PaletteRenderer.prototype.createGrid = function(items, size, dom) { |
| 117 |
var rows = []; |
| 118 |
for (var row = 0, index = 0; row < size.height; row++) { |
| 119 |
var cells = []; |
| 120 |
for (var column = 0; column < size.width; column++) { |
| 121 |
var item = items && items[index++]; |
| 122 |
cells.push(this.createCell(item, dom)); |
| 123 |
} |
| 124 |
rows.push(this.createRow(cells, dom)); |
| 125 |
} |
| 126 |
|
| 127 |
return this.createTable(rows, dom); |
| 128 |
}; |
| 129 |
|
| 130 |
|
| 131 |
/** |
| 132 |
* Returns a table element (or equivalent) that wraps the given rows. |
| 133 |
* @param {Array<Element>} rows Array of row elements. |
| 134 |
* @param {goog.dom.DomHelper} dom DOM helper for document interaction. |
| 135 |
* @return {!Element} Palette table element. |
| 136 |
*/ |
| 137 |
goog.ui.PaletteRenderer.prototype.createTable = function(rows, dom) { |
| 138 |
var table = dom.createDom( |
| 139 |
goog.dom.TagName.TABLE, goog.getCssName(this.getCssClass(), 'table'), |
| 140 |
dom.createDom( |
| 141 |
goog.dom.TagName.TBODY, goog.getCssName(this.getCssClass(), 'body'), |
| 142 |
rows)); |
| 143 |
table.cellSpacing = '0'; |
| 144 |
table.cellPadding = '0'; |
| 145 |
return table; |
| 146 |
}; |
| 147 |
|
| 148 |
|
| 149 |
/** |
| 150 |
* Returns a table row element (or equivalent) that wraps the given cells. |
| 151 |
* @param {Array<Element>} cells Array of cell elements. |
| 152 |
* @param {goog.dom.DomHelper} dom DOM helper for document interaction. |
| 153 |
* @return {!Element} Row element. |
| 154 |
*/ |
| 155 |
goog.ui.PaletteRenderer.prototype.createRow = function(cells, dom) { |
| 156 |
var row = dom.createDom( |
| 157 |
goog.dom.TagName.TR, goog.getCssName(this.getCssClass(), 'row'), cells); |
| 158 |
goog.a11y.aria.setRole(row, goog.a11y.aria.Role.ROW); |
| 159 |
return row; |
| 160 |
}; |
| 161 |
|
| 162 |
|
| 163 |
/** |
| 164 |
* Returns a table cell element (or equivalent) that wraps the given palette |
| 165 |
* item (which must be a DOM node). |
| 166 |
* @param {Node|string} node Palette item. |
| 167 |
* @param {goog.dom.DomHelper} dom DOM helper for document interaction. |
| 168 |
* @return {!Element} Cell element. |
| 169 |
*/ |
| 170 |
goog.ui.PaletteRenderer.prototype.createCell = function(node, dom) { |
| 171 |
var cell = dom.createDom( |
| 172 |
goog.dom.TagName.TD, { |
| 173 |
'class': goog.getCssName(this.getCssClass(), 'cell'), |
| 174 |
// Cells must have an ID, for accessibility, so we generate one here. |
| 175 |
'id': goog.getCssName(this.getCssClass(), 'cell-') + |
| 176 |
goog.ui.PaletteRenderer.cellId_++ |
| 177 |
}, |
| 178 |
node); |
| 179 |
goog.a11y.aria.setRole(cell, goog.a11y.aria.Role.GRIDCELL); |
| 180 |
// Initialize to an unselected state. |
| 181 |
goog.a11y.aria.setState(cell, goog.a11y.aria.State.SELECTED, false); |
| 182 |
|
| 183 |
if (!goog.dom.getTextContent(cell) && !goog.a11y.aria.getLabel(cell)) { |
| 184 |
var ariaLabelForCell = this.findAriaLabelForCell_(cell); |
| 185 |
if (ariaLabelForCell) { |
| 186 |
goog.a11y.aria.setLabel(cell, ariaLabelForCell); |
| 187 |
} |
| 188 |
} |
| 189 |
return cell; |
| 190 |
}; |
| 191 |
|
| 192 |
|
| 193 |
/** |
| 194 |
* Descends the DOM and tries to find an aria label for a grid cell |
| 195 |
* from the first child with a label or title. |
| 196 |
* @param {!Element} cell The cell. |
| 197 |
* @return {string} The label to use. |
| 198 |
* @private |
| 199 |
*/ |
| 200 |
goog.ui.PaletteRenderer.prototype.findAriaLabelForCell_ = function(cell) { |
| 201 |
var iter = new goog.dom.NodeIterator(cell); |
| 202 |
var label = ''; |
| 203 |
var node; |
| 204 |
while (!label && (node = goog.iter.nextOrValue(iter, null))) { |
| 205 |
if (node.nodeType == goog.dom.NodeType.ELEMENT) { |
| 206 |
label = |
| 207 |
goog.a11y.aria.getLabel(/** @type {!Element} */ (node)) || node.title; |
| 208 |
} |
| 209 |
} |
| 210 |
return label; |
| 211 |
}; |
| 212 |
|
| 213 |
|
| 214 |
/** |
| 215 |
* Overrides {@link goog.ui.ControlRenderer#canDecorate} to always return false. |
| 216 |
* @param {Element} element Ignored. |
| 217 |
* @return {boolean} False, since palettes don't support the decorate flow (for |
| 218 |
* now). |
| 219 |
* @override |
| 220 |
*/ |
| 221 |
goog.ui.PaletteRenderer.prototype.canDecorate = function(element) { |
| 222 |
return false; |
| 223 |
}; |
| 224 |
|
| 225 |
|
| 226 |
/** |
| 227 |
* Overrides {@link goog.ui.ControlRenderer#decorate} to be a no-op, since |
| 228 |
* palettes don't support the decorate flow (for now). |
| 229 |
* @param {goog.ui.Control} palette Ignored. |
| 230 |
* @param {Element} element Ignored. |
| 231 |
* @return {null} Always null. |
| 232 |
* @override |
| 233 |
*/ |
| 234 |
goog.ui.PaletteRenderer.prototype.decorate = function(palette, element) { |
| 235 |
return null; |
| 236 |
}; |
| 237 |
|
| 238 |
|
| 239 |
/** |
| 240 |
* Overrides {@link goog.ui.ControlRenderer#setContent} for palettes. Locates |
| 241 |
* the HTML table representing the palette grid, and replaces the contents of |
| 242 |
* each cell with a new element from the array of nodes passed as the second |
| 243 |
* argument. If the new content has too many items the table will have more |
| 244 |
* rows added to fit, if there are less items than the table has cells, then the |
| 245 |
* left over cells will be empty. |
| 246 |
* @param {Element} element Root element of the palette control. |
| 247 |
* @param {goog.ui.ControlContent} content Array of items to replace existing |
| 248 |
* palette items. |
| 249 |
* @override |
| 250 |
*/ |
| 251 |
goog.ui.PaletteRenderer.prototype.setContent = function(element, content) { |
| 252 |
var items = /** @type {Array<Node>} */ (content); |
| 253 |
if (element) { |
| 254 |
var tbody = goog.dom.getElementsByTagNameAndClass( |
| 255 |
goog.dom.TagName.TBODY, goog.getCssName(this.getCssClass(), 'body'), |
| 256 |
element)[0]; |
| 257 |
if (tbody) { |
| 258 |
var index = 0; |
| 259 |
goog.array.forEach(tbody.rows, function(row) { |
| 260 |
goog.array.forEach(row.cells, function(cell) { |
| 261 |
goog.dom.removeChildren(cell); |
| 262 |
if (items) { |
| 263 |
var item = items[index++]; |
| 264 |
if (item) { |
| 265 |
goog.dom.appendChild(cell, item); |
| 266 |
} |
| 267 |
} |
| 268 |
}); |
| 269 |
}); |
| 270 |
|
| 271 |
// Make space for any additional items. |
| 272 |
if (index < items.length) { |
| 273 |
var cells = []; |
| 274 |
var dom = goog.dom.getDomHelper(element); |
| 275 |
var width = tbody.rows[0].cells.length; |
| 276 |
while (index < items.length) { |
| 277 |
var item = items[index++]; |
| 278 |
cells.push(this.createCell(item, dom)); |
| 279 |
if (cells.length == width) { |
| 280 |
var row = this.createRow(cells, dom); |
| 281 |
goog.dom.appendChild(tbody, row); |
| 282 |
cells.length = 0; |
| 283 |
} |
| 284 |
} |
| 285 |
if (cells.length > 0) { |
| 286 |
while (cells.length < width) { |
| 287 |
cells.push(this.createCell('', dom)); |
| 288 |
} |
| 289 |
var row = this.createRow(cells, dom); |
| 290 |
goog.dom.appendChild(tbody, row); |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
// Make sure the new contents are still unselectable. |
| 295 |
goog.style.setUnselectable(element, true, goog.userAgent.GECKO); |
| 296 |
} |
| 297 |
}; |
| 298 |
|
| 299 |
|
| 300 |
/** |
| 301 |
* Returns the item corresponding to the given node, or null if the node is |
| 302 |
* neither a palette cell nor part of a palette item. |
| 303 |
* @param {goog.ui.Palette} palette Palette in which to look for the item. |
| 304 |
* @param {Node} node Node to look for. |
| 305 |
* @return {Node} The corresponding palette item (null if not found). |
| 306 |
*/ |
| 307 |
goog.ui.PaletteRenderer.prototype.getContainingItem = function(palette, node) { |
| 308 |
var root = palette.getElement(); |
| 309 |
while (node && node.nodeType == goog.dom.NodeType.ELEMENT && node != root) { |
| 310 |
if (node.tagName == goog.dom.TagName.TD && |
| 311 |
goog.dom.classlist.contains( |
| 312 |
/** @type {!Element} */ (node), |
| 313 |
goog.getCssName(this.getCssClass(), 'cell'))) { |
| 314 |
return node.firstChild; |
| 315 |
} |
| 316 |
node = node.parentNode; |
| 317 |
} |
| 318 |
|
| 319 |
return null; |
| 320 |
}; |
| 321 |
|
| 322 |
|
| 323 |
/** |
| 324 |
* Updates the highlight styling of the palette cell containing the given node |
| 325 |
* based on the value of the Boolean argument. |
| 326 |
* @param {goog.ui.Palette} palette Palette containing the item. |
| 327 |
* @param {Node} node Item whose cell is to be highlighted or un-highlighted. |
| 328 |
* @param {boolean} highlight If true, the cell is highlighted; otherwise it is |
| 329 |
* un-highlighted. |
| 330 |
*/ |
| 331 |
goog.ui.PaletteRenderer.prototype.highlightCell = function( |
| 332 |
palette, node, highlight) { |
| 333 |
if (node) { |
| 334 |
var cell = this.getCellForItem(node); |
| 335 |
goog.asserts.assert(cell); |
| 336 |
goog.dom.classlist.enable( |
| 337 |
cell, goog.getCssName(this.getCssClass(), 'cell-hover'), highlight); |
| 338 |
// See http://www.w3.org/TR/2006/WD-aria-state-20061220/#activedescendent |
| 339 |
// for an explanation of the activedescendent. |
| 340 |
if (highlight) { |
| 341 |
goog.a11y.aria.setState( |
| 342 |
palette.getElementStrict(), goog.a11y.aria.State.ACTIVEDESCENDANT, |
| 343 |
cell.id); |
| 344 |
} else if ( |
| 345 |
cell.id == |
| 346 |
goog.a11y.aria.getState( |
| 347 |
palette.getElementStrict(), |
| 348 |
goog.a11y.aria.State.ACTIVEDESCENDANT)) { |
| 349 |
goog.a11y.aria.removeState( |
| 350 |
palette.getElementStrict(), goog.a11y.aria.State.ACTIVEDESCENDANT); |
| 351 |
} |
| 352 |
} |
| 353 |
}; |
| 354 |
|
| 355 |
|
| 356 |
/** |
| 357 |
* @param {Node} node Item whose cell is to be returned. |
| 358 |
* @return {Element} The grid cell for the palette item. |
| 359 |
*/ |
| 360 |
goog.ui.PaletteRenderer.prototype.getCellForItem = function(node) { |
| 361 |
return /** @type {Element} */ (node ? node.parentNode : null); |
| 362 |
}; |
| 363 |
|
| 364 |
|
| 365 |
/** |
| 366 |
* Updates the selection styling of the palette cell containing the given node |
| 367 |
* based on the value of the Boolean argument. |
| 368 |
* @param {goog.ui.Palette} palette Palette containing the item. |
| 369 |
* @param {Node} node Item whose cell is to be selected or deselected. |
| 370 |
* @param {boolean} select If true, the cell is selected; otherwise it is |
| 371 |
* deselected. |
| 372 |
*/ |
| 373 |
goog.ui.PaletteRenderer.prototype.selectCell = function(palette, node, select) { |
| 374 |
if (node) { |
| 375 |
var cell = /** @type {!Element} */ (node.parentNode); |
| 376 |
goog.dom.classlist.enable( |
| 377 |
cell, goog.getCssName(this.getCssClass(), 'cell-selected'), select); |
| 378 |
goog.a11y.aria.setState(cell, goog.a11y.aria.State.SELECTED, select); |
| 379 |
} |
| 380 |
}; |
| 381 |
|
| 382 |
|
| 383 |
/** |
| 384 |
* Returns the CSS class to be applied to the root element of components |
| 385 |
* rendered using this renderer. |
| 386 |
* @return {string} Renderer-specific CSS class. |
| 387 |
* @override |
| 388 |
*/ |
| 389 |
goog.ui.PaletteRenderer.prototype.getCssClass = function() { |
| 390 |
return goog.ui.PaletteRenderer.CSS_CLASS; |
| 391 |
}; |