@@ -26,33 +26,24 @@ const NAME = 'loading-button'
2626const DATA_KEY = 'coreui.loading-button'
2727const EVENT_KEY = `.${ DATA_KEY } `
2828
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-
3429const EVENT_START = `start${ EVENT_KEY } `
3530const EVENT_STOP = `stop${ EVENT_KEY } `
36- const EVENT_COMPLETE = `complete${ EVENT_KEY } `
3731
3832const CLASS_NAME_IS_LOADING = 'is-loading'
39- const CLASS_NAME_LOADING_BUTTON_PROGRESS = 'btn-loading-progress'
4033const CLASS_NAME_LOADING_BUTTON_SPINNER = 'btn-loading-spinner'
4134
4235const Default = {
43- percent : 0 ,
44- progress : false ,
36+ disabledOnLoading : true ,
4537 spinner : true ,
4638 spinnerType : 'border' ,
47- timeout : 1000
39+ timeout : false
4840}
4941
5042const DefaultType = {
51- percent : 'number' ,
52- progress : 'boolean' ,
43+ disabledOnLoading : 'boolean' ,
5344 spinner : 'boolean' ,
5445 spinnerType : 'string' ,
55- timeout : 'number'
46+ timeout : '(boolean| number) '
5647}
5748
5849/**
@@ -66,10 +57,7 @@ class LoadingButton extends BaseComponent {
6657 super ( element )
6758
6859 this . _config = this . _getConfig ( config )
69- this . _pause = false
70- this . _percent = this . _config . percent
7160 this . _timeout = this . _config . timeout
72- this . _progressBar = null
7361 this . _spinner = null
7462 this . _state = 'idle'
7563
@@ -101,30 +89,35 @@ class LoadingButton extends BaseComponent {
10189 start ( ) {
10290 if ( this . _state !== 'loading' ) {
10391 this . _createSpinner ( )
104- this . _createProgressBar ( )
10592
10693 setTimeout ( ( ) => {
10794 this . _element . classList . add ( CLASS_NAME_IS_LOADING )
108- this . _loading ( )
10995 EventHandler . trigger ( this . _element , EVENT_START )
96+
97+ if ( this . _config . disabledOnLoading ) {
98+ this . _element . setAttribute ( 'disabled' , true )
99+ }
110100 } , 1 )
101+
102+ if ( this . _config . timeout ) {
103+ setTimeout ( ( ) => {
104+ this . stop ( )
105+ } , this . _config . timeout )
106+ }
111107 }
112108 }
113109
114110 stop ( ) {
115111 this . _element . classList . remove ( CLASS_NAME_IS_LOADING )
116112 const stoped = ( ) => {
117113 this . _removeSpinner ( )
118- this . _removeProgressBar ( )
119114 this . _state = 'idle'
120115
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' )
124118 }
125119
126- this . _percent = this . _config . percent
127- this . _timeout = this . _config . timeout
120+ EventHandler . trigger ( this . _element , EVENT_STOP )
128121 }
129122
130123 if ( this . _spinner ) {
@@ -138,35 +131,11 @@ class LoadingButton extends BaseComponent {
138131 stoped ( )
139132 }
140133
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-
161134 dispose ( ) {
162135 Data . removeData ( this . _element , DATA_KEY )
163136 this . _element = null
164137 }
165138
166- update ( config ) {
167- this . _config = this . _getConfig ( config )
168- }
169-
170139 _getConfig ( config ) {
171140 config = {
172141 ...Default ,
@@ -178,40 +147,6 @@ class LoadingButton extends BaseComponent {
178147 return config
179148 }
180149
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-
215150 _createSpinner ( ) {
216151 if ( this . _config . spinner ) {
217152 const spinner = document . createElement ( 'span' )
@@ -224,46 +159,13 @@ class LoadingButton extends BaseComponent {
224159 }
225160 }
226161
227- _removeProgressBar ( ) {
228- if ( this . _config . progress ) {
229- this . _progressBar . remove ( )
230- this . _progressBar = null
231- }
232- }
233-
234162 _removeSpinner ( ) {
235163 if ( this . _config . spinner ) {
236164 this . _spinner . remove ( )
237165 this . _spinner = null
238166 }
239167 }
240168
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-
267169 // Static
268170
269171 static loadingButtonInterface ( element , config ) {
0 commit comments