cvs-proxy/ui/app.js
Juan José Gutiérrez de Quevedo Pérez c15b759378 Fix: clicking on history item now shows diff for that revision
- Modified ui.js displayHistory() to pass previous revision to click handler
- Added showHistoryItemDiff() method in app.js to generate and display diff
- Diff compares selected revision with previous revision (Option A)
- First revision shows message instead of diff (no previous to compare)
2025-11-21 20:48:15 +01:00

258 lines
No EOL
8.4 KiB
JavaScript

/**
* Main Application Logic
* Orchestrates API calls and UI updates
*/
class CVSRepositoryBrowser {
constructor() {
this.currentFile = null;
this.currentHistory = null;
this.initialize();
}
/**
* Initialize the application
*/
async initialize() {
try {
// Check API health
await this.checkHealth();
// Load initial repository tree
await this.loadTree();
// Setup event listeners
this.setupEventListeners();
ui.updateStatus('Connected', 'connected');
} catch (error) {
console.error('Initialization error:', error);
ui.updateStatus('Connection Error', 'error');
}
}
/**
* Check API health
*/
async checkHealth() {
try {
const health = await api.getHealth();
console.log('API Health:', health);
return health;
} catch (error) {
throw new Error('Failed to connect to API');
}
}
/**
* Load repository tree
*/
async loadTree() {
try {
ui.treeContainer.innerHTML = '<div class="loading">Loading repository...</div>';
const files = await api.getTree();
ui.buildTree(files, (filePath) => this.loadFile(filePath));
} catch (error) {
console.error('Error loading tree:', error);
ui.treeContainer.innerHTML = `<div class="loading" style="color: #991b1b;">Error loading repository: ${error.message}</div>`;
}
}
/**
* Load file content
* @param {string} filePath - Path to the file
* @param {string} revision - Optional specific revision
*/
async loadFile(filePath, revision = null) {
try {
this.currentFile = filePath;
ui.showLoading('Loading file...');
const content = await api.getFileContent(filePath, revision);
ui.displayFile(filePath, content);
// Load history for this file
await this.loadFileHistory(filePath);
} catch (error) {
console.error('Error loading file:', error);
ui.showError(`Error loading file: ${error.message}`);
}
}
/**
* Load file at specific revision
* @param {string} filePath - Path to the file
* @param {string} revision - Specific revision
*/
async loadFileAtRevision(filePath, revision) {
try {
await this.loadFile(filePath, revision);
ui.showView(ui.fileView);
} catch (error) {
console.error('Error loading file at revision:', error);
ui.showError(`Error loading file: ${error.message}`);
}
}
/**
* Load file history
* @param {string} filePath - Path to the file
*/
async loadFileHistory(filePath) {
try {
const history = await api.getFileHistory(filePath);
this.currentHistory = history;
ui.populateRevisionSelectors(history);
} catch (error) {
console.error('Error loading history:', error);
}
}
/**
* Show file history view
*/
async showHistory() {
if (!this.currentFile) return;
try {
ui.showLoading('Loading history...');
const history = await api.getFileHistory(this.currentFile);
ui.displayHistory(history);
} catch (error) {
console.error('Error showing history:', error);
ui.showError(`Error loading history: ${error.message}`);
}
}
/**
* Show diff view
*/
showDiffView() {
if (!this.currentFile || !this.currentHistory) return;
ui.populateRevisionSelectors(this.currentHistory);
ui.showView(ui.diffView);
}
/**
* Generate and display diff
*/
async generateDiff() {
if (!this.currentFile) return;
const rev1 = ui.rev1Select.value;
const rev2 = ui.rev2Select.value;
if (!rev1 || !rev2) {
ui.showError('Please select two revisions');
return;
}
try {
ui.diffContent.innerHTML = '<div class="loading">Generating diff...</div>';
const diffResult = await api.getDiff(this.currentFile, rev1, rev2);
const diffText = diffResult.diff || diffResult;
ui.displayDiff(diffText);
} catch (error) {
console.error('Error generating diff:', error);
ui.showError(`Error generating diff: ${error.message}`);
}
}
/**
* Show diff for a history item (comparing with previous revision)
* @param {string} filePath - Path to the file
* @param {string} currentRevision - Current revision
* @param {string} previousRevision - Previous revision (or null if this is the first)
*/
async showHistoryItemDiff(filePath, currentRevision, previousRevision) {
if (!filePath) return;
try {
ui.diffContent.innerHTML = '<div class="loading">Generating diff...</div>';
if (!previousRevision) {
// If there's no previous revision, show a message
ui.diffContent.innerHTML = '<div class="loading">This is the first revision - no previous version to compare</div>';
ui.showView(ui.diffView);
return;
}
// Generate diff between previous and current revision
const diffResult = await api.getDiff(filePath, previousRevision, currentRevision);
const diffText = diffResult.diff || diffResult;
ui.displayDiff(diffText);
} catch (error) {
console.error('Error generating history item diff:', error);
ui.diffContent.innerHTML = `<div class="loading" style="color: #991b1b;">Error generating diff: ${error.message}</div>`;
ui.showView(ui.diffView);
}
}
/**
* Show patchsets view
*/
async showPatchsets() {
try {
ui.patchsetContent.innerHTML = '<div class="loading">Loading patchsets...</div>';
const patchsets = await api.getPatchsets();
ui.displayPatchsets(patchsets);
} catch (error) {
console.error('Error showing patchsets:', error);
ui.patchsetContent.innerHTML = `<div class="loading" style="color: #991b1b;">Error loading patchsets: ${error.message}</div>`;
ui.showView(ui.patchsetView);
}
}
/**
* Show patchset diff
* @param {string} patchset - Patchset number
*/
async showPatchsetDiff(patchset) {
try {
ui.patchsetDiffContent.innerHTML = '<div class="loading">Loading patchset diff...</div>';
const diffResult = await api.getPatchsetDiff(patchset);
const diffText = diffResult.diff || diffResult;
ui.displayPatchsetDiff(diffText);
} catch (error) {
console.error('Error showing patchset diff:', error);
ui.patchsetDiffContent.innerHTML = `<div class="loading" style="color: #991b1b;">Error loading patchset diff: ${error.message}</div>`;
ui.showView(ui.patchsetDiffView);
}
}
/**
* Setup event listeners
*/
setupEventListeners() {
// File view buttons
ui.historyBtn.addEventListener('click', () => this.showHistory());
ui.diffBtn.addEventListener('click', () => this.showDiffView());
// History view back button
ui.backFromHistoryBtn.addEventListener('click', () => {
ui.showView(ui.fileView);
});
// Diff view buttons
ui.generateDiffBtn.addEventListener('click', () => this.generateDiff());
ui.backFromDiffBtn.addEventListener('click', () => {
ui.showView(ui.fileView);
});
// Patchset view buttons
ui.patchsetsBtn.addEventListener('click', () => this.showPatchsets());
ui.backFromPatchsetBtn.addEventListener('click', () => {
ui.showView(ui.welcomeView);
});
// Patchset diff view back button
ui.backFromPatchsetDiffBtn.addEventListener('click', () => {
ui.showView(ui.patchsetView);
});
}
}
// Initialize application when DOM is ready
document.addEventListener('DOMContentLoaded', () => {
window.app = new CVSRepositoryBrowser();
});