@@ -83,12 +83,6 @@ export class MdButtonToggleGroup implements AfterViewInit, ControlValueAccessor
83
83
/** onTouch function registered via registerOnTouch (ControlValueAccessor). */
84
84
onTouched : ( ) => any = ( ) => { } ;
85
85
86
- /** Event emitted when the group's value changes. */
87
- private _change : EventEmitter < MdButtonToggleChange > = new EventEmitter < MdButtonToggleChange > ( ) ;
88
- @Output ( ) get change ( ) : Observable < MdButtonToggleChange > {
89
- return this . _change . asObservable ( ) ;
90
- }
91
-
92
86
/** Child button toggle buttons. */
93
87
@ContentChildren ( forwardRef ( ( ) => MdButtonToggle ) )
94
88
_buttonToggles : QueryList < MdButtonToggle > = null ;
@@ -163,6 +157,12 @@ export class MdButtonToggleGroup implements AfterViewInit, ControlValueAccessor
163
157
}
164
158
}
165
159
160
+ /** Event emitted when the group's value changes. */
161
+ @Output ( ) get change ( ) : Observable < MdButtonToggleChange > {
162
+ return this . _change . asObservable ( ) ;
163
+ }
164
+ private _change : EventEmitter < MdButtonToggleChange > = new EventEmitter < MdButtonToggleChange > ( ) ;
165
+
166
166
private _updateButtonToggleNames ( ) : void {
167
167
if ( this . _buttonToggles ) {
168
168
this . _buttonToggles . forEach ( ( toggle ) => {
@@ -289,15 +289,6 @@ export class MdButtonToggle implements OnInit {
289
289
/** Type of the button toggle. Either 'radio' or 'checkbox'. */
290
290
_type : ToggleType ;
291
291
292
- /** The unique ID for this button toggle. */
293
- @HostBinding ( )
294
- @Input ( )
295
- id : string ;
296
-
297
- /** HTML's 'name' attribute used to group radios for unique selection. */
298
- @Input ( )
299
- name : string ;
300
-
301
292
/** Whether or not this button toggle is disabled. */
302
293
private _disabled : boolean = null ;
303
294
@@ -307,64 +298,28 @@ export class MdButtonToggle implements OnInit {
307
298
/** Whether or not the button toggle is a single selection. */
308
299
private _isSingleSelector : boolean = null ;
309
300
301
+ @ViewChild ( 'input' ) _inputElement : ElementRef ;
302
+
310
303
/** The parent button toggle group (exclusive selection). Optional. */
311
304
buttonToggleGroup : MdButtonToggleGroup ;
312
305
313
306
/** The parent button toggle group (multiple selection). Optional. */
314
307
buttonToggleGroupMultiple : MdButtonToggleGroupMultiple ;
315
308
316
- /** Event emitted when the group value changes. */
317
- private _change : EventEmitter < MdButtonToggleChange > = new EventEmitter < MdButtonToggleChange > ( ) ;
318
- @Output ( ) get change ( ) : Observable < MdButtonToggleChange > {
319
- return this . _change . asObservable ( ) ;
320
- }
321
-
322
- @ViewChild ( 'input' ) _inputElement : ElementRef ;
323
-
324
- constructor ( @Optional ( ) toggleGroup : MdButtonToggleGroup ,
325
- @Optional ( ) toggleGroupMultiple : MdButtonToggleGroupMultiple ,
326
- public buttonToggleDispatcher : UniqueSelectionDispatcher ,
327
- private _renderer : Renderer ,
328
- private _elementRef : ElementRef ,
329
- private _focusOriginMonitor : FocusOriginMonitor ) {
330
- this . buttonToggleGroup = toggleGroup ;
331
-
332
- this . buttonToggleGroupMultiple = toggleGroupMultiple ;
333
-
334
- if ( this . buttonToggleGroup ) {
335
- buttonToggleDispatcher . listen ( ( id : string , name : string ) => {
336
- if ( id != this . id && name == this . name ) {
337
- this . checked = false ;
338
- }
339
- } ) ;
340
-
341
- this . _type = 'radio' ;
342
- this . name = this . buttonToggleGroup . name ;
343
- this . _isSingleSelector = true ;
344
- } else {
345
- // Even if there is no group at all, treat the button toggle as a checkbox so it can be
346
- // toggled on or off.
347
- this . _type = 'checkbox' ;
348
- this . _isSingleSelector = false ;
349
- }
350
- }
351
-
352
- ngOnInit ( ) {
353
- if ( this . id == null ) {
354
- this . id = `md-button-toggle-${ _uniqueIdCounter ++ } ` ;
355
- }
356
-
357
- if ( this . buttonToggleGroup && this . _value == this . buttonToggleGroup . value ) {
358
- this . _checked = true ;
359
- }
360
- this . _focusOriginMonitor . monitor ( this . _elementRef . nativeElement , this . _renderer , true ) ;
361
- }
362
-
363
309
/** Unique ID for the underlying `input` element. */
364
310
get inputId ( ) : string {
365
311
return `${ this . id } -input` ;
366
312
}
367
313
314
+ /** The unique ID for this button toggle. */
315
+ @HostBinding ( )
316
+ @Input ( )
317
+ id : string ;
318
+
319
+ /** HTML's 'name' attribute used to group radios for unique selection. */
320
+ @Input ( )
321
+ name : string ;
322
+
368
323
/** Whether the button is checked. */
369
324
@HostBinding ( 'class.mat-button-toggle-checked' )
370
325
@Input ( )
@@ -376,7 +331,7 @@ export class MdButtonToggle implements OnInit {
376
331
if ( this . _isSingleSelector ) {
377
332
if ( newCheckedState ) {
378
333
// Notify all button toggles with the same name (in the same group) to un-check.
379
- this . buttonToggleDispatcher . notify ( this . id , this . name ) ;
334
+ this . _buttonToggleDispatcher . notify ( this . id , this . name ) ;
380
335
}
381
336
}
382
337
@@ -402,14 +357,6 @@ export class MdButtonToggle implements OnInit {
402
357
}
403
358
}
404
359
405
- /** Dispatch change event with current value. */
406
- private _emitChangeEvent ( ) : void {
407
- let event = new MdButtonToggleChange ( ) ;
408
- event . source = this ;
409
- event . value = this . _value ;
410
- this . _change . emit ( event ) ;
411
- }
412
-
413
360
/** Whether the button is disabled. */
414
361
@HostBinding ( 'class.mat-button-toggle-disabled' )
415
362
@Input ( )
@@ -422,6 +369,56 @@ export class MdButtonToggle implements OnInit {
422
369
this . _disabled = ( value != null && value !== false ) ? true : null ;
423
370
}
424
371
372
+ /** Event emitted when the group value changes. */
373
+ private _change : EventEmitter < MdButtonToggleChange > = new EventEmitter < MdButtonToggleChange > ( ) ;
374
+ @Output ( ) get change ( ) : Observable < MdButtonToggleChange > {
375
+ return this . _change . asObservable ( ) ;
376
+ }
377
+
378
+ constructor ( @Optional ( ) toggleGroup : MdButtonToggleGroup ,
379
+ @Optional ( ) toggleGroupMultiple : MdButtonToggleGroupMultiple ,
380
+ private _buttonToggleDispatcher : UniqueSelectionDispatcher ,
381
+ private _renderer : Renderer ,
382
+ private _elementRef : ElementRef ,
383
+ private _focusOriginMonitor : FocusOriginMonitor ) {
384
+ this . buttonToggleGroup = toggleGroup ;
385
+
386
+ this . buttonToggleGroupMultiple = toggleGroupMultiple ;
387
+
388
+ if ( this . buttonToggleGroup ) {
389
+ _buttonToggleDispatcher . listen ( ( id : string , name : string ) => {
390
+ if ( id != this . id && name == this . name ) {
391
+ this . checked = false ;
392
+ }
393
+ } ) ;
394
+
395
+ this . _type = 'radio' ;
396
+ this . name = this . buttonToggleGroup . name ;
397
+ this . _isSingleSelector = true ;
398
+ } else {
399
+ // Even if there is no group at all, treat the button toggle as a checkbox so it can be
400
+ // toggled on or off.
401
+ this . _type = 'checkbox' ;
402
+ this . _isSingleSelector = false ;
403
+ }
404
+ }
405
+
406
+ ngOnInit ( ) {
407
+ if ( this . id == null ) {
408
+ this . id = `md-button-toggle-${ _uniqueIdCounter ++ } ` ;
409
+ }
410
+
411
+ if ( this . buttonToggleGroup && this . _value == this . buttonToggleGroup . value ) {
412
+ this . _checked = true ;
413
+ }
414
+ this . _focusOriginMonitor . monitor ( this . _elementRef . nativeElement , this . _renderer , true ) ;
415
+ }
416
+
417
+ /** Focuses the button. */
418
+ focus ( ) {
419
+ this . _renderer . invokeElementMethod ( this . _inputElement . nativeElement , 'focus' ) ;
420
+ }
421
+
425
422
/** Toggle the state of the current button toggle. */
426
423
private _toggle ( ) : void {
427
424
this . checked = ! this . checked ;
@@ -456,8 +453,11 @@ export class MdButtonToggle implements OnInit {
456
453
event . stopPropagation ( ) ;
457
454
}
458
455
459
- /** Focuses the button. */
460
- focus ( ) {
461
- this . _renderer . invokeElementMethod ( this . _inputElement . nativeElement , 'focus' ) ;
456
+ /** Dispatch change event with current value. */
457
+ private _emitChangeEvent ( ) : void {
458
+ let event = new MdButtonToggleChange ( ) ;
459
+ event . source = this ;
460
+ event . value = this . _value ;
461
+ this . _change . emit ( event ) ;
462
462
}
463
463
}
0 commit comments