-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathSweetAlert.js
More file actions
289 lines (241 loc) · 9.18 KB
/
SweetAlert.js
File metadata and controls
289 lines (241 loc) · 9.18 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import defaultParams, { showWarningsForParams } from './utils/params.js'
import * as dom from './utils/dom/index.js'
import { callIfFunction } from './utils/utils.js'
import { DismissReason } from './utils/DismissReason.js'
import { unsetAriaHidden } from './utils/aria.js'
import { getTemplateParams } from './utils/getTemplateParams.js'
import setParameters from './utils/setParameters.js'
import Timer from './utils/Timer.js'
import { openPopup } from './utils/openPopup.js'
import { handleInputOptionsAndValue } from './utils/dom/inputUtils.js'
import { setInnerHtml } from './utils/dom/index.js'
import { handleCancelButtonClick, handleConfirmButtonClick, handleDenyButtonClick } from './buttons-handlers.js'
import { handlePopupClick } from './popup-click-handler.js'
import { addKeydownHandler, setFocus } from './keydown-handler.js'
import * as staticMethods from './staticMethods.js'
import * as instanceMethods from './instanceMethods.js'
import privateProps from './privateProps.js'
import privateMethods from './privateMethods.js'
import globalState from './globalState.js'
let currentInstance
class SweetAlert {
constructor(...args) {
// Prevent run in Node env
if (typeof window === 'undefined') {
return
}
currentInstance = this
// @ts-ignore
const outerParams = Object.freeze(this.constructor.argsToParams(args))
Object.defineProperties(this, {
params: {
value: outerParams,
writable: false,
enumerable: true,
configurable: true,
},
})
// @ts-ignore
const promise = currentInstance._main(currentInstance.params)
privateProps.promise.set(this, promise)
}
_main(userParams, mixinParams = {}) {
showWarningsForParams(Object.assign({}, mixinParams, userParams))
if (globalState.currentInstance) {
// @ts-ignore
globalState.currentInstance._destroy()
if (dom.isModal()) {
unsetAriaHidden()
}
}
globalState.currentInstance = currentInstance
const innerParams = prepareParams(userParams, mixinParams)
setParameters(innerParams)
Object.freeze(innerParams)
// clear the previous timer
if (globalState.timeout) {
globalState.timeout.stop()
delete globalState.timeout
}
// clear the restore focus timeout
clearTimeout(globalState.restoreFocusTimeout)
const domCache = populateDomCache(currentInstance)
dom.render(currentInstance, innerParams)
privateProps.innerParams.set(currentInstance, innerParams)
return swalPromise(currentInstance, domCache, innerParams)
}
// `catch` cannot be the name of a module export, so we define our thenable methods here instead
then(onFulfilled) {
const promise = privateProps.promise.get(this)
return promise.then(onFulfilled)
}
finally(onFinally) {
const promise = privateProps.promise.get(this)
return promise.finally(onFinally)
}
}
const swalPromise = (instance, domCache, innerParams) => {
return new Promise((resolve, reject) => {
// functions to handle all closings/dismissals
const dismissWith = (dismiss) => {
instance.closePopup({ isDismissed: true, dismiss })
}
privateMethods.swalPromiseResolve.set(instance, resolve)
privateMethods.swalPromiseReject.set(instance, reject)
domCache.confirmButton.onclick = () => handleConfirmButtonClick(instance)
domCache.denyButton.onclick = () => handleDenyButtonClick(instance)
domCache.cancelButton.onclick = () => handleCancelButtonClick(instance, dismissWith)
domCache.closeButton.onclick = () => dismissWith(DismissReason.close)
handlePopupClick(instance, domCache, dismissWith)
addKeydownHandler(instance, globalState, innerParams, dismissWith)
handleInputOptionsAndValue(instance, innerParams)
openPopup(innerParams)
setupTimer(globalState, innerParams, dismissWith)
initFocus(domCache, innerParams)
// Scroll container to top on open (#1247, #1946)
setTimeout(() => {
domCache.container.scrollTop = 0
})
})
}
const prepareParams = (userParams, mixinParams) => {
const templateParams = getTemplateParams(userParams)
const params = Object.assign({}, defaultParams, mixinParams, templateParams, userParams) // precedence is described in #2131
params.showClass = Object.assign({}, defaultParams.showClass, params.showClass)
params.hideClass = Object.assign({}, defaultParams.hideClass, params.hideClass)
return params
}
/**
* @param {SweetAlert2} instance
* @returns {DomCache}
*/
const populateDomCache = (instance) => {
const domCache = {
popup: dom.getPopup(),
container: dom.getContainer(),
actions: dom.getActions(),
confirmButton: dom.getConfirmButton(),
denyButton: dom.getDenyButton(),
cancelButton: dom.getCancelButton(),
loader: dom.getLoader(),
closeButton: dom.getCloseButton(),
validationMessage: dom.getValidationMessage(),
progressSteps: dom.getProgressSteps(),
}
privateProps.domCache.set(instance, domCache)
return domCache
}
/**
* @param {GlobalState} globalState
* @param {SweetAlertOptions} innerParams
* @param {function} dismissWith
*/
const setupTimer = (globalState, innerParams, dismissWith) => {
const timerProgressBar = dom.getTimerProgressBar()
dom.hide(timerProgressBar)
if (innerParams.timer) {
globalState.timeout = new Timer(() => {
dismissWith('timer')
delete globalState.timeout
}, innerParams.timer)
if (innerParams.timerProgressBar) {
dom.show(timerProgressBar)
dom.applyCustomClass(timerProgressBar, innerParams, 'timerProgressBar')
setTimeout(() => {
if (globalState.timeout && globalState.timeout.running) {
// timer can be already stopped or unset at this point
dom.animateTimerProgressBar(innerParams.timer)
}
})
}
}
}
/**
* @param {DomCache} domCache
* @param {SweetAlertOptions} innerParams
*/
const initFocus = (domCache, innerParams) => {
if (innerParams.toast) {
return
}
if (!callIfFunction(innerParams.allowEnterKey)) {
return blurActiveElement()
}
if (!focusButton(domCache, innerParams)) {
setFocus(innerParams, -1, 1)
}
}
/**
* @param {DomCache} domCache
* @param {SweetAlertOptions} innerParams
* @returns {boolean}
*/
const focusButton = (domCache, innerParams) => {
if (innerParams.focusDeny && dom.isVisible(domCache.denyButton)) {
domCache.denyButton.focus()
return true
}
if (innerParams.focusCancel && dom.isVisible(domCache.cancelButton)) {
domCache.cancelButton.focus()
return true
}
if (innerParams.focusConfirm && dom.isVisible(domCache.confirmButton)) {
domCache.confirmButton.focus()
return true
}
return false
}
const blurActiveElement = () => {
if (document.activeElement instanceof HTMLElement && typeof document.activeElement.blur === 'function') {
document.activeElement.blur()
}
}
// This anti-war message will only be shown to Russian users visiting Russian sites
if (typeof window !== 'undefined' && /^ru\b/.test(navigator.language) && location.host.match(/\.(ru|su|xn--p1ai)$/)) {
if (Math.random() < 0.1) {
const noWar = document.createElement('div')
noWar.className = 'leave-russia-now-and-apply-your-skills-to-the-world'
setInnerHtml(
noWar,
`
<div>
Если мы не остановим войну, она придет в дом <strong>каждого из нас</strong> и её последствия будут <strong>ужасающими</strong>.
</div>
<div>
Путинский режим за 20 с лишним лет своего существования вдолбил нам, что мы бессильны и один человек не может ничего сделать. <strong>Это не так!</strong>
</div>
<div>
В нижеприведённом видео объясняется как каждый из нас может помочь в том, <strong>чтобы эта бессмысленная и бесчеловечная война остановилась</strong>:
</div>
<iframe width="560" height="315" src="https://www.youtube.com/embed/4CfDhaRkw7I" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
<div>
Нет войне!
</div>
`
)
const closeButton = document.createElement('button')
closeButton.innerHTML = '×'
closeButton.onclick = () => noWar.remove()
noWar.appendChild(closeButton)
window.addEventListener('load', () => {
setTimeout(() => {
document.body.appendChild(noWar)
}, 1000)
})
}
}
// Assign instance methods from src/instanceMethods/*.js to prototype
Object.assign(SweetAlert.prototype, instanceMethods)
// Assign static methods from src/staticMethods/*.js to constructor
Object.assign(SweetAlert, staticMethods)
// Proxy to instance methods to constructor, for now, for backwards compatibility
Object.keys(instanceMethods).forEach((key) => {
SweetAlert[key] = function (...args) {
if (currentInstance) {
return currentInstance[key](...args)
}
}
})
SweetAlert.DismissReason = DismissReason
SweetAlert.version = '11.4.19'
export default SweetAlert