-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcomponent.js
113 lines (97 loc) · 2.95 KB
/
component.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
beestat.component = function() {
const self = this;
this.rendered_ = false;
// Give every component a state object to use for storing data.
this.state_ = {};
this.layer_ = beestat.current_layer;
if (this.rerender_on_resize_ === true) {
beestat.dispatcher.addEventListener('resize', function() {
self.rerender();
});
}
};
beestat.extend(beestat.component, rocket.EventTarget);
/**
* First put everything in a container, then append the new container. This
* prevents the child from having to worry about multiple redraws since they
* aren't doing anything directly on the body.
*
* @param {rocket.Elements} parent
*
* @return {beestat.component} This
*/
beestat.component.prototype.render = function(parent) {
if (this.rendered_ === false) {
var self = this;
if (parent !== undefined) {
this.component_container_ = $.createElement('div');
Object.assign(this.component_container_[0].style, Object.assign(
{
'position': 'relative'
},
this.style_
));
this.decorate_(this.component_container_);
parent.appendChild(this.component_container_);
} else {
this.decorate_();
}
// The element should now exist on the DOM.
window.setTimeout(function() {
self.dispatchEvent('render');
}, 0);
// The render function was called.
this.rendered_ = true;
}
return this;
};
/**
* First put everything in a container, then append the new container. This
* prevents the child from having to worry about multiple redraws since they
* aren't doing anything directly on the body.
*
* @return {beestat.component} This
*/
beestat.component.prototype.rerender = function() {
if (this.rendered_ === true) {
this.rendered_ = false;
var new_container = $.createElement('div')
.style('position', 'relative');
this.decorate_(new_container);
this.component_container_
.parentNode().replaceChild(new_container, this.component_container_);
this.component_container_ = new_container;
var self = this;
window.setTimeout(function() {
self.dispatchEvent('render');
}, 0);
this.rendered_ = true;
}
return this;
};
/**
* Remove this component from the page.
*/
beestat.component.prototype.dispose = function() {
if (this.rendered_ === true) {
var child = this.component_container_;
var parent = child.parentNode();
parent.removeChild(child);
this.rendered_ = false;
}
};
beestat.component.prototype.decorate_ = function() {
// Left for the subclass to implement.
};
/**
* Add custom styling to a component container. Mostly useful for when a
* component needs margins, etc applied depending on the context.
*
* @param {object} style
*
* @return {beestat.component} This
*/
beestat.component.prototype.style = function(style) {
this.style_ = style;
return this.rerender();
};