-
Notifications
You must be signed in to change notification settings - Fork 0
/
element.js
145 lines (139 loc) · 4.5 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
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
function Element(id, io){
var This = this;
//create element holder
if(typeof(id) == 'string'){
try{
This.element = document.getElementById(id);
This.id = id;
}catch(e){
alert("Problem initializing Element object with id: " + id + ". \n" + e);
}
}else{
This.element = id;
This.id = This.element.getAttribute('id');
}
if(!io) io = {};
//private units
var units = io.units ? io.units : 'px';
//public properties, width, height, x, y.. etc.
this.x = io.x ? io.x : this.element.offsetLeft;
this.y = io.y ? io.y : this.element.offsetTop;
this.width = io.width ? io.width : false;
this.height = io.height ? io.height : false;
this.alpha = (io.alpha != undefined) ? io.alpha : 100;
this.center = (io.center) ? io.center : false;
this.contentElement = false;
if(io.contentElement)
this.contentElement = document.getElementById(io.contentElement);
this.setDimensions = function(w,h){
if(isNum(w)) { this.element.style.width = (w + units); this.width = w; }
if(isNum(h)) { this.element.style.height = (h + units); this.height = h; }
if(This.center){
var cx = (This.width-w) * This.center.x;
var cy = (This.height-h) * This.center.y;
This.setPosition( This.x + cx, This.y + cy);
}
}
this.setPosition = function(x,y){
if(isNum(y)) { this.element.style.top = y + units; this.y = y; }
if(isNum(x)) { this.element.style.left = x + units; this.x = x; }
if(this.center) this.setDimentions(this.width, this.height);
}
this.setOpacity = function(a){
if(isNum(a)){
this.element.style.opacity = (a/100);
this.element.style.filter = "Alpha(Opacity=" + a + ")";
this.alpha = a;
}
};
this.setStyleProperty = function(property, value){
eval("this.element.style." + property + "=\"" + value + "\"");
}
this.setClass = function(name){
this.element.className = name;
}
this.addClass = function(name){
if(this.element.className.toString().indexOf(name) < 0)
this.element.className = this.element.className + " " + name;
}
this.removeClass = function(name){
var cc = this.element.className.toString();
var e = cc.substring(0,cc.indexOf(name));
var b = cc.substring(cc.indexOf(name) + name.length, cc.length);
this.element.className = b + " " + e;
}
this.hide = function(disp){
this.element.style.visibility = 'hidden';
if(disp) this.element.style.display = disp;
}
this.show = function(disp){
this.element.style.visibility = 'visible';
if(disp) this.element.style.display = disp;
}
this.getRealHeight = function(){
return this.element.offsetHeight;
}
this.getRealWidth = function(){
return this.element.offsetWidth;
}
this.getRealTop = function(){
return this.element.offsetTop;
}
this.getRealLeft = function(){
return this.element.offsetLeft;
}
this.getContent = function(cont){
if(this.contentElement) return this.contentElement.innerHTML;
return this.element.innerHTML;
}
this.setContent = function(cont){
if(this.contentElement) this.contentElement.innerHTML = cont;
else this.element.innerHTML = cont;
}
this.insertContent = function(cont, pos){
var bCont = this.getContent();
if(pos=='before') this.setContent(cont + bCont);
else this.setContent(bCont + cont);
}
this.getChildren = function(tag){
return this.element.getElementsByTagName(tag);
}
this.append = function(type, content){
var ne = document.createElement(type);
this.element.appendChild(ne);
ne.innerHTML = content;
}
this.remove = function(node){
this.element.removeChild(node);
}
this.insert = function(node, before){
this.element.insertBefore(node, before);
}
this.setHandlers = function(io){
//initialize handler events
if(io.onclick) this.element.onclick = io.onclick;
if(io.onrollover) this.element.onmouseover = io.onrollover;
if(io.onrollout) this.element.onmouseout = io.onrollout;
if(io.onpress) this.element.onmousedown = io.onpress;
if(io.onrelease) this.element.onmouseup = io.onrelease;
if(io.ondouble) this.element.ondblclick = io.ondouble;
if(io.onmove) this.element.onmousemove = io.onmove;
}
this.forceAbsolute = function(){
var x = This.x; var y = This.y;
var w = This.width; var h = This.height;
This.element.style.position = 'absolute';
This.setPosition(x,y);
This.setDimensions(w, h);
}
if(this.width || this.height)
this.setDimensions(this.width, this.height);
if(this.x || this.y)
this.setPosition(this.x, this.y);
if(this.alpha)
this.setOpacity(this.alpha);
if(io)
this.setHandlers(io);
//private
function isNum(num){ if(!num) return false; else return typeof(num) == 'number' };
}