-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodal.js
66 lines (59 loc) · 1.73 KB
/
modal.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
/*global Snap, mina */
/**
* Modal
* Manages content that can be hidden or displayed at will
* @param {Snap.Element} paper Element to add modal and content as child
* @param {Snap.Element} content Child content to show
* @param {number} fadeTime Time in milliseconds to take fading in and out
*/
export class Modal {
constructor(paper, content, fadeTime=300, visible=false) {
this.paper = paper;
this._layer = this.paper.g();
this.content = content;
this._layer.add(content);
this.fadeTime = fadeTime;
// make content invisible at start
if (!visible)
this._layer.attr({opacity: 0});
}
show() {
let p = new Promise((resolve, reject) => {
if (this._layer.attr().opacity == 1)
resolve();
else if (this.fadeTime == 0) {
resolve();
this._layer.attr({opacity: 1});
} else {
this._layer.animate(
{opacity: 1},
this.fadeTime,
mina.linear,
() => resolve()
);
}
});
return p;
}
hide() {
let p = new Promise((resolve, reject) => {
if (this._layer.attr().opacity == 0)
resolve();
else if (this.fadeTime == 0) {
resolve();
this._layer.attr({opacity: 0});
} else {
this._layer.animate(
{opacity: 0},
this.fadeTime,
mina.linear,
() => resolve()
);
}
});
return p;
}
remove() {
this._layer.remove();
}
}