-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinputHelpers.js
281 lines (239 loc) · 7.66 KB
/
inputHelpers.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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*jshint esversion: 6 */
// @ts-check
// useful utility function for creating HTML
/**
* https://plainjs.com/javascript/manipulation/insert-an-element-after-or-before-another-32/
* inserts an element after another element (referenceNode)
* @param {HTMLElement} el
* @param {HTMLElement} referenceNode
*/
export function insertAfter(el, referenceNode) {
referenceNode.parentNode.insertBefore(el, referenceNode.nextSibling);
}
/**
* allow for flexible insertion
*
* Note: an HTMLElement works to meet WhereSpec since it has appendChild
* @typedef WhereSpec
* @property [after]
* @property [end]
* @property [appendChild]
* /
/**
* insert an element into the DOM - uses a WhereSpec to figure out where to
* put it
*
* @param {HTMLElement} el
* @param {WhereSpec} [where]
*/
export function insertElement(el, where=undefined) {
if (!where) {
console.log("Warning: appending element to end of body because WHERE can't figure out a better place");
document.body.appendChild(el);
} else if (where.appendChild) {
where.appendChild(el);
} else if (where.after) {
insertAfter(el,where.after);
} else if (where.end) {
where.end.appendChild(el);
} else {
console.log("Warning: appending element to end of body because WHERE can't figure out a better place");
document.body.appendChild(el);
}
}
/**
*
* @param {String} str
* @param {WhereSpec} [where]
* @param {String} [label]
*/
export function makeCheckbox(str,where,label=undefined) {
label = label ? label : str;
let safename = str.replace(/ /g,str);
let checkbox = document.createElement("input");
checkbox.setAttribute("type","checkbox");
checkbox.id = "check-"+safename;
insertElement(checkbox,where);
let checklabel = document.createElement("label");
checklabel.setAttribute("for","check-"+safename);
checklabel.innerText = label;
insertAfter(checklabel,checkbox);
return checkbox;
}
export function makeButton(str,where) {
let safename = str.replace(/ /g,str);
let button = document.createElement("button");
button.innerHTML = str;
insertElement(button,where);
return button;
}
export function makeBoxDiv(params,where) {
if (!params) params = {};
if (!params.margin) params.margin = 5;
if (!params.padding) params.padding = 5;
let style = `border:2px solid black; padding:${params.padding}px; margin:${params.margin}px; border-radius:5px`;
if (params.width) style += `; width:${Number(params.width) - 2*params.margin}px`;
let div = document.createElement("div");
div.setAttribute("style",style);
insertElement(div,where);
return div;
}
export function makeFlexDiv(where) {
let style = "display: flex; flex-direction: row;";
let div = document.createElement("div");
div.setAttribute("style",style);
insertElement(div,where);
return div;
}
export function makeOutbox(str,where, label) {
label = label ? label : str;
let safename = str.replace(/ /g,str);
let text = document.createElement("input");
insertElement(text,where);
text.id = name+"-text";
text.setAttribute("type","text");
text.style.width = "50px";
text.setAttribute("readonly","1");
let checklabel = document.createElement("label");
checklabel.setAttribute("for","check-"+safename);
checklabel.innerText = label;
insertAfter(checklabel,text);
return text;
}
/**
*
* @param {Array<String>} values
* @param {WhereSpec} where
* @param {string} [initial]
* @returns {HTMLSelectElement}
*/
export function makeSelect(values, where, initial) {
let select = document.createElement("select");
values.forEach(function(ch) {
let opt = document.createElement("option");
opt.value = ch;
opt.text = ch;
select.add(opt);
if (initial) select.value = initial;
});
insertElement(select,where);
return select;
}
/**
* just stick a break in (to start a new line)
*/
export function makeBreak(where) {
let br=document.createElement("BR");
insertElement(br,where);
return br;
}
/**
* Create a Heading and stick it into the DOM
*
* extra control to get rid of space above/below
*
* @param {string} text
* @param {WhereSpec} where
* @param {Object} params
* @property {number} [top]
* @property {number} [bottom]
* @property {boolean} [tight]
* @property {number} [level=3]
*/
export function makeHead(text,where,params={}) {
let style = "";
if ("top" in params) style += `margin-top:${params.top}px;`;
if ("bottom" in params) style += `margin-bottom:${params.bottom}px;`;
if ("tight" in params) style += `margin-top:0;margin-bottom:0`;
let level = params.level || 3;
let htype = "H"+level;
let head = document.createElement(htype);
head.setAttribute("style",style);
head.innerText = text;
insertElement(head,where);
return head;
}
export function makeParagraph(text,where) {
let par = document.createElement("p");
par.innerText = text;
insertElement(par,where);
return par;
}
export function makeSpan(text,where) {
let par = document.createElement("span");
par.innerText = text;
insertElement(par,where);
return par;
}
/**
* Label Slider is a class (since you might want to access the component things)
*
* This makes a slider and a corresponding label and textbox for the value
*/
export class LabelSlider {
/**
*
* @param {string} name
* @param {Object} params
* @param {number} [params.width = 250]
* @param {number} [params.min = -1]
* @param {number} [params.max = -1]
* @param {number} [params.step = .1]
* @param {number} [params.initial = 0]
* @param {function} [params.oninput]
* @param {WhereSpec} [params.where]
*/
constructor(name,params) {
let min = params.min || 0;
let max = params.max || 1;
let step =params.step || 0.1;
let initial = params.initial || 0;
let width = params.width || 250;
this.div = document.createElement("div");
this.label = document.createElement("label");
this.label.setAttribute("for",name+"-text");
this.label.setAttribute("style","padding:5px; width:40px; display:inline-block;");
this.label.innerText = name;
this.div.appendChild(this.label);
this.text = document.createElement("input");
this.div.appendChild(this.text);
this.text.id = name+"-text";
this.text.setAttribute("type","text");
this.text.setAttribute("style","width:40px");
this.text.setAttribute("readonly","1");
this.range = document.createElement("input");
this.div.appendChild(this.range);
this.range.id = name + "-slider";
this.range.setAttribute("type","range");
this.range.setAttribute("style",`width:${width-120}px`);
// give default values for range
this.setRange(min,max,step);
this.range.value = String(initial);
this.oninput = params.oninput;
let self=this;
function fupdate() {
self.update();
}
this.range.oninput = fupdate;
this.update();
if ("where" in params) {
insertElement(this.div,params.where);
}
}
setRange(min,max,step) {
this.range.setAttribute("min",String(min));
this.range.setAttribute("max",String(max));
this.range.setAttribute("step",String(step));
}
update() {
this.text.value = Number(this.range.value).toFixed(2);
if (this.oninput) this.oninput(this);
}
value() {
return Number(this.range.value);
}
set(val) {
this.range.value = String(val);
this.update();
}
}