|
1 | 1 | // /nginxaas-google/js/cost-calculator_v2.js |
2 | 2 | (() => { |
3 | | - // ---- Single-tier pricing ---- |
4 | | - const costs = { |
5 | | - fixedHourly: 0.10, // $/hour |
6 | | - ncuHourly: 0.008, // $/NCU/hour |
7 | | - dataPerGb: 0.0096 // $/GB (monthly) |
| 3 | + // ---- Multi-tier pricing ---- |
| 4 | + const tierCosts = { |
| 5 | + tier1: { |
| 6 | + fixedHourly: 0.10, // $/hour |
| 7 | + ncuHourly: 0.008, // $/NCU/hour |
| 8 | + dataPerGb: 0.0096 // $/GB (monthly) |
| 9 | + }, |
| 10 | + tier2: { |
| 11 | + fixedHourly: 0.133, |
| 12 | + ncuHourly: 0.0106, |
| 13 | + dataPerGb: 0.0127 |
| 14 | + }, |
| 15 | + tier3: { |
| 16 | + fixedHourly: 0.166, |
| 17 | + ncuHourly: 0.0132, |
| 18 | + dataPerGb: 0.0159 |
| 19 | + } |
8 | 20 | }; |
| 21 | + let currentTier = "tier1"; |
| 22 | + let costs = tierCosts[currentTier]; |
9 | 23 |
|
10 | 24 | const utils = { |
11 | 25 | calculateCost: (costs, values) => { |
|
31 | 45 |
|
32 | 46 | // ---- Element refs ---- |
33 | 47 | const costFormElements = { |
| 48 | + tierSelect: document.getElementById("tierSelect"), |
34 | 49 | numNcus: document.getElementById("numNcus"), |
35 | 50 | numHours: document.getElementById("numHours"), |
36 | 51 | dataProcessedGb: document.getElementById("dataProcessedGb"), |
|
47 | 62 | }; |
48 | 63 |
|
49 | 64 | // ---- Listeners ---- |
50 | | - const setupChangeListeners = (costs, values = calculatorValuesState) => { |
| 65 | + const setupChangeListeners = (values = calculatorValuesState) => { |
51 | 66 | Object.keys(costFormElements).forEach((elName) => { |
52 | 67 | costFormElements[elName].addEventListener("change", (evt) => { |
53 | | - values[elName] = Number(evt.target.value); |
54 | | - updateCost(costs); |
| 68 | + if (elName === "tierSelect") { |
| 69 | + currentTier = evt.target.value; |
| 70 | + costs = tierCosts[currentTier]; |
| 71 | + updateCost(costs, values); |
| 72 | + } else { |
| 73 | + values[elName] = Number(evt.target.value); |
| 74 | + updateCost(costs, values); |
| 75 | + } |
55 | 76 | }); |
56 | 77 | }); |
57 | 78 |
|
|
65 | 86 | Object.keys(costFormElements).forEach((elName) => { |
66 | 87 | const el = costFormElements[elName]; |
67 | 88 | if (el && (el.tagName.toLowerCase() === "input" || el.tagName.toLowerCase() === "select")) { |
68 | | - el.value = values[elName]; |
| 89 | + if (elName === "tierSelect") { |
| 90 | + el.value = currentTier; |
| 91 | + } else { |
| 92 | + el.value = values[elName]; |
| 93 | + } |
69 | 94 | } |
70 | 95 | }); |
71 | 96 | }; |
|
74 | 99 | const updateCost = (costs, values = calculatorValuesState) => { |
75 | 100 | const updatedTotalCost = utils.calculateCost(costs, values); |
76 | 101 | document.getElementById("total-value").textContent = utils.currencyFormatter(updatedTotalCost); |
77 | | - updateTotalCostDetails(values, updatedTotalCost); |
| 102 | + updateTotalCostDetails(values, updatedTotalCost, costs); |
78 | 103 | }; |
79 | 104 |
|
80 | | - const updateTotalCostDetails = (formValues, totalCost) => { |
| 105 | + const updateTotalCostDetails = (formValues, totalCost, costs) => { |
81 | 106 | totalCostDetailElements.hours.textContent = formValues.numHours; |
82 | 107 | totalCostDetailElements.ncus.textContent = formValues.numNcus; |
83 | 108 | totalCostDetailElements.fixedHourly.textContent = utils.currencyFormatter(costs.fixedHourly, 3); |
|
99 | 124 |
|
100 | 125 | // ---- Boot ---- |
101 | 126 | const start = async () => { |
102 | | - const loaded = costs; |
103 | | - setupChangeListeners(loaded); |
| 127 | + setupChangeListeners(); |
104 | 128 | initializeValues(calculatorValuesState); |
105 | | - updateCost(loaded); // immediately show total on load |
| 129 | + updateCost(costs, calculatorValuesState); // immediately show total on load |
106 | 130 | }; |
107 | 131 | start(); |
108 | 132 | })(); |
0 commit comments