bootstrap-switcher.component.ts
1.65 KB
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
import {Component, Input, Output, EventEmitter} from 'ng-forward';
export interface BootstrapSwitcherItem {
value: any;
label: string;
}
@Component({
selector: 'noosfero-bootstrap-switcher',
template: `
<span class="switcher-label" ng-bind="ctrl.label | translate"></span>
<div class="btn-group switcher">
<button ng-repeat="option in ctrl.options track by $index"
(click)="ctrl.switcherClick(option)"
ng-class="ctrl.getCssClassForItem(option)"
class="btn btn-xs" ng-bind="option.label | translate">
</button>
</div>
`,
inputs: ['activeClass', 'defaultClass', 'label', 'options', 'defaultOption'],
outputs: ['onSwitch']
})
export class BootstrapSwitcherComponent {
@Input() activeClass: string = 'active btn-danger';
@Input() defaultClass: string = 'btn-default';
@Input() label: string;
@Input() options: BootstrapSwitcherItem[];
@Input() defaultOption: BootstrapSwitcherItem;
@Output() onSwitch: EventEmitter<BootstrapSwitcherItem> = new EventEmitter<BootstrapSwitcherItem>();
selectedOption: BootstrapSwitcherItem = null;
constructor() { }
ngOnInit() {
this.selectedOption = this.defaultOption;
}
isSelectedOption(value: BootstrapSwitcherItem): boolean {
return this.selectedOption === value;
}
getCssClassForItem(value: BootstrapSwitcherItem): string {
return this.isSelectedOption(value) ? this.activeClass : this.defaultClass;
}
switcherClick(value: BootstrapSwitcherItem) {
this.selectedOption = value;
this.onSwitch.next(value);
}
}