-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrc.js
85 lines (65 loc) · 1.92 KB
/
src.js
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
const NUMBER_OF_SHEETS = 10;
const FREE_SPACE = 'Free Space';
const STORAGE_KEY = 'bingoWasHisNameO'
const bingoItemsInput = document.getElementById( 'bingoItems' );
window.addEventListener( 'load', () => {
bingoItemsInput.value = window.localStorage.getItem( STORAGE_KEY );
} );
function makeBingoTable( elements ) {
let tableHTML = `<table>`;
for ( let row = 0; row < 5; row++ ) {
tableHTML += '<tr>';
for ( let col = 0; col < 5; col++ ) {
tableHTML += '<td>'
let elementForTableData = elements.pop();
if ( row === 2 && col === 2 ) {
elementForTableData = FREE_SPACE;
}
tableHTML += `${elementForTableData}</td>`;
}
tableHTML += '</tr>\n';
}
tableHTML += '</table>\n';
return tableHTML;
}
function getBingoSheet( title, elements ) {
return `<div class="newPage">
<h2>${title}</h2>
<div class="rightSize">
${makeBingoTable( elements )}
</div>
</div>`;
}
document.getElementById( 'create' ).addEventListener( 'click', () => {
const rawInput = bingoItemsInput.value;
// save in localStorage
window.localStorage.setItem( STORAGE_KEY, rawInput );
const elements = rawInput.split( /[\n,]/ );
console.log( elements );
if ( elements.length < 24 ) {
return alert( 'you need 24 or more items for a bingo sheet!' );
}
const title = document.getElementById( 'title' ).value;
console.log( 'title', title );
let bingoSheets = '';
for ( let i = 0; i < NUMBER_OF_SHEETS; i++ ) {
bingoSheets += getBingoSheet( title, _.shuffle( elements ) );
}
const popup = window.open();
if ( !popup ) {
window.alert( 'Popup was blocked, please allow popups to launch the customized simulation.' );
}
else {
const newTab = `<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="robots" content="none"/>
<link rel="stylesheet" href="print.css">
</head>
<body>
${bingoSheets}
</body>
</html>`;
popup.document.write( newTab );
}
} );