-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
250 lines (214 loc) · 8.08 KB
/
Copy pathscript.js
File metadata and controls
250 lines (214 loc) · 8.08 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
const nameInput = document.getElementById("patientName");
const weightInput = document.getElementById("weight");
const heightInput = document.getElementById("height");
const nameErr = document.getElementById("nameErr");
const weightErr = document.getElementById("weightErr");
const heightErr = document.getElementById("heightErr");
const resultBox = document.getElementById("resultBox");
const bmiValueEl = document.getElementById("bmiValue");
const bmiCatEl = document.getElementById("bmiCategory");
const bmiAdviceEl = document.getElementById("bmiAdvice");
const shareBtn = document.getElementById("shareBtn");
const copyBtn = document.getElementById("copyBtn");
const exportBtn = document.getElementById("exportBtn");
const shareStatus = document.getElementById("shareStatus");
function sanitizeNumberInput(raw) {
if (typeof raw !== "string") return "";
let s = raw.trim().replace(",", ".");
s = s.replace(/[^0-9.]/g, "");
const parts = s.split(".");
if (parts.length > 2) {
s = parts[0] + "." + parts.slice(1).join("");
}
return s;
}
function validateInputs() {
weightInput.value = sanitizeNumberInput(weightInput.value);
heightInput.value = sanitizeNumberInput(heightInput.value);
const name = nameInput.value.trim();
const weight = parseFloat(weightInput.value);
const height = parseFloat(heightInput.value);
nameErr.textContent = "";
weightErr.textContent = "";
heightErr.textContent = "";
nameInput.classList.remove("error");
weightInput.classList.remove("error");
heightInput.classList.remove("error");
let ok = true;
if (!name) {
nameErr.textContent = "Enter patient name.";
nameInput.classList.add("error");
ok = false;
}
if (!weight || Number.isNaN(weight) || weight <= 0 || weight > 500) {
weightErr.textContent = "Enter valid weight (1–500 kg).";
weightInput.classList.add("error");
ok = false;
}
if (!height || Number.isNaN(height) || height <= 0 || height > 250) {
heightErr.textContent = "Enter valid height (50–250 cm).";
heightInput.classList.add("error");
ok = false;
}
return ok ? { name, weight, heightCm: height } : null;
}
function getCategory(bmi) {
if (bmi < 18.5) {
return {
label: "Underweight",
className: "cat-under",
advice:
"BMI below healthy range. Consider discussing nutrition and weight gain with a professional."
};
} else if (bmi < 25) {
return {
label: "Normal weight",
className: "cat-normal",
advice:
"BMI is in the healthy range. Continue balanced diet and regular physical activity."
};
} else if (bmi < 30) {
return {
label: "Overweight",
className: "cat-over",
advice:
"BMI above healthy range. Adjusting diet and increasing activity may help."
};
} else {
return {
label: "Obese",
className: "cat-obese",
advice:
"BMI in obese range. Consider a structured weight management plan with a healthcare professional."
};
}
}
function updateBMI() {
const values = validateInputs();
if (!values) {
resultBox.classList.remove("show");
bmiValueEl.textContent = "--";
bmiCatEl.innerHTML = "";
bmiAdviceEl.textContent =
"Enter patient name, weight and height above to see BMI.";
shareBtn.disabled = true;
copyBtn.disabled = true;
exportBtn.disabled = true;
shareStatus.textContent = "";
return;
}
const { weight, heightCm } = values;
const heightM = heightCm / 100;
const bmi = weight / (heightM * heightM);
const bmiRounded = Number(bmi.toFixed(1));
bmiValueEl.textContent = bmiRounded.toFixed(1);
const cat = getCategory(bmiRounded);
bmiCatEl.innerHTML = `
<span class="category-pill ${cat.className}">
<span class="badge-chip"></span>
${cat.label}
</span>
`;
bmiAdviceEl.textContent = cat.advice;
resultBox.classList.add("show");
shareBtn.disabled = false;
copyBtn.disabled = false;
exportBtn.disabled = false;
shareStatus.textContent = "";
}
async function shareResult() {
const values = validateInputs();
if (!values) {
shareStatus.textContent = "Fix input errors before sharing.";
return;
}
const { name, weight, heightCm } = values;
const bmiText = bmiValueEl.textContent;
const catText = bmiCatEl.textContent.trim();
const summary =
`Patient: ${name}\n` +
`Weight: ${weight.toFixed(1)} kg\n` +
`Height: ${heightCm.toFixed(1)} cm\n` +
`BMI: ${bmiText}\n` +
`Category: ${catText}\n` +
`Note: BMI is a general indicator and does not replace full clinical assessment.`;
try {
if (navigator.share) {
await navigator.share({
title: "Patient BMI Result",
text: summary
});
shareStatus.textContent = "Shared via system share (if app selected).";
} else {
shareStatus.textContent = "System share not supported; use copy instead.";
}
} catch (err) {
shareStatus.textContent = "Share cancelled or failed.";
}
}
async function copyNameAndBMI() {
const values = validateInputs();
if (!values) {
shareStatus.textContent = "Fix input errors before copying.";
return;
}
const { name } = values;
const bmiText = bmiValueEl.textContent;
const text = `Patient: ${name} | BMI: ${bmiText}`;
try {
if (navigator.clipboard && navigator.clipboard.writeText) {
await navigator.clipboard.writeText(text);
shareStatus.textContent = "Name & BMI copied to clipboard.";
} else {
shareStatus.textContent = "Clipboard API not supported in this browser.";
}
} catch (err) {
shareStatus.textContent = "Copy to clipboard failed.";
}
}
function exportCSV() {
const values = validateInputs();
if (!values) {
shareStatus.textContent = "Fix input errors before exporting.";
return;
}
const { name, weight, heightCm } = values;
const bmiText = bmiValueEl.textContent;
const catText = bmiCatEl.textContent.trim();
const rows = [
["Patient Name", "Weight (kg)", "Height (cm)", "BMI", "Category"],
[name, weight.toFixed(1), heightCm.toFixed(1), bmiText, catText]
];
const csv = rows
.map(row =>
row
.map(field => {
const f = String(field).replace(/"/g, '""');
return `"${f}"`;
})
.join(",")
)
.join("\n");
const blob = new Blob([csv], { type: "text/csv;charset=utf-8;" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "patient_bmi.csv";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
shareStatus.textContent = "CSV file downloaded.";
}
["input", "change", "blur"].forEach(eventName => {
nameInput.addEventListener(eventName, updateBMI);
weightInput.addEventListener(eventName, updateBMI);
heightInput.addEventListener(eventName, updateBMI);
});
shareBtn.addEventListener("click", shareResult);
copyBtn.addEventListener("click", copyNameAndBMI);
exportBtn.addEventListener("click", exportCSV);
nameInput.value = "John Doe";
weightInput.value = "70";
heightInput.value = "170";
updateBMI();