-
Notifications
You must be signed in to change notification settings - Fork 9
/
Container.js
154 lines (129 loc) · 4.29 KB
/
Container.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
var Container = pc.createScript('container');
Container.attributes.add('id', { type : 'string' });
Container.attributes.add('onInit', { type : 'string' });
Container.attributes.add('onInitTrigger', { type : 'string' });
Container.attributes.add('innerHTML', { type : 'string' });
Container.attributes.add('onDestroy', { type : 'string' });
Container.attributes.add('autoResize', { type : 'boolean', default : true });
Container.attributes.add('fullyRemove', { type : 'boolean' });
Container.attributes.add('showOnly', {
type: 'string',
enum: [
{ 'Both' : 'Both' },
{ 'Desktop' : 'Desktop' },
{ 'Mobile' : 'Mobile' }
],
default: 'Both'
});
Container.prototype.initialize = function() {
if(Utils.isMobile()){
if(this.showOnly == 'Desktop'){
this.entity.destroy();
return false;
}
}else{
if(this.showOnly == 'Mobile'){
this.entity.destroy();
return false;
}
}
this.isDestroyed = false;
this.lastUpdate = Date.now();
this.triggerOnInit();
this.on('state', function(self){
if(this.entity.enabled){
this.triggerOnInit();
this.element.style.display = 'block';
}else{
if(this.fullyRemove){
this.element.remove();
}else{
this.element.style.display = 'none';
}
}
}, this);
this.app.on('DOM:Clear', this.onDOMClear, this);
this.app.on('DOM:Update', this.onDomUpdate, this);
this.on('destroy', this._onDestroy, this);
};
Container.prototype.onDOMClear = function(){
this.entity.enabled = false;
};
Container.prototype._onDestroy = function(){
this.isDestroyed = true;
if(this.onDestroy !== 'undefined'){
try{
eval(this.onDestroy);
}catch(e){
//console.log(e);
}
try{
if(this.fullyRemove){
this.element.remove();
}
}catch(e){
}
}
};
Container.prototype.onDomUpdate = function(){
this.updateStyle();
};
Container.prototype.triggerOnInit = function() {
//if there is already a div with same id set it as an element
var element = document.getElementById(this.id);
if(element){
this.element = element;
this.element.style.width = this.entity.element.width + 'px';
this.element.style.height = this.entity.element.height + 'px';
}else{
this.element = document.createElement('div');
this.element.style.width = this.entity.element.width + 'px';
this.element.style.height = this.entity.element.height + 'px';
this.element.id = this.id;
if(this.innerHTML){
this.element.innerHTML = this.innerHTML;
}
this.element.style.position = 'absolute';
document.body.appendChild(this.element);
}
this.element.style.overflow = 'hidden';
this.updateStyle();
this._onInit();
};
Container.prototype._onInit = function() {
if(this.onInit !== 'undefined'){
try{
eval(this.onInit);
}catch(e){
//console.log(e);
}
}
if(this.onInitTrigger){
this.app.fire(this.onInitTrigger, true);
}
};
Container.prototype.updateStyle = function() {
if(this.isDestroyed){
return false;
}
if(
this.entity &&
this.entity.enabled &&
this.entity.element &&
this.entity.element.screenCorners
){
var position = this.entity.element.screenCorners;
var ratio = 1 / this.app.graphicsDevice.maxPixelRatio;
this.element.style.left = position[0].x * ratio + 'px';
this.element.style.bottom = position[0].y * ratio + 'px';
this.element.style.position = 'absolute';
this.element.style.display = 'block';
this.element.style.zIndex = 1000;
var scaleX = ((position[2].x - position[0].x) * ratio) / this.entity.element.width;
var scaleY = ((position[2].y - position[0].y) * ratio) / this.entity.element.height;
if(this.autoResize){
this.element.style.transform = 'scale(' + scaleX + ', ' + scaleY + ')';
this.element.style.transformOrigin = 'left bottom';
}
}
};