-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
340 lines (303 loc) · 14.6 KB
/
Copy pathindex.html
File metadata and controls
340 lines (303 loc) · 14.6 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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
<meta name="theme-color" content="#0b1022" />
<link rel="apple-touch-icon" href="icons/apple-touch-icon.png" />
<link rel="manifest" href="manifest.webmanifest" />
<title>Tirage au sort — Noms, objets, nourriture</title>
<style>
:root{
--bg:#0f172a;
--card:#111827ee;
--muted:#94a3b8;
--text:#e5e7eb;
--accent:#22d3ee;
--accent-2:#a78bfa;
--danger:#ef4444;
--ok:#22c55e;
--shadow: 0 20px 40px rgba(0,0,0,.45);
--radius: 18px;
}
html,body{height:100%}
body{
margin:0; font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Ubuntu, Cantarell, Noto Sans, Arial;
color:var(--text);
background: radial-gradient(1200px 600px at 15% 10%, #0ea5e955, transparent 60%),
radial-gradient(800px 400px at 85% 20%, #a78bfa44, transparent 60%),
linear-gradient(180deg, #0b1022, #0b1224 60%, #0b0f1d);
display:grid; place-items:center; padding:24px;
}
@supports (padding: max(0px)) {
body{ padding: max(24px, env(safe-area-inset-top)) max(24px, env(safe-area-inset-right)) max(24px, env(safe-area-inset-bottom)) max(24px, env(safe-area-inset-left)); }
}
.app{
width:min(100%, 960px); background:var(--card); border:1px solid #1f2937; border-radius:var(--radius);
box-shadow:var(--shadow); overflow:hidden;
}
header{display:flex; flex-wrap:wrap; gap:8px; align-items:center; justify-content:space-between; padding:18px 20px; border-bottom:1px solid #1f2937;}
header h1{font-size:clamp(18px, 4vw, 28px); margin:0; letter-spacing:.3px}
header .hint{color:var(--muted); font-size:clamp(12px, 2.6vw, 13px)}
main{padding:20px; display:grid; gap:18px}
.row{display:flex; gap:10px; flex-wrap:wrap}
.grow{flex:1 1 280px}
input[type="text"], input[type="number"], textarea, select{
width:100%; background:#0b1224; color:var(--text); border:1px solid #25314a; border-radius:12px; padding:12px 14px;
outline:none; transition:.2s border-color, .2s box-shadow; font-size:16px;
min-height:44px;
}
input[type="text"]:focus, input[type="number"]:focus, textarea:focus{
border-color:#3b82f6; box-shadow:0 0 0 3px #3b82f622;
}
button{
border:0; background:linear-gradient(135deg, var(--accent), var(--accent-2)); color:#0b1022; font-weight:700;
padding:12px 16px; border-radius:14px; cursor:pointer; transition: transform .04s ease, filter .2s ease;
font-size:16px; min-height:44px;
}
button:active{ transform: translateY(1px); }
button.secondary{ background:#0b1224; color:#e5e7eb; border:1px solid #25314a }
button.danger{ background:linear-gradient(135deg, #fb7185, #ef4444); color:#0b1022 }
button.ghost{ background:transparent; border:1px dashed #2b3a5a; color:#94a3b8 }
.chip-list{ display:flex; flex-wrap:wrap; gap:8px; padding:6px 0 }
.chip{ background:#0b1224; border:1px solid #26324c; padding:8px 10px; border-radius:999px; display:inline-flex; align-items:center; gap:8px }
.chip .x{ width:22px; height:22px; display:grid; place-items:center; border-radius:50%; background:#1e293b; color:#cbd5e1; font-size:14px; cursor:pointer }
.controls{ display:flex; flex-wrap:wrap; gap:10px; align-items:center }
.controls .box{ background:#0b1224; border:1px solid #26324c; border-radius:12px; padding:10px 12px; display:flex; align-items:center; gap:10px }
.controls label{ color:#94a3b8; font-size:14px }
.result-card{ background:linear-gradient(180deg, #0b1224, #0b1022); border:1px solid #26324c; border-radius:16px; padding:16px; display:grid; gap:8px }
.result{ font-size:clamp(24px, 6vw, 46px); font-weight:900; letter-spacing:.5px; word-break:break-word }
.result.spin{ animation: spin 0.75s ease }
@keyframes spin{ 0%{opacity:.4; filter:blur(4px)} 100%{opacity:1; filter:blur(0)} }
.history{ display:grid; gap:8px }
.history h3{ margin:.3rem 0; font-size:16px; color:#94a3b8 }
.history .items{ display:flex; flex-wrap:wrap; gap:8px }
.badge{ background:#0b1224; border:1px dashed #26324c; color:#cbd5e1; border-radius:999px; padding:6px 10px; font-size:13px }
footer{ padding:14px 20px; border-top:1px solid #1f2937; color:#94a3b8; font-size:12px; display:flex; justify-content:space-between; align-items:center; gap:8px; flex-wrap:wrap }
.links{ display:flex; gap:8px; flex-wrap:wrap }
.sr-only{ position:absolute; width:1px; height:1px; padding:0; margin:-1px; overflow:hidden; clip:rect(0,0,0,0); border:0 }
@media (max-width: 560px){
body{ padding:16px }
main{ padding:16px }
header{ padding:14px 16px }
.app{ width:100%; border-radius:12px }
.controls .box{ width:100%; justify-content:space-between }
.row > button{ flex:1 1 auto }
}
</style>
</head>
<body>
<div class="app" role="application" aria-label="Tirage au sort">
<header>
<h1>🎲 Tirage au sort</h1>
<div class="hint">Entrez des prénoms, des objets ou des plats… puis laissez le hasard décider.</div>
</header>
<main>
<section aria-labelledby="add-label">
<div class="row">
<div class="grow">
<label id="add-label" class="sr-only" for="entry">Ajouter un élément</label>
<input id="entry" type="text" placeholder="Ex: Sarah, Clavier, Pizza… (Entrée pour ajouter)" autocomplete="off" />
</div>
<button id="addBtn" title="Ajouter">Ajouter</button>
<button id="importBtn" class="secondary" title="Importer une liste">Importer</button>
<button id="clearAll" class="danger" title="Vider la liste">Tout effacer</button>
</div>
<div class="chip-list" id="list"></div>
</section>
<section class="controls" aria-label="Options">
<div class="box">
<input type="checkbox" id="noRepeat" checked />
<label for="noRepeat">Sans répétition</label>
</div>
<div class="box">
<label for="winners">Nombre de gagnants</label>
<input id="winners" type="number" min="1" value="1" style="width:110px" />
</div>
<button id="drawBtn">Tirer au sort</button>
<button id="resetDraws" class="ghost">Réinitialiser les tirages</button>
</section>
<section class="result-card" aria-live="polite">
<div class="result" id="result">Aucun tirage pour le moment</div>
<div class="history">
<h3>Historique</h3>
<div class="items" id="history"></div>
</div>
</section>
</main>
<footer>
<span>HTML/CSS/JS 100% local — vos données restent dans votre navigateur.</span>
<div class="links">
<button id="exportBtn" class="secondary">Exporter la liste</button>
<button id="copyLast" class="secondary">Copier le dernier résultat</button>
<button id="installPWA" class="secondary" style="display:none">Installer l’app</button>
</div>
</footer>
</div>
<dialog id="importDialog">
<form method="dialog" style="background:#0b1224; color:#e5e7eb; border:1px solid #26324c; border-radius:16px; padding:18px 16px; min-width:min(600px, 90vw)">
<h3 style="margin:0 0 10px 0">Importer une liste</h3>
<p style="margin:0 0 10px 0; color:#94a3b8">Collez une liste, un élément par ligne (ou séparés par des virgules).</p>
<textarea id="bulk" rows="8" placeholder="Sarah
Paul
Clavier
Pizza" style="width:100%"></textarea>
<div style="display:flex; justify-content:flex-end; gap:8px; margin-top:10px">
<button class="secondary" value="cancel">Annuler</button>
<button id="confirmImport">Importer</button>
</div>
</form>
</dialog>
<script>
(function(){
const entry = document.getElementById('entry');
const addBtn = document.getElementById('addBtn');
const importBtn = document.getElementById('importBtn');
const clearAll = document.getElementById('clearAll');
const listEl = document.getElementById('list');
const noRepeat = document.getElementById('noRepeat');
const winners = document.getElementById('winners');
const drawBtn = document.getElementById('drawBtn');
const resetDraws = document.getElementById('resetDraws');
const resultEl = document.getElementById('result');
const historyEl = document.getElementById('history');
const exportBtn = document.getElementById('exportBtn');
const copyLast = document.getElementById('copyLast');
const installBtn = document.getElementById('installPWA');
const importDialog = document.getElementById('importDialog');
const bulk = document.getElementById('bulk');
const confirmImport = document.getElementById('confirmImport');
let items = load('items', []);
let picked = load('picked', []);
function save(key, val){ localStorage.setItem('raffle_'+key, JSON.stringify(val)); }
function load(key, fallback){ try{ return JSON.parse(localStorage.getItem('raffle_'+key)) ?? fallback }catch{ return fallback } }
function sanitize(s){ return String(s).trim().replace(/[\n\r\t]+/g,' ').replace(/\s{2,}/g,' '); }
function render(){
listEl.innerHTML = '';
items.forEach((txt, idx)=>{
const chip = document.createElement('span'); chip.className='chip'; chip.title=txt;
chip.innerHTML = `<span>${escapeHtml(txt)}</span>`;
const x = document.createElement('span'); x.className='x'; x.textContent='×';
x.addEventListener('click', ()=>{ items.splice(idx,1); save('items', items); picked = picked.filter(p=>p!==txt); save('picked', picked); render(); });
chip.appendChild(x); listEl.appendChild(chip);
});
winners.max = Math.max(1, items.length);
if(Number(winners.value) > items.length){ winners.value = Math.max(1, items.length); }
historyEl.innerHTML = '';
picked.forEach(v=>{
const b = document.createElement('span'); b.className='badge'; b.textContent = v; historyEl.appendChild(b);
});
}
function addMany(values){
const before = new Set(items.map(v=>v.toLowerCase()));
const toAdd = [];
values.forEach(v=>{
const s = sanitize(v);
if(!s) return;
if(!before.has(s.toLowerCase())){ toAdd.push(s); before.add(s.toLowerCase()); }
});
if(toAdd.length){ items = items.concat(toAdd); save('items', items); render(); }
}
function escapeHtml(str){
return str.replace(/[&<>"']/g, (c)=>({"&":"&","<":"<",">":">","\"":""","'":"'"}[c]));
}
addBtn.addEventListener('click', ()=>{
const raw = entry.value.trim();
if(!raw) return;
const parts = raw.split(/[\n,]+/).map(s=>s.trim()).filter(Boolean);
addMany(parts);
entry.value=''; entry.focus();
});
entry.addEventListener('keydown', (e)=>{
if(e.key==='Enter') { e.preventDefault(); addBtn.click(); }
});
clearAll.addEventListener('click', ()=>{
if(!items.length) return;
if(confirm('Vider entièrement la liste ?')){
items = []; picked = []; save('items', items); save('picked', picked); render();
resultEl.textContent = 'Liste vidée';
}
});
importBtn.addEventListener('click', ()=>{
bulk.value = '';
importDialog.showModal();
});
confirmImport.addEventListener('click', (e)=>{
e.preventDefault();
const lines = bulk.value.split(/[\n,]+/).map(s=>s.trim()).filter(Boolean);
addMany(lines);
importDialog.close();
});
drawBtn.addEventListener('click', ()=>{
const want = Math.max(1, Math.min(Number(winners.value)||1, items.length));
const noRep = noRepeat.checked;
const available = noRep ? items.filter(v=>!picked.includes(v)) : items.slice();
if(!available.length){
resultEl.textContent = 'Plus d\'éléments disponibles. Réinitialisez les tirages ou décochez « Sans répétition ».';
resultEl.classList.remove('spin'); void resultEl.offsetWidth; resultEl.classList.add('spin');
return;
}
if(noRep && want > available.length){
alert(`Il ne reste que ${available.length} élément(s) non tiré(s). Réduisez le nombre de gagnants ou réinitialisez.`);
return;
}
const winnersNow = pickRandom(available, want);
resultEl.textContent = winnersNow.join(' • ');
resultEl.classList.remove('spin'); void resultEl.offsetWidth; resultEl.classList.add('spin');
if(noRep){
winnersNow.forEach(w=>{ if(!picked.includes(w)) picked.push(w); });
save('picked', picked); render();
}
});
resetDraws.addEventListener('click', ()=>{
picked = []; save('picked', picked); render();
resultEl.textContent = 'Tirages réinitialisés';
resultEl.classList.remove('spin'); void resultEl.offsetWidth; resultEl.classList.add('spin');
});
exportBtn.addEventListener('click', ()=>{
if(!items.length){ alert('La liste est vide.'); return; }
const blob = new Blob([items.join('\n')], {type:'text/plain'});
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = 'liste_tirage.txt';
document.body.appendChild(a); a.click(); a.remove();
setTimeout(()=>URL.revokeObjectURL(a.href), 1000);
});
copyLast.addEventListener('click', async ()=>{
const text = resultEl.textContent.trim();
if(!text || text.startsWith('Aucun') || text.startsWith('Plus d')){ alert('Aucun résultat à copier.'); return; }
try{ await navigator.clipboard.writeText(text); copyLast.textContent='Copié !'; setTimeout(()=>copyLast.textContent='Copier le dernier résultat', 1200); }
catch{ alert('Impossible de copier automatiquement.'); }
});
function pickRandom(arr, n=1){
const pool = arr.slice();
const res = [];
for(let i=0;i<n;i++){
const idx = Math.floor(Math.random()*pool.length);
res.push(pool.splice(idx,1)[0]);
}
return res;
}
// PWA registration
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('./sw.js').catch(console.warn);
}
let deferredPrompt=null;
window.addEventListener('beforeinstallprompt', (e)=>{
e.preventDefault();
deferredPrompt = e;
if(installBtn) installBtn.style.display='';
});
if(installBtn){
installBtn.addEventListener('click', async ()=>{
if(!deferredPrompt) return;
deferredPrompt.prompt();
const { outcome } = await deferredPrompt.userChoice;
installBtn.textContent = outcome === 'accepted' ? 'Installée' : 'Installation annulée';
deferredPrompt=null;
});
}
})();
</script>
</body>
</html>