refactor roi calc js into modular files
This commit is contained in:
parent
51d80364c0
commit
afe3817395
11 changed files with 1611 additions and 1144 deletions
112
hub/services/static/js/roi-calculator/input-utils.js
Normal file
112
hub/services/static/js/roi-calculator/input-utils.js
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
/**
|
||||
* Input Utilities Module
|
||||
* Handles input formatting, validation, and parsing
|
||||
*/
|
||||
class InputUtils {
|
||||
static formatNumberWithCommas(num) {
|
||||
try {
|
||||
return parseInt(num).toLocaleString('en-US');
|
||||
} catch (error) {
|
||||
console.error('Error formatting number with commas:', error);
|
||||
return String(num);
|
||||
}
|
||||
}
|
||||
|
||||
static parseFormattedNumber(str) {
|
||||
if (typeof str !== 'string') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
try {
|
||||
// Remove all non-numeric characters except decimal points and commas
|
||||
const cleaned = str.replace(/[^\d,.-]/g, '');
|
||||
|
||||
// Handle empty string
|
||||
if (!cleaned) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Remove commas and parse as float
|
||||
const result = parseFloat(cleaned.replace(/,/g, ''));
|
||||
|
||||
// Return 0 for invalid numbers or NaN
|
||||
return isNaN(result) ? 0 : result;
|
||||
} catch (error) {
|
||||
console.error('Error parsing formatted number:', error);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static handleInvestmentAmountInput(input) {
|
||||
if (!input || typeof input.value !== 'string') {
|
||||
console.error('Invalid input element provided to handleInvestmentAmountInput');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Remove non-numeric characters except commas
|
||||
let value = input.value.replace(/[^\d,]/g, '');
|
||||
|
||||
// Parse the numeric value
|
||||
let numericValue = InputUtils.parseFormattedNumber(value);
|
||||
|
||||
// Enforce min/max limits
|
||||
const minValue = 100000;
|
||||
const maxValue = 2000000;
|
||||
|
||||
if (numericValue < minValue) {
|
||||
numericValue = minValue;
|
||||
} else if (numericValue > maxValue) {
|
||||
numericValue = maxValue;
|
||||
}
|
||||
|
||||
// Update the data attribute with the raw numeric value
|
||||
input.setAttribute('data-value', numericValue.toString());
|
||||
|
||||
// Format and display the value with commas
|
||||
input.value = InputUtils.formatNumberWithCommas(numericValue);
|
||||
|
||||
// Update the slider if it exists
|
||||
const slider = document.getElementById('investment-slider');
|
||||
if (slider) {
|
||||
slider.value = numericValue;
|
||||
}
|
||||
|
||||
// Trigger calculations
|
||||
if (window.ROICalculatorApp && window.ROICalculatorApp.calculator) {
|
||||
window.ROICalculatorApp.calculator.updateCalculations();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error handling investment amount input:', error);
|
||||
// Set a safe default value
|
||||
input.setAttribute('data-value', '500000');
|
||||
input.value = '500,000';
|
||||
}
|
||||
}
|
||||
|
||||
static getCSRFToken() {
|
||||
try {
|
||||
// Try to get CSRF token from meta tag first
|
||||
const metaTag = document.querySelector('meta[name="csrf-token"]');
|
||||
if (metaTag) {
|
||||
return metaTag.getAttribute('content');
|
||||
}
|
||||
|
||||
// Fallback to cookie method
|
||||
const cookies = document.cookie.split(';');
|
||||
for (let cookie of cookies) {
|
||||
const [name, value] = cookie.trim().split('=');
|
||||
if (name === 'csrftoken') {
|
||||
return decodeURIComponent(value);
|
||||
}
|
||||
}
|
||||
|
||||
// If no CSRF token found, return empty string (will cause server error, but won't break JS)
|
||||
console.error('CSRF token not found');
|
||||
return '';
|
||||
} catch (error) {
|
||||
console.error('Error getting CSRF token:', error);
|
||||
return '';
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue