| 1 |
// Copyright 2011 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 Default renderer for {@link goog.ui.Checkbox}s. |
| 17 |
* |
| 18 |
*/ |
| 19 |
|
| 20 |
goog.provide('goog.ui.CheckboxRenderer'); |
| 21 |
|
| 22 |
goog.require('goog.a11y.aria'); |
| 23 |
goog.require('goog.a11y.aria.Role'); |
| 24 |
goog.require('goog.a11y.aria.State'); |
| 25 |
goog.require('goog.array'); |
| 26 |
goog.require('goog.asserts'); |
| 27 |
goog.require('goog.dom.TagName'); |
| 28 |
goog.require('goog.dom.classlist'); |
| 29 |
goog.require('goog.object'); |
| 30 |
goog.require('goog.ui.ControlRenderer'); |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
/** |
| 35 |
* Default renderer for {@link goog.ui.Checkbox}s. Extends the superclass |
| 36 |
* to support checkbox states: |
| 37 |
* @constructor |
| 38 |
* @extends {goog.ui.ControlRenderer} |
| 39 |
*/ |
| 40 |
goog.ui.CheckboxRenderer = function() { |
| 41 |
goog.ui.CheckboxRenderer.base(this, 'constructor'); |
| 42 |
}; |
| 43 |
goog.inherits(goog.ui.CheckboxRenderer, goog.ui.ControlRenderer); |
| 44 |
goog.addSingletonGetter(goog.ui.CheckboxRenderer); |
| 45 |
|
| 46 |
|
| 47 |
/** |
| 48 |
* Default CSS class to be applied to the root element of components rendered |
| 49 |
* by this renderer. |
| 50 |
* @type {string} |
| 51 |
*/ |
| 52 |
goog.ui.CheckboxRenderer.CSS_CLASS = goog.getCssName('goog-checkbox'); |
| 53 |
|
| 54 |
|
| 55 |
/** @override */ |
| 56 |
goog.ui.CheckboxRenderer.prototype.createDom = function(checkbox) { |
| 57 |
var element = checkbox.getDomHelper().createDom( |
| 58 |
goog.dom.TagName.SPAN, this.getClassNames(checkbox).join(' ')); |
| 59 |
|
| 60 |
var state = checkbox.getChecked(); |
| 61 |
this.setCheckboxState(element, state); |
| 62 |
|
| 63 |
return element; |
| 64 |
}; |
| 65 |
|
| 66 |
|
| 67 |
/** @override */ |
| 68 |
goog.ui.CheckboxRenderer.prototype.decorate = function(checkbox, element) { |
| 69 |
// The superclass implementation takes care of common attributes; we only |
| 70 |
// need to set the checkbox state. |
| 71 |
element = goog.ui.CheckboxRenderer.base(this, 'decorate', checkbox, element); |
| 72 |
goog.asserts.assert(element); |
| 73 |
var classes = goog.dom.classlist.get(element); |
| 74 |
// Update the checked state of the element based on its css classNames |
| 75 |
// with the following order: undetermined -> checked -> unchecked. |
| 76 |
var checked = |
| 77 |
/** @suppress {missingRequire} */ (goog.ui.Checkbox.State.UNCHECKED); |
| 78 |
if (goog.array.contains( |
| 79 |
classes, this.getClassForCheckboxState( |
| 80 |
/** @suppress {missingRequire} */ |
| 81 |
goog.ui.Checkbox.State.UNDETERMINED))) { |
| 82 |
checked = |
| 83 |
(/** @suppress {missingRequire} */ |
| 84 |
goog.ui.Checkbox.State.UNDETERMINED); |
| 85 |
} else if ( |
| 86 |
goog.array.contains( |
| 87 |
classes, this.getClassForCheckboxState( |
| 88 |
/** @suppress {missingRequire} */ goog.ui.Checkbox.State |
| 89 |
.CHECKED))) { |
| 90 |
checked = /** @suppress {missingRequire} */ goog.ui.Checkbox.State.CHECKED; |
| 91 |
} else if (goog.array.contains(classes, |
| 92 |
this.getClassForCheckboxState(/** @suppress {missingRequire} */ |
| 93 |
goog.ui.Checkbox.State.UNCHECKED))) { |
| 94 |
checked = |
| 95 |
(/** @suppress {missingRequire} */ |
| 96 |
goog.ui.Checkbox.State.UNCHECKED); |
| 97 |
} |
| 98 |
checkbox.setCheckedInternal(checked); |
| 99 |
goog.asserts.assert(element, 'The element cannot be null.'); |
| 100 |
goog.a11y.aria.setState( |
| 101 |
element, goog.a11y.aria.State.CHECKED, |
| 102 |
this.ariaStateFromCheckState_(checked)); |
| 103 |
|
| 104 |
return element; |
| 105 |
}; |
| 106 |
|
| 107 |
|
| 108 |
/** |
| 109 |
* Returns the ARIA role to be applied to checkboxes. |
| 110 |
* @return {goog.a11y.aria.Role} ARIA role. |
| 111 |
* @override |
| 112 |
*/ |
| 113 |
goog.ui.CheckboxRenderer.prototype.getAriaRole = function() { |
| 114 |
return goog.a11y.aria.Role.CHECKBOX; |
| 115 |
}; |
| 116 |
|
| 117 |
|
| 118 |
/** |
| 119 |
* Updates the appearance of the control in response to a checkbox state |
| 120 |
* change. |
| 121 |
* @param {Element} element Checkbox element. |
| 122 |
* @param {goog.ui.Checkbox.State} state Updated checkbox state. |
| 123 |
*/ |
| 124 |
goog.ui.CheckboxRenderer.prototype.setCheckboxState = function(element, state) { |
| 125 |
if (element) { |
| 126 |
goog.asserts.assert(element); |
| 127 |
var classToAdd = this.getClassForCheckboxState(state); |
| 128 |
goog.asserts.assert(classToAdd); |
| 129 |
goog.asserts.assert(element); |
| 130 |
if (goog.dom.classlist.contains(element, classToAdd)) { |
| 131 |
return; |
| 132 |
} |
| 133 |
goog.object.forEach( |
| 134 |
/** @suppress {missingRequire} */ goog.ui.Checkbox.State, |
| 135 |
function(state) { |
| 136 |
var className = this.getClassForCheckboxState(state); |
| 137 |
goog.asserts.assert(element); |
| 138 |
goog.dom.classlist.enable( |
| 139 |
element, className, className == classToAdd); |
| 140 |
}, |
| 141 |
this); |
| 142 |
goog.a11y.aria.setState( |
| 143 |
element, goog.a11y.aria.State.CHECKED, |
| 144 |
this.ariaStateFromCheckState_(state)); |
| 145 |
} |
| 146 |
}; |
| 147 |
|
| 148 |
|
| 149 |
/** |
| 150 |
* Gets the checkbox's ARIA (accessibility) state from its checked state. |
| 151 |
* @param {goog.ui.Checkbox.State} state Checkbox state. |
| 152 |
* @return {string} The value of goog.a11y.aria.state.CHECKED. Either 'true', |
| 153 |
* 'false', or 'mixed'. |
| 154 |
* @private |
| 155 |
*/ |
| 156 |
goog.ui.CheckboxRenderer.prototype.ariaStateFromCheckState_ = function(state) { |
| 157 |
if (state == |
| 158 |
/** @suppress {missingRequire} */ goog.ui.Checkbox.State.UNDETERMINED) { |
| 159 |
return 'mixed'; |
| 160 |
} else if ( |
| 161 |
state == |
| 162 |
/** @suppress {missingRequire} */ goog.ui.Checkbox.State.CHECKED) { |
| 163 |
return 'true'; |
| 164 |
} else { |
| 165 |
return 'false'; |
| 166 |
} |
| 167 |
}; |
| 168 |
|
| 169 |
|
| 170 |
/** @override */ |
| 171 |
goog.ui.CheckboxRenderer.prototype.getCssClass = function() { |
| 172 |
return goog.ui.CheckboxRenderer.CSS_CLASS; |
| 173 |
}; |
| 174 |
|
| 175 |
|
| 176 |
/** |
| 177 |
* Takes a single {@link goog.ui.Checkbox.State}, and returns the |
| 178 |
* corresponding CSS class name. |
| 179 |
* @param {goog.ui.Checkbox.State} state Checkbox state. |
| 180 |
* @return {string} CSS class representing the given state. |
| 181 |
* @protected |
| 182 |
* @suppress {missingRequire} goog.ui.Checkbox |
| 183 |
*/ |
| 184 |
goog.ui.CheckboxRenderer.prototype.getClassForCheckboxState = function(state) { |
| 185 |
var baseClass = this.getStructuralCssClass(); |
| 186 |
if (state == goog.ui.Checkbox.State.CHECKED) { |
| 187 |
return goog.getCssName(baseClass, 'checked'); |
| 188 |
} else if (state == goog.ui.Checkbox.State.UNCHECKED) { |
| 189 |
return goog.getCssName(baseClass, 'unchecked'); |
| 190 |
} else if (state == goog.ui.Checkbox.State.UNDETERMINED) { |
| 191 |
return goog.getCssName(baseClass, 'undetermined'); |
| 192 |
} |
| 193 |
throw new Error('Invalid checkbox state: ' + state); |
| 194 |
}; |