Skip to content

Commit

Permalink
linted
Browse files Browse the repository at this point in the history
  • Loading branch information
mtoensing committed Dec 20, 2024
1 parent 26b688c commit e8f5dd4
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 120 deletions.
251 changes: 132 additions & 119 deletions src/game-table/table-sort.js
Original file line number Diff line number Diff line change
@@ -1,119 +1,132 @@
document.addEventListener('DOMContentLoaded', function () {
function sortTable(table, column, asc = true) {
const dirModifier = asc ? 1 : -1;
const tBody = table.tBodies[0];
const rows = Array.from(tBody.querySelectorAll('tr'));

// New sorting logic
const sortedRows = rows.sort((a, b) => {
const aColTime = a
.querySelector(`td:nth-child(${column + 1})`)
.getAttribute('data-time');
const bColTime = b
.querySelector(`td:nth-child(${column + 1})`)
.getAttribute('data-time');

if (aColTime && bColTime) {
return (parseInt(aColTime) - parseInt(bColTime)) * dirModifier;
}

const aColText = a
.querySelector(`td:nth-child(${column + 1})`)
.textContent.trim();
const bColText = b
.querySelector(`td:nth-child(${column + 1})`)
.textContent.trim();
return (
aColText.localeCompare(bColText, undefined, {
numeric: true,
sensitivity: 'base',
}) * dirModifier
);
});

while (tBody.firstChild) {
tBody.removeChild(tBody.firstChild);
}

tBody.append(...sortedRows);

table
.querySelectorAll('th')
.forEach((th) =>
th.classList.remove('th-sort-asc', 'th-sort-desc')
);
table
.querySelector(`th:nth-child(${column + 1})`)
.classList.toggle('th-sort-asc', asc);
table
.querySelector(`th:nth-child(${column + 1})`)
.classList.toggle('th-sort-desc', !asc);
}

function updateURLParameter(param, value) {
const url = new URL(window.location);
url.searchParams.set(param, value);
window.history.replaceState({}, '', url);
}

function getSortParams() {
const urlParams = new URLSearchParams(window.location.search);
const columnName = urlParams.get('column');
const order = urlParams.get('order') === 'asc';

let column;
switch (columnName) {
case 'rating':
column = 0;
break;
case 'title':
column = 1;
break;
case 'date':
column = 2;
break;
default:
column = NaN;
}

return { column, order };
}

function initTableSort() {
const table = document.querySelector('#game-table');
const { column, order } = getSortParams();

if (!isNaN(column)) {
sortTable(table, column, order);
}

document.querySelectorAll('#game-table th').forEach((headerCell, index) => {
headerCell.addEventListener('click', () => {
const tableElement =
headerCell.parentElement.parentElement.parentElement;
const headerIndex = index;
const currentIsAscending =
headerCell.classList.contains('th-sort-asc');

const newIsAscending = !currentIsAscending;

// Update table sorting
sortTable(tableElement, headerIndex, newIsAscending);

// Map column index to name
const columnName =
headerIndex === 0
? 'rating'
: headerIndex === 1
? 'title'
: 'date';

// Update the URL with sort parameters
updateURLParameter('column', columnName);
updateURLParameter('order', newIsAscending ? 'asc' : 'desc');
});
});
}

initTableSort();
});
document.addEventListener( 'DOMContentLoaded', function () {
function sortTable( table, column, asc = true ) {
const dirModifier = asc ? 1 : -1;
const tBody = table.tBodies[ 0 ];
const rows = Array.from( tBody.querySelectorAll( 'tr' ) );

// New sorting logic
const sortedRows = rows.sort( ( a, b ) => {
const aColTime = a
.querySelector( `td:nth-child(${ column + 1 })` )
.getAttribute( 'data-time' );
const bColTime = b
.querySelector( `td:nth-child(${ column + 1 })` )
.getAttribute( 'data-time' );

if ( aColTime && bColTime ) {
return (
( parseInt( aColTime ) - parseInt( bColTime ) ) *
dirModifier
);
}

const aColText = a
.querySelector( `td:nth-child(${ column + 1 })` )
.textContent.trim();
const bColText = b
.querySelector( `td:nth-child(${ column + 1 })` )
.textContent.trim();
return (
aColText.localeCompare( bColText, undefined, {
numeric: true,
sensitivity: 'base',
} ) * dirModifier
);
} );

while ( tBody.firstChild ) {
tBody.removeChild( tBody.firstChild );
}

tBody.append( ...sortedRows );

table
.querySelectorAll( 'th' )
.forEach( ( th ) =>
th.classList.remove( 'th-sort-asc', 'th-sort-desc' )
);
table
.querySelector( `th:nth-child(${ column + 1 })` )
.classList.toggle( 'th-sort-asc', asc );
table
.querySelector( `th:nth-child(${ column + 1 })` )
.classList.toggle( 'th-sort-desc', ! asc );
}

function updateURLParameter( param, value ) {
const url = new URL( window.location );
url.searchParams.set( param, value );
window.history.replaceState( {}, '', url );
}

function getSortParams() {
const urlParams = new URLSearchParams( window.location.search );
const columnName = urlParams.get( 'column' );
const order = urlParams.get( 'order' ) === 'asc';

let column;
switch ( columnName ) {
case 'rating':
column = 0;
break;
case 'title':
column = 1;
break;
case 'date':
column = 2;
break;
default:
column = NaN;
}

return { column, order };
}

function initTableSort() {
const table = document.querySelector( '#game-table' );
const { column, order } = getSortParams();

if ( ! isNaN( column ) ) {
sortTable( table, column, order );
}

document
.querySelectorAll( '#game-table th' )
.forEach( ( headerCell, index ) => {
headerCell.addEventListener( 'click', () => {
const tableElement =
headerCell.parentElement.parentElement.parentElement;
const headerIndex = index;
const currentIsAscending =
headerCell.classList.contains( 'th-sort-asc' );

const newIsAscending = ! currentIsAscending;

// Update table sorting
sortTable( tableElement, headerIndex, newIsAscending );

// Map column index to name
let columnName;
switch ( headerIndex ) {
case 0:
columnName = 'rating';
break;
case 1:
columnName = 'title';
break;
default:
columnName = 'date';
}

// Update the URL with sort parameters
updateURLParameter( 'column', columnName );
updateURLParameter(
'order',
newIsAscending ? 'asc' : 'desc'
);
} );
} );
}

initTableSort();
} );
4 changes: 3 additions & 1 deletion src/review-box/shortscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ document.addEventListener( 'DOMContentLoaded', function () {

// Function to run the animation
const animateScore = () => {
if ( isAnimating ) return; // Ignore clicks if the animation is running
if ( isAnimating ) {
return;
} // Ignore clicks if the animation is running

isAnimating = true; // Set the animation state to 'running'
let currentScore = 0;
Expand Down

0 comments on commit e8f5dd4

Please sign in to comment.