delete-group.js
3 months ago
edit-group.js
1 month ago
form-submission.js
3 months ago
forms.css
3 months ago
icons.css
3 months ago
listShowMore.js
3 months ago
listing.css
3 months ago
listing.js
3 months ago
sort-ads.js
3 months ago
table.css
3 months ago
sort-ads.js
103 lines
| 1 | /** |
| 2 | * Extracts the sortable value from a table row. |
| 3 | * |
| 4 | * @param {HTMLTableRowElement} row The table row element. |
| 5 | * @param {string} sortBy The column to sort by. |
| 6 | * @param {number} colIndex The index of the column to sort by. |
| 7 | * |
| 8 | * @return {string} The sortable value. |
| 9 | */ |
| 10 | function getCellValue( row, sortBy, colIndex ) { |
| 11 | const cell = row.cells[ colIndex ]; |
| 12 | if ( ! cell ) { |
| 13 | return ''; |
| 14 | } |
| 15 | |
| 16 | if ( 'weight' === sortBy ) { |
| 17 | const select = cell.querySelector( 'select' ); |
| 18 | return select ? Number( select.value ) || 0 : 0; |
| 19 | } |
| 20 | |
| 21 | if ( 'ad' === sortBy ) { |
| 22 | const link = cell.querySelector( 'a' ); |
| 23 | return link ? link.textContent.trim() : ''; |
| 24 | } |
| 25 | |
| 26 | return cell.textContent.trim(); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Generic comparator that handles numbers and strings safely. |
| 31 | * |
| 32 | * @param {number|string} a The first value to compare. |
| 33 | * @param {number|string} b The second value to compare. |
| 34 | * @param {boolean} ascending Whether to sort in ascending order. |
| 35 | * |
| 36 | * @return {number} The comparison result. |
| 37 | */ |
| 38 | function compareValues( a, b, ascending ) { |
| 39 | if ( typeof a === 'number' && typeof b === 'number' ) { |
| 40 | return ascending ? a - b : b - a; |
| 41 | } |
| 42 | |
| 43 | return ascending |
| 44 | ? String( a ).localeCompare( String( b ) ) |
| 45 | : String( b ).localeCompare( String( a ) ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Sorts table rows by column and updates UI state. |
| 50 | * |
| 51 | * @param {HTMLTableElement} table The table to sort. |
| 52 | * @param {string} sortBy The column to sort by. |
| 53 | * @param {boolean} ascending Whether to sort in ascending order. |
| 54 | * |
| 55 | * @return {void} |
| 56 | */ |
| 57 | function sortTable( table, sortBy, ascending ) { |
| 58 | const headers = Array.from( table.querySelectorAll( 'th' ) ); |
| 59 | const colIndex = headers.findIndex( |
| 60 | ( th ) => th.dataset.sortby === sortBy |
| 61 | ); |
| 62 | |
| 63 | if ( colIndex === -1 ) { |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | const tbody = table.tBodies[ 0 ]; |
| 68 | const rows = Array.from( tbody.rows ); |
| 69 | |
| 70 | rows.sort( ( rowA, rowB ) => { |
| 71 | const valueA = getCellValue( rowA, sortBy, colIndex ); |
| 72 | const valueB = getCellValue( rowB, sortBy, colIndex ); |
| 73 | return compareValues( valueA, valueB, ascending ); |
| 74 | } ); |
| 75 | |
| 76 | // Reattach sorted rows |
| 77 | rows.forEach( ( row ) => tbody.appendChild( row ) ); |
| 78 | |
| 79 | // Update sort indicator classes |
| 80 | headers.forEach( ( th ) => th.classList.remove( 'asc', 'desc' ) ); |
| 81 | headers[ colIndex ].classList.add( ascending ? 'asc' : 'desc' ); |
| 82 | } |
| 83 | |
| 84 | export function sortAds() { |
| 85 | document.querySelectorAll( '.advads-group-ads' ).forEach( ( table ) => { |
| 86 | const sortStates = { |
| 87 | ad: true, |
| 88 | status: true, |
| 89 | weight: true, |
| 90 | }; |
| 91 | |
| 92 | table.querySelectorAll( 'th.group-sort' ).forEach( ( header ) => { |
| 93 | header.addEventListener( 'click', () => { |
| 94 | const sortBy = header.dataset.sortby; |
| 95 | |
| 96 | sortTable( table, sortBy, sortStates[ sortBy ] ); |
| 97 | |
| 98 | sortStates[ sortBy ] = ! sortStates[ sortBy ]; |
| 99 | } ); |
| 100 | } ); |
| 101 | } ); |
| 102 | } |
| 103 |