@@ -85,12 +85,6 @@ export class MdButtonToggleGroup implements AfterViewInit, ControlValueAccessor
85
85
/** onTouch function registered via registerOnTouch (ControlValueAccessor). */
86
86
onTouched : ( ) => any = ( ) => { } ;
87
87
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
-
94
88
/** Child button toggle buttons. */
95
89
@ContentChildren ( forwardRef ( ( ) => MdButtonToggle ) )
96
90
_buttonToggles : QueryList < MdButtonToggle > = null ;
@@ -165,6 +159,12 @@ export class MdButtonToggleGroup implements AfterViewInit, ControlValueAccessor
165
159
}
166
160
}
167
161
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
+
168
168
private _updateButtonToggleNames ( ) : void {
169
169
if ( this . _buttonToggles ) {
170
170
this . _buttonToggles . forEach ( ( toggle ) => {
@@ -291,15 +291,6 @@ export class MdButtonToggle implements OnInit {
291
291
/** Type of the button toggle. Either 'radio' or 'checkbox'. */
292
292
_type : ToggleType ;
293
293
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
-
303
294
/** Whether or not this button toggle is disabled. */
304
295
private _disabled : boolean = null ;
305
296
@@ -309,64 +300,28 @@ export class MdButtonToggle implements OnInit {
309
300
/** Whether or not the button toggle is a single selection. */
310
301
private _isSingleSelector : boolean = null ;
311
302
303
+ @ViewChild ( 'input' ) _inputElement : ElementRef ;
304
+
312
305
/** The parent button toggle group (exclusive selection). Optional. */
313
306
buttonToggleGroup : MdButtonToggleGroup ;
314
307
315
308
/** The parent button toggle group (multiple selection). Optional. */
316
309
buttonToggleGroupMultiple : MdButtonToggleGroupMultiple ;
317
310
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
-
365
311
/** Unique ID for the underlying `input` element. */
366
312
get inputId ( ) : string {
367
313
return `${ this . id } -input` ;
368
314
}
369
315
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
+
370
325
/** Whether the button is checked. */
371
326
@HostBinding ( 'class.mat-button-toggle-checked' )
372
327
@Input ( )
@@ -378,7 +333,7 @@ export class MdButtonToggle implements OnInit {
378
333
if ( this . _isSingleSelector ) {
379
334
if ( newCheckedState ) {
380
335
// 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 ) ;
382
337
}
383
338
}
384
339
@@ -404,14 +359,6 @@ export class MdButtonToggle implements OnInit {
404
359
}
405
360
}
406
361
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
-
415
362
/** Whether the button is disabled. */
416
363
@HostBinding ( 'class.mat-button-toggle-disabled' )
417
364
@Input ( )
@@ -424,6 +371,56 @@ export class MdButtonToggle implements OnInit {
424
371
this . _disabled = ( value != null && value !== false ) ? true : null ;
425
372
}
426
373
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
+
427
424
/** Toggle the state of the current button toggle. */
428
425
private _toggle ( ) : void {
429
426
this . checked = ! this . checked ;
@@ -458,8 +455,11 @@ export class MdButtonToggle implements OnInit {
458
455
event . stopPropagation ( ) ;
459
456
}
460
457
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 ) ;
464
464
}
465
465
}
0 commit comments