forked from javascript-tutorial/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspinner.js
More file actions
executable file
·43 lines (33 loc) · 1.35 KB
/
spinner.js
File metadata and controls
executable file
·43 lines (33 loc) · 1.35 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
// Usage:
// 1) new Spinner({ elem: elem}) -> start/stop()
// 2) new Spinner() -> somewhere.append(spinner.elem) -> start/stop
function Spinner(options) {
options = options || {};
this.elem = options.elem;
this.size = options.size || 'medium';
// any class to add to spinner (make spinner special here)
this.class = options.class ? (' ' + options.class) : '';
// any class to add to element (to hide it's content for instance)
this.elemClass = options.elemClass;
if (this.size != 'medium' && this.size != 'small' && this.size != 'large') {
throw new Error("Unsupported size: " + this.size);
}
if (!this.elem) {
this.elem = document.createElement('div');
}
}
Spinner.prototype.start = function() {
if (this.elemClass) {
this.elem.classList.toggle(this.elemClass);
}
this.elem.insertAdjacentHTML('beforeend', '<span class="spinner spinner_active spinner_' + this.size + this.class + '"><span class="spinner__dot spinner__dot_1"></span><span class="spinner__dot spinner__dot_2"></span><span class="spinner__dot spinner__dot_3"></span></span>');
};
Spinner.prototype.stop = function() {
var spinnerElem = this.elem.querySelector('.spinner');
if (!spinnerElem) return; // already stopped or never started
spinnerElem.remove();
if (this.elemClass) {
this.elem.classList.toggle(this.elemClass);
}
};
module.exports = Spinner;