forked from mozilla-b2g/gaia
-
Notifications
You must be signed in to change notification settings - Fork 1
/
overlay.js
188 lines (171 loc) · 5.85 KB
/
overlay.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
/* global _ */
'use strict';
var utils = window.utils || {};
(function() {
utils.overlay = {};
var overlay = document.querySelector('#loading-overlay'),
statusContainer = overlay.querySelector('p[role="status"]'),
progressActivity = document.querySelector('#progress-activity'),
progressTitle = document.querySelector('#progress-title'),
progressElement = document.querySelector('#progress-element'),
progressMsg = document.querySelector('#progress-msg'),
menu = document.querySelector('#loading-overlay menu'),
cancelButton = document.querySelector('#cancel-overlay');
// Constructor for the progress element
function ProgressBar(pMsgId, pClass) {
var counter = 0;
var total = 0;
var progressTextId = pMsgId || 'genericProgress';
var clazz = pClass;
progressElement.setAttribute('value', 0);
function showMessage() {
progressMsg.textContent = _(progressTextId, {
current: counter,
total: total
});
}
/**
* Updates progress bar and message. It takes an optional `value` parameter
* to overwrite the internal counter with this value.
* @param {Number} value Overrides internal counter.
*/
this.update = function(value) {
if (value && value <= total && value >= counter) {
counter = value;
} else {
counter++;
}
progressElement.setAttribute('value',
((counter * 100) / total).toFixed()); // Percent fraction is not needed.
showMessage();
};
this.setTotal = function(ptotal) {
total = ptotal;
};
this.setClass = function(clazzName) {
setClass(clazzName);
clazz = clazzName;
// To refresh the message according to the new clazzName
if (clazzName === 'activityBar' || clazzName === 'spinner') {
progressMsg.textContent = null;
}
};
this.setHeaderMsg = function(headerMsg) {
progressTitle.textContent = headerMsg;
};
} // ProgressBar
utils.overlay.isAnimationPlaying = false;
utils.overlay.isShown = false;
utils.overlay._show = function _show(message, progressClass, textId) {
progressActivity.classList.remove('hide');
progressTitle.textContent = message;
progressMsg.textContent = null;
if (utils.overlay.isShown) {
return;
}
overlay.classList.remove('hide');
overlay.classList.remove('fade-out');
overlay.classList.add('fade-in');
utils.overlay.isAnimationPlaying = true;
// Custom event that can be used to apply (screen reader) visibility
// changes.
window.dispatchEvent(new CustomEvent('loadingoverlayshowing'));
overlay.addEventListener('animationend', function ov_onFadeIn(ev) {
utils.overlay.isAnimationPlaying = false;
overlay.removeEventListener('animationend', ov_onFadeIn);
overlay.classList.remove('no-opacity');
utils.overlay.isShown = true;
});
};
utils.overlay.show = function show(message, progressClass, textId) {
var out;
// In the case of an spinner this object will not be really used
out = new ProgressBar(textId, progressClass);
setClass(progressClass);
utils.overlay.hideMenu(); // By default
if (!utils.overlay.isAnimationPlaying) {
utils.overlay._show(message, progressClass, textId);
return out;
}
overlay.addEventListener('animationend',
function ov_showWhenFinished(ev) {
overlay.removeEventListener('animationend', ov_showWhenFinished);
utils.overlay._show(message, progressClass, textId);
}
);
return out;
};
utils.overlay.showMenu = function showMenu() {
menu.classList.add('showed');
};
utils.overlay.hideMenu = function hideMenu() {
menu.classList.remove('showed');
};
Object.defineProperty(utils.overlay, 'oncancel', {
set: function(cancelCb) {
if (typeof cancelCb === 'function') {
cancelButton.disabled = false;
cancelButton.onclick = function on_cancel(e) {
delete cancelButton.onclick;
cancelCb();
return false;
};
}
}
});
function setAsProgress() {
statusContainer.classList.remove('loading-icon');
progressElement.setAttribute('max', '100');
progressElement.setAttribute('value', '0');
}
function setClass(clazzName) {
switch (clazzName) {
case 'spinner':
progressElement.classList.remove('pack-activity');
statusContainer.classList.add('loading-icon');
progressElement.removeAttribute('max');
progressElement.removeAttribute('value');
break;
case 'activityBar':
case 'progressActivity':
progressElement.classList.add('pack-activity');
setAsProgress();
break;
case 'progressBar':
progressElement.classList.remove('pack-activity');
setAsProgress();
break;
}
}
utils.overlay._hide = function ov__hide() {
if (!utils.overlay.isShown) {
return;
}
overlay.classList.remove('fade-in');
overlay.classList.add('fade-out');
utils.overlay.isAnimationPlaying = true;
// Custom event that can be used to apply (screen reader) visibility
// changes.
window.dispatchEvent(new CustomEvent('loadingoverlayhiding'));
overlay.addEventListener('animationend', function ov_onFadeOut(ev) {
utils.overlay.isAnimationPlaying = false;
overlay.removeEventListener('animationend', ov_onFadeOut);
progressActivity.classList.add('hide');
overlay.classList.add('no-opacity');
overlay.classList.add('hide');
utils.overlay.isShown = false;
});
};
utils.overlay.hide = function ov_hide() {
if (!utils.overlay.isAnimationPlaying) {
utils.overlay._hide();
return;
}
overlay.addEventListener('animationend',
function ov_hideWhenFinished(ev) {
overlay.removeEventListener('animationend', ov_hideWhenFinished);
utils.overlay._hide();
}
);
};
})();