-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomizer.js
134 lines (123 loc) · 4.14 KB
/
randomizer.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
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
// Constants
const kTableBodyIdx = 0;
function rand(max){
var value = Math.random() * max;
var intValue = Math.floor(value);
return intValue;
}
function randomize(){
// Create array to iterate over
var tableIDs = ['body', 'cockpit', 'booster'];
// Iterate over each table
for (var index in tableIDs){
// Get string from array
var tableID = tableIDs[index];
// Get tables from DOM
var table = document.getElementById(tableID);
// Get indexes for rows which are enabled
var indexes = getCheckedRowIndexes(table);
// Get random sub-selection
var inputFieldID = 'input-' + tableID;
var total = document.getElementById(inputFieldID).value;
var subIndexes = getRandomSubSelection(indexes, total);
// Disable all rows, enable those selected
setRowsState(table, 'none');
setRowsStateByIndex(table, 'inherit', subIndexes);
}
}
function getCheckedRowIndexes(table){
//
var indexes = [];
//
var tableBody = table.children[kTableBodyIdx];
//
for (var i in tableBody.children){
// Getting a weird error where item() is last element returned? Skip it.
if (isNaN(i))
continue;
var tableRow = tableBody.children[i];
// Convert to zero-based index
var index = tableRow.children[0].innerText - 1;
// Checkbox is element 1 of table row. Input is sub element (0).
var checkbox = tableRow.children[1].children[0];
var isChecked = checkbox.checked;
if (isChecked){
indexes.push(index);
//console.log('checked: ' + index);
}
}
return indexes;
}
function setRowsState(table, state){
//
var tbody = table.children[kTableBodyIdx];
// Turn off all rows
for (var index in tbody.children){
// Getting a weird error where item is last element returned? Skip it.
if (isNaN(index))
continue;
//
var tableRow = tbody.children[index];
tableRow.style.display = state;
}
}
function setRowsStateByIndex(table, state, indexes){
//
var tbody = table.children[kTableBodyIdx];
// Enable selected rows
for (var i in indexes){
var index = indexes[i];
var tableRow = tbody.children[index];
tableRow.style.display = state;
}
}
function getRandomSubSelection(indexes, total){
// Get min value from set
var min = total < indexes.length ? total : indexes.length;
// Clone array. We need to remove selected items from list as we iterate to prevent duplicates.
var indexesClone = [...indexes]
// Init parts array for randomly selected parts. Iterate for n parts, pick at random.
var subIndexes = [];
//
for (var i = 0; i < min; i++){
// Get random index to select
var randomIndex = rand(indexesClone.length);
// Get index from list, add to selection
var index = indexesClone[randomIndex];
subIndexes.push(index);
// Remove item from clone list
indexesClone.splice(randomIndex, 1);
}
return subIndexes;
}
function setRowsCheckboxByClass(classID, isChecked){
var rows = document.getElementsByClassName(classID);
for (var i in rows){
// Getting a weird error where item is last element returned? Skip it.
if (isNaN(i))
continue;
//
var row = rows[i];
// Checkbox is element 1 of table row. Input is sub element (0).
var checkbox = row.children[1].children[0];
checkbox.checked = isChecked;
}
}
function setRowsChecked(isChecked){
setRowsCheckboxByClass('gx', isChecked);
setRowsCheckboxByClass('ax', isChecked);
setRowsCheckboxByClass('ex', isChecked);
}
function showHideTables(state){
// Create array to iterate over
var tableIDs = ['body', 'cockpit', 'booster'];
// Iterate over each table
for (var index in tableIDs){
// Get string from array
var tableID = tableIDs[index];
// Get tables from DOM
var table = document.getElementById(tableID);
// Disable all rows, enable those selected
setRowsState(table, state);
}
}