-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
134 lines (110 loc) · 3.74 KB
/
Copy pathscript.js
File metadata and controls
134 lines (110 loc) · 3.74 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
// Get the current language from the URL path
const lang = window.location.pathname.includes('/fr') ? 'fr' : 'en';
let words = []; // Full word list
let currentDeck = []; // Current shuffled deck
// Update the fetch path to use language-specific wordlist
fetch(`wordlist_${lang}.txt`)
.then(response => {
console.log('Fetched file path:', response.url);
return response.text();
})
.then(wordList => {
words = wordList
.trim()
.split('\n')
.filter(Boolean); // Removes empty lines
// Initialize shuffled deck
currentDeck = [...words];
shuffleDeck();
// Initialize display after words are loaded
displayWords();
setupModal();
setupRefreshButton();
})
.catch(error => {
console.error('Error reading file:', error.message);
});
// shuffling deck to avoid word repetitions
function shuffleDeck() {
for (let i = currentDeck.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[currentDeck[i], currentDeck[j]] = [currentDeck[j], currentDeck[i]];
}
}
function getRandomWords(count) {
// reshuffle if we don't have enough words left
if (currentDeck.length < count) {
currentDeck = [...words];
shuffleDeck();
}
return currentDeck.splice(0, count);
}
function createCard(word) {
const card = document.createElement('div');
card.className = 'card';
card.textContent = word;
card.draggable = true;
card.addEventListener('click', () => {
const newWord = getRandomWords(1)[0];
card.textContent = newWord;
});
card.addEventListener('dragstart', (e) => {
card.classList.add('dragging');
});
card.addEventListener('dragend', (e) => {
card.classList.remove('dragging');
});
return card;
}
function displayWords() {
const container = document.getElementById('cardsContainer');
container.innerHTML = '';
const count = Math.floor(Math.random() * 2) + 2; // 2 or 3 words
const selectedWords = getRandomWords(count);
selectedWords.forEach(word => {
container.appendChild(createCard(word));
});
}
function setupRefreshButton() {
const refreshButton = document.querySelector('.refresh-button');
refreshButton.addEventListener('click', displayWords);
}
const container = document.getElementById('cardsContainer');
container.addEventListener('dragover', handleDragOver);
function handleDragOver(e) {
e.preventDefault();
const container = document.getElementById('cardsContainer');
const draggingCard = container.querySelector('.dragging');
const cards = [...container.querySelectorAll('.card:not(.dragging)')];
const afterCard = cards.reduce((closest, child) => {
const box = child.getBoundingClientRect();
const offset = e.clientX - (box.left + box.width / 2);
if (offset < 0 && offset > closest.offset) {
return { offset, element: child };
} else {
return closest;
}
}, { offset: Number.NEGATIVE_INFINITY }).element;
if (afterCard) {
container.insertBefore(draggingCard, afterCard);
} else {
container.appendChild(draggingCard);
}
}
function setupModal() {
const modal = document.getElementById('helpModal');
const modalBtn = document.querySelector('.modal-button');
const closeBtn = document.querySelector('.close-modal');
modal.style.display = 'none';
modalBtn.onclick = () => {
modal.style.display = 'block';
};
closeBtn.onclick = () => {
modal.style.display = 'none';
};
window.onclick = (event) => {
if (event.target === modal) {
modal.style.display = 'none';
}
};
}