Skip to content

Commit 3200a0a

Browse files
committed
refactor: optimise code
1 parent 397aad8 commit 3200a0a

File tree

2 files changed

+58
-54
lines changed

2 files changed

+58
-54
lines changed

js/src/loading-button.js

Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
import {
99
getjQuery,
10+
TRANSITION_END,
11+
emulateTransitionEnd,
12+
getTransitionDurationFromElement,
1013
typeCheckConfig
1114
} from './util/index'
1215
import Data from './dom/data'
@@ -19,9 +22,9 @@ import Manipulator from './dom/manipulator'
1922
* ------------------------------------------------------------------------
2023
*/
2124

22-
const NAME = 'loadingbutton'
25+
const NAME = 'loading-button'
2326
const VERSION = '3.4.0'
24-
const DATA_KEY = 'coreui.loadingbutton'
27+
const DATA_KEY = 'coreui.loading-button'
2528
const EVENT_KEY = `.${DATA_KEY}`
2629
const DATA_API_KEY = '.data-api'
2730

@@ -34,9 +37,9 @@ const SELECTOR_COMPONENT = '[data-coreui="loading-button"]'
3437

3538
const EVENT_START = `start${EVENT_KEY}`
3639
const EVENT_STOP = `stop${EVENT_KEY}`
40+
const EVENT_COMPLETE = `complete${EVENT_KEY}`
3741
const EVENT_LOAD_DATA_API = `load${EVENT_KEY}${DATA_API_KEY}`
3842

39-
const CLASS_NAME_LOADING_BUTTON = 'c-loading-button'
4043
const CLASS_NAME_LOADING_BUTTON_LOADING = 'c-loading-button-loading'
4144
const CLASS_NAME_LOADING_BUTTON_PROGRESS = 'c-loading-button-progress'
4245
const CLASS_NAME_LOADING_BUTTON_SPINNER = 'c-loading-button-spinner'
@@ -65,11 +68,6 @@ const DefaultType = {
6568

6669
class LoadingButton {
6770
constructor(element, config) {
68-
if (Data.getData(element, DATA_KEY)) { // already found
69-
console.warn('Instance already exist.')
70-
return;
71-
}
72-
7371
this._element = element
7472
this._config = this._getConfig(config)
7573
this._pause = false
@@ -79,9 +77,6 @@ class LoadingButton {
7977
this._spinner = null
8078
this._state = 'idle'
8179

82-
this._createSpinner()
83-
this._createProgressBar()
84-
8580
if (this._element) {
8681
Data.setData(element, DATA_KEY, this)
8782
}
@@ -105,29 +100,42 @@ class LoadingButton {
105100

106101
start() {
107102
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+
}
109113

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)
113124
}
114125

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
117128
}
118-
}
119129

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)
123135
return
124136
}
125137

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()
131139
}
132140

133141
pause() {
@@ -175,37 +183,23 @@ class LoadingButton {
175183
return config
176184
}
177185

178-
_triggerStartEvent(element) {
179-
return EventHandler.trigger(element, EVENT_START)
180-
}
181-
182-
_triggerStopEvent(element) {
183-
return EventHandler.trigger(element, EVENT_STOP)
184-
}
185-
186186
_loading() {
187187
const progress = setInterval(() => {
188188
this._state = 'loading'
189189
if (this._percent >= MAX_PERCENT) {
190-
clearInterval(progress)
191190
this.stop()
191+
clearInterval(progress)
192+
return
192193
}
193194

194195
if (this._pause) {
195196
clearInterval(progress)
197+
return
196198
}
197199

198200
const frames = this._timeout / (MAX_PERCENT - this._percent) / MILLISECONDS
199-
200-
// console.log(this._percent)
201201
this._percent = Math.round((this._percent + (1 / frames)) * 100) / 100
202202
this._timeout -= MILLISECONDS
203-
// this._progressBar.style.width = `${this._percent}%`
204-
205-
// console.log(frames)
206-
// console.log(this._percent)
207-
// console.log('---')
208-
209203
this._animateProgressBar()
210204
}, MILLISECONDS)
211205
}
@@ -235,6 +229,20 @@ class LoadingButton {
235229
}
236230
}
237231

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+
238246
_progressBarBg() {
239247
// The yiq lightness value that determines when the lightness of color changes from "dark" to "light". Acceptable values are between 0 and 255.
240248
const yiqContrastedThreshold = 150
@@ -255,12 +263,6 @@ class LoadingButton {
255263
return PROGRESS_BAR_BG_COLOR_LIGHT
256264
}
257265

258-
_resetProgressBar() {
259-
if (this._config.progress) {
260-
this._progressBar.style.width = `${this._config.percent}%`
261-
}
262-
}
263-
264266
_animateProgressBar() {
265267
if (this._config.progress) {
266268
this._progressBar.style.width = `${this._percent}%`
@@ -289,6 +291,10 @@ class LoadingButton {
289291
case 'dispose':
290292
case 'start':
291293
case 'stop':
294+
case 'pause':
295+
case 'resume':
296+
case 'complete':
297+
case 'updatePercent':
292298
data[config]()
293299
break
294300
}
@@ -306,7 +312,6 @@ class LoadingButton {
306312
}
307313
}
308314

309-
310315
/**
311316
* ------------------------------------------------------------------------
312317
* Data Api implementation

scss/_loading-button.scss

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@
1313
left: 0;
1414
width: 0%;
1515
height: 100%;
16-
background: rgba(0, 0, 0, .1);
1716
}
1817

1918
.c-loading-button-spinner {
20-
width: 0;
21-
margin-right: 0;
19+
margin-right: $spacer;
20+
margin-left: - ($spacer * 2);
2221
opacity: 0;
2322
transition: margin $layout-transition-speed, opacity $layout-transition-speed, border $layout-transition-speed;
2423
}
@@ -27,7 +26,7 @@
2726

2827
.c-loading-button-spinner {
2928
width: 1rem;
30-
margin-right: $spacer;
29+
margin-left: 0;
3130
opacity: 1;
3231
}
3332
}

0 commit comments

Comments
 (0)