Some checks failed
Deploy to GitHub Pages / build-and-deploy (push) Has been cancelled
117 lines
3.5 KiB
JavaScript
117 lines
3.5 KiB
JavaScript
// State management module
|
|
const StateManager = (function() {
|
|
class StateManager {
|
|
constructor() {
|
|
this.history = [];
|
|
this.settings = {
|
|
theme: 'dark',
|
|
mode: 'standard', // 'standard' or 'rpn'
|
|
historyLimit: 50
|
|
};
|
|
this.loadFromStorage();
|
|
}
|
|
|
|
// Add a calculation to history
|
|
addToHistory(expression, result) {
|
|
const entry = {
|
|
id: Date.now().toString(),
|
|
expression: expression,
|
|
result: result,
|
|
timestamp: new Date()
|
|
};
|
|
|
|
this.history.unshift(entry); // Add to beginning of array
|
|
|
|
// Limit history size
|
|
if (this.history.length > this.settings.historyLimit) {
|
|
this.history = this.history.slice(0, this.settings.historyLimit);
|
|
}
|
|
|
|
this.saveToStorage();
|
|
}
|
|
|
|
// Get calculation history
|
|
getHistory() {
|
|
return [...this.history]; // Return a copy to prevent external modification
|
|
}
|
|
|
|
// Get settings
|
|
getSettings() {
|
|
return { ...this.settings }; // Return a copy
|
|
}
|
|
|
|
// Update settings
|
|
updateSettings(newSettings) {
|
|
this.settings = { ...this.settings, ...newSettings };
|
|
this.saveToStorage();
|
|
}
|
|
|
|
// Toggle calculator mode (standard/RPN)
|
|
toggleMode() {
|
|
this.settings.mode = this.settings.mode === 'standard' ? 'rpn' : 'standard';
|
|
this.saveToStorage();
|
|
return this.settings.mode;
|
|
}
|
|
|
|
// Get current mode
|
|
getMode() {
|
|
return this.settings.mode;
|
|
}
|
|
|
|
// Clear history
|
|
clearHistory() {
|
|
this.history = [];
|
|
this.saveToStorage();
|
|
}
|
|
|
|
// Save state to localStorage
|
|
saveToStorage() {
|
|
try {
|
|
const stateData = {
|
|
history: this.history,
|
|
settings: this.settings
|
|
};
|
|
localStorage.setItem('geekCalculatorState', JSON.stringify(stateData));
|
|
} catch (error) {
|
|
console.error('Failed to save state to localStorage:', error);
|
|
}
|
|
}
|
|
|
|
// Load state from localStorage
|
|
loadFromStorage() {
|
|
try {
|
|
const stateData = localStorage.getItem('geekCalculatorState');
|
|
if (stateData) {
|
|
const parsedData = JSON.parse(stateData);
|
|
this.history = Array.isArray(parsedData.history) ? parsedData.history : [];
|
|
this.settings = parsedData.settings || this.settings;
|
|
}
|
|
} catch (error) {
|
|
console.error('Failed to load state from localStorage:', error);
|
|
// If loading fails, keep default state
|
|
}
|
|
}
|
|
|
|
// Get the last calculation result
|
|
getLastResult() {
|
|
if (this.history.length > 0) {
|
|
return this.history[0].result; // First item is the most recent
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// Re-run a specific calculation from history
|
|
rerunCalculation(id) {
|
|
const entry = this.history.find(item => item.id === id);
|
|
if (entry) {
|
|
return {
|
|
expression: entry.expression,
|
|
result: entry.result
|
|
};
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|
|
return { StateManager };
|
|
})(); |