-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathloading-button.js
192 lines (150 loc) · 4.15 KB
/
loading-button.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* --------------------------------------------------------------------------
* CoreUI PRO loading-button.js
* License (https://coreui.io/pro/license/)
* --------------------------------------------------------------------------
*/
import BaseComponent from './base-component.js'
import Data from './dom/data.js'
import EventHandler from './dom/event-handler.js'
import { defineJQueryPlugin } from './util/index.js'
/**
* Constants
*/
const NAME = 'loading-button'
const DATA_KEY = 'coreui.loading-button'
const EVENT_KEY = `.${DATA_KEY}`
const DATA_API_KEY = '.data-api'
const EVENT_START = `start${EVENT_KEY}`
const EVENT_STOP = `stop${EVENT_KEY}`
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
const CLASS_NAME_IS_LOADING = 'is-loading'
const CLASS_NAME_LOADING_BUTTON = 'btn-loading'
const CLASS_NAME_LOADING_BUTTON_SPINNER = 'btn-loading-spinner'
const SELECTOR_DATA_TOGGLE = '[data-coreui-toggle="loading-button"]'
const Default = {
disabledOnLoading: false,
spinner: true,
spinnerType: 'border',
timeout: false
}
const DefaultType = {
disabledOnLoading: 'boolean',
spinner: 'boolean',
spinnerType: 'string',
timeout: '(boolean|number)'
}
/**
* Class definition
*/
class LoadingButton extends BaseComponent {
constructor(element, config) {
super(element)
this._config = this._getConfig(config)
this._timeout = this._config.timeout
this._spinner = null
this._state = 'idle'
if (this._element) {
Data.set(element, DATA_KEY, this)
}
this._createButton()
}
// Getters
static get Default() {
return Default
}
static get DefaultType() {
return DefaultType
}
static get NAME() {
return NAME
}
// Public
start() {
if (this._state !== 'loading') {
this._createSpinner()
this._state = 'loading'
setTimeout(() => {
this._element.classList.add(CLASS_NAME_IS_LOADING)
EventHandler.trigger(this._element, EVENT_START)
if (this._config.disabledOnLoading) {
this._element.setAttribute('disabled', true)
}
}, 1)
if (this._config.timeout) {
setTimeout(() => {
this.stop()
}, this._config.timeout)
}
}
}
stop() {
this._element.classList.remove(CLASS_NAME_IS_LOADING)
const stoped = () => {
this._removeSpinner()
this._state = 'idle'
if (this._config.disabledOnLoading) {
this._element.removeAttribute('disabled')
}
EventHandler.trigger(this._element, EVENT_STOP)
}
if (this._spinner) {
this._queueCallback(stoped, this._spinner, true)
return
}
stoped()
}
dispose() {
Data.removeData(this._element, DATA_KEY)
this._element = null
}
_createButton() {
this._element.classList.add(CLASS_NAME_LOADING_BUTTON)
}
_createSpinner() {
if (this._config.spinner) {
const spinner = document.createElement('span')
const type = this._config.spinnerType
spinner.classList.add(CLASS_NAME_LOADING_BUTTON_SPINNER, `spinner-${type}`, `spinner-${type}-sm`)
spinner.setAttribute('role', 'status')
spinner.setAttribute('aria-hidden', 'true')
this._element.insertBefore(spinner, this._element.firstChild)
this._spinner = spinner
}
}
_removeSpinner() {
if (this._config.spinner) {
this._spinner.remove()
this._spinner = null
}
}
// Static
static loadingButtonInterface(element, config) {
const data = LoadingButton.getOrCreateInstance(element, config)
if (typeof config === 'string') {
if (typeof data[config] === 'undefined') {
throw new TypeError(`No method named "${config}"`)
}
data[config]()
}
}
static jQueryInterface(config) {
return this.each(function () {
LoadingButton.loadingButtonInterface(this, config)
})
}
}
/**
* Data API implementation
*/
EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, event => {
event.preventDefault()
const button = event.target.closest(SELECTOR_DATA_TOGGLE)
const data = LoadingButton.getOrCreateInstance(button)
data.start()
})
/**
* jQuery
*/
defineJQueryPlugin(LoadingButton)
export default LoadingButton