-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.js
165 lines (141 loc) · 5.89 KB
/
parser.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
class QLCParser {
constructor(updateCallback) {
this._updateCallback = updateCallback;
this._content = null;
this.parser = new DOMParser();
this.xml = null;
}
set content(content) {
this._content = content;
this._updateCallback(content);
this.xml = this.parser.parseFromString(content, 'text/xml');
}
get content() {
return this._content;
}
set fixtureList(fixtures) {
let parent = this.xml.querySelector('Engine');
this.xml.querySelectorAll('Engine > Fixture').forEach((fixture) => {
fixture.remove();
});
for (let fixture of fixtures) {
parent.appendChild(fixture);
}
this.updateContent();
}
get fixtureList() {
return this.xml.querySelectorAll('Engine > Fixture');
}
updateContent() {
this._content = new XMLSerializer().serializeToString(this.xml).replace(/xmlns="[^"]*" ?/, '');
this._updateCallback(this._content);
}
sortFixtures() {
let fixtures = this.fixtureList;
this.fixtureList = Array.from(fixtures).sort((a, b) => {
let aID = parseInt(a.querySelector('ID').textContent);
let bID = parseInt(b.querySelector('ID').textContent);
return aID - bID;
});
}
seperateIndexValue(text) {
let values = {};
let pairs = text.split(',');
for (let i = 0; i < pairs.length; i+=2) {
values[pairs[i]] = pairs[i+1];
}
return values;
}
updateChannelgroups() {
let channelgroups = this.xml.querySelectorAll('ChannelsGroup');
let scenes = this.xml.querySelectorAll('Function[Type="Scene"]:has(ChannelGroupsVal)');
for (let scene of scenes) {
let channelgroupsVal = scene.querySelector('ChannelGroupsVal');
let values = this.seperateIndexValue(channelgroupsVal.textContent);
let fixtureValues = scene.querySelectorAll('FixtureVal');
for (let fixtureValue of fixtureValues) {
fixtureValue.remove();
}
Object.entries(values).forEach(([fixturegroupid, value]) => {
let devices = this.seperateIndexValue(channelgroups[fixturegroupid].textContent);
Object.entries(devices).forEach(([deviceid, channel]) => {
let fixtureValue = scene.querySelector(`FixtureVal[ID="${deviceid}"]`) || this.xml.createElement('FixtureVal');
fixtureValue.setAttribute('ID', deviceid);
if (fixtureValue.textContent) {
fixtureValue.textContent += ',';
}
fixtureValue.textContent += `${channel},${value}`;
scene.appendChild(fixtureValue);
});
});
}
this.updateContent();
}
updateChannelgroupsSingle(id) {
let channelgroups = this.xml.querySelectorAll('ChannelsGroup');
let scenes = this.xml.querySelectorAll('Function[Type="Scene"]:has(ChannelGroupsVal)');
for (let scene of scenes) {
let channelgroupsVal = scene.querySelector('ChannelGroupsVal');
let values = this.seperateIndexValue(channelgroupsVal.textContent);
let fixtureValues = scene.querySelectorAll(`Function[ID="${id}"] > FixtureVal`);
for (let fixtureValue of fixtureValues) {
fixtureValue.remove();
}
Object.entries(values).forEach(([fixturegroupid, value]) => {
let devices = this.seperateIndexValue(channelgroups[fixturegroupid].textContent);
Object.entries(devices).forEach(([deviceid, channel]) => {
let fixtureValue = scene.querySelector(`FixtureVal[ID="${deviceid}"]`) || this.xml.createElement('FixtureVal');
fixtureValue.setAttribute('ID', deviceid);
if (fixtureValue.textContent) {
fixtureValue.textContent += ',';
}
fixtureValue.textContent += `${channel},${value}`;
scene.appendChild(fixtureValue);
});
});
}
this.updateContent();
}
get functionTree() {
let xmlfunctions = this.xml.querySelectorAll('Engine > Function');
let functions = {};
for (let xmlfunction of xmlfunctions) {
let type = xmlfunction.getAttribute('Type');
let name = xmlfunction.getAttribute('Name');
let id = xmlfunction.getAttribute('ID');
let path = xmlfunction.getAttribute('Path') || '';
let functionType = functions[type] || {};
let pathArray = path.split('/');
if (pathArray[0] === '') {
pathArray.shift();
}
let current = functionType;
for (let i = 0; i < pathArray.length; i++) {
let pathPart = pathArray[i];
if (!current[pathPart]) {
current[pathPart] = {};
}
current = current[pathPart];
}
current[name] = id;
functions[type] = functionType;
}
return functions;
}
renameFunction(id, name) {
let functionElement = this.xml.querySelector(`Engine > Function[ID="${id}"]`);
functionElement.setAttribute('Name', name);
this.updateContent();
}
addFolderInFunctionName(folderPath, folderName) {
let functionElements = this.xml.querySelectorAll(`Engine > Function[Path="${folderPath}"]`);
for (let functionElement of functionElements) {
let name = functionElement.getAttribute('Name');
if (name.includes(folderName)) {
continue;
}
functionElement.setAttribute('Name', `${folderName}/${name}`);
}
this.updateContent();
}
}