Spaces:
Running
Running
File size: 10,934 Bytes
afc0068 7f8f9a6 afc0068 7f8f9a6 afc0068 613d6e8 afc0068 |
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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
/**
* Form Manager Module
* Handles form operations, validation, and data management
*/
export class FormManager {
constructor(apiClient, uiManager) {
this.apiClient = apiClient;
this.uiManager = uiManager;
this.uploadedPhotos = {};
this.audioFile = null;
this.currentEditId = null;
}
async initialize() {
try {
const formOptions = await this.apiClient.loadFormOptions();
this.renderMultiSelect('utilityOptions', formOptions.utilities);
this.renderMultiSelect('phenologyOptions', formOptions.phenologyStages);
this.renderPhotoCategories(formOptions.photoCategories);
} catch (error) {
console.error('Error initializing form:', error);
throw error;
}
}
renderMultiSelect(containerId, options) {
const container = document.getElementById(containerId);
if (!container) return;
container.innerHTML = '';
options.forEach(option => {
const label = document.createElement('label');
label.innerHTML = `
<input type="checkbox" value="${option}"> ${option}
`;
container.appendChild(label);
});
}
renderPhotoCategories(categories) {
const container = document.getElementById('photoCategories');
if (!container) return;
container.innerHTML = '';
categories.forEach(category => {
const categoryDiv = document.createElement('div');
categoryDiv.className = 'photo-category';
categoryDiv.innerHTML = `
<div class="photo-category-header">
<div class="photo-category-title">
<div class="photo-category-icon">IMG</div>
${category}
</div>
</div>
<div class="photo-upload-area">
<div class="photo-upload" data-category="${category}">
<div class="photo-upload-icon">+</div>
<div>Click to select ${category} photo</div>
</div>
<button type="button" class="camera-btn" data-category="${category}">
Camera
</button>
</div>
<div class="uploaded-file" id="photo-${category}" style="display: none;"></div>
`;
container.appendChild(categoryDiv);
});
}
getSelectedValues(containerId) {
const container = document.getElementById(containerId);
if (!container) return [];
const checkboxes = container.querySelectorAll('input[type="checkbox"]:checked');
return Array.from(checkboxes).map(cb => cb.value);
}
getFormData() {
const utilityValues = this.getSelectedValues('utilityOptions');
const phenologyValues = this.getSelectedValues('phenologyOptions');
return {
latitude: this.getNumericValue('latitude'),
longitude: this.getNumericValue('longitude'),
location_name: this.getStringValue('locationName'),
local_name: this.getStringValue('localName'),
scientific_name: this.getStringValue('scientificName'),
common_name: this.getStringValue('commonName'),
tree_code: this.getStringValue('treeCode'),
height: this.getNumericValue('height'),
width: this.getNumericValue('width'),
utility: utilityValues.length > 0 ? utilityValues : [],
phenology_stages: phenologyValues.length > 0 ? phenologyValues : [],
storytelling_text: this.getStringValue('storytellingText'),
storytelling_audio: this.audioFile,
photographs: Object.keys(this.uploadedPhotos).length > 0 ? this.uploadedPhotos : null,
notes: this.getStringValue('notes')
};
}
getNumericValue(fieldId) {
const element = document.getElementById(fieldId);
if (!element || !element.value) return null;
const value = parseFloat(element.value);
return isNaN(value) ? null : value;
}
getStringValue(fieldId) {
const element = document.getElementById(fieldId);
return element && element.value ? element.value : null;
}
populateForm(treeData) {
this.setFieldValue('latitude', treeData.latitude);
this.setFieldValue('longitude', treeData.longitude);
this.setFieldValue('locationName', treeData.location_name || '');
this.setFieldValue('localName', treeData.local_name || '');
this.setFieldValue('scientificName', treeData.scientific_name || '');
this.setFieldValue('commonName', treeData.common_name || '');
this.setFieldValue('treeCode', treeData.tree_code || '');
this.setFieldValue('height', treeData.height || '');
this.setFieldValue('width', treeData.width || '');
this.setFieldValue('storytellingText', treeData.storytelling_text || '');
this.setFieldValue('notes', treeData.notes || '');
// Handle utility checkboxes
if (treeData.utility && Array.isArray(treeData.utility)) {
this.setCheckboxValues('utilityOptions', treeData.utility);
}
// Handle phenology checkboxes
if (treeData.phenology_stages && Array.isArray(treeData.phenology_stages)) {
this.setCheckboxValues('phenologyOptions', treeData.phenology_stages);
}
}
setFieldValue(fieldId, value) {
const element = document.getElementById(fieldId);
if (element) {
element.value = value;
}
}
setCheckboxValues(containerId, values) {
const container = document.getElementById(containerId);
if (!container) return;
container.querySelectorAll('input[type="checkbox"]').forEach(checkbox => {
checkbox.checked = values.includes(checkbox.value);
});
}
resetForm(silent = false) {
const form = document.getElementById('treeForm');
if (form) {
form.reset();
}
this.uploadedPhotos = {};
this.audioFile = null;
this.currentEditId = null;
// Clear uploaded file indicators
document.querySelectorAll('.uploaded-file').forEach(el => {
el.style.display = 'none';
el.innerHTML = '';
});
// Reset audio controls
this.resetAudioControls();
if (!silent) {
this.uiManager.showMessage('Form has been reset.', 'success');
}
}
resetAudioControls() {
const audioElement = document.getElementById('audioPlayback');
if (audioElement) {
audioElement.classList.add('hidden');
audioElement.src = '';
}
const recordingStatus = document.getElementById('recordingStatus');
if (recordingStatus) {
recordingStatus.textContent = 'Click to start recording';
}
const audioUploadResult = document.getElementById('audioUploadResult');
if (audioUploadResult) {
audioUploadResult.innerHTML = '';
}
}
async handleFileUpload(file, type, category = null) {
try {
const result = await this.apiClient.uploadFile(file, type, category);
if (type === 'image' && category) {
this.uploadedPhotos[category] = result.filename;
this.showUploadSuccess(category, file.name, 'photo');
} else if (type === 'audio') {
this.audioFile = result.filename;
this.showUploadSuccess(null, file.name, 'audio');
}
return result;
} catch (error) {
console.error(`Error uploading ${type}:`, error);
this.uiManager.showMessage(`Error uploading ${type}: ${error.message}`, 'error');
throw error;
}
}
showUploadSuccess(category, filename, type) {
if (type === 'photo' && category) {
const resultDiv = document.getElementById(`photo-${category}`);
if (resultDiv) {
resultDiv.style.display = 'block';
resultDiv.innerHTML = `${filename} uploaded successfully`;
}
} else if (type === 'audio') {
const resultDiv = document.getElementById('audioUploadResult');
if (resultDiv) {
resultDiv.innerHTML = `<div class="uploaded-file">${filename} uploaded successfully</div>`;
}
}
}
setEditMode(treeId) {
this.currentEditId = treeId;
// Update submit button
const submitBtn = document.querySelector('button[type="submit"]');
if (submitBtn) {
submitBtn.textContent = 'Update Tree Record';
}
// Add cancel edit button if it doesn't exist
this.addCancelButton();
}
addCancelButton() {
if (document.getElementById('cancelEdit')) return;
const cancelBtn = document.createElement('button');
cancelBtn.type = 'button';
cancelBtn.id = 'cancelEdit';
cancelBtn.className = 'tt-btn tt-btn-outline';
cancelBtn.textContent = 'Cancel Edit';
const formActions = document.querySelector('.form-actions');
const submitBtn = document.querySelector('button[type="submit"]');
if (formActions && submitBtn) {
formActions.insertBefore(cancelBtn, submitBtn);
}
}
exitEditMode() {
this.currentEditId = null;
// Restore original submit button text
const submitBtn = document.querySelector('button[type="submit"]');
if (submitBtn) {
submitBtn.textContent = 'Save Tree Record';
}
// Remove cancel button
const cancelBtn = document.getElementById('cancelEdit');
if (cancelBtn) {
cancelBtn.remove();
}
}
isInEditMode() {
return this.currentEditId !== null;
}
getCurrentEditId() {
return this.currentEditId;
}
validateForm() {
const latitude = this.getNumericValue('latitude');
const longitude = this.getNumericValue('longitude');
if (latitude === null || longitude === null) {
throw new Error('Latitude and longitude are required');
}
if (latitude < -90 || latitude > 90) {
throw new Error('Latitude must be between -90 and 90 degrees');
}
if (longitude < -180 || longitude > 180) {
throw new Error('Longitude must be between -180 and 180 degrees');
}
return true;
}
}
|