You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
274 lines
9.0 KiB
274 lines
9.0 KiB
const UI = {
|
|
CLASSNAMES: {
|
|
EVENT: {
|
|
ITEM: 'event-item',
|
|
YEAR: 'event-year',
|
|
FLAG: 'event-flag',
|
|
LOCATION: 'event-location',
|
|
},
|
|
|
|
SORT: {
|
|
ITEM: 'sort-item',
|
|
TEXT: 'sort-text',
|
|
},
|
|
|
|
ROUND: {
|
|
ITEM: 'round-item',
|
|
HIDE: 'round-hide',
|
|
TEXT: 'round-text',
|
|
},
|
|
},
|
|
|
|
SORT_TYPES: {
|
|
GOALS: 'goals',
|
|
COUNTRY: 'country',
|
|
POPULATION: 'population',
|
|
},
|
|
|
|
ROUND_TYPES: {
|
|
PRELIM: 'prelims',
|
|
ROUNDOF16: 'round-of-16',
|
|
QUARTERFINAL: 'quarterfinals',
|
|
SEMIFINAL: 'semifinals',
|
|
CONSOLATION: 'consolation',
|
|
FINAL: 'finals',
|
|
},
|
|
|
|
DATA: {
|
|
ROOT: 'data-root',
|
|
EVENT: 'data-event-key',
|
|
ROUND: 'data-round-key',
|
|
SORT: 'data-sort-key',
|
|
},
|
|
|
|
I18N: {
|
|
HIDE: 'hide',
|
|
SHOW: 'show',
|
|
},
|
|
|
|
findRoot: (node) => {
|
|
while (node !== document.body && node.getAttribute(UI.DATA.ROOT) === null) {
|
|
node = node.parentNode;
|
|
}
|
|
|
|
return node;
|
|
},
|
|
|
|
clearActive: (nodes) => {
|
|
nodes.forEach(node => {
|
|
const classes = node.className.split(' ');
|
|
classes.splice(1, classes.indexOf('active'));
|
|
node.className = classes.join(' ');
|
|
});
|
|
},
|
|
|
|
updateEventsPane: (eventKey) => {
|
|
const eventItems = document.querySelectorAll(`.${UI.CLASSNAMES.EVENT.ITEM}`);
|
|
UI.clearActive(eventItems);
|
|
|
|
const activeEventNode = document.querySelector(`[${UI.DATA.EVENT}="${eventKey}"`);
|
|
activeEventNode.className += ' active';
|
|
},
|
|
|
|
updateRoundsPane: (roundsToShow, allRounds) => {
|
|
const roundsItems = document.querySelectorAll(`.${UI.CLASSNAMES.ROUND.ITEM}`);
|
|
UI.clearActive(roundsItems);
|
|
|
|
roundsItems.forEach(item => {
|
|
const type = item.getAttribute(UI.DATA.ROUND);
|
|
const hide = item.querySelector(`.${UI.CLASSNAMES.ROUND.HIDE}`);
|
|
hide.innerHTML = UI.I18N.SHOW;
|
|
|
|
if (roundsToShow.indexOf(type) > -1) {
|
|
item.className += ' active';
|
|
hide.innerHTML = UI.I18N.HIDE;
|
|
}
|
|
});
|
|
|
|
const ribbons = document.querySelectorAll('.ribbon');
|
|
ribbons.forEach(ribbon => {
|
|
const name = allRounds[ribbon.getAttribute("data-round-id")];
|
|
const type = UI.getRoundType(name);
|
|
|
|
roundsToShow.indexOf(type) === -1
|
|
? ribbon.style.display = 'none'
|
|
: ribbon.style.display = 'inline';
|
|
});
|
|
},
|
|
|
|
updateSortPane: (sort) => {
|
|
const sortItems = document.querySelectorAll('.sort-item');
|
|
UI.clearActive(sortItems);
|
|
|
|
sortItems.forEach(node => {
|
|
if (node.getAttribute(UI.DATA.SORT) === sort) {
|
|
node.className += ' active';
|
|
}
|
|
});
|
|
},
|
|
|
|
buildEventsPane: (onClick) => {
|
|
const eventsList = [
|
|
{ year: 1930, location: "Uruguay", icon: "uy" },
|
|
{ year: 1934, location: "Italy", icon: "it" },
|
|
{ year: 1938, location: "France", icon: "fr" },
|
|
{ year: 1954, location: "Switzerland", icon: "ch" },
|
|
{ year: 1958, location: "Sweden", icon: "se" },
|
|
{ year: 1962, location: "Chile", icon: "cl" },
|
|
{ year: 1966, location: "England", icon: "gb" },
|
|
{ year: 1970, location: "Mexico", icon: "mx" },
|
|
{ year: 1974, location: "West Germany", icon: "de" },
|
|
{ year: 1978, location: "Argentina", icon: "ar" },
|
|
{ year: 1982, location: "Spain", icon: "es" },
|
|
{ year: 1986, location: "Mexico", icon: "mx" },
|
|
{ year: 1990, location: "Italy", icon: "it" },
|
|
{ year: 1994, location: "USA", icon: "us" },
|
|
{ year: 1998, location: "France", icon: "fr" },
|
|
{ year: 2002, location: "Japan / South Korea", icon: "jp" },
|
|
{ year: 2006, location: "Germany", icon: "de" },
|
|
{ year: 2010, location: "Johannesburg", icon: "za" },
|
|
{ year: 2014, location: "Brazil", icon: "br" },
|
|
];
|
|
|
|
const eventsDiv = document.querySelector('.events');
|
|
|
|
while (eventsDiv.hasChildNodes()) {
|
|
eventsDiv.firstChild.remove();
|
|
}
|
|
|
|
eventsList.forEach((evt) => {
|
|
const item = document.createElement('div');
|
|
const year = document.createElement('div');
|
|
const location = document.createElement('div');
|
|
const flag = document.createElement('div');
|
|
const flagIcon = document.createElement('div');
|
|
|
|
item.className = UI.CLASSNAMES.EVENT.ITEM;
|
|
item.addEventListener('click', onClick);
|
|
item.setAttribute(UI.DATA.EVENT, evt.year);
|
|
item.setAttribute(UI.DATA.ROOT, true);
|
|
|
|
year.className = UI.CLASSNAMES.EVENT.YEAR;
|
|
year.innerHTML = evt.year;
|
|
|
|
location.className = UI.CLASSNAMES.EVENT.LOCATION;
|
|
location.innerHTML = evt.location;
|
|
|
|
flag.className = UI.CLASSNAMES.EVENT.FLAG;
|
|
flagIcon.className = `flag-icon flag-icon-${evt.icon}`;
|
|
|
|
flag.appendChild(flagIcon);
|
|
item.appendChild(flag);
|
|
item.appendChild(year);
|
|
item.appendChild(location);
|
|
|
|
eventsDiv.appendChild(item);
|
|
});
|
|
},
|
|
|
|
buildSortPane: (onClick) => {
|
|
const sortList = [
|
|
{ text: 'Order by continent', value: null },
|
|
{ text: 'Order by goals scored', value: UI.SORT_TYPES.GOALS },
|
|
{ text: 'Order by country name', value: UI.SORT_TYPES.COUNTRY },
|
|
{ text: 'Order by country population', value: UI.SORT_TYPES.POPULATION },
|
|
];
|
|
|
|
const sortDiv = document.querySelector('.sort');
|
|
|
|
while (sortDiv.hasChildNodes()) {
|
|
sortDiv.firstChild.remove();
|
|
}
|
|
|
|
sortList.forEach(sort => {
|
|
const item = document.createElement('div');
|
|
const text = document.createElement('div');
|
|
|
|
item.className = UI.CLASSNAMES.SORT.ITEM;
|
|
item.addEventListener('click', onClick);
|
|
item.setAttribute(UI.DATA.SORT, sort.value);
|
|
item.setAttribute(UI.DATA.ROOT, true);
|
|
|
|
text.className = UI.CLASSNAMES.SORT.TEXT;
|
|
text.innerHTML = sort.text;
|
|
|
|
item.appendChild(text);
|
|
sortDiv.appendChild(item);
|
|
});
|
|
},
|
|
|
|
buildRoundsPane: (onClick) => {
|
|
const roundsList = [
|
|
{ text: 'Preliminaries', value: UI.ROUND_TYPES.PRELIM },
|
|
{ text: 'Round of 16', value: UI.ROUND_TYPES.ROUNDOF16 },
|
|
{ text: 'Quarterfinals', value: UI.ROUND_TYPES.QUARTERFINAL },
|
|
{ text: 'Semifinals', value: UI.ROUND_TYPES.SEMIFINAL },
|
|
{ text: 'Consolation', value: UI.ROUND_TYPES.CONSOLATION },
|
|
{ text: 'Final', value: UI.ROUND_TYPES.FINAL },
|
|
];
|
|
|
|
const roundsDiv = document.querySelector('.rounds');
|
|
|
|
while (roundsDiv.hasChildNodes()) {
|
|
roundsDiv.firstChild.remove();
|
|
}
|
|
|
|
roundsList.forEach(round => {
|
|
const item = document.createElement('div');
|
|
item.className = UI.CLASSNAMES.ROUND.ITEM;
|
|
item.addEventListener('click', onClick);
|
|
item.setAttribute(UI.DATA.ROUND, round.value);
|
|
item.setAttribute(UI.DATA.ROOT, true);
|
|
|
|
const hide = document.createElement('div');
|
|
hide.className = UI.CLASSNAMES.ROUND.HIDE;
|
|
hide.innerHTML = 'hide';
|
|
|
|
const text = document.createElement('div');
|
|
text.className = UI.CLASSNAMES.ROUND.TEXT;
|
|
text.innerHTML = round.text;
|
|
|
|
item.appendChild(text);
|
|
item.appendChild(hide);
|
|
roundsDiv.appendChild(item);
|
|
});
|
|
},
|
|
|
|
getRoundType: (name) => {
|
|
const name2 = name.match(/^Matchday/) ? 'First round' : name;
|
|
|
|
switch (name2) {
|
|
case 'Preliminary round':
|
|
case 'First round':
|
|
case 'First round replays':
|
|
case 'Group-1 Play-off':
|
|
case 'Group-2 Play-off':
|
|
case 'Group-3 Play-off':
|
|
case 'Group-4 Play-off':
|
|
return UI.ROUND_TYPES.PRELIM;
|
|
case 'Round of 16':
|
|
return UI.ROUND_TYPES.ROUNDOF16;
|
|
case 'Third place match':
|
|
case 'Third-place match':
|
|
case 'Match for third place':
|
|
case 'Third place play-off':
|
|
case 'Third-place play-off':
|
|
return UI.ROUND_TYPES.CONSOLATION;
|
|
case 'Quarterfinals':
|
|
case 'Quarter-finals':
|
|
case 'Quarter-finals replays':
|
|
return UI.ROUND_TYPES.QUARTERFINAL;
|
|
case 'Semifinals':
|
|
case 'Semi-finals':
|
|
return UI.ROUND_TYPES.SEMIFINAL;
|
|
case 'Final':
|
|
case 'Finals':
|
|
return UI.ROUND_TYPES.FINAL;
|
|
}
|
|
|
|
console.error(`Unknown round name: ${name}`);
|
|
return name;
|
|
},
|
|
};
|
|
|
|
export default UI;
|
|
|