hmaras commited on
Commit
9a99f1d
·
verified ·
1 Parent(s): 0573433

// Build Calculator and Utility Handling

Browse files

document.addEventListener('DOMContentLoaded', function() {
const buildForm = document.getElementById('build-form');
const previewElements = {
toon: document.getElementById('preview-toon'),
buildName: document.getElementById('preview-build-name'),
trinkets: document.getElementById('preview-trinkets'),
rationale: document.getElementById('preview-rationale'),
ratingSection: document.getElementById('build-rating')
};

// --- Configuration Maps ---
const config = {
toonNames: { 'vee': 'Vee', 'pebble': 'Pebble', 'sprout': 'Sprout', 'shelly': 'Shelly' },
goalNames: { 'ichor': 'Ichor Farming', 'survival': 'Solo Survival', 'research': 'Research Farming', 'support': 'Team Support' },
trinketNames: { 'remote': "Vee's Remote", 'magnifying': 'Magnifying Glass', 'plush': 'Dandy Plush', 'bow': 'Pink Bow' }
};

if (buildForm) {
// --- Real-time Preview Update ---
buildForm.addEventListener('input', function() {
const formData = new FormData(buildForm);
const data = Object.fromEntries(formData.entries());

// Toon and Goal Display
const toonDisplay = config.toonNames[data.toon] || 'Select a Toon';
previewElements.toon.textContent = toonDisplay;

// Build Name and Goal
let buildNameDisplay = data.buildName || 'No build name yet';
if (data.goal && config.goalNames[data.goal]) {
buildNameDisplay = `${buildNameDisplay} - ${config.goalNames[data.goal]}`;
}
previewElements.buildName.textContent = buildNameDisplay;

// Trinket Display
const trinkets = [];
if (data.trinket1 && config.trinketNames[data.trinket1]) trinkets.push(config.trinketNames[data.trinket1]);
if (data.trinket2 && config.trinketNames[data.trinket2]) trinkets.push(config.trinketNames[data.trinket2]);
previewElements.trinkets.textContent = trinkets.join(' + ') || 'No trinkets selected';

// Rationale Display
previewElements.rationale.textContent = data.rationale || 'Add your strategic rationale to see it here';

// Show rating section when key fields are filled
const isComplete = data.toon && data.goal && data.trinket1 && data.trinket2 && data.buildName;
previewElements.ratingSection.classList.toggle('hidden', !isComplete);

// Re-render Feather icons if used in the preview
feather.replace();
});

// --- Form Submission and Local Storage Save ---
buildForm.addEventListener('submit', function(e) {
e.preventDefault();
const formData = new FormData(buildForm);
const data = Object.fromEntries(formData.entries());

// Simple validation check (assuming select fields have default value="")
if (!data.toon || !data.goal || !data.trinket1 || !data.trinket2 || !data.buildName || !data.rationale) {
alert('Please fill all required fields, including the rationale!');
return;
}

// Save to localStorage
const builds = JSON.parse(localStorage.getItem('builds') || '[]');
builds.push({ ...data, date: new Date().toISOString() }); // Use spread syntax for efficiency
localStorage.setItem('builds', JSON.stringify(builds));

alert('Build saved successfully!');
buildForm.reset();

// Re-render Feather icons after reset
feather.replace();
});
}

// --- Smooth Scrolling for Anchor Links ---
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
if (target) {
target.scrollIntoView({ behavior: 'smooth' });
}
});
});

// --- Initialize Tooltips ---
const tooltipTriggers = document.querySelectorAll('[data-tooltip]');

tooltipTriggers.forEach(trigger => {
const tooltip = document.createElement('div');
// Use consistent classes for styling
tooltip.className = 'tooltip hidden md:block bg-gray-800 text-white px-3 py-1 rounded text-xs absolute z-40 whitespace-nowrap opacity-95 transition-opacity duration-200';
tooltip.textContent = trigger.getAttribute('data-tooltip');

// Use a relative container for better positioning
trigger.style.position = 'relative';
trigger.appendChild(tooltip);

trigger.addEventListener('mouseenter', () => {
tooltip.classList.remove('hidden');
});

trigger.addEventListener('mouseleave', () => {
tooltip.classList.add('hidden');
});
});
});

Files changed (1) hide show
  1. script.js +68 -101
script.js CHANGED
@@ -1,9 +1,7 @@
1
 
