2015-05-22 13:01:23 -07:00
|
|
|
import {Component, View, Attribute} from 'angular2/angular2';
|
2015-02-17 11:56:24 -08:00
|
|
|
import {isPresent} from 'angular2/src/facade/lang';
|
2015-05-22 13:01:23 -07:00
|
|
|
import {KEY_SPACE} from 'angular2_material/src/core/constants';
|
2015-04-17 12:59:07 -07:00
|
|
|
import {KeyboardEvent} from 'angular2/src/facade/browser';
|
2015-04-23 13:15:20 -07:00
|
|
|
import {NumberWrapper} from 'angular2/src/facade/lang';
|
2015-02-17 11:56:24 -08:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'md-checkbox',
|
2015-05-26 15:54:10 +02:00
|
|
|
properties: ['checked', 'disabled'],
|
2015-06-09 12:33:40 +02:00
|
|
|
host: {
|
|
|
|
'(keydown)': 'onKeydown($event)',
|
|
|
|
'[tabindex]': 'tabindex',
|
2015-06-14 14:24:19 +02:00
|
|
|
'[attr.role]': '"checkbox"',
|
2015-06-09 12:33:40 +02:00
|
|
|
'[attr.aria-checked]': 'checked',
|
|
|
|
'[attr.aria-disabled]': 'disabled'
|
2015-02-17 11:56:24 -08:00
|
|
|
}
|
|
|
|
})
|
2015-05-22 13:01:23 -07:00
|
|
|
@View({templateUrl: 'angular2_material/src/components/checkbox/checkbox.html', directives: []})
|
2015-02-17 11:56:24 -08:00
|
|
|
export class MdCheckbox {
|
|
|
|
/** Whether this checkbox is checked. */
|
2015-04-21 11:47:53 -07:00
|
|
|
checked: boolean;
|
2015-02-17 11:56:24 -08:00
|
|
|
|
2015-04-17 12:59:07 -07:00
|
|
|
/** Whether this checkbox is disabled. */
|
2015-05-04 18:34:06 -07:00
|
|
|
_disabled: boolean;
|
2015-04-17 12:59:07 -07:00
|
|
|
|
2015-04-21 11:47:53 -07:00
|
|
|
/** Setter for tabindex */
|
2015-04-23 13:15:20 -07:00
|
|
|
tabindex: number;
|
2015-02-17 11:56:24 -08:00
|
|
|
|
2015-05-22 13:01:23 -07:00
|
|
|
constructor(@Attribute('tabindex') tabindex: string) {
|
2015-02-17 11:56:24 -08:00
|
|
|
this.checked = false;
|
2015-04-23 13:15:20 -07:00
|
|
|
this.tabindex = isPresent(tabindex) ? NumberWrapper.parseInt(tabindex, 10) : 0;
|
2015-05-04 18:34:06 -07:00
|
|
|
this._disabled = false;
|
2015-02-17 11:56:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
get disabled() {
|
2015-05-04 18:34:06 -07:00
|
|
|
return this._disabled;
|
2015-02-17 11:56:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
set disabled(value) {
|
2015-05-04 18:34:06 -07:00
|
|
|
this._disabled = isPresent(value) && value !== false;
|
2015-02-17 11:56:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
onKeydown(event: KeyboardEvent) {
|
2015-04-17 12:59:07 -07:00
|
|
|
if (event.keyCode == KEY_SPACE) {
|
2015-02-17 11:56:24 -08:00
|
|
|
event.preventDefault();
|
|
|
|
this.toggle(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toggle(event) {
|
|
|
|
if (this.disabled) {
|
|
|
|
event.stopPropagation();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.checked = !this.checked;
|
|
|
|
}
|
|
|
|
}
|