-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBracketScript.js
More file actions
138 lines (111 loc) · 5.39 KB
/
BracketScript.js
File metadata and controls
138 lines (111 loc) · 5.39 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
//Right now if you add only 2 participants brackets plugin does not want to accept it in DE version
let buildBrackets = document.getElementById('StartTournament');
let bracketInner; // DIV element where we store our bracket that has a class equals to the weight category
let itemOption; // variable that stores weigh class value retrieved from Options of a select box
let select = document.getElementById('bracketsList'); // variable that stores select box of hide-show brackets
let selectElimType = document.getElementById('StartBrackets');
let eliminationType;
let numberOfBrackets; // according to the type, if someone choose tripple elimination we will create 3 single elimination brackets.
buildBrackets.addEventListener("click", brackets);
selectElimType.addEventListener("click", pick_elimination);
select.addEventListener("change", showBrakets);
$('#innerModal button').on("click", () => {
$('.modal').removeClass('active');
})
function pick_elimination(){
let selectedElimType = $("#PickElimination").val();
if(selectedElimType == 'Single elimination'){
eliminationType = [];
}
else if(selectedElimType == 'Double elimination'){
eliminationType = [[[[]]], [], []];
}
else if(selectedElimType == 'Triple elimination'){
eliminationType = [];
}
else{
$('#innerModal > p').text('Please select elimination type');
$('.modal').addClass('active');
}
}
function brackets(){
let bracketMain = document.createElement('DIV');
bracketMain.classList.add('BracketsVisual')
select.parentNode.insertBefore(bracketMain, select.nextSibling); //inserting main Brackets DIV right after the Brackets Select
let inputs = document.getElementsByClassName('weight');
// CHECKING IF WE HAVE AT LEAST ONE WEIGHT cLASS ENTERED
if(inputs.length >= 1 && inputs[0].value !== ""){
// removing ALL options so we can insert new ones
for(let i = select.options.length - 1 ; i > 0 ; i--)
{
select.options.remove(i);
}
// Inserting new options from input elements
for (let i = 0; i < inputs.length; i++) {
let itemText = inputs[i].value;
let option = document.createElement('option');
option.textContent = itemText;
option.value = itemText;
option.id = itemText;
select.appendChild(option);
}
}
let hiddenDivs = document.querySelectorAll('#page-2 > .hiddenDiv').length; // get the number of hidden divs on page2
let partisipantName;// contains all info about participants in a div #i
var hugeData = [];
for (let i = 0; i < hiddenDivs; i++){
partisipantName = document.querySelectorAll(`.p${i} > .participants`);
var mediumData = [];
let smallData = [];
for(let i = 0; i < partisipantName.length; i++){
if(i != 0 && i%2 == 0){ // here we are controlling the push so it is made every 2! itterations
mediumData.push(smallData);
smallData = [];
}
smallData.push(partisipantName[i].childNodes[0].value + ' ' + partisipantName[i].childNodes[1].value);
if((i+1)==partisipantName.length){ // here we checks if the loop got to the last element we are making last push to the hugeData array and end working on this weight class
if(smallData.length%2 != 0){ // here we checks if last small data class contains just one participant (number of participants might not be even) we are adding NULL so brackets can work with it
smallData.push(null);
}
mediumData.push(smallData);
smallData = [];
}
}
hugeData.push(mediumData);
//insert brackets into newely created divs
itemOption = select.options[i+1].innerText;
bracketInner = document.createElement('DIV');
bracketInner.classList.add(itemOption, 'hiddenDiv');
bracketMain.appendChild(bracketInner);
var bigData = {
teams: hugeData[i],
results: eliminationType // minimal array to initialize DE bracket
}
$(function () {
$(`.${itemOption}`).bracket(
{
init: bigData,
save: function () {
},
disableToolbar: true,
disableTeamEdit: true,
skipConsolationRound: true,
centerConnectors: true
}
)
})
}
}
function showBrakets(){
let hiddenDivs = document.querySelectorAll('.BracketsVisual > .hiddenDiv'); // get all divs with class Hidden from a parent div of Brackets
let selectedOptionIndex = select.options[select.selectedIndex]; // get an index of selected option
for (let index = 0; index < hiddenDivs.length; index++){
if(hiddenDivs[index].classList.item(0) == selectedOptionIndex.id ){
let selectedDiv = hiddenDivs[index];
selectedDiv.classList.add('showDiv');
}
else{
hiddenDivs[index].classList.remove('showDiv');
}
}
}