salix/front/core/components/button/index.js

49 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

2019-10-18 19:36:30 +00:00
import ngModule from '../../module';
import FormInput from '../form-input';
import './style.scss';
export default class Button extends FormInput {
constructor($element, $scope) {
super($element, $scope);
this.design = 'colored';
2019-10-24 08:17:32 +00:00
this.initTabIndex();
2019-10-18 19:36:30 +00:00
this.element.addEventListener('keyup', e => this.onKeyup(e));
this.element.addEventListener('click', e => this.onClick(e));
2020-02-06 06:41:51 +00:00
this.button = this.element.querySelector('button');
2019-10-18 19:36:30 +00:00
}
$onInit() {
this.element.classList.add(this.design);
if (!this.type) this.type = 'button';
}
onKeyup(event) {
2019-10-24 08:17:32 +00:00
if (event.defaultPrevented) return;
switch (event.key) {
case ' ':
2019-10-24 08:17:32 +00:00
case 'Enter':
2020-02-06 06:41:51 +00:00
if (this.button)
return this.button.click();
2019-10-24 08:17:32 +00:00
return this.element.click();
}
2019-10-18 19:36:30 +00:00
}
onClick(event) {
if (this.disabled) {
event.preventDefault();
2019-10-24 08:17:32 +00:00
event.stopImmediatePropagation();
}
2019-10-18 19:36:30 +00:00
}
}
Button.$inject = ['$element', '$scope'];
ngModule.vnComponent('vnButton', {
controller: Button,
template: require('./index.html'),
bindings: {
icon: '@?',
type: '@?'
}
});