-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow_builder.js
172 lines (168 loc) · 4.35 KB
/
window_builder.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//DOM42 objects enable adding widgets to windows
class DOM42 {
constructor(win) {
this.win = win;
this.add = function(obj) {
this.win.el.body.appendChild(obj.elem);
}
this.remove = function(obj) {
this.win.el.body.removeChild(obj.elem);
}
}
}
/*
Instead of var root = new DOM42($window({...}));
you can do var root = $window42({...});
but if you want to have access to your window
as $window and DOM42 object, do this:
var win = $window({...});
var root = new DOM42(win);
*/
function $window42(a) {
return new DOM42($window(a));
}
//All widgets go here
var $widget = {
msr: 'px',
//positionBy("pixel") = size in pixels, positionBy("relative") = size relative to size of window (100 = window height / width)
positionBy(t) {
ptype = {'pixel': 'px', 'relative': '%'};
$widget.msr = ptype[t];
},
Export() {
/*
This function makes all $widgets global
so instead of $widget.Button you can do
just Button
*/
for(var item in this) {
eval(item + '=' + '$widget.' + item);
}
},
Button(text) {
var elem = document.createElement('button');
elem.innerText = text;
return {
elem: elem,
onClick(f) {
this.elem.onclick = f;
},
setPosition(x, y, w, h) {
this.elem.style.position = 'absolute';
this.elem.style.top = y.toString() + $widget.msr;
this.elem.style.left = x.toString() + $widget.msr;
if(w != undefined) {
this.elem.style.width = w.toString() + $widget.msr;
}
if(h != undefined) {
this.elem.style.height = h.toString() + $widget.msr;
}
},
setStyle(s) {
this.elem.style = {...this.elem.style, ...style};
}
}
},
Label(text, size) {
var elem = document.createElement('p');
if(size != undefined) {
elem.style.fontSize = size.toString() + $widget.msr;
}
elem.innerText = text;
return {
elem: elem,
setPosition(x, y) {
this.elem.style.position = 'absolute';
this.elem.style.top = y.toString() + $widget.msr;
this.elem.style.left = x.toString() + $widget.msr;
},
setStyle(s) {
this.elem.style = {...this.elem.style, ...style};
}
}
},
Entry(size) {
var elem = document.createElement('textarea');
if(size != undefined) {
elem.style.fontSize = size.toString() + $widget.msr;
}
return {
elem: elem,
setPosition(x, y, w, h) {
this.elem.style.position = 'absolute';
this.elem.style.top = y.toString() + $widget.msr;
this.elem.style.left = x.toString() + $widget.msr;
if(w != undefined) {
this.elem.style.width = w.toString() + $widget.msr;
}
if(h != undefined) {
this.elem.style.height = h.toString() + $widget.msr;
}
},
setText(text) {
this.elem.value = text;
},
getText(text) {
return this.elem.value;
},
setStyle(s) {
this.elem.style = {...this.elem.style, ...style};
}
}
},
Input(type, size) {
var elem = document.createElement('input');
if(type != undefined) {
elem.type = type;
}
if(size != undefined) {
elem.style.fontSize = size.toString() + $widget.msr;
}
return {
elem: elem,
setPosition(x, y, w, h) {
this.elem.style.position = 'absolute';
this.elem.style.top = y.toString() + $widget.msr;
this.elem.style.left = x.toString() + $widget.msr;
if(w != undefined) {
this.elem.style.width = w.toString() + $widget.msr;
}
if(h != undefined) {
this.elem.style.height = h.toString() + $widget.msr;
}
},
setText(text) {
this.elem.value = text;
},
getText(text) {
return this.elem.value;
},
setStyle(s) {
this.elem.style = {...this.elem.style, ...style};
}
}
},
Section() {
var elem = document.createElement('div');
elem.style.border = '2px inset';
elem.style.overflow = 'auto';
return {
elem: elem,
el: {body: elem}, //So a section could be turned into DOM42 element
setPosition(x, y, w, h) {
this.elem.style.position = 'absolute';
this.elem.style.top = y.toString() + $widget.msr;
this.elem.style.left = x.toString() + $widget.msr;
if(w != undefined) {
this.elem.style.width = w.toString() + $widget.msr;
}
if(h != undefined) {
this.elem.style.height = h.toString() + $widget.msr;
}
},
setStyle(s) {
this.elem.style = {...this.elem.style, ...style};
}
}
}
}