UNPKG

tods-react-draws

Version:

React Components for displaying draw structures.

1,432 lines (1,314 loc) 49.2 kB
import React from 'react'; import { makeStyles, Grid, TableContainer, Table, TableHead, TableRow, TableBody, TableCell } from '@material-ui/core'; import AccessTimeIcon from '@material-ui/icons/AccessTime'; import cx from 'classnames'; import Paper from '@material-ui/core/Paper'; import { factoryConstants } from 'tods-competition-factory'; var useStyles = /*#__PURE__*/makeStyles(function (theme) { return { paper: { padding: theme.spacing(2), marginBottom: theme.spacing(3) }, drawRoot: { width: '100%', flexWrap: 'nowrap' }, connectorColumn: { minWidth: 15, maxWidth: 20 }, initialColumn: {}, roundColumn: {}, participant: { flexGrow: 1, paddingLeft: 3, paddingRight: 3, whiteSpace: 'nowrap', '&:hover': { cursor: 'pointer', backgroundColor: '#f5f5f5' } }, detailsColumn: { flexWrap: 'nowrap', whiteSpace: 'nowrap' }, positionDetails: { fontSize: 'smaller', marginBottom: '1px' }, sourceDrawPositionRange: { fontSize: 'smaller', marginRight: '.5em', whiteSpace: 'nowrap' }, seededParticipant: { color: '#3e424e', fontWeight: 900 }, hoveredPrticipant: { '&:hover': { cursor: 'pointer', backgroundColor: '#f5f5f5' } }, readyToScore: { height: 17, backgroundColor: '#edf4fc', cursor: 'pointer' }, score: { paddingLeft: 3 }, roundName: { height: '1.5em' }, roundScheduleIcon: { fontSize: 'small', marginRight: '1em' }, noBoderBottom: { paddingBottom: '2px' }, thickBorderBottom: { borderBottom: '2px solid black' }, verticalDivider: { width: 1, marginTop: '4em', borderRight: '1px solid black' }, borderRight: { borderRight: '1px solid black' }, borderBottom: { borderBottom: '1px solid black' }, bracketTop: { borderBottom: '1px solid black' }, bracketBottom: { borderBottom: '1px solid black' }, bracketContent: {} }; }); function generateStandardElimination(_ref) { var height = _ref.height, roundsDefinition = _ref.roundsDefinition, nameFilter = _ref.nameFilter; var priorFeedOffset = 0; var topMargin = roundsDefinition.find(function (r) { return r.feedTop; }) && height / 2; var columns = roundsDefinition.map(function (round, index) { var _round$matchUps; var matchUpsCount = ((_round$matchUps = round.matchUps) == null ? void 0 : _round$matchUps.length) || round.matchUpsCount; return generateColumn({ matchUpsCount: matchUpsCount, round: round, index: index }); }); return columns; function calcHeight(_ref2) { var height = _ref2.height, columnMultiplier = _ref2.columnMultiplier; if (!columnMultiplier) return 0; return height * Math.pow(2, columnMultiplier - 1) + Math.pow(2, columnMultiplier - 1) - 1; } function calcFirstHeight(_ref3) { var height = _ref3.height, columnMultiplier = _ref3.columnMultiplier; if (!columnMultiplier) return 0; var multiplier = columnMultiplier > 1 ? Math.pow(2, columnMultiplier - 2) - 1 : 0; var base = height + height * multiplier; var offset = columnMultiplier > 1 ? height * 0.5 : 0; var borders = multiplier || 1; return base + offset + borders; } function generateColumn(_ref4) { var round = _ref4.round; var columnMultiplier = round.columnMultiplier, feedTop = round.feedTop, feedBottom = round.feedBottom; var matchUpHeight = nameFilter ? height : calcHeight({ height: height, columnMultiplier: columnMultiplier }); var feedOffset = priorFeedOffset + matchUpHeight * (feedTop && -0.5 || feedBottom && 0.5 || 0); priorFeedOffset = feedOffset; var firstMatchUpHeight = nameFilter ? height : calcFirstHeight({ height: height, columnMultiplier: columnMultiplier }) + (topMargin || 0) + feedOffset; var isFinal = round.finalMatchUp; var matchUps = round.matchUps || []; var stackedMatchUpValues = isFinal ? getFinalMatchUpValues({ matchUp: round.finalMatchUp }) : matchUps.map(function (matchUp) { return getStackedMatchUpValues({ matchUp: matchUp, feedBottom: feedBottom }); }).flat(); var frames = isFinal ? stackedMatchUpValues : getFrames(stackedMatchUpValues); return { round: round, matchUpHeight: matchUpHeight, firstMatchUpHeight: firstMatchUpHeight, frames: frames }; } } function getFrames(values) { var firstFrame = [values.shift()]; var frames = multiChunk(values, [3, 2]); frames.unshift(firstFrame); return frames; } function multiChunk(arr, _ref5) { var size = _ref5[0], otherSizes = _ref5.slice(1); return arr.length ? [arr.slice(0, size)].concat(multiChunk(arr.slice(size), [].concat(otherSizes, [size]))) : []; } function getFinalMatchUpValues(_ref6) { var _matchUp$score; var matchUp = _ref6.matchUp; var _ref7 = matchUp || {}, sides = _ref7.sides, winningSide = _ref7.winningSide; var scoreString = matchUp == null ? void 0 : (_matchUp$score = matchUp.score) == null ? void 0 : _matchUp$score.scoreStringSide1; var side = winningSide && sides.find(function (_ref8) { var sideNumber = _ref8.sideNumber; return sideNumber === winningSide; }); return [[{ side: side, matchUp: matchUp, readyToScore: matchUp.readyToScore }], [{ scoreString: scoreString, sourceMatchUp: matchUp }]]; } function getStackedMatchUpValues(_ref9) { var matchUp = _ref9.matchUp, feedBottom = _ref9.feedBottom; var _ref10 = matchUp || {}, sides = _ref10.sides, schedule = _ref10.schedule; var sideDetails = [0, 1].map(function (index) { var _side$sourceMatchUp, _side$sourceMatchUp$s, _side$sourceMatchUp2; var indexSide = matchUp == null ? void 0 : matchUp.sides[index]; var contextIndex = (indexSide == null ? void 0 : indexSide.displaySideNumber) !== (indexSide == null ? void 0 : indexSide.sideNumber) ? (indexSide == null ? void 0 : indexSide.displaySideNumber) - 1 : index; var sideIndex = feedBottom ? 1 - contextIndex : contextIndex; var side = sides[sideIndex]; var scoreString = side == null ? void 0 : (_side$sourceMatchUp = side.sourceMatchUp) == null ? void 0 : (_side$sourceMatchUp$s = _side$sourceMatchUp.score) == null ? void 0 : _side$sourceMatchUp$s.scoreStringSide1; return [{ side: side, matchUp: matchUp, sideIndex: sideIndex, readyToScore: side == null ? void 0 : (_side$sourceMatchUp2 = side.sourceMatchUp) == null ? void 0 : _side$sourceMatchUp2.readyToScore }, { sideIndex: sideIndex, scoreString: scoreString, sourceMatchUp: side == null ? void 0 : side.sourceMatchUp }]; }); return [sideDetails[0], schedule, sideDetails[1]].flat(); } function generateRoundsDefinition(_ref) { var _finalRound$matchUps; var _ref$roundMatchUps = _ref.roundMatchUps, roundMatchUps = _ref$roundMatchUps === void 0 ? {} : _ref$roundMatchUps, nameFilter = _ref.nameFilter; var feedTop = true; var roundProfile = Object.assign.apply(Object, [{}].concat(Object.keys(roundMatchUps).map(function (roundNumber) { var _roundMatchUps$roundN, _ref2; return _ref2 = {}, _ref2[roundNumber] = { matchUpsCount: (_roundMatchUps$roundN = roundMatchUps[roundNumber]) == null ? void 0 : _roundMatchUps$roundN.length }, _ref2; }))); var roundNumbers = Object.keys(roundMatchUps).map(function (key) { return parseInt(key); }); roundNumbers.forEach(function (roundNumber) { if (roundProfile[roundNumber + 1] && roundProfile[roundNumber + 1].matchUpsCount === roundProfile[roundNumber].matchUpsCount) { roundProfile[roundNumber + 1].feedRound = true; } }); var firstRoundMatchUpsCount = (roundNumbers == null ? void 0 : roundNumbers.length) && roundProfile[roundNumbers[0]].matchUpsCount || 0; var roundsColumns = roundNumbers.map(function (roundNumber) { var currentRoundMatchUps = roundMatchUps[roundNumber]; var matchUps = currentRoundMatchUps.filter(function (_ref3) { var sides = _ref3.sides; var participantNames = sides == null ? void 0 : sides.map(function (_ref4) { var _participant$particip; var participant = _ref4.participant; return participant == null ? void 0 : (_participant$particip = participant.participantName) == null ? void 0 : _participant$particip.toLowerCase(); }).filter(function (f) { return f; }); return !nameFilter || participantNames.find(function (participantName) { return participantName.indexOf(nameFilter) >= 0; }); }); var matchUpsCount = matchUps.length; var columnMultiplier = Math.log2(firstRoundMatchUpsCount / matchUpsCount) + 1; var roundName = (currentRoundMatchUps == null ? void 0 : currentRoundMatchUps.length) && currentRoundMatchUps[0].roundName || "Round " + roundNumber; var definition = { matchUpsCount: matchUps.length, columnType: 'classic', columnMultiplier: columnMultiplier, roundNumber: roundNumber, roundName: roundName, matchUps: matchUps }; if (roundProfile[roundNumber].feedRound) { if (feedTop) { definition.feedTop = true; feedTop = false; } else { definition.feedBottom = true; matchUps.forEach(function (matchUp) { return Object.assign(matchUp, { feedBottom: true }); }); feedTop = true; } } var getSourceRoundPosition = function getSourceRoundPosition(_ref5) { var roundNumber = _ref5.roundNumber, roundPosition = _ref5.roundPosition, sideNumber = _ref5.sideNumber; return roundProfile[roundNumber].feedRound ? roundPosition : (roundPosition - 1) * 2 + sideNumber; }; var previousRoundMatchUps = roundNumber > 1 && roundMatchUps[roundNumber - 1]; matchUps.forEach(function (matchUp) { var roundPosition = matchUp.roundPosition; matchUp.sides.forEach(function (side, sideIndex) { var sideNumber = sideIndex + 1; if (previousRoundMatchUps) { if (matchUp.feedRound) { // for feed rounds { sideNumber: 1 } is always the fed position if (sideNumber !== 1) { side.sourceMatchUp = previousRoundMatchUps.find(function (matchUp) { return matchUp.roundPosition === roundPosition; }); } } else { var sourceRoundPosiiton = getSourceRoundPosition({ roundNumber: roundNumber, roundPosition: roundPosition, sideNumber: sideNumber }); var sourceMatchUp = previousRoundMatchUps.find(function (matchUp) { return matchUp.roundPosition === sourceRoundPosiiton; }); side.sourceMatchUp = sourceMatchUp; } } }); return matchUp; }); return definition; }); var firstRoundMatchUps = roundMatchUps[roundNumbers[0]]; var detailsColumn = { columnType: 'details', details: ['drawPosition'], columnMultiplier: 1, matchUps: firstRoundMatchUps, matchUpsCount: (firstRoundMatchUps == null ? void 0 : firstRoundMatchUps.length) || 0 }; // const dividerColumn = { columnType: 'divider' }; // const roundsDefinition = [detailsColumn, dividerColumn, ...roundsColumns]; var roundsDefinition = [detailsColumn].concat(roundsColumns); var finalRound = roundsColumns[roundsColumns.length - 1]; if ((finalRound == null ? void 0 : (_finalRound$matchUps = finalRound.matchUps) == null ? void 0 : _finalRound$matchUps.length) === 1) { var _final = { matchUpsCount: 0, finalMatchUp: finalRound.matchUps[0], columnMultiplier: finalRound.columnMultiplier + 1, columnType: 'classic', roundName: 'Winner' }; roundsDefinition.push(_final); } return { roundsDefinition: roundsDefinition }; } function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } var Side = function Side(_ref) { var _side$sourceMatchUp, _cx, _cx2; var round = _ref.round, nameFilter = _ref.nameFilter, sideDetails = _ref.sideDetails, displayOnly = _ref.displayOnly, _onClick = _ref.onClick; var classes = useStyles(); var isFinal = round.finalMatchUp; var displayText = (round == null ? void 0 : round.columnType) === "classic"; var displayDetails = (round == null ? void 0 : round.columnType) === "details"; var _ref2 = sideDetails || {}, side = _ref2.side, matchUp = _ref2.matchUp, sideIndex = _ref2.sideIndex, feedBottom = _ref2.feedBottom; var _ref3 = side || {}, bye = _ref3.bye, drawPosition = _ref3.drawPosition, participant = _ref3.participant, sourceDrawPositionRange = _ref3.sourceDrawPositionRange; var _ref4 = matchUp || {}, feedRound = _ref4.feedRound, roundNumber = _ref4.roundNumber; var drawPositionText = !nameFilter && (sourceDrawPositionRange || drawPosition || ""); var readyToScore = isFinal ? matchUp == null ? void 0 : matchUp.readyToScore : side == null ? void 0 : (_side$sourceMatchUp = side.sourceMatchUp) == null ? void 0 : _side$sourceMatchUp.readyToScore; var sideText = bye ? "BYE" : (participant == null ? void 0 : participant.participantName) || ""; var seed = side == null ? void 0 : side.seedValue; if (seed) sideText += " (" + seed + ")"; var unfilledPosition = !sideText && displayText && ( // (!matchUp.stage || matchUp.stage === 'MAIN') && roundNumber === 1 || feedRound && sideIndex === 0); var participantStyle = !displayOnly ? { className: cx(classes.participant, (_cx = {}, _cx[classes.seededParticipant] = seed, _cx[classes.readyToScore] = readyToScore && !displayOnly || unfilledPosition, _cx[classes.hoveredPrticipant] = !displayOnly, _cx)), width: "100%" } : { className: cx(classes.participant, (_cx2 = {}, _cx2[classes.seededParticipant] = seed, _cx2)) }; var participantProps = { onClick: function onClick(e) { _onClick({ feedBottom: feedBottom, sideIndex: sideIndex, matchUp: matchUp, e: e }); }, width: "100%", direction: "row", justifyContent: "space-between" }; var sourceDrawPositionRangeDisplay = feedRound && sourceDrawPositionRange; return /*#__PURE__*/React.createElement(Grid, _extends({ container: true }, participantProps, { wrap: "nowrap" }), /*#__PURE__*/React.createElement(Grid, { container: true, wrap: "nowrap" }, /*#__PURE__*/React.createElement(Grid, { item: true, className: sourceDrawPositionRangeDisplay && classes.sourceDrawPositionRange }, sourceDrawPositionRangeDisplay || ""), /*#__PURE__*/React.createElement(Grid, _extends({ item: true }, participantStyle), displayText && sideText || displayDetails && drawPositionText)), /*#__PURE__*/React.createElement(Grid, { item: true }, (participant == null ? void 0 : participant.displayInfo) || "")); }; var ColumnComponents = function ColumnComponents(_ref) { var column = _ref.column, nameFilter = _ref.nameFilter, contextData = _ref.contextData, displayOnly = _ref.displayOnly, eventHandlers = _ref.eventHandlers; var classes = useStyles(); var frames = column.frames, round = column.round, matchUpHeight = column.matchUpHeight, firstMatchUpHeight = column.firstMatchUpHeight; var handleParticipantClick = function handleParticipantClick(_ref2) { var e = _ref2.e, feedBottom = _ref2.feedBottom, matchUp = _ref2.matchUp, sideIndex = _ref2.sideIndex; if (typeof (eventHandlers == null ? void 0 : eventHandlers.onParticipantClick) === "function") { var side = (matchUp == null ? void 0 : matchUp.sides) && (matchUp == null ? void 0 : matchUp.sides[sideIndex]); var _ref3 = side || {}, participant = _ref3.participant, drawPosition = _ref3.drawPosition; eventHandlers.onParticipantClick({ e: e, feedBottom: feedBottom, matchUp: matchUp, participant: participant, drawPosition: drawPosition, sideIndex: sideIndex }); } }; var RoundName = function RoundName(_ref4) { var round = _ref4.round, onRoundNameClick = _ref4.onRoundNameClick, onScheduleClick = _ref4.onScheduleClick; var isFinal = round.finalMatchUp; var notConnectors = round.columnType !== "connectors"; var roundName = notConnectors && round.roundName; var roundNumber = notConnectors && round.roundNumber; var bottom = roundName && classes.thickBorderBottom || classes.noBoderBottom; var handleRoundNameClick = function handleRoundNameClick(e) { e.stopPropagation(); if (typeof onRoundNameClick === "function") onRoundNameClick(_extends({ e: e, roundNumber: roundNumber }, contextData, { isFinal: isFinal })); }; var handleOnScheduleClick = function handleOnScheduleClick(e) { if (typeof onScheduleClick === "function") onScheduleClick(_extends({ e: e, roundNumber: roundNumber }, contextData, { isFinal: isFinal })); }; var roundNameProps = { width: "100%", wrap: "nowrap", direction: "row", justifyContent: "space-between", onClick: !isFinal && onScheduleClick ? handleOnScheduleClick : handleRoundNameClick, className: classes.roundName + " " + bottom }; return /*#__PURE__*/React.createElement(Grid, _extends({ container: true }, roundNameProps), /*#__PURE__*/React.createElement(Grid, { item: true, onClick: handleRoundNameClick }, roundName || " "), /*#__PURE__*/React.createElement(Grid, { item: true }, roundNumber && onScheduleClick && /*#__PURE__*/React.createElement(AccessTimeIcon, { className: classes.roundScheduleIcon }))); }; var Score = function Score(_ref5) { var round = _ref5.round, scoreDetails = _ref5.scoreDetails, displayOnly = _ref5.displayOnly, _onClick = _ref5.onClick; var _ref6 = scoreDetails || {}, scoreString = _ref6.scoreString, sourceMatchUp = _ref6.sourceMatchUp, sideIndex = _ref6.sideIndex, feedBottom = _ref6.feedBottom; var displayText = (round == null ? void 0 : round.columnType) === "classic"; var scoreProps = { onClick: function onClick(e) { !displayOnly && _onClick({ matchUp: sourceMatchUp, e: e, feedBottom: feedBottom, sideIndex: sideIndex }); }, className: classes.score }; return /*#__PURE__*/React.createElement("div", scoreProps, displayText && scoreString); }; var Frame = function Frame(_ref7) { var frame = _ref7.frame, index = _ref7.index; var isFinal = round.finalMatchUp; var isDetails = (round == null ? void 0 : round.columnType) === "details"; var scoreDetails = index && frame[0] || []; var sideDetails = frame[frame.length - 1]; var columnEnd = index && frame.length === 1; var isEven = index % 2; var height = columnEnd ? undefined : index ? matchUpHeight : firstMatchUpHeight; var contentProps = { style: { height: height }, direction: "column", justifyContent: isDetails ? "flex-end" : "space-between", className: isDetails && classes.detailsColumn || isEven && !isFinal && classes.borderRight || undefined }; var className = isDetails ? classes.positionDetails : columnEnd ? "" : isEven ? classes.bracketBottom : classes.bracketTop; return /*#__PURE__*/React.createElement(Grid, { key: index, item: true, className: className }, /*#__PURE__*/React.createElement(Grid, _extends({ container: true }, contentProps), /*#__PURE__*/React.createElement(Score, { round: round, onClick: eventHandlers == null ? void 0 : eventHandlers.onScoreClick, scoreDetails: scoreDetails, displayOnly: displayOnly }), !columnEnd && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Side, { round: round, nameFilter: nameFilter, sideDetails: sideDetails, displayOnly: displayOnly, onClick: handleParticipantClick }), isDetails && /*#__PURE__*/React.createElement("div", { style: { marginBlock: "1px" } })))); }; var Frames = function Frames(_ref8) { var frames = _ref8.frames; return /*#__PURE__*/React.createElement(React.Fragment, null, frames.map(function (frame, index) { return /*#__PURE__*/React.createElement(Frame, { frame: frame, key: index, index: index }); })); }; return /*#__PURE__*/React.createElement("div", { style: { marginLeft: "4px" } }, /*#__PURE__*/React.createElement(RoundName, { onScheduleClick: eventHandlers == null ? void 0 : eventHandlers.onScheduleClick, onRoundNameClick: eventHandlers == null ? void 0 : eventHandlers.onRoundNameClick, round: round }), /*#__PURE__*/React.createElement(Frames, { frames: frames })); }; function EliminationStructure(props) { var _drawData$structures; var classes = useStyles(); var eventData = props.eventData, eventHandlers = props.eventHandlers, nameFilter = props.nameFilter; var drawsData = eventData.drawsData, eventId = eventData.eventInfo.eventId; var drawId = props.drawId, structureId = props.structureId; var drawData = drawsData == null ? void 0 : drawsData.find(function (drawData) { return drawData.drawId === drawId; }); var structureData = structureId ? drawData == null ? void 0 : (_drawData$structures = drawData.structures) == null ? void 0 : _drawData$structures.find(function (structureData) { return structureData.structureId === structureId; }) : (drawData == null ? void 0 : drawData.structures) && drawData.structures[0]; var _ref = structureData || {}, roundMatchUps = _ref.roundMatchUps; var _generateRoundsDefini = generateRoundsDefinition({ roundMatchUps: roundMatchUps, nameFilter: nameFilter }), roundsDefinition = _generateRoundsDefini.roundsDefinition; var columns = generateStandardElimination({ height: 70, roundsDefinition: roundsDefinition, nameFilter: nameFilter }); var contextData = { eventId: eventId, drawId: drawId, structureId: structureId }; var EliminationColumn = function EliminationColumn(_ref2) { var column = _ref2.column, columnIndex = _ref2.columnIndex; var round = column.round; var firstRound = round.roundNumber === 1; var classNames = { details: 'detailsColumn', divider: 'verticalDivider', connectors: 'connectorColumn', classic: firstRound ? 'initialColumn' : 'roundColumn' }; var divider = round.columnType === 'divider'; var columnClass = classNames[round.columnType]; var className = classes[columnClass]; var width = (round == null ? void 0 : round.columnType) === 'details' ? 'auto' : undefined; return /*#__PURE__*/React.createElement(Grid, { container: true, direction: "column", key: columnIndex, className: className, style: { width: width } }, !divider && /*#__PURE__*/React.createElement(ColumnComponents, { column: column, nameFilter: nameFilter, contextData: contextData, eventHandlers: eventHandlers })); }; return /*#__PURE__*/React.createElement(Grid, { container: true, direction: "row", className: classes.drawRoot }, columns.map(function (column, columnIndex) { return /*#__PURE__*/React.createElement(EliminationColumn, { column: column, columnIndex: columnIndex, key: columnIndex }); })); } var useStyles$1 = /*#__PURE__*/makeStyles(function (theme) { return { paper: { padding: theme.spacing(2), marginBottom: theme.spacing(3) }, table: { minWidth: 400 }, root: { padding: '0 16px 0 16px', fontFamily: 'inherit' }, body: { padding: '0 16px 0 16px' }, head: { padding: '0 16px 0 16px' }, groupName: { borderBottom: '2px solid #040E2C', fontWeight: 'bold', maxWidth: 30, fontSize: 12, lineHeight: 1.5, letterSpacing: 0.25, textTransform: 'uppercase', whiteSpace: 'nowrap' }, drawPositions: { maxWidth: 30 }, positions: { border: 'solid 1px #e1e3e6', borderBottom: '2px solid #040E2C', minWidth: 100, textTransform: 'uppercase', fontSize: 12, lineHeight: 1.5, letterSpacing: 0.35 }, cellContent: { border: 'solid 1px #e1e3e6', fontSize: 12, lineHeight: 1.5, letterSpacing: 0.35 }, loserContent: { border: 'solid 1px #e1e3e6', fontSize: 12, color: 'red', lineHeight: 1.5, letterSpacing: 0.35 }, byeContent: { border: 'solid 1px #e1e3e6', background: '#FFFCE9 ' }, reflexiveContent: { border: 'solid 1px #e1e3e6', background: '#e1e3e6' }, contentContainer: { minWidth: 'max-content' }, centerContent: { justifyContent: 'center' }, valueHeader: { border: 'solid 1px #e1e3e6', borderBottom: '2px solid #040E2C', textTransform: 'uppercase', width: 30, fontSize: 12, lineHeight: 1.5 }, centerValue: { justifyContent: 'center', width: 30 }, participantHeader: { borderBottom: '2px solid #040E2C', minWidth: 100, fontWeight: 'bold' }, participantContainer: { '&:hover': { backgroundColor: '#edf4fc', cursor: 'pointer' } }, participantContent: { minWidth: 100 }, tableContainer: { margin: '20px 0 30px 0' } }; }); var HeaderCell = function HeaderCell(_ref) { var _component$getHeader; var component = _ref.component, row = _ref.row, _ref$colspan = _ref.colspan, colspan = _ref$colspan === void 0 ? 1 : _ref$colspan; var classes = useStyles$1(); var cellValues = component.getHeader == null ? void 0 : component.getHeader(row); var _ref2 = cellValues || {}, cellClassName = _ref2.cellClassName, contentClassName = _ref2.contentClassName; var handleOnClick = function handleOnClick(e) { component.headerClick == null ? void 0 : component.headerClick(e, row); }; return /*#__PURE__*/React.createElement(TableCell, { colSpan: colspan, key: component.key, onClick: handleOnClick, className: cellClassName, classes: { root: classes.root, head: classes.head } }, /*#__PURE__*/React.createElement(Grid, { container: true, className: contentClassName }, /*#__PURE__*/React.createElement(Grid, { item: true }, (component.getHeader == null ? void 0 : (_component$getHeader = component.getHeader(row)) == null ? void 0 : _component$getHeader.children) || ''))); }; var RowCell = function RowCell(_ref3) { var _component$getValue; var component = _ref3.component, row = _ref3.row; var classes = useStyles$1(); var _ref4 = (component.getValue == null ? void 0 : component.getValue(row)) || {}, cellClassName = _ref4.cellClassName, contentClassName = _ref4.contentClassName; var handleOnClick = function handleOnClick(e) { component.onClick == null ? void 0 : component.onClick(e, row); }; return /*#__PURE__*/React.createElement(TableCell, { key: component.key, onClick: handleOnClick, className: cellClassName, classes: { root: classes.root, head: classes.head } }, /*#__PURE__*/React.createElement(Grid, { container: true, className: contentClassName }, /*#__PURE__*/React.createElement(Grid, { item: true }, (component.getValue == null ? void 0 : (_component$getValue = component.getValue(row)) == null ? void 0 : _component$getValue.children) || ''))); }; function GroupTable(props) { var classes = useStyles$1(); var columnComponents = props.columnComponents, rowData = props.rowData; return /*#__PURE__*/React.createElement(TableContainer, { component: Paper, className: classes.tableContainer }, /*#__PURE__*/React.createElement(Table, { className: classes.table, "aria-label": "groupTable" }, /*#__PURE__*/React.createElement(TableHead, null, /*#__PURE__*/React.createElement(TableRow, null, columnComponents.map(function (component, index) { var row = rowData.slice(0, 1); return index !== 1 && /*#__PURE__*/React.createElement(HeaderCell, { colspan: index ? 1 : 2, key: "" + component.key + index, index: index, row: row && row[0], component: component }); }))), /*#__PURE__*/React.createElement(TableBody, null, rowData.map(function (row, index) { return /*#__PURE__*/React.createElement(TableRow, { key: "" + row.drawPosition + index }, columnComponents.map(function (component, index) { return /*#__PURE__*/React.createElement(RowCell, { key: "" + index + component.key, row: row, index: index, component: component }); })); })))); } var _shortCodes; var _factoryConstants$mat = factoryConstants == null ? void 0 : factoryConstants.matchUpStatusConstants, RETIRED = _factoryConstants$mat.RETIRED, WALKOVER = _factoryConstants$mat.WALKOVER, DOUBLE_WALKOVER = _factoryConstants$mat.DOUBLE_WALKOVER, SUSPENDED = _factoryConstants$mat.SUSPENDED, ABANDONED = _factoryConstants$mat.ABANDONED, DEFAULTED = _factoryConstants$mat.DEFAULTED, CANCELLED = _factoryConstants$mat.CANCELLED, INCOMPLETE = _factoryConstants$mat.INCOMPLETE, NOT_PLAYED = _factoryConstants$mat.NOT_PLAYED, DEAD_RUBBER = _factoryConstants$mat.DEAD_RUBBER; var shortCodes = (_shortCodes = {}, _shortCodes[RETIRED] = 'RET', _shortCodes[WALKOVER] = 'WO', _shortCodes[DOUBLE_WALKOVER] = 'WO/WO', _shortCodes[SUSPENDED] = 'SUS', _shortCodes[ABANDONED] = 'Abandoned', _shortCodes[DEFAULTED] = 'Def', _shortCodes[CANCELLED] = 'Cancelled', _shortCodes[INCOMPLETE] = 'Inc', _shortCodes[NOT_PLAYED] = 'Not played', _shortCodes[DEAD_RUBBER] = 'Dead rubber', _shortCodes); function getContextScoreString(_ref) { var _matchUp$score, _matchUp$score2; var dictionary = _ref.dictionary, matchUp = _ref.matchUp, sideNumber = _ref.sideNumber; var scoreString = sideNumber === 1 ? matchUp == null ? void 0 : (_matchUp$score = matchUp.score) == null ? void 0 : _matchUp$score.scoreStringSide1 : sideNumber === 2 ? matchUp == null ? void 0 : (_matchUp$score2 = matchUp.score) == null ? void 0 : _matchUp$score2.scoreStringSide2 : ''; var matchUpStatus = matchUp == null ? void 0 : matchUp.matchUpStatus; var statusCode = dictionary && dictionary[matchUpStatus] || shortCodes[matchUpStatus] || ''; var isWalkover = matchUpStatus === WALKOVER; var isLosingSide = sideNumber && (matchUp == null ? void 0 : matchUp.winningSide) && matchUp.winningSide !== sideNumber; var winIndicator = isWalkover && (isLosingSide ? ' (-)' : ' (+)'); var contextScoreString = (scoreString || '') + (statusCode && " " + statusCode) + (winIndicator || ''); var highlightLosingSide = isLosingSide && isWalkover; return { contextScoreString: contextScoreString, highlightLosingSide: highlightLosingSide }; } function getColumnComponents(_ref) { var _rowData$, _rowData$$positionCol; var contextData = _ref.contextData, dictionary = _ref.dictionary, eventHandlers = _ref.eventHandlers, rowData = _ref.rowData; var classes = useStyles$1(); var rowDetails = [{ key: "drawPosition", getHeader: function getHeader(row) { return { children: /*#__PURE__*/React.createElement(React.Fragment, null, (row == null ? void 0 : row.structureName) || ""), cellClassName: classes.groupName, contentClassName: "" }; }, headerClick: function headerClick(e, row) { if (typeof (eventHandlers == null ? void 0 : eventHandlers.onHeaderClick) === "function") { var groupStructureId = row.groupStructureId, structureName = row.structureName; eventHandlers.onHeaderClick(_extends({ e: e }, contextData, { groupStructureId: groupStructureId, columnName: "structureName", structureName: structureName })); } }, getValue: function getValue(row) { return { children: /*#__PURE__*/React.createElement(React.Fragment, null, row == null ? void 0 : row.drawPosition), cellClassName: classes.drawPositions }; }, onClick: function onClick(e, row) { if (typeof (eventHandlers == null ? void 0 : eventHandlers.onStatsClick) === "function") { var _ref2 = row || {}, drawPosition = _ref2.drawPosition, participant = _ref2.participant, participantResult = _ref2.participantResult; eventHandlers.onStatsClick(_extends({ e: e, drawPosition: drawPosition, participant: participant }, contextData, { participantResult: participantResult, columnName: "drawPosition" })); } } }, { key: "participant", getHeader: function getHeader() { return { node: "", cellClassName: classes.participantHeader }; }, getValue: function getValue(row) { var _row$participant; var participantDisplay = (row == null ? void 0 : (_row$participant = row.participant) == null ? void 0 : _row$participant.participantName) || (row == null ? void 0 : row.bye) && "BYE" || ""; return { children: /*#__PURE__*/React.createElement(Grid, { container: true, justifyContent: "space-between" }, /*#__PURE__*/React.createElement(Grid, { item: true }, participantDisplay)), cellClassName: !participantDisplay && classes.participantContainer || "", contentClassName: classes.participantContent }; }, onClick: function onClick(e, row) { var _ref3 = row || {}, drawPosition = _ref3.drawPosition, participant = _ref3.participant; if (typeof (eventHandlers == null ? void 0 : eventHandlers.onParticipantClick) === "function") { eventHandlers.onParticipantClick(_extends({ e: e, participant: participant, drawPosition: drawPosition }, contextData)); } } }]; var columnMatchUps = (_rowData$ = rowData[0]) == null ? void 0 : (_rowData$$positionCol = _rowData$.positionColumns) == null ? void 0 : _rowData$$positionCol.map(function (position, i) { var _position$participant; var byeColumn = position == null ? void 0 : position.bye; var participantDisplay = (position == null ? void 0 : (_position$participant = position.participant) == null ? void 0 : _position$participant.participantName) || byeColumn && "BYE" || ""; var getSideNumber = function getSideNumber(matchUp, row) { var _matchUp$sides, _matchUp$sides$find; return matchUp == null ? void 0 : (_matchUp$sides = matchUp.sides) == null ? void 0 : (_matchUp$sides$find = _matchUp$sides.find(function (side) { return side.drawPosition && side.drawPosition === (row == null ? void 0 : row.drawPosition); })) == null ? void 0 : _matchUp$sides$find.sideNumber; }; return { key: "drawPosition" + i.toString(), getHeader: function getHeader() { return { children: participantDisplay, cellClassName: classes.positions, contentClassName: classes.centerContent }; }, headerClick: function headerClick(e) { if (typeof (eventHandlers == null ? void 0 : eventHandlers.onParticipantClick) === "function") { var participant = position.participant, drawPosition = position.drawPosition; eventHandlers.onParticipantClick(_extends({ e: e, drawPosition: drawPosition, participant: participant }, contextData)); } }, onClick: function onClick(e, row) { var matchUp = (row == null ? void 0 : row.matchUps) && (row == null ? void 0 : row.matchUps[i]); var sideNumber = getSideNumber(matchUp, row); if (typeof (eventHandlers == null ? void 0 : eventHandlers.onScoreClick) === "function") { eventHandlers.onScoreClick(_extends({ e: e, matchUp: matchUp, sideNumber: sideNumber }, contextData)); } }, getValue: function getValue(row) { var matchUp = (row == null ? void 0 : row.matchUps) && (row == null ? void 0 : row.matchUps[i]); var sideNumber = getSideNumber(matchUp, row); var _getContextScoreStrin = getContextScoreString({ matchUp: matchUp, sideNumber: sideNumber, dictionary: dictionary }), contextScoreString = _getContextScoreStrin.contextScoreString, highlightLosingSide = _getContextScoreStrin.highlightLosingSide; var reflexive = (row == null ? void 0 : row.rowIndex) === position.rowIndex; var byeContent = byeColumn || row.bye; var cellClassName = reflexive ? classes.reflexiveContent : byeContent ? classes.byeContent : highlightLosingSide ? classes.loserContent : classes.cellContent; return { matchUp: matchUp, byeColumn: byeColumn, children: contextScoreString || "", cellClassName: cellClassName, positionIndex: position.rowIndex, contentClassName: classes.centerContent }; } }; }); var participantResults = [{ key: "winLoss", getHeader: function getHeader() { return { children: (dictionary == null ? void 0 : dictionary.winLoss) || "W/L", cellClassName: classes.valueHeader, contentClassName: classes.centerValue }; }, headerClick: function headerClick(e) { console.log("winLossColumnClick"); if (typeof (eventHandlers == null ? void 0 : eventHandlers.onHeaderClick) === "function") { eventHandlers.onHeaderClick(_extends({ e: e, columnName: "winLoss" }, contextData)); } }, getValue: function getValue(row) { var _row$participantResul; return { children: /*#__PURE__*/React.createElement(Grid, { container: true, justifyContent: "space-between" }, /*#__PURE__*/React.createElement(Grid, { item: true }, row == null ? void 0 : (_row$participantResul = row.participantResult) == null ? void 0 : _row$participantResul.result)), cellClassName: classes.cellContent, contentClassName: classes.centerValue }; }, onClick: function onClick(e, row) { if (typeof (eventHandlers == null ? void 0 : eventHandlers.onStatsClick) === "function") { var _ref4 = row || {}, drawPosition = _ref4.drawPosition, participant = _ref4.participant, participantResult = _ref4.participantResult; eventHandlers.onStatsClick(_extends({ e: e }, contextData, { drawPosition: drawPosition, participant: participant, participantResult: participantResult, columnName: "winLoss" })); } } }, { key: "finishingPosition", getHeader: function getHeader() { return { children: (dictionary == null ? void 0 : dictionary.finishingPosition) || "Pos", cellClassName: classes.valueHeader, contentClassName: classes.centerValue }; }, headerClick: function headerClick(e) { console.log("finishingPositionColumnClick"); if (typeof (eventHandlers == null ? void 0 : eventHandlers.onHeaderClick) === "function") { eventHandlers.onHeaderClick(_extends({ e: e }, contextData, { columnName: "finishingPosition" })); } }, getValue: function getValue(row) { var _row$participantResul2; return { children: (row == null ? void 0 : (_row$participantResul2 = row.participantResult) == null ? void 0 : _row$participantResul2.groupOrder) || "", cellClassName: classes.cellContent, contentClassName: classes.centerValue }; }, onClick: function onClick(e, row) { if (typeof (eventHandlers == null ? void 0 : eventHandlers.onStatsClick) === "function") { var _ref5 = row || {}, drawPosition = _ref5.drawPosition, participant = _ref5.participant, participantResult = _ref5.participantResult; eventHandlers.onStatsClick(_extends({ e: e, drawPosition: drawPosition, participant: participant }, contextData, { participantResult: participantResult, columnName: "finishingPosition" })); } } }]; var columnComponents = [].concat(rowDetails, columnMatchUps, participantResults); return { columnComponents: columnComponents }; } function RoundRobinGroup(props) { var classes = useStyles$1(); var contextData = props.contextData, dictionary = props.dictionary, rowData = props.rowData, eventHandlers = props.eventHandlers; var _getColumnComponents = getColumnComponents({ rowData: rowData, dictionary: dictionary, contextData: contextData, eventHandlers: eventHandlers }), columnComponents = _getColumnComponents.columnComponents; return /*#__PURE__*/React.createElement("div", { className: classes.tableContainer }, /*#__PURE__*/React.createElement(GroupTable, { columnData: [], rowData: rowData, columnComponents: columnComponents, data: rowData })); } function generateRoundRobinRows(_ref) { var roundMatchUps = _ref.roundMatchUps, participantResults = _ref.participantResults; var allMatchUps = Object.values(roundMatchUps).flat(); var groupedMatchUps = allMatchUps.reduce(function (grouped, matchUp) { var _ref2 = matchUp || {}, structureId = _ref2.structureId; if (!grouped[structureId]) grouped[structureId] = []; grouped[structureId].push(matchUp); return grouped; }, {}); var sum = function sum(arr) { return arr.reduce(function (a, b) { return a + b; }); }; var matchUpColumnSort = function matchUpColumnSort(a, b) { return sum(a.drawPositions) - sum(b.drawPositions); }; var numericSort = function numericSort(a, b) { return a - b; }; var groupStructureIds = Object.keys(groupedMatchUps); var rows = groupStructureIds.map(function (groupStructureId) { var _groupMatchUps$find; var groupMatchUps = groupedMatchUps[groupStructureId]; var structureName = groupMatchUps && ((_groupMatchUps$find = groupMatchUps.find(function (_ref3) { var structureName = _ref3.structureName; return structureName; })) == null ? void 0 : _groupMatchUps$find.structureName); var sides = groupMatchUps.map(function (_ref4) { var sides = _ref4.sides; return sides; }).flat(); var drawPositions = groupMatchUps.reduce(function (drawPositions, matchUp) { var _matchUp$drawPosition; (_matchUp$drawPosition = matchUp.drawPositions) == null ? void 0 : _matchUp$drawPosition.forEach(function (drawPosition) { if (!drawPositions.includes(drawPosition)) drawPositions.push(drawPosition); }); return drawPositions; }, []).sort(numericSort); var positionRows = drawPositions.map(function (drawPosition, rowIndex) { var _participantResults$f; var side = sides.find(function (side) { return side.drawPosition === drawPosition; }); var _ref5 = side || {}, participant = _ref5.participant, bye = _ref5.bye, qualifier = _ref5.qualifier; var participantResult = (_participantResults$f = participantResults.find(function (result) { return result.drawPosition === drawPosition; })) == null ? void 0 : _participantResults$f.participantResult; var matchUps = groupMatchUps.filter(function (_ref6) { var drawPositions = _ref6.drawPositions; return drawPositions.includes(drawPosition); }).sort(matchUpColumnSort); matchUps.splice(rowIndex, 0, { reflexive: true }); return { drawPosition: drawPosition, matchUps: matchUps, participant: participant, participantResult: participantResult, qualifier: qualifier, bye: bye, rowIndex: rowIndex }; }); var positionColumns = positionRows.map(function (_ref7, rowIndex) { var drawPosition = _ref7.drawPosition, participant = _ref7.participant, bye = _ref7.bye, qualifier = _ref7.qualifier; return { drawPosition: drawPosition, participant: participant, qualifier: qualifier, bye: bye, rowIndex: rowIndex }; }); var headerRow = { groupStructureId: groupStructureId, structureName: structureName, positionColumns: positionColumns }; return [headerRow].concat(positionRows); }); return { rows: rows }; } function RoundRobinStructure(props) { var _drawData$structures; var eventData = props.eventData, dictionary = props.dictionary, eventHandlers = props.eventHandlers; var eventId = eventData.eventInfo.eventId; var drawsData = eventData.drawsData; var drawId = props.drawId, structureId = props.structureId; var drawData = drawsData == null ? void 0 : drawsData.find(function (drawData) { return drawData.drawId === drawId; }); var structureData = drawData == null ? void 0 : (_drawData$structures = drawData.structures) == null ? void 0 : _drawData$structures.find(function (structure) { return structure.structureId === structureId; }); var _ref = structureData || {}, roundMatchUps = _ref.roundMatchUps, participantResults = _ref.participantResults; var _generateRoundRobinRo = generateRoundRobinRows({ roundMatchUps: roundMatchUps, participantResults: participantResults }), rows = _generateRoundRobinRo.rows; var contextData = { drawId: drawId, eventId: eventId, structureId: structureId }; return /*#__PURE__*/React.createElement(React.Fragment, null, rows.map(function (rowData, key) { return /*#__PURE__*/React.createElement(RoundRobinGroup, { rowData: rowData, key: "CHRRG-" + key, dictionary: dictionary, contextData: contextData, eventHandlers: eventHandlers }); })); } /* */ function DrawStructure(props) { var _drawData$structures, _drawData$structures2; var dictionary = props.dictionary, eventData = props.eventData, eventHandlers = props.eventHandlers, nameFilter = props.nameFilter; var drawsData = eventData == null ? void 0 : eventData.drawsData; var drawId = props.drawId, structureId = props.structureId; if (!drawId) drawId = (drawsData == null ? void 0 : drawsData.length) && drawsData[0].drawId; var drawData = drawsData == null ? void 0 : drawsData.find(function (drawData) { return (drawData == null ? void 0 : drawData.drawId) === drawId; }); if (!structureId) structureId = (drawData == null ? void 0 : (_drawData$structures = drawData.structures) == null ? void 0 : _drawData$structures.length) && drawData.structures[0].structureId; var structure = drawData == null ? void 0 : (_drawData$structures2 = drawData.structures) == null ? void 0 : _drawData$structures2.find(function (structure) { return structure.structureId === structureId; }); // ROUND_ROBIN structures have { structureType: CONTAINER } var isRoundRobin = (structure == null ? void 0 : structure.structureType) === 'CONTAINER'; var args = { drawId: drawId, eventData: eventData, structureId: structureId, dictionary: dictionary, eventHandlers: eventHandlers, nameFilter: nameFilter }; return /*#__PURE__*/React.createElement(React.Fragment, null, isRoundRobin ? /*#__PURE__*/React.createElement(RoundRobinStructure, args) : /*#__PURE__*/React.createElement(EliminationStructure, args)); } export { DrawStructure }; //# sourceMappingURL=tods-react-draws.esm.js.map