-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathValueTree.js
More file actions
81 lines (81 loc) · 1.93 KB
/
ValueTree.js
File metadata and controls
81 lines (81 loc) · 1.93 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
console.log('ValueTree v1.01');
function ValueTree(name) {
this.children = [];
this.value = "";
this.name = name;
return this;
}
ValueTree.prototype.of = function (name) {
for (var i = 0; i < this.children.length; i++) {
if (this.children[i].name == name) {
return this.children[i];
}
}
var v = new ValueTree(name);
this.children.push(v);
return v;
};
ValueTree.prototype.numeric = function (minValue, defaultValue, maxValue) {
var r = defaultValue;
try {
r = Number.parseFloat(this.value);
} catch (ex) {
console.log(ex);
}
if (isNaN(r)) {
r = defaultValue;
}
if (r < minValue) {
r = minValue;
}
if (r > maxValue) {
r = maxValue;
}
return r;
};
ValueTree.prototype.inlist = function (values) {
for(var i=0;i<values.length;i++){
if(this.value==values[i]){
return this.value;
}
}
return values[0];
};
ValueTree.prototype.all = function (name) {
var r = [];
for (var i = 0; i < this.children.length; i++) {
if (this.children[i].name == name) {
r.push(this.children[i]);
}
}
return r;
};
ValueTree.prototype.fromXMLstring = function (xml) {
var windowDOMParser = new window.DOMParser();
var dom = windowDOMParser.parseFromString(xml, "text/xml");
this.fromNodes(dom.childNodes);
};
ValueTree.prototype.dump = function (pad,symbol) {
console.log(pad,this.name,':',this.value);
for (var i = 0; i < this.children.length; i++) {
this.children[i].dump(pad+symbol,symbol);
}
};
ValueTree.prototype.fromNodes = function (nodes) {
for (var i = 0; i < nodes.length; i++) {
if (nodes[i].nodeType == 1) {
var v = new ValueTree(nodes[i].nodeName);
this.children.push(v);
v.fromNodes(nodes[i].childNodes);
for (var n = 0; n < nodes[i].attributes.length; n++) {
var a = new ValueTree(nodes[i].attributes[n].name);
a.value = nodes[i].attributes[n].value;
v.children.push(a);
}
} else {
if (nodes[i].nodeType == 3) {
this.value = this.value + nodes[i].nodeValue.trim();
}
}
}
};