forked from Meiguro/simplyjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelement.js
106 lines (88 loc) · 2.49 KB
/
element.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
var util2 = require('util2');
var Vector2 = require('vector2');
var myutil = require('myutil');
var WindowStack = require('ui/windowstack');
var Propable = require('ui/propable');
var simply = require('ui/simply');
var elementProps = [
'position',
'size',
'borderColor',
'backgroundColor',
];
var accessorProps = elementProps;
var nextId = 1;
var StageElement = function(elementDef) {
this.state = elementDef || {};
this.state.id = nextId++;
this._queue = [];
};
util2.copy(Propable.prototype, StageElement.prototype);
Propable.makeAccessors(accessorProps, StageElement.prototype);
StageElement.prototype._reset = function() {
this._queue = [];
};
StageElement.prototype._id = function() {
return this.state.id;
};
StageElement.prototype._type = function() {
return this.state.type;
};
StageElement.prototype._prop = function(elementDef) {
if (!this.state.position) {
this.state.position = new Vector2();
}
if (!this.state.size) {
this.state.size = new Vector2();
}
if (this.parent === WindowStack.top()) {
simply.impl.stageElement(this._id(), this._type(), elementDef);
}
};
StageElement.prototype.index = function() {
if (!this.parent) { return -1; }
return this.parent.index(this);
};
StageElement.prototype.remove = function(broadcast) {
if (!this.parent) { return this; }
this.parent.remove(this, broadcast);
return this;
};
StageElement.prototype._animate = function(animateDef, duration) {
if (this.parent === WindowStack.top()) {
simply.impl.stageAnimate(this._id(), animateDef, duration || 400, animateDef.easing || 'easeInOut');
}
};
StageElement.prototype.animate = function(field, value, duration) {
if (typeof field === 'object') {
duration = value;
}
var animateDef = myutil.toObject(field, value);
util2.copy(animateDef, this.state);
function animate() {
this._animate(animateDef, duration);
}
if (this._queue.length === 0) {
animate.call(this);
} else {
this.queue(animate);
}
return this;
};
StageElement.prototype.queue = function(callback) {
this._queue.push(callback);
};
StageElement.prototype.dequeue = function() {
var callback = this._queue.shift();
if (!callback) { return; }
callback.call(this, this.dequeue.bind(this));
};
StageElement.emitAnimateDone = function(index) {
if (index === -1) { return; }
var wind = WindowStack.top();
if (!wind || !wind._dynamic) { return; }
var element = wind.at(index);
if (!element) { return; }
element.dequeue();
};
module.exports = StageElement;