-
Notifications
You must be signed in to change notification settings - Fork 23
/
aubs-btn-radio.js
53 lines (41 loc) · 1.27 KB
/
aubs-btn-radio.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import {inject, bindable, bindingMode} from "aurelia-framework";
@inject(Element)
export class AubsBtnRadioCustomAttribute {
@bindable({defaultBindingMode: bindingMode.twoWay}) model;
@bindable value;
constructor(element) {
this.element = element;
if(this.element.tagName !== 'BUTTON' && this.element.tagName !== 'A'){
throw new Error("The aubs-btn-radio attribute can only be used in button and anchor elements");
}
this.clickedListener = () => this.buttonClicked();
}
bind() {
if(this.value == null || this.value == undefined){
throw new Error('Must provide a value for the radio button');
}
}
attached() {
this.element.addEventListener('click', this.clickedListener);
this.setClass();
}
detached(){
this.element.removeEventListener('click', this.clickedListener);
}
modelChanged() {
this.setClass();
}
buttonClicked() {
if(this.model == this.value) {
return;
}
this.model = this.value;
}
setClass() {
if (this.model == this.value) {
this.element.classList.add('active');
} else {
this.element.classList.remove('active');
}
}
}