-
Notifications
You must be signed in to change notification settings - Fork 25
/
temp_js.h
210 lines (186 loc) · 8.14 KB
/
temp_js.h
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
const char temp_js[] PROGMEM = R"=====(
var json = "none";
var page = 0;
var obj;
var app;
var pages;
var pub_int = []
var pub_int_cnt = 0
function make(){
obj = JSON.parse(json);
app = obj.app;
ID = obj.ID;
pages = obj.menu.length;
menu();
content();
}
function parse(){
var xhr = new XMLHttpRequest();
xhr.open('GET', '/echo', true);
xhr.onload = function () {
json = xhr.responseText;
make();
};
xhr.send(null);
}
function new_page(p){
page = p;
menu();
content();
}
function inner(id, content){
document.getElementById(id).innerHTML += content;
}
function menu_item(name, current, p){
if (!current)
return "<li class=\"pure-menu-item\" onclick=\"new_page(" + p + ")\"><a href=\"#\" class=\"pure-menu-link\">" + name + "</a></li>";
else
return "<li class=\"pure-menu-item menu-item-divided pure-menu-selected\" onclick=\"new_page(" + p + ")\"><a href=\"#\" class=\"pure-menu-link\">" + name + "</a></li>";
}
function menu(){
var content = "<div class=\"pure-menu\"><a class=\"pure-menu-heading\" href=\"#\">" + app + "</a><ul class=\"pure-menu-list\">";
for(var i = 0; i < pages; i++){
var current;
if(page == i) current = true;
else current = false;
content += menu_item(obj.menu[i], current, i);
}
content += "<br><br>";
content += "<p>  ID: <b>" + ID + "</b><p>";
content += "</ul>";
content += "</div>";
document.getElementById("menu").innerHTML = content;
}
function content(){
for(var i = 0; i < pub_int_cnt; i++){
clearInterval(pub_int[i])
}
document.getElementById("main").innerHTML = "";
var content = "<div class=\"header\"><h1>" + obj.menu[page] + "</h1></div>";
content += "<div class=\"content\">";
content += "<form class=\"pure-form pure-form-stacked\">";
content += "<fieldset>";
content += "<div class=\"pure-g\">";
var items = obj.content[page].length;
for(var i = 0; i < items; i++){
content += content_item(obj.content[page][i], i);
}
content += "</div>";
content += "</fieldset>";
content += "</form>";
content += "</div>";
document.getElementById("main").innerHTML = content;
}
function content_item(item, i){
var content = ''
if(item.type == "pub"){
content += "<div class=\"pure-u-1\" style=\"background-color: " + item.bg_color + "; text-align: center; color: " + item.text_color + ";\">"
if(item.label != "") content += "<h2>" + item.label + "</h2>"
content += "<p style=\"font-size: 32pt\" id=\"" + item.id + "\">" + item.value + item.unit + "</p>"
content += "</div>"
pub_int[pub_int_cnt] = setInterval(() => {
pub(item.id, item.unit)
}, 3000);
pub_int_cnt++
return content
}
content += "<div class=\"pure-u-1\">";
if (item.type == "checkbox"){
content += "<br>"
content += "<div style=\"height: 40px;\">"
content += "<input "
if (typeof item.type == 'string') content += "type=\"" + item.type + "\""
if (typeof item.id == 'string') content += "id=\"" + item.id + "\""
if (typeof item.value == 'string' && item.value == "true") content += " checked "
content += "class='checkbox' oninput=\"data('" + item.type + "', this.id, this.value, '" + item.label + "', " + page + ", " + i + ")\">";
content += "<label class='switch' for='" + item.id + "'>"
content += "               " + item.label
content += "</label>"
content += "</div>";
}
else if(item.html == "input"){
content += "<label id=\"" + item.id + "-val\">" + item.label + " "
if(item.type == "text" || item.type == "password") content += "(" + item.value.length + ")"
else content += item.value
content += "</label>";
content += "<input ";
if (typeof item.type == 'string') content += "type=\"" + item.type + "\"";
if (typeof item.id == 'string') content += "id=\"" + item.id + "\"";
if (typeof item.name == 'string') content += "name=\"" + item.name + "\"";
if (typeof item.min == 'string') content += "min=\"" + item.min + "\"";
if (typeof item.max == 'string') content += "max=\"" + item.max + "\"";
if (typeof item.step == 'string') content += "step=\"" + item.step + "\"";
if (typeof item.value == 'string') content += "value=\"" + item.value + "\"";
if (typeof item.placeholder == 'string') content += "placeholder=\"" + item.placeholder + "\"";
content += "class=\"pure-u-5-5\""
if(item.type == "range"){
content += "oninput=\"datarange(this.id, this.value, '" + item.label + "')\" onchange=\"data('" + item.type + "', this.id, this.value, '" + item.label + "', " + page + ", " + i + ")\">";
}
if(item.type == "text" || item.type == "password" || item.type == "number" || item.type == "time" || item.type == "date" || item.type == "datetime-local"){
content += "oninput=\"data('" + item.type + "', this.id, this.value, '" + item.label + "', " + page + ", " + i + ")\">";
}
if(item.type == "color"){
content += "style=\"height: 50px\" oninput=\"data('" + item.type + "', this.id, this.value, '" + item.label + "', " + page + ", " + i + ")\">";
}
}
if(item.html == "button"){
if (typeof item.color != 'string') item.color = ''
content += "<br>";
content += "<input type=\"button\" ";
content += "id=\"" + item.id + "\" style=\"background-color: " + item.color + "\" value=\"" + item.label + "\" class=\"pure-u-5-5 pure-button pure-button-primary\" onclick=\"data('" + item.type + "', 'BTN_' + this.id, this.value, '" + item.label + "', " + page + ", " + i + ")\">";
}
if(item.html == "textarea"){
content += "<textarea class=\"pure-u-24-24\" maxlength=\"255\" id=\"" + item.id + "\" oninput=\"data('" + item.type + "', this.id, this.value, '" + item.label + "', " + page + ", " + i + ")\">" + item.value + "</textarea>";
}
if(item.html == "select"){
content += "<label id=\"" + item.id + "-val\">" + item.label + "</label>";
content += "<select ";
if (typeof item.id == 'string') content += "id=\"" + item.id + "\"";
if (typeof item.name == 'string') content += "name=\"" + item.name + "\"";
if (typeof item.value == 'string') content += "value=\"" + item.value + "\"";
content += "class=\"pure-u-5-5\"";
content += "oninput=\"data(" + item.type + ", this.id, this.value, '" + item.label + "', '" + page + "', '" + i + "')\">";
for(var i = 0; i < item.options.length; i++){
if (item.options[i].value == item.value) content += "<option value=\"" + item.options[i].value + "\" selected>";
else content += "<option value=\"" + item.options[i].value + "\">";
content += item.options[i].label;
content += "</option >";
}
content += "</select>";
}
content += "</div>";
return content;
}
function datarange(id, value, label){
document.getElementById(id + "-val").innerHTML = label + ": " + value;
}
function data(type, id, value, label, page, i){
if (type == "range") document.getElementById(id + "-val").innerHTML = label + ": " + value;
if (type == "text" || type == "password") document.getElementById(id + "-val").innerHTML = label + " (" + value.length + ")";
if (type == "checkbox"){
var chbox=document.getElementById(id);
if (chbox.checked) value = "true";
else value = "false";
}
obj.content[page][i].value = value;
send(id, value)
}
function send(id, value){
var formData = new FormData();
formData.append(id, value);
var xhr = new XMLHttpRequest();
xhr.open('POST', '/post', true);
xhr.onload = function () {
var res = xhr.responseText;
};
xhr.send(formData);
}
function pub(id, unit){
var xhr = new XMLHttpRequest();
xhr.open('GET', '/pub?' + id, true);
xhr.onload = function () {
document.getElementById(id).innerHTML = xhr.responseText + unit;
};
xhr.send();
}
)=====";