-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstage.js
99 lines (75 loc) · 2.03 KB
/
stage.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
/**
* @class Stage
* Manages a stack of layers
*
* @param {Snap.Element} paper Snap parent element
*/
export class Stage {
constructor(paper) {
this.paper = paper;
this.layers = [];
this._layer = paper.g().addClass('stage');
}
/**
* push
* Place object at front of stage and display its layer
* Calls onHidden on object at top of old stack, and onVisible on new top of stack
* @param {Object} layer Object with `_layer` property, which is a Snap.Element
* @return layer
*/
push(layer) {
let layerCount = this.layers.length;
if (layerCount > 0) {
let top = this.layers[layerCount - 1];
if (top.onHidden) {
top.onHidden();
}
}
this.layers.push(layer);
this._layer.clear();
this._layer.add(layer._layer);
if (layer.onVisible) {
layer.onVisible();
}
return layer;
}
/**
* pop
* Remove topmost object from stage, removing its `_layer` property and calling `remove`
* Also calls onHidden on layer being removed, and onVisible on object at top of stack
* @return The popped object
*/
pop() {
const top = this.layers.pop();
if (top) {
if (top.onHidden) {
top.onHidden();
}
top.remove();
}
this._layer.clear();
const layerCount = this.layers.length;
if (layerCount > 0) {
let newTop = this.layers[layerCount - 1];
this._layer.add(newTop._layer);
if (newTop.onVisible) {
newTop.onVisible();
}
}
return top;
}
/**
* clear
* Pops all layers from stack
*/
clear() {
this._layer.clear();
while (this.layers.length > 0) {
let p = this.layers.pop();
if (p) {
p.onHidden && p.onHidden();
p.remove && p.remove();
}
}
}
};