-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.js
154 lines (133 loc) · 4.61 KB
/
ui.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
const parser = new QLCParser((content) => {
$('#file-data').text(content);
});
var selectedPath = [];
var selectedFunction = null;
function loadFile(event) {
let fileInput = event.target;
let file = fileInput.files[0];
$('#btn-open').hide();
$('#txt-filename').text(file.name).show();
$('#btn-save').show();
document.title = file.name;
let reader = new FileReader();
reader.onload = function(e) {
parser.content = e.target.result;
onFileLoaded();
};
reader.readAsText(file);
}
function openFile() {
$('#qlcfile').click();
}
function onFileLoaded() {
updateFixtures();
updateFunctions();
}
function updateFixtures() {
$('#fixtures').empty();
let fixtures = parser.fixtureList;
for (let fixture of fixtures) {
let name = fixture.querySelector('Name').textContent;
let id = fixture.querySelector('ID').textContent;
let address = fixture.querySelector('Address').textContent;
let fixtureHTML = `
<div class="fixture" id="fixture-${id}">
<span class="fixture-id">${id}</span>
<span class="fixture-name">${name}</span>
<span class="fixture-address">${address}</span>
</div>
`;
$('#fixtures').append(fixtureHTML);
}
}
function updateFunctions() {
$('#functions').empty();
let functions = parser.functionTree;
Object.entries(functions).forEach(([name, folder]) => {
$('#functions').append(createFolder(folder, name, [name]));
});
}
function createFolder(obj, name, path) {
let folderHTML = $(`<div class="function-folder f collapsed"><span class="folder-name"><span class="arrow"></span>${name}</span></div>`);
Object.entries(obj).forEach(([name, value]) => {
if (typeof value === 'object') {
folderHTML.append(createFolder(value, name, path.concat([name])));
} else {
let functionHTML = $(`<div class="function f"><span>${value}</span><span>${name}</span></div>`);
folderHTML.append(functionHTML);
functionHTML.click((event) => selectFunction(event, value, path.concat([name])));
}
});
$(folderHTML).children('.folder-name').click((event) => selectFunction(event, undefined, path));
$(folderHTML).children('.folder-name').children('.arrow').click(toggleFolder);
return folderHTML;
}
function toggleFolder(event) {
let folder = $(event.target).closest('.function-folder');
folder.toggleClass('collapsed');
if (folder.hasClass('selected') || folder.find('.selected').length > 0) {
unselect();
}
}
function selectFunction(event, id, path) {
if (event.target.classList.contains('arrow')) return;
$('#operations-selection').show();
$('.f').removeClass('selected');
$(event.target).closest('.f').addClass('selected');
$('#operations-function').toggle(id !== undefined);
$('#operations-folder').toggle(id === undefined);
console.log(id, path);
selectedFunction = id;
selectedPath = path;
}
function unselect() {
$('#operations-selection').hide();
$('.f').removeClass('selected');
selectedFunction = null;
selectedPath = [];
}
function saveAs(blob, filename) {
let url = URL.createObjectURL(blob);
let a = document.createElement('a');
a.href = url;
a.download = filename;
a.click();
URL.revokeObjectURL(url);
}
$(document).ready(function() {
$('#btn-sort-fixtures').click(function() {
parser.sortFixtures();
updateFixtures();
});
$('#btn-save').click(function() {
let content = $('#file-data').text();
let filename = $('#txt-filename').text();
let blob = new Blob([content], {type: 'text/xml'});
saveAs(blob, filename);
});
$('#btn-update-groups').click(function() {
parser.updateChannelgroups();
});
$('#btn-update-groups-single').click(function() {
parser.updateChannelgroupsSingle(selectedFunction);
});
$('#btn-add-foldername').click(function() {
let path = selectedPath.slice(1).join('/');
let foldername = selectedPath[selectedPath.length - 1];
parser.addFolderInFunctionName(path, foldername);
updateFunctions();
});
$('#tab-fixtures').click(function() {
$('#fixtures').show();
$('#functions').hide();
$('#tab-fixtures').addClass('active');
$('#tab-functions').removeClass('active');
});
$('#tab-functions').click(function() {
$('#fixtures').hide();
$('#functions').show();
$('#tab-fixtures').removeClass('active');
$('#tab-functions').addClass('active');
});
});