Spaces:
Running
Running
File size: 7,459 Bytes
7c060f8 | 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 | document.addEventListener('DOMContentLoaded', () => {
// DOM Elements
const personInput = document.getElementById('personInput');
const addPersonBtn = document.getElementById('addPersonBtn');
const peopleList = document.getElementById('peopleList');
const clearListBtn = document.getElementById('clearListBtn');
const importBtn = document.getElementById('importBtn');
const exportBtn = document.getElementById('exportBtn');
const groupSize = document.getElementById('groupSize');
const shuffleMethod = document.getElementById('shuffleMethod');
const generateBtn = document.getElementById('generateBtn');
const resultsSection = document.getElementById('resultsSection');
const groupsContainer = document.getElementById('groupsContainer');
const copyResultsBtn = document.getElementById('copyResultsBtn');
// Load people from localStorage
let people = JSON.parse(localStorage.getItem('groupGeniePeople')) || [];
// Render people list
function renderPeopleList() {
peopleList.innerHTML = '';
people.forEach((person, index) => {
const li = document.createElement('li');
li.className = 'flex justify-between items-center bg-gray-50 p-2 rounded';
li.innerHTML = `
<span>${person}</span>
<button data-index="${index}" class="text-red-500 hover:text-red-700">
<i data-feather="x"></i>
</button>
`;
peopleList.appendChild(li);
});
feather.replace();
}
// Add person(s) to list
function addPerson() {
const input = personInput.value.trim();
if (input.includes(',') || input.includes(' ') || input.includes('\n')) {
addMultiplePeople(input);
} else if (input && !people.includes(input)) {
people.push(input);
localStorage.setItem('groupGeniePeople', JSON.stringify(people));
}
personInput.value = '';
renderPeopleList();
}
// Remove person from list
function removePerson(index) {
people.splice(index, 1);
localStorage.setItem('groupGeniePeople', JSON.stringify(people));
renderPeopleList();
}
// Clear all people
function clearPeople() {
if (confirm('Are you sure you want to clear all people?')) {
people = [];
localStorage.removeItem('groupGeniePeople');
renderPeopleList();
}
}
// Import people from text with multiple separators (comma, space, or newline)
function importPeople() {
const text = prompt('Enter names separated by commas, spaces, or new lines:');
if (text) {
const newPeople = text.split(/[\n,\s]+/) // Split by comma, space, or newline
.map(name => name.trim())
.filter(name => name);
people = [...new Set([...people, ...newPeople])];
localStorage.setItem('groupGeniePeople', JSON.stringify(people));
renderPeopleList();
}
}
// Add multiple people from input
function addMultiplePeople(names) {
const newPeople = names.split(/[\n,\s]+/)
.map(name => name.trim())
.filter(name => name);
newPeople.forEach(name => {
if (name && !people.includes(name)) {
people.push(name);
}
});
localStorage.setItem('groupGeniePeople', JSON.stringify(people));
renderPeopleList();
}
// Export people to clipboard
function exportPeople() {
navigator.clipboard.writeText(people.join('\n'))
.then(() => alert('People list copied to clipboard!'))
.catch(err => console.error('Failed to copy:', err));
}
// Fisher-Yates shuffle algorithm
function fisherYatesShuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
// Random sort shuffle (less reliable)
function randomSortShuffle(array) {
return array.sort(() => Math.random() - 0.5);
}
// Generate groups
function generateGroups() {
if (people.length === 0) {
alert('Please add some people first!');
return;
}
const membersPerGroup = parseInt(groupSize.value) || 3;
if (membersPerGroup < 1) {
alert('Members per group must be at least 1');
return;
}
// Shuffle based on selected method
const shuffledPeople = shuffleMethod.value === 'fisher-yates'
? fisherYatesShuffle([...people])
: randomSortShuffle([...people]);
// Create groups
const groups = [];
for (let i = 0; i < shuffledPeople.length; i += membersPerGroup) {
groups.push(shuffledPeople.slice(i, i + membersPerGroup));
}
// Display groups
displayGroups(groups);
}
// Display groups in UI
function displayGroups(groups) {
groupsContainer.innerHTML = '';
groups.forEach((group, index) => {
const groupCard = document.createElement('div');
groupCard.className = `group-card bg-group-${(index % 6) + 1} p-4 rounded-lg shadow-sm`;
groupCard.innerHTML = `
<h3 class="font-bold text-lg mb-3">Group ${index + 1}</h3>
<ul class="space-y-2">
${group.map(member => `<li class="flex items-center gap-2"><i data-feather="user" class="w-4 h-4"></i> ${member}</li>`).join('')}
</ul>
`;
groupsContainer.appendChild(groupCard);
});
resultsSection.classList.remove('hidden');
feather.replace();
}
// Copy results to clipboard
function copyResults() {
const groupElements = document.querySelectorAll('#groupsContainer > div');
let text = '';
groupElements.forEach((group, index) => {
text += `Group ${index + 1}:\n`;
const members = group.querySelectorAll('li');
members.forEach(member => {
text += `- ${member.textContent.trim()}\n`;
});
text += '\n';
});
navigator.clipboard.writeText(text.trim())
.then(() => alert('Groups copied to clipboard!'))
.catch(err => console.error('Failed to copy:', err));
}
// Event listeners
addPersonBtn.addEventListener('click', (e) => {
addPerson();
e.preventDefault(); // Prevent form submission if in a form
});
personInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
addPerson();
e.preventDefault(); // Prevent form submission if in a form
}
});
peopleList.addEventListener('click', (e) => {
if (e.target.closest('button')) {
const index = e.target.closest('button').dataset.index;
removePerson(index);
}
});
clearListBtn.addEventListener('click', clearPeople);
importBtn.addEventListener('click', importPeople);
exportBtn.addEventListener('click', exportPeople);
generateBtn.addEventListener('click', generateGroups);
copyResultsBtn.addEventListener('click', copyResults);
// Initial render
renderPeopleList();
}); |