<!DOCTYPE html>
<html>
<head>
<base href="." />
<title>ng-bootstrap demo</title>
<link rel="stylesheet" href="http://v4-alpha.getbootstrap.com/dist/css/bootstrap.min.css" />
<script src="https://unpkg.com/zone.js@^0.6.23/dist/zone.js"></script>
<script src="https://unpkg.com/zone.js@^0.6.23/dist/long-stack-trace-zone.js"></script>
<script src="https://unpkg.com/reflect-metadata@^0.1.8/Reflect.js"></script>
<script src="https://unpkg.com/systemjs@^0.19.40/dist/system.js"></script>
<script src="config.js"></script>
<script>
System.import('app').catch(console.error.bind(console));
</script>
</head>
<body>
<my-app>loading...</my-app>
</body>
</html>
System.config({
//use typescript for compilation
transpiler: 'typescript',
//typescript compiler options
typescriptOptions: {
emitDecoratorMetadata: true
},
paths: {
'npm:': 'https://unpkg.com/'
},
map: {
'app': './src',
'@angular/core': 'npm:@angular/core@2.0.0/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common@2.0.0/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler@2.0.0/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser@2.0.0/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic@2.0.0/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http@2.0.0/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router@3.0.0/bundles/router.umd.js',
'@angular/forms': 'npm:@angular/forms@2.0.0/bundles/forms.umd.js',
'@angular/core/testing': 'npm:@angular/core@2.0.0/bundles/core-testing.umd.js',
'@angular/common/testing': 'npm:@angular/common@2.0.0/bundles/common-testing.umd.js',
'@angular/compiler/testing': 'npm:@angular/compiler@2.0.0/bundles/compiler-testing.umd.js',
'@angular/platform-browser/testing': 'npm:@angular/platform-browser@2.0.0/bundles/platform-browser-testing.umd.js',
'@angular/platform-browser-dynamic/testing': 'npm:@angular/platform-browser-dynamic@2.0.0/bundles/platform-browser-dynamic-testing.umd.js',
'@angular/http/testing': 'npm:@angular/http@2.0.0/bundles/http-testing.umd.js',
'@angular/router/testing': 'npm:@angular/router@3.0.0/bundles/router-testing.umd.js',
'rxjs': 'npm:rxjs@5.0.0-beta.12',
'typescript': 'npm:typescript@^2.0.6/lib/typescript.js',
'@ng-bootstrap/ng-bootstrap': 'npm:@ng-bootstrap/ng-bootstrap@1.0.0-alpha.22/bundles/ng-bootstrap.js'
},
packages: {
app: {
main: './main.ts',
defaultExtension: 'ts'
},
rxjs: {
defaultExtension: 'js'
}
}
});
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {AppModule} from './app';
platformBrowserDynamic().bootstrapModule(AppModule);
import { Component, NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { JsonpModule } from '@angular/http';
import { NgbButtonsModule } from './ngb-radio-module';
import { NgbdButtonsRadio } from './buttons-radio';
@Component({
selector: 'my-app',
template: `
<div class="container-fluid">
<ngbd-buttons-radio></ngbd-buttons-radio>
</div>
`
})
export class App {
}
@NgModule({
imports: [
BrowserModule,
FormsModule,
ReactiveFormsModule,
JsonpModule,
NgbButtonsModule.forRoot()
],
declarations: [App, NgbdButtonsRadio]
bootstrap: [App]
})
export class AppModule {}
import {Component} from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
@Component({
selector: 'ngbd-buttons-radio',
templateUrl: 'src/buttons-radio.html'
})
export class NgbdButtonsRadio {
testForm = new FormGroup({
choice: new FormControl(0)
});
}
<form [formGroup]="testForm">
<input type="radio" formControlName="choice" [value]="0"> Left
<input type="radio" formControlName="choice" [value]="1"> Middle
<input type="radio" formControlName="choice" [value]="2"> Right
<br>
<br>
<br>
<div ngbRadioGroup formControlName="choice">
<label class="btn btn-primary">
<input type="radio" name="foo" [value]="0"> Left
</label>
<label class="btn btn-primary">
<input type="radio" name="foo" [value]="1"> Middle
</label>
<label class="btn btn-primary">
<input type="radio" name="foo" [value]="2"> Right
</label>
</div>
</form>
import {Directive, forwardRef, Optional, Input, Renderer, ElementRef, OnDestroy} from '@angular/core';
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
export const NGB_RADIO_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => NgbRadioGroup),
multi: true
};
/**
* Easily create Bootstrap-style radio buttons. A value of a selected button is bound to a variable
* specified via ngModel.
*/
@Directive({
selector: '[ngbRadioGroup]',
host: {'data-toggle': 'buttons', 'class': 'btn-group'},
providers: [NGB_RADIO_VALUE_ACCESSOR]
})
export class NgbRadioGroup implements ControlValueAccessor {
private _radios: Set<NgbRadio> = new Set<NgbRadio>();
private _value = null;
private _disabled: boolean;
get disabled() { return this._disabled; }
set disabled(isDisabled: boolean) { this.setDisabledState(isDisabled); }
onChange = (_: any) => {};
onTouched = () => {};
onRadioChange(radio: NgbRadio) {
this.writeValue(radio.value);
this.onChange(radio.value);
}
onRadioValueUpdate() { this._updateRadiosValue(); }
register(radio: NgbRadio) { this._radios.add(radio); }
registerOnChange(fn: (value: any) => any): void { this.onChange = fn; }
registerOnTouched(fn: () => any): void { this.onTouched = fn; }
setDisabledState(isDisabled: boolean): void {
this._disabled = isDisabled;
this._updateRadiosDisabled();
}
unregister(radio: NgbRadio) { this._radios.delete(radio); }
writeValue(value) {
this._value = value;
this._updateRadiosValue();
}
private _updateRadiosValue() { this._radios.forEach((radio) => radio.updateValue(this._value)); }
private _updateRadiosDisabled() { this._radios.forEach((radio) => radio.updateDisabled()); }
}
@Directive({selector: 'label.btn'})
export class NgbActiveLabel {
constructor(private _renderer: Renderer, private _elRef: ElementRef) {}
set active(isActive: boolean) { this._renderer.setElementClass(this._elRef.nativeElement, 'active', isActive); }
set disabled(isDisabled: boolean) {
this._renderer.setElementClass(this._elRef.nativeElement, 'disabled', isDisabled);
}
set focused(isFocused: boolean) { this._renderer.setElementClass(this._elRef.nativeElement, 'focus', isFocused); }
}
/**
* Marks an input of type "radio" as part of the NgbRadioGroup.
*/
@Directive({
selector: 'input[type=radio]',
host: {
'[checked]': 'checked',
'[disabled]': 'disabled',
'(change)': 'onChange()',
'(focus)': 'focused = true',
'(blur)': 'focused = false'
}
})
export class NgbRadio implements OnDestroy {
private _checked: boolean;
private _disabled: boolean;
private _value: any = null;
/**
* You can specify model value of a given radio by binding to the value property.
*/
@Input('value')
set value(value: any) {
this._value = value;
const stringValue = value ? value.toString() : '';
this._renderer.setElementProperty(this._element.nativeElement, 'value', stringValue);
if (this._group) {
this._group.onRadioValueUpdate();
}
}
@Input('checked')
set checked(value: any) {
this._checked = this._element.nativeElement.hasAttribute('checked') ? true : value;
}
@Input('disabled')
set disabled(isDisabled: any) {
this._disabled = isDisabled !== false;
this.updateDisabled();
}
set focused(isFocused: boolean) {
if (this._label) {
this._label.focused = isFocused;
}
}
get value() { return this._value; }
get checked() { return this._checked; }
get disabled() { return (this._group && this._group.disabled) || this._disabled; }
constructor(
@Optional() private _group: NgbRadioGroup, @Optional() private _label: NgbActiveLabel,
private _renderer: Renderer, private _element: ElementRef) {
if (this._group) {
this._group.register(this);
}
}
ngOnDestroy() {
if (this._group) {
this._group.unregister(this);
}
}
onChange() {
if (this._group) {
this._group.onRadioChange(this);
}
}
updateValue(value) {
this._checked = (this.value === value && value !== null);
this._label.active = this._checked;
}
updateDisabled() {
let disabled = (this._group && this._group.disabled) || this._disabled;
if (this._label) {
this._label.disabled = disabled;
}
}
}
import {NgModule, ModuleWithProviders} from '@angular/core';
import {NgbRadio, NgbActiveLabel, NgbRadioGroup, NGB_RADIO_VALUE_ACCESSOR} from './ngb-radio';
export {NgbRadio, NgbActiveLabel, NgbRadioGroup} from './ngb-radio';
const NGB_RADIO_DIRECTIVES = [NgbRadio, NgbActiveLabel, NgbRadioGroup];
@NgModule({declarations: NGB_RADIO_DIRECTIVES, exports: NGB_RADIO_DIRECTIVES})
export class NgbButtonsModule {
static forRoot(): ModuleWithProviders { return {ngModule: NgbButtonsModule, providers: [NGB_RADIO_VALUE_ACCESSOR]}; }
}