2
- // Build Calculator Form Handling
3
  document.addEventListener('DOMContentLoaded', function() {
4
  const buildForm = document.getElementById('build-form');
5
- if (!buildForm) return;
6
-
7
  const previewElements = {
8
  toon: document.getElementById('preview-toon'),
9
  buildName: document.getElementById('preview-build-name'),
@@ -12,125 +10,94 @@ document.addEventListener('DOMContentLoaded', function() {
12
  ratingSection: document.getElementById('build-rating')
13
  };
14
 
15
- // Maps for display names
16
- const displayNames = {
17
- toon: {
18
- 'vee': 'Vee',
19
- 'pebble': 'Pebble',
20
- 'sprout': 'Sprout',
21
- 'shelly': 'Shelly'
22
- },
23
- goal: {
24
- 'ichor': 'Ichor Farming',
25
- 'survival': 'Solo Survival',
26
- 'research': 'Research Farming',
27
- 'support': 'Team Support'
28
- },
29
- trinket: {
30
- 'remote': "Vee's Remote",
31
- 'magnifying': 'Magnifying Glass',
32
- 'plush': 'Dandy Plush',
33
- 'bow': 'Pink Bow'
34
- }
35
  };
36
 
37
- // Update preview function
38
- function updatePreview() {
39
- const formData = new FormData(buildForm);
40
- const data = Object.fromEntries(formData.entries());
41
-
42
- // Update toon preview
43
- if (data.toon && displayNames.toon[data.toon]) {
44
- previewElements.toon.textContent = displayNames.toon[data.toon];
45
- } else {
46
- previewElements.toon.textContent = 'Select a Toon';
47
- }
48
-
49
- // Update build name preview
50
- if (data.buildName) {
51
- previewElements.buildName.textContent = data.buildName;
52
- if (data.goal && displayNames.goal[data.goal]) {
53
- previewElements.buildName.textContent += ` - ${displayNames.goal[data.goal]}`;
54
  }
55
- } else {
56
- previewElements.buildName.textContent = 'No build name yet';
57
- }
58
 
59
- // Update trinkets preview
60
- if (data.trinket1 || data.trinket2) {
61
  const trinkets = [];
62
- if (data.trinket1 && displayNames.trinket[data.trinket1]) trinkets.push(displayNames.trinket[data.trinket1]);
63
- if (data.trinket2 && displayNames.trinket[data.trinket2]) trinkets.push(displayNames.trinket[data.trinket2]);
64
  previewElements.trinkets.textContent = trinkets.join(' + ') || 'No trinkets selected';
65
- } else {
66
- previewElements.trinkets.textContent = 'No trinkets selected';
67
- }
68
 
69
- // Update rationale preview
70
- if (data.rationale) {
71
- previewElements.rationale.textContent = data.rationale;
72
- } else {
73
- previewElements.rationale.textContent = 'Add your strategic rationale to see it here';
74
- }
75
 
76
- // Show rating section when form is complete
77
- if (data.toon && data.goal && data.trinket1 && data.trinket2 && data.buildName) {
78
- previewElements.ratingSection.classList.remove('hidden');
79
- } else {
80
- previewElements.ratingSection.classList.add('hidden');
81
- }
82
 
83
- feather.replace();
84
- }
85
-
86
- // Initialize preview
87
- updatePreview();
88
 
89
- // Listen for all form changes
90
- buildForm.addEventListener('input', updatePreview);
91
- buildForm.addEventListener('change', updatePreview);
92
- // Handle form submission
93
- buildForm.addEventListener('submit', function(e) {
94
- e.preventDefault();
95
- const formData = new FormData(buildForm);
96
- const data = Object.fromEntries(formData.entries());
 
 
 
97
 
98
- // Validate form
99
- if (!data.toon || !data.goal || !data.trinket1 || !data.trinket2 || !data.buildName) {
100
- alert('Please fill all required fields!');
101
- return;
102
- }
103
 
104
- // Save to localStorage
105
- const builds = JSON.parse(localStorage.getItem('builds') || '[]');
106
- builds.push({
107
- ...data,
108
- date: new Date().toISOString()
109
  });
110
- localStorage.setItem('builds', JSON.stringify(builds));
111
 
112
- alert('Build saved successfully!');
113
- buildForm.reset();
114
- updatePreview(); // Reset preview after submission
115
- });
116
- // Smooth scrolling for anchor links
117
- document.querySelectorAll('a[href^="#"]').forEach(anchor => {
118
- anchor.addEventListener('click', function (e) {
119
- e.preventDefault();
120
- document.querySelector(this.getAttribute('href')).scrollIntoView({
121
- behavior: 'smooth'
122
  });
123
  });
124
- });
125
 
126
- // Initialize tooltips
127
- document.addEventListener('DOMContentLoaded', function() {
128
  const tooltipTriggers = document.querySelectorAll('[data-tooltip]');
129
 
130
  tooltipTriggers.forEach(trigger => {
131
  const tooltip = document.createElement('div');
132
- tooltip.className = 'tooltip hidden md:block bg-dark text-white px-3 py-2 rounded text-sm absolute z-10 whitespace-nowrap';
 
133
  tooltip.textContent = trigger.getAttribute('data-tooltip');
 
 
 
134
  trigger.appendChild(tooltip);
135
 
136
  trigger.addEventListener('mouseenter', () => {
@@ -141,4 +108,4 @@ document.addEventListener('DOMContentLoaded', function() {
141
  tooltip.classList.add('hidden');
142
  });
143
  });
144
- });
 
1
 
2
+ // Build Calculator and Utility Handling
3
  document.addEventListener('DOMContentLoaded', function() {
4
  const buildForm = document.getElementById('build-form');
 
 
5
  const previewElements = {
6
  toon: document.getElementById('preview-toon'),
7
  buildName: document.getElementById('preview-build-name'),
 
10
  ratingSection: document.getElementById('build-rating')
11
  };
12
 
13
+ // --- Configuration Maps ---
14
+ const config = {
15
+ toonNames: { 'vee': 'Vee', 'pebble': 'Pebble', 'sprout': 'Sprout', 'shelly': 'Shelly' },
16
+ goalNames: { 'ichor': 'Ichor Farming', 'survival': 'Solo Survival', 'research': 'Research Farming', 'support': 'Team Support' },
17
+ trinketNames: { 'remote': "Vee's Remote", 'magnifying': 'Magnifying Glass', 'plush': 'Dandy Plush', 'bow': 'Pink Bow' }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  };
19
 
20
+ if (buildForm) {
21
+ // --- Real-time Preview Update ---
22
+ buildForm.addEventListener('input', function() {
23
+ const formData = new FormData(buildForm);
24
+ const data = Object.fromEntries(formData.entries());
25
+
26
+ // Toon and Goal Display
27
+ const toonDisplay = config.toonNames[data.toon] || 'Select a Toon';
28
+ previewElements.toon.textContent = toonDisplay;
29
+
30
+ // Build Name and Goal
31
+ let buildNameDisplay = data.buildName || 'No build name yet';
32
+ if (data.goal && config.goalNames[data.goal]) {
33
+ buildNameDisplay = `${buildNameDisplay} - ${config.goalNames[data.goal]}`;
 
 
 
34
  }
35
+ previewElements.buildName.textContent = buildNameDisplay;
 
 
36
 
37
+ // Trinket Display
 
38
  const trinkets = [];
39
+ if (data.trinket1 && config.trinketNames[data.trinket1]) trinkets.push(config.trinketNames[data.trinket1]);
40
+ if (data.trinket2 && config.trinketNames[data.trinket2]) trinkets.push(config.trinketNames[data.trinket2]);
41
  previewElements.trinkets.textContent = trinkets.join(' + ') || 'No trinkets selected';
 
 
 
42
 
43
+ // Rationale Display
44
+ previewElements.rationale.textContent = data.rationale || 'Add your strategic rationale to see it here';
 
 
 
 
45
 
46
+ // Show rating section when key fields are filled
47
+ const isComplete = data.toon && data.goal && data.trinket1 && data.trinket2 && data.buildName;
48
+ previewElements.ratingSection.classList.toggle('hidden', !isComplete);
 
 
 
49
 
50
+ // Re-render Feather icons if used in the preview
51
+ feather.replace();
52
+ });
 
 
53
 
54
+ // --- Form Submission and Local Storage Save ---
55
+ buildForm.addEventListener('submit', function(e) {
56
+ e.preventDefault();
57
+ const formData = new FormData(buildForm);
58
+ const data = Object.fromEntries(formData.entries());
59
+
60
+ // Simple validation check (assuming select fields have default value="")
61
+ if (!data.toon || !data.goal || !data.trinket1 || !data.trinket2 || !data.buildName || !data.rationale) {
62
+ alert('Please fill all required fields, including the rationale!');
63
+ return;
64
+ }
65
 
66
+ // Save to localStorage
67
+ const builds = JSON.parse(localStorage.getItem('builds') || '[]');
68
+ builds.push({ ...data, date: new Date().toISOString() }); // Use spread syntax for efficiency
69
+ localStorage.setItem('builds', JSON.stringify(builds));
 
70
 
71
+ alert('Build saved successfully!');
72
+ buildForm.reset();
73
+
74
+ // Re-render Feather icons after reset
75
+ feather.replace();
76
  });
77
+ }
78
 
79
+ // --- Smooth Scrolling for Anchor Links ---
80
+ document.querySelectorAll('a[href^="#"]').forEach(anchor => {
81
+ anchor.addEventListener('click', function (e) {
82
+ e.preventDefault();
83
+ const target = document.querySelector(this.getAttribute('href'));
84
+ if (target) {
85
+ target.scrollIntoView({ behavior: 'smooth' });
86
+ }
 
 
87
  });
88
  });
 
89
 
90
+ // --- Initialize Tooltips ---
 
91
  const tooltipTriggers = document.querySelectorAll('[data-tooltip]');
92
 
93
  tooltipTriggers.forEach(trigger => {
94
  const tooltip = document.createElement('div');
95
+ // Use consistent classes for styling
96
+ tooltip.className = 'tooltip hidden md:block bg-gray-800 text-white px-3 py-1 rounded text-xs absolute z-40 whitespace-nowrap opacity-95 transition-opacity duration-200';
97
  tooltip.textContent = trigger.getAttribute('data-tooltip');
98
+
99
+ // Use a relative container for better positioning
100
+ trigger.style.position = 'relative';
101
  trigger.appendChild(tooltip);
102
 
103
  trigger.addEventListener('mouseenter', () => {
 
108
  tooltip.classList.add('hidden');
109
  });
110
  });
111
+ });