1- import { Component , ElementRef , Input , OnInit , Renderer2 , ViewEncapsulation } from '@angular/core' ;
1+ import {
2+ Component ,
3+ ElementRef ,
4+ Input ,
5+ OnInit ,
6+ Renderer2 ,
7+ ViewEncapsulation ,
8+ SimpleChanges ,
9+ OnChanges ,
10+ } from '@angular/core' ;
211
312@Component ( {
413 // tslint:disable-next-line:component-selector
@@ -8,7 +17,7 @@ import { Component, ElementRef, Input, OnInit, Renderer2, ViewEncapsulation } fr
817 encapsulation : ViewEncapsulation . None ,
918} )
1019// tslint:disable-next-line:component-class-suffix
11- export class MdbBtnDirective implements OnInit {
20+ export class MdbBtnDirective implements OnInit , OnChanges {
1221 @Input ( ) color = '' ;
1322 @Input ( ) rounded = false ;
1423 @Input ( ) gradient = '' ;
@@ -17,67 +26,148 @@ export class MdbBtnDirective implements OnInit {
1726 @Input ( ) size = '' ;
1827 @Input ( ) block = false ;
1928 @Input ( ) floating = false ;
29+
30+ public simpleChange : string ;
31+ public simpleChangeValue : string ;
32+
33+ private colorClass : string ;
34+ private gradientClass : string ;
35+ private outlineClass : string ;
36+ private flatClass : string ;
37+ private roundedClass : string ;
38+ private sizeClass : string ;
39+ private blockClass : string ;
40+ private floatingClass : string ;
41+
2042 constructor ( private el : ElementRef , private renderer : Renderer2 ) { }
2143
2244 ngOnInit ( ) {
23- const colorClass = 'btn-' + this . color ;
24- const gradientClass = this . gradient + '-gradient' ;
25- const outlineClass = 'btn-outline-' + this . color ;
26- const flatClass = 'btn-flat' ;
27- const roundedClass = 'btn-rounded' ;
28- const sizeClass = 'btn-' + this . size ;
29- const blockClass = 'btn-block' ;
30- const floatingClass = 'btn-floating' ;
31-
45+ this . colorClass = 'btn-' + this . color ;
46+ this . gradientClass = this . gradient + '-gradient' ;
47+ this . outlineClass = 'btn-outline-' + this . color ;
48+ this . flatClass = 'btn-flat' ;
49+ this . roundedClass = 'btn-rounded' ;
50+ this . sizeClass = 'btn-' + this . size ;
51+ this . blockClass = 'btn-block' ;
52+ this . floatingClass = 'btn-floating' ;
3253 this . renderer . addClass ( this . el . nativeElement , 'btn' ) ;
3354
55+ this . initClasses ( ) ;
56+ }
57+
58+ ngOnChanges ( changes : SimpleChanges ) {
59+ if ( changes . color ) {
60+ this . renderer . removeClass ( this . el . nativeElement , this . colorClass ) ;
61+ if ( this . color !== '' ) {
62+ this . colorClass = 'btn-' + this . color ;
63+ this . renderer . addClass ( this . el . nativeElement , this . colorClass ) ;
64+ }
65+ }
66+ if ( changes . gradient ) {
67+ this . renderer . removeClass ( this . el . nativeElement , this . gradientClass ) ;
68+ if ( this . gradient !== '' ) {
69+ this . gradientClass = this . gradient + '-gradient' ;
70+ this . renderer . addClass ( this . el . nativeElement , this . gradientClass ) ;
71+ }
72+ }
73+ if ( changes . outline ) {
74+ this . renderer . removeClass ( this . el . nativeElement , this . outlineClass ) ;
75+ if ( this . outline ) {
76+ this . outlineClass = 'btn-outline-' + this . color ;
77+ this . renderer . addClass ( this . el . nativeElement , this . outlineClass ) ;
78+ }
79+ }
80+ if ( changes . flat ) {
81+ this . renderer . removeClass ( this . el . nativeElement , this . flatClass ) ;
82+ if ( this . flat ) {
83+ this . flatClass = 'btn-flat' ;
84+ this . renderer . addClass ( this . el . nativeElement , this . flatClass ) ;
85+ }
86+ }
87+
88+ if ( changes . rounded ) {
89+ this . renderer . removeClass ( this . el . nativeElement , this . roundedClass ) ;
90+ if ( this . rounded ) {
91+ this . roundedClass = 'btn-rounded' ;
92+ this . renderer . addClass ( this . el . nativeElement , this . roundedClass ) ;
93+ }
94+ }
95+ if ( changes . size ) {
96+ this . renderer . removeClass ( this . el . nativeElement , this . sizeClass ) ;
97+ if ( this . size !== '' ) {
98+ this . sizeClass = 'btn-' + this . size ;
99+ this . renderer . addClass ( this . el . nativeElement , this . sizeClass ) ;
100+ }
101+ }
102+ if ( changes . block ) {
103+ this . renderer . removeClass ( this . el . nativeElement , this . blockClass ) ;
104+ if ( this . block ) {
105+ this . blockClass = 'btn-block' ;
106+ this . renderer . addClass ( this . el . nativeElement , this . blockClass ) ;
107+ }
108+ }
109+ if ( changes . floating ) {
110+ if ( ! this . floating ) {
111+ this . renderer . removeClass ( this . el . nativeElement , this . floatingClass ) ;
112+ this . renderer . addClass ( this . el . nativeElement , 'btn' ) ;
113+ }
114+
115+ if ( this . floating ) {
116+ this . floatingClass = 'btn-floating' ;
117+ this . renderer . addClass ( this . el . nativeElement , this . floatingClass ) ;
118+ this . renderer . removeClass ( this . el . nativeElement , 'btn' ) ;
119+ }
120+ }
121+ }
122+
123+ initClasses ( ) {
34124 if ( this . color !== '' ) {
35- this . renderer . addClass ( this . el . nativeElement , colorClass ) ;
125+ this . renderer . addClass ( this . el . nativeElement , this . colorClass ) ;
36126 }
37127
38128 if ( this . rounded ) {
39- this . renderer . addClass ( this . el . nativeElement , roundedClass ) ;
129+ this . renderer . addClass ( this . el . nativeElement , this . roundedClass ) ;
40130 }
41131
42132 if ( this . gradient ) {
43133 if ( this . color !== '' ) {
44- this . renderer . removeClass ( this . el . nativeElement , colorClass ) ;
134+ this . renderer . removeClass ( this . el . nativeElement , this . colorClass ) ;
45135 }
46- this . renderer . addClass ( this . el . nativeElement , gradientClass ) ;
136+ this . renderer . addClass ( this . el . nativeElement , this . gradientClass ) ;
47137 }
48138
49139 if ( this . outline ) {
50- this . renderer . removeClass ( this . el . nativeElement , colorClass ) ;
51- this . renderer . addClass ( this . el . nativeElement , outlineClass ) ;
140+ this . renderer . removeClass ( this . el . nativeElement , this . colorClass ) ;
141+ this . renderer . addClass ( this . el . nativeElement , this . outlineClass ) ;
52142 }
53143
54144 if ( this . flat ) {
55145 if ( this . color ) {
56- this . renderer . removeClass ( this . el . nativeElement , colorClass ) ;
146+ this . renderer . removeClass ( this . el . nativeElement , this . colorClass ) ;
57147 }
58148 if ( this . gradient ) {
59- this . renderer . removeClass ( this . el . nativeElement , gradientClass ) ;
149+ this . renderer . removeClass ( this . el . nativeElement , this . gradientClass ) ;
60150 }
61151 if ( this . outline ) {
62- this . renderer . removeClass ( this . el . nativeElement , outlineClass ) ;
152+ this . renderer . removeClass ( this . el . nativeElement , this . outlineClass ) ;
63153 }
64154 if ( this . rounded ) {
65- this . renderer . removeClass ( this . el . nativeElement , roundedClass ) ;
155+ this . renderer . removeClass ( this . el . nativeElement , this . roundedClass ) ;
66156 }
67- this . renderer . addClass ( this . el . nativeElement , flatClass ) ;
157+ this . renderer . addClass ( this . el . nativeElement , this . flatClass ) ;
68158 }
69159
70160 if ( this . size ) {
71- this . renderer . addClass ( this . el . nativeElement , sizeClass ) ;
161+ this . renderer . addClass ( this . el . nativeElement , this . sizeClass ) ;
72162 }
73163
74164 if ( this . block ) {
75- this . renderer . addClass ( this . el . nativeElement , blockClass ) ;
165+ this . renderer . addClass ( this . el . nativeElement , this . blockClass ) ;
76166 }
77167
78168 if ( this . floating ) {
169+ this . renderer . addClass ( this . el . nativeElement , this . floatingClass ) ;
79170 this . renderer . removeClass ( this . el . nativeElement , 'btn' ) ;
80- this . renderer . addClass ( this . el . nativeElement , floatingClass ) ;
81171 }
82172 }
83173}
0 commit comments