-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
105 lines (88 loc) · 3.97 KB
/
script.js
File metadata and controls
105 lines (88 loc) · 3.97 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
document.addEventListener('DOMContentLoaded', function() {
const calculateBtn = document.getElementById('calculateBtn');
const printTimeInput = document.getElementById('printTime');
const filamentWeightInput = document.getElementById('filamentWeight');
const printPlatesInput = document.getElementById('printPlates');
const complexitySelect = document.getElementById('complexity');
// Result elements
const laborCostPerPlate = document.getElementById('laborCostPerPlate');
const totalLaborCost = document.getElementById('totalLaborCost');
const machineCost = document.getElementById('machineCost');
const filamentCost = document.getElementById('filamentCost');
const baseCost = document.getElementById('baseCost');
const finalPrice = document.getElementById('finalPrice');
// Pricing constants
const MACHINE_COST_PER_HOUR = 2;
const FILAMENT_COST_PER_GRAM = 0.02;
function calculatePrice() {
// Get input values
const printTime = parseFloat(printTimeInput.value) || 0;
const filamentWeight = parseFloat(filamentWeightInput.value) || 0;
const printPlates = parseInt(printPlatesInput.value) || 1;
const selectedLaborCostPerPlate = parseFloat(complexitySelect.value) || 5;
// Calculate costs
const laborCostValue = printPlates * selectedLaborCostPerPlate;
const machineCostValue = printTime * MACHINE_COST_PER_HOUR;
const filamentCostValue = filamentWeight * FILAMENT_COST_PER_GRAM;
// Calculate base cost
const baseCostValue = laborCostValue + machineCostValue + filamentCostValue;
// Final price is the same as base cost
const finalPriceValue = baseCostValue;
// Update display
laborCostPerPlate.textContent = `$${selectedLaborCostPerPlate.toFixed(2)}`;
totalLaborCost.textContent = `$${laborCostValue.toFixed(2)}`;
machineCost.textContent = `$${machineCostValue.toFixed(2)}`;
filamentCost.textContent = `$${filamentCostValue.toFixed(2)}`;
baseCost.textContent = `$${baseCostValue.toFixed(2)}`;
finalPrice.textContent = `$${finalPriceValue.toFixed(2)}`;
// Add visual feedback
finalPrice.style.color = '#667eea';
finalPrice.style.fontWeight = 'bold';
}
// Calculate button click event
calculateBtn.addEventListener('click', calculatePrice);
// Real-time calculation on input change
printTimeInput.addEventListener('input', calculatePrice);
filamentWeightInput.addEventListener('input', calculatePrice);
printPlatesInput.addEventListener('input', calculatePrice);
complexitySelect.addEventListener('change', calculatePrice);
// Add some helpful input validation and formatting
printTimeInput.addEventListener('blur', function() {
if (this.value && parseFloat(this.value) < 0) {
this.value = 0;
calculatePrice();
}
});
filamentWeightInput.addEventListener('blur', function() {
if (this.value && parseFloat(this.value) < 0) {
this.value = 0;
calculatePrice();
}
});
printPlatesInput.addEventListener('blur', function() {
if (this.value && parseInt(this.value) < 1) {
this.value = 1;
calculatePrice();
}
});
// Initialize with default values
calculatePrice();
// Add keyboard shortcuts
document.addEventListener('keydown', function(e) {
if (e.key === 'Enter' && (e.target.tagName === 'INPUT' || e.target.tagName === 'SELECT')) {
calculatePrice();
}
});
// Add some helpful tooltips or info
const inputs = document.querySelectorAll('input, select');
inputs.forEach(input => {
input.addEventListener('focus', function() {
this.style.borderColor = '#667eea';
});
input.addEventListener('blur', function() {
if (!this.value) {
this.style.borderColor = '#e1e8ed';
}
});
});
});