Spaces:
Running
Running
File size: 13,634 Bytes
f32cae0 | 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 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 | // Global variables
let qiMenData = [];
let filteredData = [];
let currentPage = 1;
let itemsPerPage = 10;
let currentView = 'table'; // 'table' or 'calendar'
let currentMonth = new Date();
let palaceNames = [
['东北', '正东', '东南'],
['正北', '中宫', '正南'],
['西北', '正西', '西南']
];
// DOM Elements
const resultsTableBody = document.getElementById('resultsTableBody');
const resultCount = document.getElementById('resultCount');
const chartModal = document.getElementById('chartModal');
const chartDisplay = document.getElementById('chartDisplay');
const closeModal = document.getElementById('closeModal');
const importModal = document.getElementById('importModal');
const closeImportModal = document.getElementById('closeImportModal');
const importBtn = document.getElementById('importBtn');
const jsonData = document.getElementById('jsonData');
const processData = document.getElementById('processData');
const cancelImport = document.getElementById('cancelImport');
const applyFilters = document.getElementById('applyFilters');
const viewToggle = document.getElementById('viewToggle');
const tableView = document.getElementById('tableView');
const calendarView = document.getElementById('calendarView');
const calendarGrid = document.getElementById('calendarGrid');
const currentMonthEl = document.getElementById('currentMonth');
const prevMonth = document.getElementById('prevMonth');
const nextMonth = document.getElementById('nextMonth');
const prevPage = document.getElementById('prevPage');
const nextPage = document.getElementById('nextPage');
// Initialize
document.addEventListener('DOMContentLoaded', () => {
// Set default date range
const startDate = document.getElementById('startDate');
const endDate = document.getElementById('endDate');
startDate.value = '2024-02-01';
endDate.value = '2025-12-31';
// Event listeners
importBtn.addEventListener('click', () => {
importModal.classList.remove('hidden');
});
closeImportModal.addEventListener('click', () => {
importModal.classList.add('hidden');
});
cancelImport.addEventListener('click', () => {
importModal.classList.add('hidden');
});
closeModal.addEventListener('click', () => {
chartModal.classList.add('hidden');
});
processData.addEventListener('click', processImportedData);
applyFilters.addEventListener('click', applyDataFilters);
viewToggle.addEventListener('click', toggleView);
prevMonth.addEventListener('click', () => {
currentMonth.setMonth(currentMonth.getMonth() - 1);
renderCalendar();
});
nextMonth.addEventListener('click', () => {
currentMonth.setMonth(currentMonth.getMonth() + 1);
renderCalendar();
});
prevPage.addEventListener('click', () => {
if (currentPage > 1) {
currentPage--;
renderTable();
}
});
nextPage.addEventListener('click', () => {
const maxPage = Math.ceil(filteredData.length / itemsPerPage);
if (currentPage < maxPage) {
currentPage++;
renderTable();
}
});
// Close modals when clicking outside
window.addEventListener('click', (e) => {
if (e.target === chartModal) {
chartModal.classList.add('hidden');
}
if (e.target === importModal) {
importModal.classList.add('hidden');
}
});
// Initialize with sample data
initializeSampleData();
applyDataFilters();
renderCalendar();
});
// Initialize with sample data
function initializeSampleData() {
// Generate sample data for demonstration
const sampleData = [];
const startDate = new Date(2024, 1, 1); // February 1, 2024
const endDate = new Date(2024, 2, 31); // March 31, 2024
for (let d = new Date(startDate); d <= endDate; d.setDate(d.getDate() + 1)) {
for (let hour = 0; hour < 24; hour++) {
// Generate a random 3x3 chart
const chart = [];
for (let i = 0; i < 3; i++) {
const row = [];
for (let j = 0; j < 3; j++) {
// Random elements for each cell
const elements = [
`门${Math.floor(Math.random() * 10)}`,
`星${Math.floor(Math.random() * 10)}`,
`神${Math.floor(Math.random() * 10)}`
];
// Randomly add Zhi Shi to one cell per chart
if (i === Math.floor(Math.random() * 3) && j === Math.floor(Math.random() * 3)) {
elements.push('Zhi Shi (值使)');
}
row.push(elements.join('\n'));
}
chart.push(row);
}
sampleData.push({
date: d.toISOString().split('T')[0],
hour: hour,
chart: chart
});
}
}
qiMenData = sampleData;
}
// Process imported JSON data
function processImportedData() {
try {
const data = JSON.parse(jsonData.value);
if (Array.isArray(data)) {
qiMenData = data;
importModal.classList.add('hidden');
applyDataFilters();
renderCalendar();
} else {
alert('Invalid data format. Please provide an array of chart objects.');
}
} catch (e) {
alert('Invalid JSON format. Please check your data.');
console.error(e);
}
}
// Apply filters to data
function applyDataFilters() {
const startDate = document.getElementById('startDate').value;
const endDate = document.getElementById('endDate').value;
const hourFilter = document.getElementById('hourFilter').value;
const cellFilter = document.getElementById('cellFilter').value;
filteredData = qiMenData.filter(item => {
// Date filter
if (startDate && item.date < startDate) return false;
if (endDate && item.date > endDate) return false;
// Hour filter
if (hourFilter !== '' && item.hour != hourFilter) return false;
// Cell filter
if (cellFilter) {
const [row, col] = cellFilter.split(',').map(Number);
const zhiShiPos = findZhiShiPosition(item.chart);
if (!zhiShiPos || zhiShiPos.row !== row || zhiShiPos.col !== col) return false;
}
return true;
});
currentPage = 1;
renderTable();
}
// Find Zhi Shi position in chart
function findZhiShiPosition(chart) {
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (chart[i][j].includes('Zhi Shi') || chart[i][j].includes('值使')) {
return { row: i, col: j };
}
}
}
return null;
}
// Render table view
function renderTable() {
const startIndex = (currentPage - 1) * itemsPerPage;
const endIndex = startIndex + itemsPerPage;
const pageData = filteredData.slice(startIndex, endIndex);
resultsTableBody.innerHTML = '';
pageData.forEach(item => {
const zhiShiPos = findZhiShiPosition(item.chart);
const row = document.createElement('tr');
row.className = 'hover:bg-gray-50';
row.innerHTML = `
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">${item.date}</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">${item.hour.toString().padStart(2, '0')}:00</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
${zhiShiPos ? `${palaceNames[zhiShiPos.row][zhiShiPos.col]} (${zhiShiPos.row},${zhiShiPos.col})` : 'Not found'}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
${zhiShiPos ? extractZhiShiCharacter(item.chart[zhiShiPos.row][zhiShiPos.col]) : 'N/A'}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
<button class="view-chart text-yellow-600 hover:text-yellow-900" data-date="${item.date}" data-hour="${item.hour}">
View Chart
</button>
</td>
`;
resultsTableBody.appendChild(row);
});
// Add event listeners to view chart buttons
document.querySelectorAll('.view-chart').forEach(button => {
button.addEventListener('click', (e) => {
const date = e.target.dataset.date;
const hour = e.target.dataset.hour;
showChart(date, hour);
});
});
resultCount.textContent = filteredData.length;
// Update pagination
const maxPage = Math.ceil(filteredData.length / itemsPerPage);
prevPage.disabled = currentPage === 1;
nextPage.disabled = currentPage === maxPage || maxPage === 0;
}
// Extract Zhi Shi character from cell content
function extractZhiShiCharacter(cellContent) {
const lines = cellContent.split('\n');
for (const line of lines) {
if (line.includes('门') || line.includes('星') || line.includes('神')) {
return line;
}
}
return 'Unknown';
}
// Show chart in modal
function showChart(date, hour) {
const chartData = qiMenData.find(item => item.date === date && item.hour == hour);
if (!chartData) return;
chartDisplay.innerHTML = '';
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
const cell = document.createElement('div');
cell.className = 'chart-cell border border-gray-200';
// Check if this is the Zhi Shi cell
if (chartData.chart[i][j].includes('Zhi Shi') || chartData.chart[i][j].includes('值使')) {
cell.classList.add('zhi-shi-cell');
}
cell.innerHTML = `<div class="text-sm">${chartData.chart[i][j].replace(/\n/g, '<br>')}</div>`;
chartDisplay.appendChild(cell);
}
}
chartModal.classList.remove('hidden');
}
// Toggle between table and calendar view
function toggleView() {
if (currentView === 'table') {
currentView = 'calendar';
tableView.classList.add('hidden');
calendarView.classList.remove('hidden');
viewToggle.innerHTML = '<i data-feather="list" class="mr-2"></i> Table View';
feather.replace();
renderCalendar();
} else {
currentView = 'table';
calendarView.classList.add('hidden');
tableView.classList.remove('hidden');
viewToggle.innerHTML = '<i data-feather="calendar" class="mr-2"></i> Calendar View';
feather.replace();
renderTable();
}
}
// Render calendar view
function renderCalendar() {
const year = currentMonth.getFullYear();
const month = currentMonth.getMonth();
// Set current month text
currentMonthEl.textContent = `${year}年 ${month + 1}月`;
// Get first day of month and last day
const firstDay = new Date(year, month, 1);
const lastDay = new Date(year, month + 1, 0);
// Get day of week for first day (0 = Sunday, 1 = Monday, etc.)
const firstDayOfWeek = firstDay.getDay();
// Get number of days in month
const daysInMonth = lastDay.getDate();
// Clear calendar grid
calendarGrid.innerHTML = '';
// Add empty cells for days before first day of month
for (let i = 0; i < firstDayOfWeek; i++) {
const emptyCell = document.createElement('div');
emptyCell.className = 'calendar-day p-2';
calendarGrid.appendChild(emptyCell);
}
// Add cells for each day of month
for (let day = 1; day <= daysInMonth; day++) {
const dayCell = document.createElement('div');
dayCell.className = 'calendar-day p-2 border border-gray-200';
// Format date string for comparison
const dateStr = `${year}-${(month + 1).toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`;
// Check if we have data for this day
const dayData = filteredData.filter(item => item.date === dateStr);
if (dayData.length > 0) {
dayCell.classList.add('has-data');
// Count Zhi Shi positions
const positionCounts = {};
dayData.forEach(item => {
const zhiShiPos = findZhiShiPosition(item.chart);
if (zhiShiPos) {
const key = `${zhiShiPos.row},${zhiShiPos.col}`;
positionCounts[key] = (positionCounts[key] || 0) + 1;
}
});
// Create position summary
let positionSummary = '';
for (const [pos, count] of Object.entries(positionCounts)) {
const [row, col] = pos.split(',').map(Number);
positionSummary += `<div>${palaceNames[row][col]}: ${count}</div>`;
}
dayCell.innerHTML = `
<div class="font-medium">${day}</div>
<div class="text-xs mt-1">
<div class="zhi-shi-marker">值使</div>
<div class="mt-1">${Object.keys(positionCounts).length} positions</div>
</div>
`;
} else {
dayCell.innerHTML = `<div class="font-medium">${day}</div>`;
}
calendarGrid.appendChild(dayCell);
}
} |