-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathjsutils.js
111 lines (99 loc) · 3.1 KB
/
jsutils.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
/* Author: Willem Hengeveld <itsme@xs4all.nl> */
/* https://github.com/nlitsme/bitcoinexplainer */
"use strict";
// some functions for helping create html tags.
function el()
{
// create an html element by tagname, optionally adding children and attributes.
// arguments of:
// - instance 'Element' are added as children,
// - type string, are passed to replaceChildren, and converted to text
// - objects and Map are treated as attribute lists.
if (arguments.length==0)
throw "el needs at least one argument";
var tag = arguments[0];
var e = document.createElement(tag);
var args = [];
var attr;
for (var i=1 ; i<arguments.length ; i++)
{
var arg = arguments[i];
if (arg instanceof Element)
args.push(arg);
else if (arg instanceof Map)
arg.forEach( (v,k)=>e.setAttribute(k, v) );
else if (typeof(arg) == "string")
args.push(arg);
else if (typeof(arg) == "object")
Object.entries(arg).forEach(([k,v])=>e.setAttribute(k, v))
else
throw "unsupported el argument";
}
if (args)
e.replaceChildren(...args);
return e;
}
function makeslider(label, idtag, minvalue, maxvalue, initval, texttoval, valtotext)
{
// create a slider with label, min/max, value converters.
var lbl = el('span')
lbl.innerHTML = label;
var disp = el('span')
disp.id = idtag+".value";
var inp = el('input')
inp.id = idtag;
inp.type = "range";
inp.min = minvalue;
inp.max = maxvalue;
inp.value = initval;
inp.oninput = function() { texttoval(this.value); disp.innerHTML = valtotext(); update(); };
//inp.style.transform = "rotate(90deg)";
//inp.style.width = 100;
//inp.style.height = 100;
texttoval(inp.value);
disp.innerHTML = valtotext();
var div = el('div', {id:idtag+"div"});
div.append(inp, lbl, disp);
return div;
}
function decodecsv(txt)
{
// decode a csv file to an array of objects.
var table = [];
var names;
for (var line of txt.split("\n")) {
if (!line) continue;
fields = line.split(",");
if (!names)
names = fields;
else
table.push(Object.fromEntries(names.map( (name, i)=> [name, fields[i]] )))
}
return table;
}
// various formatting functions.
function fmtdate(t)
{
// format a time as yyyy-mm-dd
var y = t.getFullYear();
var m = (t.getMonth()+1).toLocaleString('en-US', {minimumIntegerDigits: 2, useGrouping:false});
var d = t.getDate().toLocaleString('en-US', {minimumIntegerDigits: 2, useGrouping:false});
return y+"-"+m+"-"+d;
}
function makedatetd(t)
{
// create a center aligned table cell with a formatted date
var td = el('td', fmtdate(t));
td.style.textAlign = 'center';
return td;
}
function makenumtd(txt, decimals=0)
{
// create a right aligned table cell, for numbers mostly,
// format with the specified number of decimals.
if (typeof(txt)=="number")
txt = txt.toFixed(decimals);
var td = el('td', txt);
td.style.textAlign = 'right';
return td;
}