2019-10-04 10:33:16 +00:00
|
|
|
import ngModule from '../../module';
|
2019-10-09 22:47:29 +00:00
|
|
|
import FormInput from '../form-input';
|
2019-10-04 10:33:16 +00:00
|
|
|
import './style.scss';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base component with common logic and styles for checkbox and radio button.
|
|
|
|
*
|
|
|
|
* @property {Boolean} checked Whether the checkbox is checked
|
|
|
|
*/
|
2019-10-09 22:47:29 +00:00
|
|
|
export default class Toggle extends FormInput {
|
2019-10-04 10:33:16 +00:00
|
|
|
constructor($element, $) {
|
|
|
|
super($element, $);
|
2019-10-24 08:17:32 +00:00
|
|
|
this.initTabIndex();
|
|
|
|
this.classList.add('vn-toggle');
|
|
|
|
this.element.addEventListener('click', e => this.onClick(e));
|
|
|
|
this.element.addEventListener('keydown', e => this.onKeydown(e));
|
2019-10-04 10:33:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onKeydown(event) {
|
2019-10-24 08:17:32 +00:00
|
|
|
if (!event.defaultPrevented && event.code == 'Space')
|
|
|
|
this.element.click();
|
2019-10-04 10:33:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onClick(event) {
|
2019-10-23 15:38:35 +00:00
|
|
|
if (!this.editable || event.defaultPrevented)
|
2019-10-04 10:33:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
changed() {
|
|
|
|
this.$.$applyAsync();
|
|
|
|
this.element.dispatchEvent(new Event('change'));
|
|
|
|
this.emit('change', {value: this.field});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-08 21:57:02 +00:00
|
|
|
ngModule.vnComponent('vnToggle', {
|
2019-10-04 10:33:16 +00:00
|
|
|
controller: Toggle,
|
|
|
|
bindings: {
|
|
|
|
checked: '<?'
|
|
|
|
}
|
|
|
|
});
|