-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
363 lines (300 loc) · 11.3 KB
/
Copy pathmain.js
File metadata and controls
363 lines (300 loc) · 11.3 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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**
* Main Application - Entry point and orchestration
*
* This file serves as the main entry point for the Legal System Network Diagram application.
* It coordinates between the data layer, helper functions, and visualization components.
*/
// Global application state
let visualization = null;
let isInitialized = false;
/**
* Initialize the application
* Sets up the visualization with data and configuration
*/
function init() {
try {
// Get data from window.data at runtime
const { judicialEntityMapData, groupingData, relationshipGroupingData, config, colorMap } = window.data || {};
// Validate required data is available
if (!judicialEntityMapData || !groupingData || !config || !colorMap) {
throw new Error('Required data not loaded. Make sure data.js is included.');
}
// Create visualization instance (relationshipGroupingData is optional)
visualization = new LegalSystemVisualization({
judicialEntityMapData,
groupingData,
relationshipGroupingData: relationshipGroupingData || [],
config,
colorMap
});
// Initialize the visualization
visualization.init();
// Mark as initialized
isInitialized = true;
console.log('Legal System Network Diagram initialized successfully');
} catch (error) {
console.error('Failed to initialize application:', error);
showErrorMessage('Failed to load the visualization. Please refresh the page.');
}
}
/**
* Show error message to user
* @param {string} message - Error message to display
*/
function showErrorMessage(message) {
const container = document.querySelector('.visualization-container');
if (container) {
container.innerHTML = `
<div style="display: flex; align-items: center; justify-content: center; height: 100%; color: #d32f2f; font-size: 18px; text-align: center;">
<div>
<h3>Error Loading Visualization</h3>
<p>${message}</p>
<button onclick="location.reload()" style="margin-top: 20px; padding: 10px 20px; background: #1976d2; color: white; border: none; border-radius: 4px; cursor: pointer;">
Reload Page
</button>
</div>
</div>
`;
}
}
// Setup the data table
function setupTable() {
const tableBody = d3.select("#table-body");
// Clear existing rows
tableBody.selectAll("tr").remove();
// Get data from window.data at runtime
const { judicialEntityMapData } = window.data || {};
// Get filtered data if filter is active
const dataToShow = window.helpers?.getFilteredTableData(judicialEntityMapData, visualization?.filteredNodeId, visualization?.filteredEntityGroupId, visualization?.filteredRelationshipId, visualization?.filteredRelationshipGroupId, window.data?.relationshipGroupingData, window.data?.groupingData) || judicialEntityMapData;
// Create rows for each lawsuit
const rows = tableBody.selectAll("tr")
.data(dataToShow)
.enter()
.append("tr")
.attr("class", "table-row")
.style("opacity", 0)
.transition()
.duration(500)
.style("opacity", 1);
// Add cells for each row
rows.each(function(d) {
const row = d3.select(this);
row.append("td").text(d.source);
row.append("td").text(d.label);
row.append("td").text(d.target);
});
}
// Setup event listeners
function setupEventListeners() {
document.getElementById("toggleViewBtn").addEventListener("click", toggleView);
document.getElementById("nodeFilter").addEventListener("change", handleNodeFilter);
document.getElementById("clearFilter").addEventListener("click", clearNodeFilter);
// Setup collapsible project note toggle
const projectNoteToggle = document.getElementById("projectNoteToggle");
if (projectNoteToggle) {
projectNoteToggle.addEventListener("click", toggleProjectNote);
}
}
// Toggle project note visibility
function toggleProjectNote() {
const toggle = document.getElementById("projectNoteToggle");
const content = document.getElementById("projectNoteContent");
const diagramView = document.getElementById("diagram-view");
if (!toggle || !content) return;
const isExpanded = toggle.getAttribute("aria-expanded") === "true";
if (isExpanded) {
toggle.setAttribute("aria-expanded", "false");
content.classList.add("collapsed");
// Add class to diagram-view to move legend down (if diagram view exists)
if (diagramView) {
diagramView.classList.add("project-note-collapsed");
}
} else {
toggle.setAttribute("aria-expanded", "true");
content.classList.remove("collapsed");
// Remove class to move legend back up (if diagram view exists)
if (diagramView) {
diagramView.classList.remove("project-note-collapsed");
}
}
}
// Toggle between diagram and table views
function toggleView() {
if (visualization?.isAnimating) return;
if (visualization?.currentView === 'diagram') {
showTableView();
} else {
visualization?.showDiagramView();
}
}
// Show table view
function showTableView() {
if (visualization?.isAnimating) return;
visualization.isAnimating = true;
const toggleBtn = document.getElementById("toggleViewBtn");
toggleBtn.disabled = true;
toggleBtn.textContent = "Show Diagram View";
const tableView = document.getElementById("table-view");
const diagramView = document.getElementById("diagram-view");
const container = document.querySelector('.container');
// Add class to hide legend and project note
if (container) {
container.classList.add('table-view-active');
}
// Stop simulation
visualization?.simulation?.stop();
// Fade out diagram
diagramView.classList.remove("active");
diagramView.classList.add("fade-out");
visualization?.nodeElements
?.transition()
.duration(500)
.style("opacity", 0);
visualization?.linkElements
?.transition()
.duration(500)
.style("opacity", 0);
setTimeout(() => {
tableView.classList.remove("fade-out");
tableView.classList.add("active");
setupTable();
setTimeout(() => {
toggleBtn.disabled = false;
visualization.isAnimating = false;
visualization.currentView = 'table';
}, 1000);
}, 500);
}
// Setup filter dropdown
function setupFilterDropdown() {
const nodeFilter = document.getElementById("nodeFilter");
// Get data from window.data at runtime
const { judicialEntityMapData } = window.data || {};
// Get unique node names
const uniqueNodes = [...new Set([
...judicialEntityMapData.map(d => d.source),
...judicialEntityMapData.map(d => d.target)
])].sort();
// Clear existing options
nodeFilter.innerHTML = '<option value="">Show All Entities</option>';
// Add options for each node
uniqueNodes.forEach(nodeName => {
const option = document.createElement("option");
option.value = nodeName;
option.textContent = nodeName;
nodeFilter.appendChild(option);
});
}
// Handle node filter change
function handleNodeFilter(event) {
const selectedNodeId = event.target.value;
if (selectedNodeId) {
applyNodeFilter(selectedNodeId);
} else {
clearNodeFilter();
}
}
// Apply node filter
function applyNodeFilter(nodeId) {
visualization.filteredNodeId = nodeId;
// Get data from window.data at runtime
const { judicialEntityMapData } = window.data || {};
// Filter nodes
visualization?.nodeElements
?.style("opacity", d => {
const isConnected = judicialEntityMapData.some(link =>
(link.source === nodeId && link.target === d.id) ||
(link.target === nodeId && link.source === d.id)
);
return isConnected ? 1 : 0.2;
});
// Filter links
visualization?.linkElements
?.style("opacity", d => {
const isConnected = (d.source.id === nodeId || d.target.id === nodeId);
return isConnected ? 1 : 0.2;
});
// Show relationship labels for filtered node
if (visualization?.currentView === 'diagram') {
setTimeout(() => {
visualization?.showFilteredNodeLabels(nodeId);
}, 200);
}
// Update table
setupTable();
}
// Clear node filter
function clearNodeFilter() {
visualization.filteredNodeId = null;
// Reset all nodes and links to full opacity
visualization?.nodeElements?.style("opacity", 1);
visualization?.linkElements?.style("opacity", 1);
// Hide relationship labels
visualization?.hideRelationshipLabel();
// Reset dropdown
document.getElementById("nodeFilter").value = "";
// Update table
setupTable();
}
// Orientation detection and lock
function checkOrientation() {
const isPortrait = window.innerHeight > window.innerWidth;
const body = document.body;
if (isPortrait) {
body.classList.add('portrait-mode');
// Prevent scrolling in portrait mode
body.style.overflow = 'hidden';
} else {
body.classList.remove('portrait-mode');
body.style.overflow = '';
}
}
// Initialize when DOM is loaded
document.addEventListener('DOMContentLoaded', function() {
// Check initial orientation
checkOrientation();
// Listen for orientation changes
window.addEventListener('resize', checkOrientation);
window.addEventListener('orientationchange', function() {
// Delay check to allow orientation change to complete
setTimeout(checkOrientation, 100);
});
// Initialize the application
init();
// Setup event listeners
setupEventListeners();
// Set initial state for collapsed project note (legend position)
const diagramView = document.getElementById("diagram-view");
const projectNoteContent = document.getElementById("projectNoteContent");
if (diagramView && projectNoteContent && projectNoteContent.classList.contains("collapsed")) {
diagramView.classList.add("project-note-collapsed");
}
});
/**
* Global event handlers for HTML interactions
* These functions are exposed to the global scope for use in HTML event handlers
*/
// Toggle between diagram and table views
window.toggleView = () => {
if (!isInitialized || !visualization) {
console.warn('Visualization not initialized yet');
return;
}
visualization.toggleView();
};
// Handle node filter selection from dropdown
window.handleNodeFilter = (event) => {
if (!isInitialized || !visualization) {
console.warn('Visualization not initialized yet');
return;
}
visualization.handleNodeFilter(event);
};
// Clear the current node filter
window.clearNodeFilter = () => {
if (!isInitialized || !visualization) {
console.warn('Visualization not initialized yet');
return;
}
visualization.clearNodeFilter();
};