7
7
8
8
import {
9
9
getjQuery ,
10
+ TRANSITION_END ,
11
+ emulateTransitionEnd ,
12
+ getTransitionDurationFromElement ,
10
13
typeCheckConfig
11
14
} from './util/index'
12
15
import Data from './dom/data'
@@ -19,9 +22,9 @@ import Manipulator from './dom/manipulator'
19
22
* ------------------------------------------------------------------------
20
23
*/
21
24
22
- const NAME = 'loadingbutton '
25
+ const NAME = 'loading-button '
23
26
const VERSION = '3.4.0'
24
- const DATA_KEY = 'coreui.loadingbutton '
27
+ const DATA_KEY = 'coreui.loading-button '
25
28
const EVENT_KEY = `.${ DATA_KEY } `
26
29
const DATA_API_KEY = '.data-api'
27
30
@@ -34,9 +37,9 @@ const SELECTOR_COMPONENT = '[data-coreui="loading-button"]'
34
37
35
38
const EVENT_START = `start${ EVENT_KEY } `
36
39
const EVENT_STOP = `stop${ EVENT_KEY } `
40
+ const EVENT_COMPLETE = `complete${ EVENT_KEY } `
37
41
const EVENT_LOAD_DATA_API = `load${ EVENT_KEY } ${ DATA_API_KEY } `
38
42
39
- const CLASS_NAME_LOADING_BUTTON = 'c-loading-button'
40
43
const CLASS_NAME_LOADING_BUTTON_LOADING = 'c-loading-button-loading'
41
44
const CLASS_NAME_LOADING_BUTTON_PROGRESS = 'c-loading-button-progress'
42
45
const CLASS_NAME_LOADING_BUTTON_SPINNER = 'c-loading-button-spinner'
@@ -65,11 +68,6 @@ const DefaultType = {
65
68
66
69
class LoadingButton {
67
70
constructor ( element , config ) {
68
- if ( Data . getData ( element , DATA_KEY ) ) { // already found
69
- console . warn ( 'Instance already exist.' )
70
- return ;
71
- }
72
-
73
71
this . _element = element
74
72
this . _config = this . _getConfig ( config )
75
73
this . _pause = false
@@ -79,9 +77,6 @@ class LoadingButton {
79
77
this . _spinner = null
80
78
this . _state = 'idle'
81
79
82
- this . _createSpinner ( )
83
- this . _createProgressBar ( )
84
-
85
80
if ( this . _element ) {
86
81
Data . setData ( element , DATA_KEY , this )
87
82
}
@@ -105,29 +100,42 @@ class LoadingButton {
105
100
106
101
start ( ) {
107
102
if ( this . _state !== 'loading' ) {
108
- let rootElement = this . _element
103
+ this . _createSpinner ( )
104
+ this . _createProgressBar ( )
105
+
106
+ setTimeout ( ( ) => {
107
+ this . _element . classList . add ( CLASS_NAME_LOADING_BUTTON_LOADING )
108
+ this . _loading ( )
109
+ EventHandler . trigger ( this . _element , EVENT_START )
110
+ } , 1 )
111
+ }
112
+ }
109
113
110
- const customEvent = this . _triggerStartEvent ( rootElement )
111
- if ( customEvent === null || customEvent . defaultPrevented ) {
112
- return
114
+ stop ( ) {
115
+ this . _element . classList . remove ( CLASS_NAME_LOADING_BUTTON_LOADING )
116
+ const stoped = ( ) => {
117
+ this . _removeSpinner ( )
118
+ this . _removeProgressBar ( )
119
+ this . _state = 'idle'
120
+
121
+ EventHandler . trigger ( this . _element , EVENT_STOP )
122
+ if ( this . _percent >= 100 ) {
123
+ EventHandler . trigger ( this . _element , EVENT_COMPLETE )
113
124
}
114
125
115
- this . _element . classList . add ( CLASS_NAME_LOADING_BUTTON_LOADING )
116
- this . _loading ( )
126
+ this . _percent = this . _config . percent
127
+ this . _timeout = this . _config . timeout
117
128
}
118
- }
119
129
120
- stop ( ) {
121
- const customEvent = this . _triggerStopEvent ( this . _element )
122
- if ( customEvent === null || customEvent . defaultPrevented ) {
130
+ if ( this . _spinner ) {
131
+ const transitionDuration = getTransitionDurationFromElement ( this . _spinner )
132
+
133
+ EventHandler . one ( this . _spinner , TRANSITION_END , stoped )
134
+ emulateTransitionEnd ( this . _spinner , transitionDuration )
123
135
return
124
136
}
125
137
126
- this . _element . classList . remove ( CLASS_NAME_LOADING_BUTTON_LOADING )
127
- this . _percent = this . _config . percent
128
- this . _timeout = this . _config . timeout
129
- this . _state = 'idle'
130
- this . _resetProgressBar ( )
138
+ stoped ( )
131
139
}
132
140
133
141
pause ( ) {
@@ -175,37 +183,23 @@ class LoadingButton {
175
183
return config
176
184
}
177
185
178
- _triggerStartEvent ( element ) {
179
- return EventHandler . trigger ( element , EVENT_START )
180
- }
181
-
182
- _triggerStopEvent ( element ) {
183
- return EventHandler . trigger ( element , EVENT_STOP )
184
- }
185
-
186
186
_loading ( ) {
187
187
const progress = setInterval ( ( ) => {
188
188
this . _state = 'loading'
189
189
if ( this . _percent >= MAX_PERCENT ) {
190
- clearInterval ( progress )
191
190
this . stop ( )
191
+ clearInterval ( progress )
192
+ return
192
193
}
193
194
194
195
if ( this . _pause ) {
195
196
clearInterval ( progress )
197
+ return
196
198
}
197
199
198
200
const frames = this . _timeout / ( MAX_PERCENT - this . _percent ) / MILLISECONDS
199
-
200
- // console.log(this._percent)
201
201
this . _percent = Math . round ( ( this . _percent + ( 1 / frames ) ) * 100 ) / 100
202
202
this . _timeout -= MILLISECONDS
203
- // this._progressBar.style.width = `${this._percent}%`
204
-
205
- // console.log(frames)
206
- // console.log(this._percent)
207
- // console.log('---')
208
-
209
203
this . _animateProgressBar ( )
210
204
} , MILLISECONDS )
211
205
}
@@ -235,6 +229,20 @@ class LoadingButton {
235
229
}
236
230
}
237
231
232
+ _removeProgressBar ( ) {
233
+ if ( this . _config . progress ) {
234
+ this . _progressBar . remove ( )
235
+ this . _progressBar = null
236
+ }
237
+ }
238
+
239
+ _removeSpinner ( ) {
240
+ if ( this . _config . spinner ) {
241
+ this . _spinner . remove ( )
242
+ this . _spinner = null
243
+ }
244
+ }
245
+
238
246
_progressBarBg ( ) {
239
247
// The yiq lightness value that determines when the lightness of color changes from "dark" to "light". Acceptable values are between 0 and 255.
240
248
const yiqContrastedThreshold = 150
@@ -255,12 +263,6 @@ class LoadingButton {
255
263
return PROGRESS_BAR_BG_COLOR_LIGHT
256
264
}
257
265
258
- _resetProgressBar ( ) {
259
- if ( this . _config . progress ) {
260
- this . _progressBar . style . width = `${ this . _config . percent } %`
261
- }
262
- }
263
-
264
266
_animateProgressBar ( ) {
265
267
if ( this . _config . progress ) {
266
268
this . _progressBar . style . width = `${ this . _percent } %`
@@ -289,6 +291,10 @@ class LoadingButton {
289
291
case 'dispose' :
290
292
case 'start' :
291
293
case 'stop' :
294
+ case 'pause' :
295
+ case 'resume' :
296
+ case 'complete' :
297
+ case 'updatePercent' :
292
298
data [ config ] ( )
293
299
break
294
300
}
@@ -306,7 +312,6 @@ class LoadingButton {
306
312
}
307
313
}
308
314
309
-
310
315
/**
311
316
* ------------------------------------------------------------------------
312
317
* Data Api implementation
0 commit comments