Improved a11y of the loading indicator
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / js / WoltLabSuite / Core / Ajax / Status.js
CommitLineData
79ba2d03
AE
1/**
2 * Provides the AJAX status overlay.
3 *
4 * @author Alexander Ebert
7b7b9764 5 * @copyright 2001-2019 WoltLab GmbH
79ba2d03 6 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
58d7e8f8 7 * @module WoltLabSuite/Core/Ajax/Status
79ba2d03
AE
8 */
9define(['Language'], function(Language) {
10 "use strict";
11
12 var _activeRequests = 0;
13 var _overlay = null;
14 var _timeoutShow = null;
15
16 /**
58d7e8f8 17 * @exports WoltLabSuite/Core/Ajax/Status
79ba2d03 18 */
bc8cd0ff 19 var AjaxStatus = {
79ba2d03
AE
20 /**
21 * Initializes the status overlay on first usage.
22 */
23 _init: function() {
d0023381 24 _overlay = elCreate('div');
79ba2d03 25 _overlay.classList.add('spinner');
cc2f17ab 26 elAttr(_overlay, 'role', 'status');
79ba2d03 27
d0023381 28 var icon = elCreate('span');
79ba2d03
AE
29 icon.className = 'icon icon48 fa-spinner';
30 _overlay.appendChild(icon);
31
d0023381 32 var title = elCreate('span');
79ba2d03
AE
33 title.textContent = Language.get('wcf.global.loading');
34 _overlay.appendChild(title);
35
36 document.body.appendChild(_overlay);
37 },
38
39 /**
40 * Shows the loading overlay.
41 */
42 show: function() {
43 if (_overlay === null) {
44 this._init();
45 }
46
47 _activeRequests++;
48
49 if (_timeoutShow === null) {
50 _timeoutShow = window.setTimeout(function() {
51 if (_activeRequests) {
52 _overlay.classList.add('active');
53 }
54
55 _timeoutShow = null;
56 }, 250);
57 }
58 },
59
60 /**
61 * Hides the loading overlay.
62 */
63 hide: function() {
64 _activeRequests--;
65
66 if (_activeRequests === 0) {
67 if (_timeoutShow !== null) {
68 window.clearTimeout(_timeoutShow);
69 }
70
71 _overlay.classList.remove('active');
72 }
73 }
74 };
75
bc8cd0ff 76 return AjaxStatus;
79ba2d03 77});