123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- goog.provide('goog.ui.Checkbox');
- goog.provide('goog.ui.Checkbox.State');
- goog.require('goog.a11y.aria');
- goog.require('goog.a11y.aria.State');
- goog.require('goog.events.EventType');
- goog.require('goog.events.KeyCodes');
- goog.require('goog.string');
- goog.require('goog.ui.CheckboxRenderer');
- goog.require('goog.ui.Component');
- goog.require('goog.ui.Control');
- goog.require('goog.ui.registry');
- goog.ui.Checkbox = function(opt_checked, opt_domHelper, opt_renderer) {
- var renderer = opt_renderer || goog.ui.CheckboxRenderer.getInstance();
- goog.ui.Control.call(this, null, renderer, opt_domHelper);
-
-
-
-
- this.checked_ =
- goog.isDef(opt_checked) ? opt_checked : goog.ui.Checkbox.State.UNCHECKED;
- };
- goog.inherits(goog.ui.Checkbox, goog.ui.Control);
- goog.tagUnsealableClass(goog.ui.Checkbox);
- goog.ui.Checkbox.State = {
- CHECKED: true,
- UNCHECKED: false,
- UNDETERMINED: null
- };
- goog.ui.Checkbox.prototype.label_ = null;
- goog.ui.Checkbox.prototype.getChecked = function() {
- return this.checked_;
- };
- goog.ui.Checkbox.prototype.isChecked = function() {
- return this.checked_ == goog.ui.Checkbox.State.CHECKED;
- };
- goog.ui.Checkbox.prototype.isUnchecked = function() {
- return this.checked_ == goog.ui.Checkbox.State.UNCHECKED;
- };
- goog.ui.Checkbox.prototype.isUndetermined = function() {
- return this.checked_ == goog.ui.Checkbox.State.UNDETERMINED;
- };
- goog.ui.Checkbox.prototype.setChecked = function(checked) {
- if (checked != this.checked_) {
- this.checked_ = (checked);
- this.getRenderer().setCheckboxState(this.getElement(), this.checked_);
- }
- };
- goog.ui.Checkbox.prototype.setCheckedInternal = function(checked) {
- this.checked_ = checked;
- };
- goog.ui.Checkbox.prototype.setLabel = function(label) {
- if (this.isInDocument()) {
- var wasFocused = this.isFocused();
- this.exitDocument();
- this.label_ = label;
- this.enterDocument();
- if (wasFocused) {
- this.getElementStrict().focus();
- }
- } else {
- this.label_ = label;
- }
- };
- goog.ui.Checkbox.prototype.toggle = function() {
- this.setChecked(
- this.checked_ ? goog.ui.Checkbox.State.UNCHECKED :
- goog.ui.Checkbox.State.CHECKED);
- };
- goog.ui.Checkbox.prototype.enterDocument = function() {
- goog.ui.Checkbox.base(this, 'enterDocument');
- if (this.isHandleMouseEvents()) {
- var handler = this.getHandler();
-
- if (this.label_) {
-
-
-
- handler
- .listen(
- this.label_, goog.events.EventType.CLICK,
- this.handleClickOrSpace_)
- .listen(
- this.label_, goog.events.EventType.MOUSEOVER,
- this.handleMouseOver)
- .listen(
- this.label_, goog.events.EventType.MOUSEOUT, this.handleMouseOut)
- .listen(
- this.label_, goog.events.EventType.MOUSEDOWN,
- this.handleMouseDown)
- .listen(
- this.label_, goog.events.EventType.MOUSEUP, this.handleMouseUp);
- }
-
- handler.listen(
- this.getElement(), goog.events.EventType.CLICK,
- this.handleClickOrSpace_);
- }
-
- var checkboxElement = this.getElementStrict();
- if (this.label_ && checkboxElement != this.label_ &&
- goog.string.isEmptyOrWhitespace(
- goog.a11y.aria.getLabel(checkboxElement))) {
- if (!this.label_.id) {
- this.label_.id = this.makeId('lbl');
- }
- goog.a11y.aria.setState(
- checkboxElement, goog.a11y.aria.State.LABELLEDBY, this.label_.id);
- }
- };
- goog.ui.Checkbox.prototype.setEnabled = function(enabled) {
- goog.ui.Checkbox.base(this, 'setEnabled', enabled);
- var el = this.getElement();
- if (el) {
- el.tabIndex = this.isEnabled() ? 0 : -1;
- }
- };
- goog.ui.Checkbox.prototype.handleClickOrSpace_ = function(e) {
- e.stopPropagation();
- var eventType = this.checked_ ? goog.ui.Component.EventType.UNCHECK :
- goog.ui.Component.EventType.CHECK;
- if (this.isEnabled() && !e.target.href && this.dispatchEvent(eventType)) {
- e.preventDefault();
- this.toggle();
- this.dispatchEvent(goog.ui.Component.EventType.CHANGE);
- }
- };
- goog.ui.Checkbox.prototype.handleKeyEventInternal = function(e) {
- if (e.keyCode == goog.events.KeyCodes.SPACE) {
- this.performActionInternal(e);
- this.handleClickOrSpace_(e);
- }
- return false;
- };
- goog.ui.registry.setDecoratorByClassName(
- goog.ui.CheckboxRenderer.CSS_CLASS,
- function() { return new goog.ui.Checkbox(); });
|