-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
92 lines (78 loc) · 1.87 KB
/
utils.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
function loadText(url, callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open("GET", url, true);
xobj.onreadystatechange = function () {
if (xobj.readyState == 4 && xobj.status == 200) {
callback(xobj.responseText);
}
}
xobj.send(null);
}
function loadTSV(url, callback) {
loadText(url + '.tsv', function(text){
const lines = text.split("\n").slice(1, -1);
const rows = lines.map(function(line) {
return line.split("\t");
});
callback(rows);
});
}
function loadCSVFromText(text, callback) {
const splat = text.split("\n");
const header = splat[0].split(",");
const lines = splat.slice(1, -1);
const rows = lines.map(function(line) {
const row = {};
line.split(",").forEach(function(cellValue, index) {
row[header[index]] = cellValue;
});
return row;
});
callback(rows);
}
function getById(id) {
return document.getElementById(id);
}
function getValue(id) {
return getById(id).value;
}
function setValue(id, value) {
getById(id).value = value;
}
function hideById(id) {
getById(id).style.display = "none";
}
function showById(id) {
getById(id).style.display = "block";
}
function setStateById(id, key, value) {
getById(id).setAttribute(key, value);
}
function removeClass(id, className) {
var tag = getById(id);
tag.className = tag.className.replace(className, "").trim();
}
function addClass(id, className) {
var tag = getById(id);
tag.className = (tag.className.replace(className, "") + " " + className).trim();
}
function serializeXmlNode(xmlNode) {
if (typeof window.XMLSerializer != "undefined") {
return (new window.XMLSerializer()).serializeToString(xmlNode);
} else if (typeof xmlNode.xml != "undefined") {
return xmlNode.xml;
}
return "";
}
function toNumber(inpt) {
try {
if (inpt !== "") {
return Number(inpt);
} else {
return inpt;
}
} catch (error) {
return inpt;
}
}