@@ -26,33 +26,24 @@ const NAME = 'loading-button'
26
26
const DATA_KEY = 'coreui.loading-button'
27
27
const EVENT_KEY = `.${ DATA_KEY } `
28
28
29
- const MAX_PERCENT = 100
30
- const MILLISECONDS = 10
31
- const PROGRESS_BAR_BG_COLOR_LIGHT = 'rgba(255, 255, 255, .2)'
32
- const PROGRESS_BAR_BG_COLOR_DARK = 'rgba(0, 0, 0, .2)'
33
-
34
29
const EVENT_START = `start${ EVENT_KEY } `
35
30
const EVENT_STOP = `stop${ EVENT_KEY } `
36
- const EVENT_COMPLETE = `complete${ EVENT_KEY } `
37
31
38
32
const CLASS_NAME_IS_LOADING = 'is-loading'
39
- const CLASS_NAME_LOADING_BUTTON_PROGRESS = 'btn-loading-progress'
40
33
const CLASS_NAME_LOADING_BUTTON_SPINNER = 'btn-loading-spinner'
41
34
42
35
const Default = {
43
- percent : 0 ,
44
- progress : false ,
36
+ disabledOnLoading : true ,
45
37
spinner : true ,
46
38
spinnerType : 'border' ,
47
- timeout : 1000
39
+ timeout : false
48
40
}
49
41
50
42
const DefaultType = {
51
- percent : 'number' ,
52
- progress : 'boolean' ,
43
+ disabledOnLoading : 'boolean' ,
53
44
spinner : 'boolean' ,
54
45
spinnerType : 'string' ,
55
- timeout : 'number'
46
+ timeout : '(boolean| number) '
56
47
}
57
48
58
49
/**
@@ -66,10 +57,7 @@ class LoadingButton extends BaseComponent {
66
57
super ( element )
67
58
68
59
this . _config = this . _getConfig ( config )
69
- this . _pause = false
70
- this . _percent = this . _config . percent
71
60
this . _timeout = this . _config . timeout
72
- this . _progressBar = null
73
61
this . _spinner = null
74
62
this . _state = 'idle'
75
63
@@ -101,30 +89,35 @@ class LoadingButton extends BaseComponent {
101
89
start ( ) {
102
90
if ( this . _state !== 'loading' ) {
103
91
this . _createSpinner ( )
104
- this . _createProgressBar ( )
105
92
106
93
setTimeout ( ( ) => {
107
94
this . _element . classList . add ( CLASS_NAME_IS_LOADING )
108
- this . _loading ( )
109
95
EventHandler . trigger ( this . _element , EVENT_START )
96
+
97
+ if ( this . _config . disabledOnLoading ) {
98
+ this . _element . setAttribute ( 'disabled' , true )
99
+ }
110
100
} , 1 )
101
+
102
+ if ( this . _config . timeout ) {
103
+ setTimeout ( ( ) => {
104
+ this . stop ( )
105
+ } , this . _config . timeout )
106
+ }
111
107
}
112
108
}
113
109
114
110
stop ( ) {
115
111
this . _element . classList . remove ( CLASS_NAME_IS_LOADING )
116
112
const stoped = ( ) => {
117
113
this . _removeSpinner ( )
118
- this . _removeProgressBar ( )
119
114
this . _state = 'idle'
120
115
121
- EventHandler . trigger ( this . _element , EVENT_STOP )
122
- if ( this . _percent >= 100 ) {
123
- EventHandler . trigger ( this . _element , EVENT_COMPLETE )
116
+ if ( this . _config . disabledOnLoading ) {
117
+ this . _element . removeAttribute ( 'disabled' )
124
118
}
125
119
126
- this . _percent = this . _config . percent
127
- this . _timeout = this . _config . timeout
120
+ EventHandler . trigger ( this . _element , EVENT_STOP )
128
121
}
129
122
130
123
if ( this . _spinner ) {
@@ -138,35 +131,11 @@ class LoadingButton extends BaseComponent {
138
131
stoped ( )
139
132
}
140
133
141
- pause ( ) {
142
- this . _pause = true
143
- this . _state = 'pause'
144
- }
145
-
146
- resume ( ) {
147
- this . _pause = false
148
- this . _loading ( )
149
- }
150
-
151
- complete ( ) {
152
- this . _timeout = 1000
153
- }
154
-
155
- updatePercent ( percent ) {
156
- const diff = ( this . _percent - percent ) / 100
157
- this . _timeout *= ( 1 + diff )
158
- this . _percent = percent
159
- }
160
-
161
134
dispose ( ) {
162
135
Data . removeData ( this . _element , DATA_KEY )
163
136
this . _element = null
164
137
}
165
138
166
- update ( config ) {
167
- this . _config = this . _getConfig ( config )
168
- }
169
-
170
139
_getConfig ( config ) {
171
140
config = {
172
141
...Default ,
@@ -178,40 +147,6 @@ class LoadingButton extends BaseComponent {
178
147
return config
179
148
}
180
149
181
- _loading ( ) {
182
- const progress = setInterval ( ( ) => {
183
- this . _state = 'loading'
184
- if ( this . _percent >= MAX_PERCENT ) {
185
- this . stop ( )
186
- clearInterval ( progress )
187
- return
188
- }
189
-
190
- if ( this . _pause ) {
191
- clearInterval ( progress )
192
- return
193
- }
194
-
195
- const frames = this . _timeout / ( MAX_PERCENT - this . _percent ) / MILLISECONDS
196
- this . _percent = Math . round ( ( this . _percent + ( 1 / frames ) ) * 100 ) / 100
197
- this . _timeout -= MILLISECONDS
198
- this . _animateProgressBar ( )
199
- } , MILLISECONDS )
200
- }
201
-
202
- _createProgressBar ( ) {
203
- if ( this . _config . progress ) {
204
- const progress = document . createElement ( 'div' )
205
- progress . classList . add ( CLASS_NAME_LOADING_BUTTON_PROGRESS )
206
- progress . setAttribute ( 'role' , 'progressbar' )
207
- progress . setAttribute ( 'aria-hidden' , 'true' )
208
- progress . style . backgroundColor = this . _progressBarBg ( )
209
-
210
- this . _element . insertBefore ( progress , this . _element . firstChild )
211
- this . _progressBar = progress
212
- }
213
- }
214
-
215
150
_createSpinner ( ) {
216
151
if ( this . _config . spinner ) {
217
152
const spinner = document . createElement ( 'span' )
@@ -224,46 +159,13 @@ class LoadingButton extends BaseComponent {
224
159
}
225
160
}
226
161
227
- _removeProgressBar ( ) {
228
- if ( this . _config . progress ) {
229
- this . _progressBar . remove ( )
230
- this . _progressBar = null
231
- }
232
- }
233
-
234
162
_removeSpinner ( ) {
235
163
if ( this . _config . spinner ) {
236
164
this . _spinner . remove ( )
237
165
this . _spinner = null
238
166
}
239
167
}
240
168
241
- _progressBarBg ( ) {
242
- // The yiq lightness value that determines when the lightness of color changes from "dark" to "light". Acceptable values are between 0 and 255.
243
- const yiqContrastedThreshold = 150
244
- const color = window . getComputedStyle ( this . _element ) . getPropertyValue ( 'background-color' ) === 'rgba(0, 0, 0, 0)' ? 'rgb(255, 255, 255)' : window . getComputedStyle ( this . _element ) . getPropertyValue ( 'background-color' )
245
-
246
- const rgb = color . match ( / ^ r g b ? [ \s + ] ? \( [ \s + ] ? ( \d + ) [ \s + ] ? , [ \s + ] ? ( \d + ) [ \s + ] ? , [ \s + ] ? ( \d + ) [ \s + ] ? / i)
247
-
248
- const r = Number . parseInt ( rgb [ 1 ] , 10 )
249
- const g = Number . parseInt ( rgb [ 2 ] , 10 )
250
- const b = Number . parseInt ( rgb [ 3 ] , 10 )
251
-
252
- const yiq = ( ( r * 299 ) + ( g * 587 ) + ( b * 114 ) ) / 1000
253
-
254
- if ( yiq > yiqContrastedThreshold ) {
255
- return PROGRESS_BAR_BG_COLOR_DARK
256
- }
257
-
258
- return PROGRESS_BAR_BG_COLOR_LIGHT
259
- }
260
-
261
- _animateProgressBar ( ) {
262
- if ( this . _config . progress ) {
263
- this . _progressBar . style . width = `${ this . _percent } %`
264
- }
265
- }
266
-
267
169
// Static
268
170
269
171
static loadingButtonInterface ( element , config ) {
0 commit comments