-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplash-screen.js
More file actions
409 lines (350 loc) · 12.7 KB
/
splash-screen.js
File metadata and controls
409 lines (350 loc) · 12.7 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/**
* SplashScreen - A customizable splash screen component for web applications
* @version 1.0.1
*/
class SplashScreen {
/**
* Create a new splash screen
* @param {Object} options - Configuration options
* @param {string} [options.containerId='splash-screen-container'] - ID for the splash screen container
* @param {string} [options.backgroundColor='#ffffff'] - Background color
* @param {string} [options.logoSrc] - URL to the logo image
* @param {number} [options.logoWidth=120] - Logo width in pixels
* @param {number} [options.logoHeight=120] - Logo height in pixels
* @param {string} [options.logoAlt='Logo'] - Alt text for the logo
* @param {string} [options.text] - Text to display below the logo
* @param {string} [options.textColor='#333333'] - Color of the text
* @param {number} [options.duration=2500] - Duration in milliseconds before the splash screen disappears
* @param {number} [options.fadeOutDuration=500] - Duration of the fade out animation in milliseconds
* @param {boolean} [options.showProgressBar=false] - Whether to show a progress bar
* @param {string} [options.progressBarColor='#4CAF50'] - Color of the progress bar
* @param {boolean} [options.showSpinner=false] - Whether to show a loading spinner
* @param {string} [options.spinnerColor='#333333'] - Color of the spinner
* @param {Function} [options.onHide] - Callback function to execute after the splash screen is hidden
* @param {string} [options.logoAnimation='pulse'] - Animation for the logo: 'pulse', 'rotate', 'bounce', or 'none'
* @param {boolean} [options.removeAfterHide=false] - Whether to remove the splash screen from the DOM after hiding
*/
constructor(options = {}) {
this.options = {
containerId: options.containerId || 'splash-screen-container',
backgroundColor: options.backgroundColor || '#ffffff',
logoSrc: options.logoSrc || null,
logoWidth: options.logoWidth || 120,
logoHeight: options.logoHeight || 120,
logoAlt: options.logoAlt || 'Logo',
text: options.text || null,
textColor: options.textColor || '#333333',
duration: options.duration !== undefined ? options.duration : 2500,
fadeOutDuration: options.fadeOutDuration || 500,
showProgressBar: options.showProgressBar || false,
progressBarColor: options.progressBarColor || '#4CAF50',
showSpinner: options.showSpinner || false,
spinnerColor: options.spinnerColor || '#333333',
onHide: options.onHide || null,
animation: options.animation || 'fade', // 'fade', 'zoom', 'slide'
zIndex: options.zIndex || 9999,
logoAnimation: options.logoAnimation || 'pulse',
removeAfterHide: options.removeAfterHide || false
};
this.container = null;
this.progressInterval = null;
this.isHidden = false;
this.isInitialized = false;
// Initialize when DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => this.initialize());
} else {
this.initialize();
}
}
/**
* Initialize the splash screen
*/
initialize() {
if (this.isInitialized) return;
this.isInitialized = true;
// Create and inject styles
this._injectStyles();
// Create splash screen element
this._createSplashScreen();
// Auto-hide after duration if duration is greater than 0
if (this.options.duration > 0) {
this._autoHide();
}
}
/**
* Inject required CSS styles
* @private
*/
_injectStyles() {
const styleId = 'splash-screen-styles';
// Don't add styles if they already exist
if (document.getElementById(styleId)) return;
const style = document.createElement('style');
style.id = styleId;
style.textContent = `
.splash-screen-container {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
transition: opacity ${this.options.fadeOutDuration}ms ease-out, visibility ${this.options.fadeOutDuration}ms ease-out;
z-index: ${this.options.zIndex};
}
.splash-screen-container.hidden {
opacity: 0;
visibility: hidden;
}
.splash-screen-logo {
max-width: 100%;
height: auto;
margin-bottom: 20px;
}
.splash-screen-logo.pulse {
animation: splash-screen-pulse 2s infinite;
}
.splash-screen-logo.rotate {
animation: splash-screen-rotate 2s infinite linear;
}
.splash-screen-logo.bounce {
animation: splash-screen-bounce 2s infinite;
}
.splash-screen-text {
font-family: Arial, sans-serif;
font-size: 18px;
margin-top: 16px;
text-align: center;
}
.splash-screen-progress-container {
width: 200px;
height: 4px;
background-color: rgba(0, 0, 0, 0.1);
margin-top: 20px;
border-radius: 2px;
overflow: hidden;
}
.splash-screen-progress-bar {
height: 100%;
width: 0%;
transition: width 0.3s ease-out;
}
.splash-screen-spinner {
margin-top: 20px;
width: 40px;
height: 40px;
border: 3px solid rgba(0, 0, 0, 0.1);
border-radius: 50%;
border-top-color: #333;
animation: splash-screen-spin 1s linear infinite;
}
@keyframes splash-screen-pulse {
0% { transform: scale(1); }
50% { transform: scale(1.1); }
100% { transform: scale(1); }
}
@keyframes splash-screen-rotate {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
@keyframes splash-screen-bounce {
0%, 20%, 50%, 80%, 100% { transform: translateY(0); }
40% { transform: translateY(-20px); }
60% { transform: translateY(-10px); }
}
@keyframes splash-screen-spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
`;
document.head.appendChild(style);
}
/**
* Create the splash screen DOM elements
* @private
*/
_createSplashScreen() {
// Check if container already exists
const existingContainer = document.getElementById(this.options.containerId);
if (existingContainer) {
existingContainer.remove();
}
// Create container
this.container = document.createElement('div');
this.container.id = this.options.containerId;
this.container.className = 'splash-screen-container';
this.container.style.backgroundColor = this.options.backgroundColor;
// Create logo if provided
if (this.options.logoSrc) {
const logo = document.createElement('img');
logo.src = this.options.logoSrc;
logo.alt = this.options.logoAlt;
logo.width = this.options.logoWidth;
logo.height = this.options.logoHeight;
logo.className = 'splash-screen-logo';
// Add animation class if specified
if (this.options.logoAnimation && this.options.logoAnimation !== 'none') {
logo.classList.add(this.options.logoAnimation);
}
// Handle image loading errors
logo.onerror = () => {
console.error(`Failed to load logo image: ${this.options.logoSrc}`);
logo.style.display = 'none';
};
this.container.appendChild(logo);
}
// Create text if provided
if (this.options.text) {
const text = document.createElement('div');
text.className = 'splash-screen-text';
text.style.color = this.options.textColor;
text.textContent = this.options.text;
this.container.appendChild(text);
}
// Create progress bar if enabled
if (this.options.showProgressBar) {
const progressContainer = document.createElement('div');
progressContainer.className = 'splash-screen-progress-container';
const progressBar = document.createElement('div');
progressBar.className = 'splash-screen-progress-bar';
progressBar.style.backgroundColor = this.options.progressBarColor;
progressContainer.appendChild(progressBar);
this.container.appendChild(progressContainer);
// Animate progress bar
this._animateProgressBar(progressBar);
}
// Create spinner if enabled
if (this.options.showSpinner) {
const spinner = document.createElement('div');
spinner.className = 'splash-screen-spinner';
spinner.style.borderTopColor = this.options.spinnerColor;
this.container.appendChild(spinner);
}
// Add to DOM
document.body.appendChild(this.container);
}
/**
* Animate the progress bar
* @param {HTMLElement} progressBar - The progress bar element
* @private
*/
_animateProgressBar(progressBar) {
if (this.progressInterval) {
clearInterval(this.progressInterval);
}
let width = 0;
const duration = this.options.duration;
const interval = 30; // Update every 30ms
const increment = (interval / duration) * 100;
this.progressInterval = setInterval(() => {
if (width >= 100) {
clearInterval(this.progressInterval);
} else {
width += increment;
progressBar.style.width = `${Math.min(width, 100)}%`;
}
}, interval);
}
/**
* Auto-hide the splash screen after the specified duration
* @private
*/
_autoHide() {
setTimeout(() => {
this.hide();
}, this.options.duration);
}
/**
* Hide the splash screen
*/
hide() {
if (this.isHidden || !this.container) return;
this.isHidden = true;
this.container.classList.add('hidden');
// Clean up after transition
const handleTransitionEnd = () => {
if (this.progressInterval) {
clearInterval(this.progressInterval);
}
// Execute callback if provided
if (typeof this.options.onHide === 'function') {
this.options.onHide();
}
// Remove from DOM if needed
if (this.options.removeAfterHide) {
this.container.remove();
}
this.container.removeEventListener('transitionend', handleTransitionEnd);
};
this.container.addEventListener('transitionend', handleTransitionEnd);
// Fallback in case the transition event doesn't fire
setTimeout(() => {
if (this.container.classList.contains('hidden') &&
this.container.parentNode &&
!this.container._transitionHandled) {
handleTransitionEnd();
}
}, this.options.fadeOutDuration + 50);
}
/**
* Show the splash screen (if it was hidden)
*/
show() {
if (!this.isHidden || !this.container) return;
this.isHidden = false;
this.container.classList.remove('hidden');
// Reset progress bar if it exists
if (this.options.showProgressBar) {
const progressBar = this.container.querySelector('.splash-screen-progress-bar');
if (progressBar) {
progressBar.style.width = '0%';
this._animateProgressBar(progressBar);
}
}
// Auto-hide again if duration is set
if (this.options.duration > 0) {
this._autoHide();
}
}
/**
* Update splash screen options
* @param {Object} newOptions - New options to apply
*/
updateOptions(newOptions) {
// Update options
Object.assign(this.options, newOptions);
// Update DOM elements based on new options
if (this.container) {
this.container.style.backgroundColor = this.options.backgroundColor;
const logo = this.container.querySelector('.splash-screen-logo');
if (logo && this.options.logoSrc) {
logo.src = this.options.logoSrc;
logo.alt = this.options.logoAlt;
logo.width = this.options.logoWidth;
logo.height = this.options.logoHeight;
// Update animation
logo.classList.remove('pulse', 'rotate', 'bounce');
if (this.options.logoAnimation && this.options.logoAnimation !== 'none') {
logo.classList.add(this.options.logoAnimation);
}
}
const text = this.container.querySelector('.splash-screen-text');
if (text && this.options.text) {
text.textContent = this.options.text;
text.style.color = this.options.textColor;
}
const progressBar = this.container.querySelector('.splash-screen-progress-bar');
if (progressBar) {
progressBar.style.backgroundColor = this.options.progressBarColor;
}
const spinner = this.container.querySelector('.splash-screen-spinner');
if (spinner) {
spinner.style.borderTopColor = this.options.spinnerColor;
}
}
}
}