-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
157 lines (127 loc) · 4.87 KB
/
app.js
File metadata and controls
157 lines (127 loc) · 4.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
/**
* Main Application Module
* Handles the main application logic and initialization
*/
/**
* Generate HTML table from JSON input and display on page
*/
function generateTable() {
const input = document.getElementById('jsonInput').value.trim();
const output = document.getElementById('output');
const pdfBtn = document.getElementById('pdfBtn');
if (!input) {
output.innerHTML = '<div class="error">Please enter JSON data</div>';
pdfBtn.disabled = true;
return;
}
try {
const jsonData = JSON.parse(input);
currentJsonData = jsonData;
let html = '<div id="tableContent">';
if (Array.isArray(jsonData)) {
html += '<div class="value-column">' + createArrayDisplay(jsonData) + '</div>';
} else if (typeof jsonData === 'object' && jsonData !== null) {
html += createMainTable(jsonData);
} else {
html += `<table class="main-table">
<tr>
<td class="key-column">Value</td>
<td class="value-column">${formatPrimitiveValue(jsonData)}</td>
</tr>
</table>`;
}
html += '</div>';
output.innerHTML = html;
// Enable PDF button
pdfBtn.disabled = false;
} catch (error) {
output.innerHTML = `<div class="error">Invalid JSON: ${error.message}</div>`;
pdfBtn.disabled = true;
}
}
/**
* Toggle between dark and light themes
*/
function toggleTheme() {
const currentTheme = document.documentElement.getAttribute('data-theme');
const newTheme = currentTheme === 'light' ? 'dark' : 'light';
document.documentElement.setAttribute('data-theme', newTheme);
// Save theme preference
localStorage.setItem('theme', newTheme);
}
/**
* Initialize theme from localStorage or default to dark
*/
function initializeTheme() {
const savedTheme = localStorage.getItem('theme') || 'dark';
document.documentElement.setAttribute('data-theme', savedTheme);
}
/**
* Initialize resize functionality for the input section
*/
function initializeResize() {
const resizeHandle = document.getElementById('resizeHandle');
const inputSection = document.querySelector('.input-section');
const mainContent = document.querySelector('.main-content');
if (!resizeHandle || !inputSection || !mainContent) return;
let isResizing = false;
let startX = 0;
let startWidth = 0;
resizeHandle.addEventListener('mousedown', (e) => {
isResizing = true;
startX = e.clientX;
startWidth = parseInt(getComputedStyle(inputSection).width, 10);
document.addEventListener('mousemove', handleResize);
document.addEventListener('mouseup', stopResize);
document.body.style.cursor = 'col-resize';
document.body.style.userSelect = 'none';
e.preventDefault();
});
function handleResize(e) {
if (!isResizing) return;
const deltaX = e.clientX - startX;
const newWidth = startWidth + deltaX;
const containerWidth = mainContent.clientWidth;
const minWidth = 300;
const maxWidth = containerWidth * 0.6;
const clampedWidth = Math.max(minWidth, Math.min(newWidth, maxWidth));
inputSection.style.flexBasis = clampedWidth + 'px';
}
function stopResize() {
isResizing = false;
document.removeEventListener('mousemove', handleResize);
document.removeEventListener('mouseup', stopResize);
document.body.style.cursor = '';
document.body.style.userSelect = '';
}
// Touch support for mobile
resizeHandle.addEventListener('touchstart', (e) => {
isResizing = true;
startX = e.touches[0].clientX;
startWidth = parseInt(getComputedStyle(inputSection).width, 10);
document.addEventListener('touchmove', handleTouchResize);
document.addEventListener('touchend', stopTouchResize);
e.preventDefault();
});
function handleTouchResize(e) {
if (!isResizing) return;
const deltaX = e.touches[0].clientX - startX;
const newWidth = startWidth + deltaX;
const containerWidth = mainContent.clientWidth;
const minWidth = 300;
const maxWidth = containerWidth * 0.6;
const clampedWidth = Math.max(minWidth, Math.min(newWidth, maxWidth));
inputSection.style.flexBasis = clampedWidth + 'px';
}
function stopTouchResize() {
isResizing = false;
document.removeEventListener('touchmove', handleTouchResize);
document.removeEventListener('touchend', stopTouchResize);
}
}
// Initialize the application when the page loads
window.onload = function() {
initializeTheme();
initializeResize();
generateTable();
};