-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.js
More file actions
173 lines (158 loc) · 6.87 KB
/
ui.js
File metadata and controls
173 lines (158 loc) · 6.87 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
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
import { dom } from './config.js';
import { getActiveTab } from './state.js';
import { parseResultForTable } from './utils.js';
import { updateTimelineToggleButton, populateTimelineFieldSelect, renderTimelineChart, showRowDetails, updateGraphToggleButton } from './components.js';
export function showAppMessage(message, type = 'info', isSticky = false) {
if (!dom.statusMessage) return;
dom.statusMessage.textContent = message;
dom.statusMessage.className = 'alert d-none flex-grow-1 ms-auto p-2 mb-0 text-sm';
let alertClass = 'alert-info';
switch (type) {
case 'error': alertClass = 'alert-danger'; break;
case 'success': alertClass = 'alert-success'; break;
case 'warning': alertClass = 'alert-warning'; break;
}
dom.statusMessage.classList.add(alertClass);
dom.statusMessage.classList.remove('d-none');
if (!isSticky && (type === 'info' || type === 'success')) {
setTimeout(() => {
if (dom.statusMessage.textContent === message) hideAppMessage();
}, 5000);
}
}
export function hideAppMessage() {
if (dom.statusMessage) dom.statusMessage.classList.add('d-none');
}
export function populateSelect(selectElement, options, selectedValue, valueKey = 'value', textKey = 'text') {
if (!selectElement) return;
selectElement.innerHTML = '';
options.forEach(opt => {
const option = document.createElement('option');
option.value = opt[valueKey] !== undefined ? opt[valueKey] : opt.template;
option.textContent = opt[textKey] !== undefined ? opt[textKey] : opt.name;
selectElement.appendChild(option);
});
if (selectedValue !== undefined) selectElement.value = selectedValue;
}
export function updateResultDisplay(tab) {
if (!tab) return;
dom.tableResultOutputContainer.classList.add('d-none');
dom.textResultOutput.classList.add('d-none');
dom.noResultsMessage.classList.add('d-none');
dom.toggleViewBtn.classList.add('d-none');
dom.pivotResultsBtn.classList.add('d-none');
dom.toggleGraphBtn.classList.add('d-none');
dom.exportBtn.disabled = true;
const hasResults = tab.currentRawOutput && tab.currentRawOutput.trim() !== "";
if (hasResults) dom.toggleGraphBtn.classList.remove('d-none');
if (tab.gridInstance) {
dom.tableResultOutputContainer.classList.remove('d-none');
dom.toggleViewBtn.textContent = "View Raw";
dom.toggleViewBtn.classList.remove('d-none');
dom.pivotResultsBtn.classList.remove('d-none');
dom.exportBtn.disabled = false;
} else if (hasResults) {
dom.resultOutputCode.textContent = tab.currentRawOutput;
dom.textResultOutput.classList.remove('d-none');
dom.pivotResultsBtn.classList.remove('d-none');
dom.exportBtn.disabled = false;
const canBeTable = parseResultForTable(tab.currentRawOutput, tab.outputFormat);
if (canBeTable) {
dom.toggleViewBtn.textContent = "View Table";
dom.toggleViewBtn.classList.remove('d-none');
}
} else {
dom.noResultsMessage.classList.remove('d-none');
}
const hasGridResults = !!tab.gridInstance;
updateTimelineToggleButton(tab);
if (!hasGridResults && tab.timelineVisible) {
tab.timelineVisible = false;
dom.timelineContainer.classList.add('d-none');
if (tab.timelineChartInstance) {
tab.timelineChartInstance.destroy();
tab.timelineChartInstance = null;
}
tab.timelineChartDataCache = null;
} else if (hasGridResults) {
populateTimelineFieldSelect(tab);
if (tab.timelineVisible && tab.timelineChartDataCache) {
renderTimelineChart(tab, tab.timelineChartDataCache.labels, tab.timelineChartDataCache.datasets);
} else if (tab.timelineVisible && !tab.timelineChartDataCache) {
dom.timelineChartWrapper.classList.add('d-none');
}
}
updateGraphToggleButton(tab);
dom.timelineContainer.classList.toggle('d-none', !tab.timelineVisible || !hasGridResults);
dom.graphContainer.classList.toggle('d-none', !tab.graphVisible);
}
export function displayTableWithGridJs(parsedData, containerElement, tab) {
if (tab.gridInstance) {
try { tab.gridInstance.destroy(); }
catch (e) {}
}
containerElement.innerHTML = '';
if (!parsedData || !parsedData.headers || !parsedData.dataRows) {
tab.gridInstance = null;
return;
}
if (typeof gridjs === 'undefined') {
showAppMessage("Error: Table library (Grid.js) not loaded.", "error", true);
tab.gridInstance = null;
return;
}
const { headers, dataRows } = parsedData;
const gridColumns = headers.map(header => ({ name: String(header) }));
gridColumns.unshift({
name: 'Details',
width: '80px',
sort: false,
formatter: (cell, row) => gridjs.h('button', {
className: 'btn btn-sm btn-outline-info py-0 px-1',
title: 'Show row details',
onClick: () => showRowDetails(row, gridColumns)
}, gridjs.html('<i class="fa-solid fa-circle-info"></i>'))
});
const dataWithDetailsPlaceholder = dataRows.map(row => [null, ...row]);
try {
tab.gridInstance = new gridjs.Grid({
columns: gridColumns,
data: dataWithDetailsPlaceholder,
search: { debounceTimeout: 250 },
sort: true,
pagination: { enabled: true, limit: 50, summary: true },
resizable: true,
fixedHeader: true,
}).render(containerElement);
} catch (gridError) {
showAppMessage(`Error displaying results table: ${gridError.message}`, 'error', true);
tab.gridInstance = null;
}
}
export function lockUI(isLocked, message = '') {
const elementsToDisable = [
dom.runQueryBtn, dom.exportBtn, dom.runScannerBtn, dom.addTabBtn,
dom.loadTestDataBtn, dom.loadPredefinedRuleBtn, dom.applyShaperBtn,
dom.fileInput, dom.dataInput, dom.queryInput, dom.shaperScriptsSelect
];
elementsToDisable.forEach(el => { if (el) el.disabled = isLocked; });
if (isLocked) showAppMessage(message, 'info', true);
else hideAppMessage();
}
export function toggleResultsViewHandler() {
const activeTab = getActiveTab();
if (!activeTab || !activeTab.currentRawOutput) return;
if (activeTab.gridInstance) {
try { activeTab.gridInstance.destroy(); } catch(e){}
activeTab.gridInstance = null;
dom.tableResultOutputContainer.innerHTML = '';
} else {
const tableData = parseResultForTable(activeTab.currentRawOutput, activeTab.outputFormat);
if (tableData) {
displayTableWithGridJs(tableData, dom.tableResultOutputContainer, activeTab);
} else {
showAppMessage('Cannot display as table. Output might be an error or unsuitable format.', 'warning');
}
}
updateResultDisplay(activeTab);
}