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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
{% if selected_disk is defined and setup_data is defined %}
<form class="form-inline mx-5" method="POST" id="setupForm">
{{ disk_form.hidden_tag() }}
{{ disk_form.ips() }}
<table class="table">
<thead class="text-center">
<tr>
<th>{{ _('Disk') }}</th>
<th>{{ _('Partition Table Type') }}</th>
<th>{{ _('Total Disk Size') }} (MiB)</th>
</tr>
</thead>
<tbody data-target="partitons-fieldset" id="diskTable" class="text-center">
<tr>
<td>
{{ disk_form.disk(class_="form-control", onchange="handleDiskChange(this)") }}
</td>
{{ disk_form.hidden_tag() }}
<td>
{% if readonly_disk_inspector is defined %}
{{ disk_form.disk_type(class_="form-control", id="diskType", readonly="readonly") }}
{% else %}
{{ disk_form.disk_type(class_="form-control", id="diskType") }}
{% endif %}
</td>
<td id="diskSize">{{ disk_size }}</td>
</tr>
</tbody>
</table>
<table class="table">
<thead class="text-center">
<tr>
<th>{{ _('Partition') }}</th>
<th>{{ _('Type') }}</th>
<th>{{ _('Filesystem') }}</th>
<th>{{ _('Size') }} (MiB)</th>
{% if show_free_size is defined %}
<th>{{ _('Free') }} (MiB)</th>
{% endif %}
{% if show_part_images is defined %}
<th>{{ _('Image') }}</th>
{% endif %}
<th colspan="2"></th>
</tr>
</thead>
<tbody data-target="partitons-fieldset" id="partitionsTable" class="text-center">
{% for partition in disk_form.partitions %}
<tr data-toggle="fieldset-entry">
{{ partition.hidden_tag() }}
<td>{{ partition.partition.data }}</td>
<td>
{% if readonly_disk_inspector is defined %}
{{ partition.part_type(class_="form-control", readonly="readonly") }}
{% else %}
{{ partition.part_type(class_="form-control") }}
{% endif %}
</td>
<td>
{% if readonly_disk_inspector is defined %}
{{ partition.fs(class_="form-control", readonly="readonly") }}
{% else %}
{{ partition.fs(class_="form-control") }}
{% endif %}
</td>
<td>
{% if not readonly_disk_inspector is defined %}
{{ partition.size(class_="form-control", oninput="handleEdit(this)") }}
{% endif %}
</td>
{% if show_free_size is defined %}
<td></td>
{% endif %}
{% if show_part_images is defined %}
<td></td>
{% endif %}
<td>
<button class="btn btn-danger" type="button" onclick="RemovePartition(this, true)"
{% if readonly_disk_inspector is defined %}style="display: none;"{% endif %}>
{{ _('Remove') }}
</button>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</form>
<div class="form-group mx-5">
<div id="clientWarningMsg"></div>
</div>
<div class="form-group mx-5">
<button class="btn btn-primary" data-target="#partitionsTable" id="addPartButton" onclick="AddPartition(this, true)"
{% if readonly_disk_inspector is defined %}style="display: none;"{% endif %}>
{{ _('Add a new partition') }}
</button>
{% if not readonly_disk_inspector is defined %}
<button class="btn btn-success" form="setupForm">
{{ _('Submit') }}
</button>
{% endif %}
</div>
<div class="card text-center mx-5">
<div class="card-header">
{{ _('Partition graph') }}
</div>
<div class="card-body mx-auto col-7">
<canvas id="partitionChart" class="mb-2"></canvas>
</div>
</div>
<!-- jQuery -->
<script src="{{ url_for('static', filename='AdminLTE/plugins/jquery/jquery.min.js') }}"></script>
<!-- ChartJS -->
<script src="{{ url_for('static', filename='AdminLTE/plugins/chart.js/Chart.min.js') }}"></script>
<script>
const usedColor = 'rgb(255, 99, 132)';
const freeColor = 'rgb(54, 162, 235)';
let selectedDisk = {{selected_disk}};
let setupData = {{setup_data|tojson|safe}}
{% if excluded_client_disks is defined %}
let excludedClientDisks = {{excluded_client_disks|tojson|safe}}
{% endif %}
let diskSize = 0;
let chartConfig = {
type: 'doughnut',
data: {
labels: [],
datasets: [
{
label: 'Partitions',
data: [],
backgroundColor: [],
},
],
},
options: {
responsive: true,
plugins: {
legend: {
position: 'top',
},
title: {
display: true,
text: 'Chart.js Doughnut Chart'
},
},
},
};
let partitionChart = new Chart(
document.getElementById('partitionChart'),
chartConfig,
);
function addPartitionToChart(chart, label, value, bgColor) {
chart.data.labels.push(label)
chart.data.datasets[0].data.push(value);
chart.data.datasets[0].backgroundColor.push(bgColor);
}
function resetChart() {
partitionChart.data.labels = [];
partitionChart.data.datasets[0].data = [];
partitionChart.data.datasets[0].backgroundColor = [];
}
function updatePartitionChart() {
resetChart();
let freeSpace = diskSize;
let partNum = 1;
$('#partitionsTable tr').each(function() {
{% if readonly_disk_inspector is defined %}
let partitionSize = parseInt($(this).find('td').eq(3).text().trim());
{% else %}
let partitionSize = parseInt($(this).find('td').eq(3).find('input').val().trim());
{% endif %}
if (isNaN(partitionSize)) {
partitionSize = 0;
}
freeSpace -= partitionSize;
addPartitionToChart(partitionChart, 'Partition ' + partNum, partitionSize, usedColor);
partNum++;
});
addPartitionToChart(partitionChart, 'Free', Math.max(freeSpace, 0), freeColor);
partitionChart.update();
}
function handleEdit(element) {
const numericValue = parseInt(element.value);
if (isNaN(numericValue)) {
updatePartitionChart();
return;
}
let freeSpace = diskSize;
$('#partitionsTable tr').each(function() {
let partitionSize = parseInt($(this).find('td').eq(3).find('input').val().trim());
if (isNaN(partitionSize)) {
partitionSize = 0;
}
freeSpace -= partitionSize;
});
if (freeSpace < 0) {
element.value = numericValue + freeSpace;
}
updatePartitionChart();
}
function AddPartition(evt, updateChart) {
const target = $($(evt).data("target"));
const oldrow = target.find("[data-toggle=fieldset-entry]:last");
const row = oldrow.clone(true, true);
const elem_id = row.find(".form-control")[0].id;
const elem_num = parseInt(elem_id.replace(/(.*)-(\d{1,4})/m, '$2')) + 1;
// Increment WTForms unique identifiers
row.find('.form-control').each(function() {
const id = $(this).attr('id').replace(/(.*)-(\d{1,4})-(.*)/, `$1-${elem_num}-$3`);
$(this).attr('name', id).attr('id', id).val('').removeAttr("checked");
});
let part_field = row.find('td').filter(':first')[0];
part_field.innerText = elem_num + 1;
row.show();
oldrow.after(row);
if (updateChart) {
updatePartitionChart();
}
}
function RemovePartition(evt, updateChart) {
const target = $(evt).parent().parent();
const table = target.parent();
if (table[0].children.length > 1) {
target.remove();
// Reassign rows ids
table.find('tr').each(function(index) {
function update_references() {
const id = $(this).attr('id').replace(/(.*)-(\d{1,4})-(.*)/, `$1-${index}-$3`);
$(this).attr('name', id).attr('id', id);
}
let part_field = $(this).find('td').filter(':first')[0];
part_field.innerText = index + 1;
$(this).find('input').filter(':first').each(update_references);
$(this).find('.form-control').each(update_references);
});
} else {
table.find('tr').each(function(index) {
$(this).find('.form-control').each(function() {
$(this).val('').removeAttr("checked");
});
});
}
if (updateChart) {
updatePartitionChart();
}
}
function setDiskData(diskNumber) {
selectedDisk = diskNumber;
let partitions = setupData[selectedDisk];
diskSize = Math.floor(partitions[0].size / 1024);
$('#diskSize').text(diskSize);
$('#diskType').val(partitions[0].code);
const partitionsTable = $('#partitionsTable');
let partNumber = partitionsTable.find('tr').length;
const targetPartNumber = partitions.length - 1;
while (partNumber < targetPartNumber) {
AddPartition($('#addPartButton'), false);
partNumber++;
}
while (partNumber > targetPartNumber) {
RemovePartition(partitionsTable.find('tr:last').find('button'), false);
partNumber--;
}
for (let i = 1; i < partitions.length; i++) {
let p = partitions[i];
let row = partitionsTable.find('tr').eq(i - 1);
var idx = 0;
row.find('td').eq(idx++).text(p.partition);
row.find('td').eq(idx++).find('select').val(p.code);
row.find('td').eq(idx++).find('select').val(p.filesystem);
{% if readonly_disk_inspector is defined %}
row.find('td').eq(idx++).text(Math.floor(p.size / 1024));
{% else %}
row.find('td').eq(idx++).find('input').val(Math.floor(p.size / 1024));
{% endif %}
{% if show_free_size is defined %}
row.find('td').eq(idx++).text(Math.floor(p.free_size / (1024 * 1024)));
{% endif %}
{% if show_part_images is defined %}
row.find('td').eq(idx++).text(p.image);
{% endif %}
}
updatePartitionChart();
{% if excluded_client_disks is defined %}
let missingClientsText = "";
if (excludedClientDisks[selectedDisk].length) {
missingClientsText = "<p>{{ _('Clients missing the target disk:') }}</p>";
}
for (const c of excludedClientDisks[selectedDisk]) {
missingClientsText += '<div class="card d-inline-block" style="padding: 5px; margin: 3px;">' + c + '</div>';
}
$('#clientWarningMsg').html(missingClientsText);
{% endif %}
}
function handleDiskChange(selectElement) {
setDiskData(selectElement.value);
}
$(document).ready(function() {
setDiskData(selectedDisk);
});
</script>
{% endif %}
|