Skip to content

Commit ce0e933

Browse files
authored
clean up button toggle docs (#3521)
1 parent d173eb1 commit ce0e933

File tree

1 file changed

+74
-74
lines changed

1 file changed

+74
-74
lines changed

src/lib/button-toggle/button-toggle.ts

Lines changed: 74 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,6 @@ export class MdButtonToggleGroup implements AfterViewInit, ControlValueAccessor
8585
/** onTouch function registered via registerOnTouch (ControlValueAccessor). */
8686
onTouched: () => any = () => {};
8787

88-
/** Event emitted when the group's value changes. */
89-
private _change: EventEmitter<MdButtonToggleChange> = new EventEmitter<MdButtonToggleChange>();
90-
@Output() get change(): Observable<MdButtonToggleChange> {
91-
return this._change.asObservable();
92-
}
93-
9488
/** Child button toggle buttons. */
9589
@ContentChildren(forwardRef(() => MdButtonToggle))
9690
_buttonToggles: QueryList<MdButtonToggle> = null;
@@ -165,6 +159,12 @@ export class MdButtonToggleGroup implements AfterViewInit, ControlValueAccessor
165159
}
166160
}
167161

162+
/** Event emitted when the group's value changes. */
163+
@Output() get change(): Observable<MdButtonToggleChange> {
164+
return this._change.asObservable();
165+
}
166+
private _change: EventEmitter<MdButtonToggleChange> = new EventEmitter<MdButtonToggleChange>();
167+
168168
private _updateButtonToggleNames(): void {
169169
if (this._buttonToggles) {
170170
this._buttonToggles.forEach((toggle) => {
@@ -291,15 +291,6 @@ export class MdButtonToggle implements OnInit {
291291
/** Type of the button toggle. Either 'radio' or 'checkbox'. */
292292
_type: ToggleType;
293293

294-
/** The unique ID for this button toggle. */
295-
@HostBinding()
296-
@Input()
297-
id: string;
298-
299-
/** HTML's 'name' attribute used to group radios for unique selection. */
300-
@Input()
301-
name: string;
302-
303294
/** Whether or not this button toggle is disabled. */
304295
private _disabled: boolean = null;
305296

@@ -309,64 +300,28 @@ export class MdButtonToggle implements OnInit {
309300
/** Whether or not the button toggle is a single selection. */
310301
private _isSingleSelector: boolean = null;
311302

303+
@ViewChild('input') _inputElement: ElementRef;
304+
312305
/** The parent button toggle group (exclusive selection). Optional. */
313306
buttonToggleGroup: MdButtonToggleGroup;
314307

315308
/** The parent button toggle group (multiple selection). Optional. */
316309
buttonToggleGroupMultiple: MdButtonToggleGroupMultiple;
317310

318-
/** Event emitted when the group value changes. */
319-
private _change: EventEmitter<MdButtonToggleChange> = new EventEmitter<MdButtonToggleChange>();
320-
@Output() get change(): Observable<MdButtonToggleChange> {
321-
return this._change.asObservable();
322-
}
323-
324-
@ViewChild('input') _inputElement: ElementRef;
325-
326-
constructor(@Optional() toggleGroup: MdButtonToggleGroup,
327-
@Optional() toggleGroupMultiple: MdButtonToggleGroupMultiple,
328-
public buttonToggleDispatcher: UniqueSelectionDispatcher,
329-
private _renderer: Renderer,
330-
private _elementRef: ElementRef,
331-
private _focusOriginMonitor: FocusOriginMonitor) {
332-
this.buttonToggleGroup = toggleGroup;
333-
334-
this.buttonToggleGroupMultiple = toggleGroupMultiple;
335-
336-
if (this.buttonToggleGroup) {
337-
buttonToggleDispatcher.listen((id: string, name: string) => {
338-
if (id != this.id && name == this.name) {
339-
this.checked = false;
340-
}
341-
});
342-
343-
this._type = 'radio';
344-
this.name = this.buttonToggleGroup.name;
345-
this._isSingleSelector = true;
346-
} else {
347-
// Even if there is no group at all, treat the button toggle as a checkbox so it can be
348-
// toggled on or off.
349-
this._type = 'checkbox';
350-
this._isSingleSelector = false;
351-
}
352-
}
353-
354-
ngOnInit() {
355-
if (this.id == null) {
356-
this.id = `md-button-toggle-${_uniqueIdCounter++}`;
357-
}
358-
359-
if (this.buttonToggleGroup && this._value == this.buttonToggleGroup.value) {
360-
this._checked = true;
361-
}
362-
this._focusOriginMonitor.monitor(this._elementRef.nativeElement, this._renderer, true);
363-
}
364-
365311
/** Unique ID for the underlying `input` element. */
366312
get inputId(): string {
367313
return `${this.id}-input`;
368314
}
369315

316+
/** The unique ID for this button toggle. */
317+
@HostBinding()
318+
@Input()
319+
id: string;
320+
321+
/** HTML's 'name' attribute used to group radios for unique selection. */
322+
@Input()
323+
name: string;
324+
370325
/** Whether the button is checked. */
371326
@HostBinding('class.mat-button-toggle-checked')
372327
@Input()
@@ -378,7 +333,7 @@ export class MdButtonToggle implements OnInit {
378333
if (this._isSingleSelector) {
379334
if (newCheckedState) {
380335
// Notify all button toggles with the same name (in the same group) to un-check.
381-
this.buttonToggleDispatcher.notify(this.id, this.name);
336+
this._buttonToggleDispatcher.notify(this.id, this.name);
382337
}
383338
}
384339

@@ -404,14 +359,6 @@ export class MdButtonToggle implements OnInit {
404359
}
405360
}
406361

407-
/** Dispatch change event with current value. */
408-
private _emitChangeEvent(): void {
409-
let event = new MdButtonToggleChange();
410-
event.source = this;
411-
event.value = this._value;
412-
this._change.emit(event);
413-
}
414-
415362
/** Whether the button is disabled. */
416363
@HostBinding('class.mat-button-toggle-disabled')
417364
@Input()
@@ -424,6 +371,56 @@ export class MdButtonToggle implements OnInit {
424371
this._disabled = (value != null && value !== false) ? true : null;
425372
}
426373

374+
/** Event emitted when the group value changes. */
375+
private _change: EventEmitter<MdButtonToggleChange> = new EventEmitter<MdButtonToggleChange>();
376+
@Output() get change(): Observable<MdButtonToggleChange> {
377+
return this._change.asObservable();
378+
}
379+
380+
constructor(@Optional() toggleGroup: MdButtonToggleGroup,
381+
@Optional() toggleGroupMultiple: MdButtonToggleGroupMultiple,
382+
private _buttonToggleDispatcher: UniqueSelectionDispatcher,
383+
private _renderer: Renderer,
384+
private _elementRef: ElementRef,
385+
private _focusOriginMonitor: FocusOriginMonitor) {
386+
this.buttonToggleGroup = toggleGroup;
387+
388+
this.buttonToggleGroupMultiple = toggleGroupMultiple;
389+
390+
if (this.buttonToggleGroup) {
391+
_buttonToggleDispatcher.listen((id: string, name: string) => {
392+
if (id != this.id && name == this.name) {
393+
this.checked = false;
394+
}
395+
});
396+
397+
this._type = 'radio';
398+
this.name = this.buttonToggleGroup.name;
399+
this._isSingleSelector = true;
400+
} else {
401+
// Even if there is no group at all, treat the button toggle as a checkbox so it can be
402+
// toggled on or off.
403+
this._type = 'checkbox';
404+
this._isSingleSelector = false;
405+
}
406+
}
407+
408+
ngOnInit() {
409+
if (this.id == null) {
410+
this.id = `md-button-toggle-${_uniqueIdCounter++}`;
411+
}
412+
413+
if (this.buttonToggleGroup && this._value == this.buttonToggleGroup.value) {
414+
this._checked = true;
415+
}
416+
this._focusOriginMonitor.monitor(this._elementRef.nativeElement, this._renderer, true);
417+
}
418+
419+
/** Focuses the button. */
420+
focus() {
421+
this._renderer.invokeElementMethod(this._inputElement.nativeElement, 'focus');
422+
}
423+
427424
/** Toggle the state of the current button toggle. */
428425
private _toggle(): void {
429426
this.checked = !this.checked;
@@ -458,8 +455,11 @@ export class MdButtonToggle implements OnInit {
458455
event.stopPropagation();
459456
}
460457

461-
/** Focuses the button. */
462-
focus() {
463-
this._renderer.invokeElementMethod(this._inputElement.nativeElement, 'focus');
458+
/** Dispatch change event with current value. */
459+
private _emitChangeEvent(): void {
460+
let event = new MdButtonToggleChange();
461+
event.source = this;
462+
event.value = this._value;
463+
this._change.emit(event);
464464
}
465465
}

0 commit comments

Comments
 (0)