-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparamlist2.html
More file actions
144 lines (118 loc) · 3.76 KB
/
Copy pathparamlist2.html
File metadata and controls
144 lines (118 loc) · 3.76 KB
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
<!DOCTYPE html>
<head>
<style>
table, td, th {
border: 1px solid lightgray;
border-collapse: collapse;
}
td, th {
padding: 4px;
}
</style>
<script>
class Paramlist extends HTMLElement {
constructor()
{
super();
this._values = [];
this.change_list = [];
this.input_nodes = [];
}
render(names, header)
{
let th = header.split(',');
if (th.length < 4)
{
th = ['Param', 'Value', 'New', 'Action'];
}
let html_str = `
<table>
<tr>
<th>${th[0].trim()}</th>
<th>${th[1].trim()}</th>
</tr>`;
for (const n of names.split(','))
{
const name = n.trim();
html_str += `<tr>
<td>${name}</td>
<td><input type="number" value="0"></td>
</tr>`;
this.values.push(0);
}
html_str += '</table>';
this.innerHTML = html_str;
this.tbody = this.querySelector('table tbody');
this.add_event_listeners();
}
add_event_listeners()
{
for (let i=1; i<this.tbody.children.length; i++)
{
const node = this.tbody.children[i].children[1].firstChild;
this.input_nodes.push(node)
node.addEventListener('keyup', (e) => {if (e.key == "Enter") this.change(i)});
}
}
change(i)
{
console.log(`request ${i} = ${this.input_nodes[i-1].value}`);
this.change_list.push([i-1, this.input_nodes[i-1].value]);
this.input_nodes[i-1].blur();
this.dispatchEvent(new CustomEvent('chgrequest'));
}
set changes(list)
{
this.change_list = list;
}
get changes()
{
const r = this.change_list;
this.change_list = [];
return r;
}
connectedCallback()
{
const names = this.attributes.names.value;
const header = this.attributes.header.value;
this.render(names, header);
}
update_values()
{
for (let i=1; i<this.tbody.children.length; i++)
{
if (this.input_nodes[i-1] !== document.activeElement)
{
this.input_nodes[i-1].value = this._values[i-1];
}
}
}
set values(v)
{
this._values = v;
this.update_values();
}
get values()
{
return this._values;
}
}
window.customElements.define('pwtk-paramlist', Paramlist);
</script>
</head>
<html>
<body>
<h1>Web component testing ground</h1>
<pwtk-paramlist id='pl-id' names='A, B, C, D' header='Name, Value, New, Action' ></pwtk-bitfield>
</body>
<script>
let test_cnt = 0;
const pl = document.getElementById("pl-id");
function test()
{
pl.values = [test_cnt, test_cnt, test_cnt, test_cnt];
test_cnt++;
}
setInterval(test, 1000);
</script>
</html>