From 35d6eba59943c7cd92f651402348633db9708d4f Mon Sep 17 00:00:00 2001 From: Anthony Correa Date: Tue, 5 Mar 2024 09:15:18 -0600 Subject: [PATCH] support for flags in eventLineup edit --- src/controllers/eventlineup.js | 17 +++++++++------- src/lib/utils.js | 18 ++++++++++++++--- src/public/js/eventlineup.js | 27 ++++++++++++++++++++----- src/views/eventlineup/edit.hbs | 2 +- src/views/eventlineup/partials/slot.hbs | 1 + 5 files changed, 49 insertions(+), 16 deletions(-) diff --git a/src/controllers/eventlineup.js b/src/controllers/eventlineup.js index 4e747dc..7be972e 100644 --- a/src/controllers/eventlineup.js +++ b/src/controllers/eventlineup.js @@ -1,6 +1,6 @@ const path = require('path') const fs = require('fs') -const {embeddedSvgFromPath, parsePositionLabel} = require("../lib/utils") +const {embeddedSvgFromPath, parsePositionLabel, compilePositionLabel} = require("../lib/utils") const tsUtils = require('../lib/utils') const { loadEventLineupEntries } = require('teamsnap.js') @@ -15,6 +15,7 @@ const statusCodeIcons = { } exports.helpers = { + flagsString: (flags) => flags?.join(","), plus1: (i) => Number(i)+1, positions: () => ["P", "C", "1B", "2B", "3B", "SS", "LF", "CF", "RF", "EH", "DH"], defense_positions: () => ["C", "1B", "2B", "3B", "SS", "LF", "CF", "RF", "P"], @@ -49,7 +50,7 @@ exports.helpers = { return (member.benchcoach?.eventLineupEntry != null); }, isInStartingLineup: (member) => { - if (member.benchcoach.eventLineupEntry == null) return false; + if (member.benchcoach.eventLineupEntry == null || member.benchcoach.eventLineupEntry.label == '') return false; const {positionFlags} = parsePositionLabel(member.benchcoach.eventLineupEntry?.label); return (positionFlags != "PO") }, @@ -59,11 +60,11 @@ exports.helpers = { return (member.benchcoach.eventLineupEntry != null && positionFlags == "PO") }, isInBench: (member) => { - if (member.benchcoach.eventLineupEntry != null || member.isNonPlayer) return false; + if ((member.benchcoach.eventLineupEntry != null && member.benchcoach.eventLineupEntry.label != '') || member.isNonPlayer) return false; return (member.benchcoach.availability?.statusCode != 0 && member.benchcoach.availability?.statusCode != null) }, isInOut: (member) => { - if (member.benchcoach.eventLineupEntry != null || member.isNonPlayer) return false; + if ((member.benchcoach.eventLineupEntry != null && member.benchcoach.eventLineupEntry.label != '') || member.isNonPlayer) return false; return (member.benchcoach.availability?.statusCode == 0 || member.benchcoach.availability?.statusCode == null) }, availabilityStatusShort: (availability) => { @@ -103,8 +104,9 @@ attachBenchcoachPropertiesToMember = (members, event_lineup_entries, availabilit if (event_lineup_entry != null) { // member.link('eventLineupEntry', event_lineup_entry) member.benchcoach.eventLineupEntry = event_lineup_entry - const {positionLabelWithoutFlag} = parsePositionLabel(event_lineup_entry.label); + const {positionLabelWithoutFlag, positionFlags} = parsePositionLabel(event_lineup_entry.label); member.benchcoach.eventLineupEntry.positionLabelWithoutFlag = positionLabelWithoutFlag + member.benchcoach.eventLineupEntry.flags = positionFlags } else { member.benchcoach.eventLineupEntry = null @@ -157,11 +159,12 @@ const processPostedEventLineupEntries = (body, eventLineupEntries, eventLineup) const lineupEntryId = body.eventLineupEntryId[i] const lineupEntryLabel = body.label[i] const lineupEntrySequence = body.sequence[i] + const lineupEntryFlags = body.flags[i] if (lineupEntryId != '') { // Update lineup entry const eventLineupEntry = eventLineupEntries.find((e)=>e.id==Number(lineupEntryId)) eventLineupEntry.sequence = lineupEntrySequence - eventLineupEntry.label = lineupEntryLabel + eventLineupEntry.label = compilePositionLabel(lineupEntryLabel, lineupEntryFlags) newEventLineupEntries.push(eventLineupEntry) } else if (lineupEntryLabel != '') { @@ -170,7 +173,7 @@ const processPostedEventLineupEntries = (body, eventLineupEntries, eventLineup) eventLineupEntry.eventLineupId = eventLineup.id eventLineupEntry.memberId = memberId eventLineupEntry.sequence = lineupEntrySequence - eventLineupEntry.label = lineupEntryLabel + eventLineupEntry.label = compilePositionLabel(lineupEntryLabel, lineupEntryFlags) newEventLineupEntries.push(eventLineupEntry) } else { diff --git a/src/lib/utils.js b/src/lib/utils.js index ee59ed1..dd7e7c2 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -150,7 +150,19 @@ exports.parsePositionLabel = (label) => { positionFlags: null } } - const positionLabelWithoutFlags= label.replace(/(.*?)\s\[(.*?)\]/, "$1") - const positionFlags = label.replace(/(.*?)\s\[(.*?)\]/, "$2") + const pattern = /(?[A-Z0-9]+)(?:\s\[(?.[A-z,]+)\])?/g + const {pos, flags} = pattern.exec(label)?.groups || {} + const positionLabelWithoutFlags= pos + const positionFlags = flags?.split(',').map(f=>f.trim()) || [] return {positionLabelWithoutFlags, positionFlags} - } \ No newline at end of file + } + +exports.compilePositionLabel = (label, flags) => { + if (flags == null || flags == '' || flags.lengh == 0) { + return label + } + else { + const flags_set = new Set(flags.split(',').map(s=>s.trim())) + return `${label} [${Array.from(flags_set).sort().join(',')}]` + } +} \ No newline at end of file diff --git a/src/public/js/eventlineup.js b/src/public/js/eventlineup.js index d13aa7a..995c083 100644 --- a/src/public/js/eventlineup.js +++ b/src/public/js/eventlineup.js @@ -8,7 +8,7 @@ function onPositionSelectChange(elem) { } }); colorPositions(); - refreshLineupOrder(); + refreshLineup(); } function togglePopup(el) { @@ -39,7 +39,7 @@ function colorPositions() { } } -function refreshLineupOrder() { +function refreshLineup() { Array.from(document.querySelectorAll("[id^=event-lineup]")).forEach((bcLineup) => { Array.from( bcLineup.querySelectorAll( @@ -55,6 +55,20 @@ function refreshLineupOrder() { } else { slot.querySelector("input[name*=label]").value = null; } + if (slot.closest('.position-only')){ + const flags = new Set(slot.querySelector("input[name*=flags]").value.split(',').map(s=>s.trim())) + flags.add('PO');flags.delete('') + slot.querySelector("input[name*=flags]").value = Array.from(flags).join(","); + } + else { + const flags = new Set(slot.querySelector("input[name*=flags]").value.split(',').map(s=>s.trim())) + flags.delete('PO');flags.delete('') + slot.querySelector("input[name*=flags]").value = Array.from(flags).join(","); + } + if (slot.closest('.bench')){ + slot.querySelector("input[name*=sequence]").value = ''; + slot.querySelector("input[name*=label]").value = ''; + } }); }); } @@ -74,13 +88,13 @@ for (bcLineup of document.querySelectorAll("[id^=event-lineup]")) { // Add to Lineup var itemEl = evt.item; // dragged HTMLElement - refreshLineupOrder(); + refreshLineup(); }, onUpdate: function (/**Event*/ evt) { console.log("update to lineup"); // var itemEl = evt.item; // dragged HTMLElement // refresh_lineup_order(itemEl); - refreshLineupOrder(); + refreshLineup(); }, }; new Sortable.create(bcLineup.querySelector("[id^=lineup-starting] .slot-set"), options); @@ -91,13 +105,16 @@ for (bcLineup of document.querySelectorAll("[id^=event-lineup]")) { } for (lineup_slot of document.querySelectorAll("[id^=lineup-out] .lineup-slot")) { - console.log(lineup_slot) const cells = lineup_slot.querySelectorAll('.Panel-cell:has(.sequence), .Panel-cell:has(.drag-handle), .Panel-cell:has(.position-select-box) ') Array.from(cells).forEach(cell=>{ cell.classList.add('u-hidden') }) } +function refreshFlags(){ + +} + function copyEmailTable(itemEl, subject, recipients) { // Create container for the HTML // [1] diff --git a/src/views/eventlineup/edit.hbs b/src/views/eventlineup/edit.hbs index b1a72d2..e79b51f 100644 --- a/src/views/eventlineup/edit.hbs +++ b/src/views/eventlineup/edit.hbs @@ -114,7 +114,7 @@ diff --git a/src/views/eventlineup/partials/slot.hbs b/src/views/eventlineup/partials/slot.hbs index 23bdcc7..2e937cb 100644 --- a/src/views/eventlineup/partials/slot.hbs +++ b/src/views/eventlineup/partials/slot.hbs @@ -1,5 +1,6 @@
+