-
Notifications
You must be signed in to change notification settings - Fork 104
refactor(table2): move striping logic to client-side JavaScript #7209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Eetwalt
wants to merge
11
commits into
main
Choose a base branch
from
table2-striping-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e0dfe83
table2-striping
Eetwalt c868004
dont add odd as it doesn need class
Eetwalt f03a6e6
update
Eetwalt a74bda6
fix it
Eetwalt 721f741
consider subheaders
Eetwalt 9195b05
only set attribute to rowspans
Eetwalt bbac216
make hovering on rowspan rows work
Eetwalt cb62198
remove standalone table hover rules as js handles that
Eetwalt 5ada54d
bring back striping for mediawiki tables
Eetwalt 902503a
js spesific to widget based tables. make sure grouped highlight rows …
Eetwalt 78ebebd
add basic tests
Eetwalt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ const jsModules = [ | |
| 'Selectall', | ||
| 'Slider', | ||
| 'SwitchButtons', | ||
| 'Table2', | ||
| 'Tabs' | ||
| ]; | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| /******************************************************************************* | ||
| * Description: Handles dynamic striping for Table2 widgets, | ||
| * applying odd/even classes with rowspan grouping support. | ||
| ******************************************************************************/ | ||
|
|
||
| const TABLE2_CONFIG = { | ||
| SELECTORS: { | ||
| TABLE: '.table2 .table2__table', | ||
| BODY_ROW: 'tbody tr.table2__row--body', | ||
| ALL_ROWS: 'tbody tr' | ||
| }, | ||
| CLASSES: { | ||
| EVEN: 'table2__row--even', | ||
| HEAD: 'table2__row--head' | ||
| } | ||
| }; | ||
|
|
||
| class Table2Striper { | ||
| constructor( table ) { | ||
| this.table = table; | ||
| this.isSortable = table.classList.contains( 'sortable' ); | ||
| this.shouldStripe = table.getAttribute( 'data-striped' ) !== 'false'; | ||
| this.groupCounter = 0; | ||
| } | ||
|
|
||
| init() { | ||
| if ( this.shouldStripe ) { | ||
| this.restripe(); | ||
|
|
||
| if ( this.isSortable ) { | ||
| this.setupSortListener(); | ||
| } | ||
| } | ||
| this.setupHoverListeners(); | ||
| } | ||
|
|
||
| setupSortListener() { | ||
| mw.loader.using( 'jquery.tablesorter' ).then( () => { | ||
| $( this.table ).on( 'sortEnd.tablesorter', () => this.restripe() ); | ||
| } ); | ||
| } | ||
|
|
||
| restripe() { | ||
| const rows = this.table.querySelectorAll( TABLE2_CONFIG.SELECTORS.ALL_ROWS ); | ||
| let isEven = true; | ||
| let groupRemaining = 0; | ||
| this.groupCounter = 0; | ||
|
|
||
| rows.forEach( ( row ) => { | ||
| const isBodyRow = row.classList.contains( 'table2__row--body' ); | ||
| const isSubheader = row.classList.contains( TABLE2_CONFIG.CLASSES.HEAD ); | ||
|
|
||
| if ( isSubheader ) { | ||
| isEven = true; | ||
| groupRemaining = 0; | ||
Eetwalt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| row.removeAttribute( 'data-group-id' ); | ||
| return; | ||
| } | ||
|
|
||
| if ( !isBodyRow || row.style.display === 'none' ) { | ||
| return; | ||
| } | ||
|
|
||
| if ( groupRemaining === 0 ) { | ||
| isEven = !isEven; | ||
| this.groupCounter++; | ||
| } | ||
|
|
||
| const rowspanCount = parseInt( row.dataset.rowspanCount, 10 ) || 1; | ||
| groupRemaining = Math.max( groupRemaining, rowspanCount ); | ||
|
|
||
| row.classList.toggle( TABLE2_CONFIG.CLASSES.EVEN, isEven ); | ||
| row.setAttribute( 'data-group-id', this.groupCounter ); | ||
|
|
||
| groupRemaining--; | ||
| } ); | ||
| } | ||
|
|
||
| setupHoverListeners() { | ||
| const bodyRows = this.table.querySelectorAll( TABLE2_CONFIG.SELECTORS.BODY_ROW ); | ||
|
|
||
| bodyRows.forEach( ( row ) => { | ||
| row.addEventListener( 'mouseenter', ( e ) => this.onRowHoverEnter( e ) ); | ||
| row.addEventListener( 'mouseleave', ( e ) => this.onRowHoverLeave( e ) ); | ||
| } ); | ||
| } | ||
|
|
||
| onRowHoverEnter( event ) { | ||
| const row = event.target.closest( TABLE2_CONFIG.SELECTORS.BODY_ROW ); | ||
| if ( !row ) { | ||
| return; | ||
| } | ||
|
|
||
| const groupId = row.getAttribute( 'data-group-id' ); | ||
| if ( !groupId ) { | ||
| return; | ||
| } | ||
|
|
||
| const groupRows = this.table.querySelectorAll( | ||
| `${ TABLE2_CONFIG.SELECTORS.BODY_ROW }[data-group-id="${ groupId }"]` | ||
| ); | ||
| groupRows.forEach( ( r ) => r.classList.add( 'table2__row--group-hover' ) ); | ||
| } | ||
|
|
||
| onRowHoverLeave( event ) { | ||
| const row = event.target.closest( TABLE2_CONFIG.SELECTORS.BODY_ROW ); | ||
| if ( !row ) { | ||
| return; | ||
| } | ||
|
|
||
| const groupId = row.getAttribute( 'data-group-id' ); | ||
| if ( !groupId ) { | ||
| return; | ||
| } | ||
|
|
||
| const groupRows = this.table.querySelectorAll( | ||
| `${ TABLE2_CONFIG.SELECTORS.BODY_ROW }[data-group-id="${ groupId }"]` | ||
| ); | ||
| groupRows.forEach( ( r ) => r.classList.remove( 'table2__row--group-hover' ) ); | ||
| } | ||
| } | ||
|
|
||
| class Table2Module { | ||
| init() { | ||
| const tables = document.querySelectorAll( TABLE2_CONFIG.SELECTORS.TABLE ); | ||
|
|
||
| tables.forEach( ( table ) => { | ||
| new Table2Striper( table ).init(); | ||
| } ); | ||
| } | ||
| } | ||
|
|
||
| liquipedia.table2 = new Table2Module(); | ||
| liquipedia.core.modules.push( 'table2' ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /** | ||
| * @jest-environment jsdom | ||
| */ | ||
| /* global jest */ | ||
|
|
||
| const { test, expect, beforeAll, describe } = require( '@jest/globals' ); | ||
|
|
||
| describe( 'Table2 module', () => { | ||
| beforeAll( () => { | ||
| globalThis.liquipedia = { | ||
| core: { | ||
| modules: [] | ||
| } | ||
| }; | ||
|
|
||
| globalThis.mw = { | ||
| loader: { | ||
| using: jest.fn( () => Promise.resolve() ) | ||
| } | ||
| }; | ||
|
|
||
| require( '../commons/Table2.js' ); | ||
| } ); | ||
|
|
||
| test( 'should register itself as a module', () => { | ||
| expect( globalThis.liquipedia.core.modules ).toContain( 'table2' ); | ||
| } ); | ||
|
|
||
| test( 'should initialize table2 instance', () => { | ||
| expect( globalThis.liquipedia.table2 ).toBeTruthy(); | ||
| } ); | ||
|
|
||
| test( 'should have init method', () => { | ||
| expect( typeof globalThis.liquipedia.table2.init ).toBe( 'function' ); | ||
| } ); | ||
|
|
||
| test( 'should assign data-group-id to body rows during striping', () => { | ||
| document.body.innerHTML = ` | ||
| <div class="table2"> | ||
| <table class="table2__table"> | ||
| <tbody> | ||
| <tr class="table2__row--body"></tr> | ||
| <tr class="table2__row--body"></tr> | ||
| <tr class="table2__row--body"></tr> | ||
| </tbody> | ||
| </table> | ||
| </div> | ||
| `; | ||
|
|
||
| globalThis.liquipedia.table2.init(); | ||
|
|
||
| const rows = document.querySelectorAll( '.table2 .table2__table tbody tr.table2__row--body' ); | ||
| expect( rows.length ).toBe( 3 ); | ||
|
|
||
| rows.forEach( ( row ) => { | ||
| expect( row.getAttribute( 'data-group-id' ) ).toBeTruthy(); | ||
| } ); | ||
| } ); | ||
|
|
||
| test( 'should apply even class to alternating rows', () => { | ||
| document.body.innerHTML = ` | ||
| <div class="table2"> | ||
| <table class="table2__table"> | ||
| <tbody> | ||
| <tr class="table2__row--body"></tr> | ||
| <tr class="table2__row--body"></tr> | ||
| <tr class="table2__row--body"></tr> | ||
| <tr class="table2__row--body"></tr> | ||
| </tbody> | ||
| </table> | ||
| </div> | ||
| `; | ||
|
|
||
| globalThis.liquipedia.table2.init(); | ||
|
|
||
| const rows = document.querySelectorAll( '.table2 .table2__table tbody tr.table2__row--body' ); | ||
|
|
||
| expect( rows[ 0 ].classList.contains( 'table2__row--even' ) ).toBe( false ); | ||
| expect( rows[ 1 ].classList.contains( 'table2__row--even' ) ).toBe( true ); | ||
| expect( rows[ 2 ].classList.contains( 'table2__row--even' ) ).toBe( false ); | ||
| expect( rows[ 3 ].classList.contains( 'table2__row--even' ) ).toBe( true ); | ||
| } ); | ||
|
|
||
| test( 'should ignore standalone tables without .table2 wrapper', () => { | ||
| document.body.innerHTML = ` | ||
| <table class="table2__table"> | ||
| <tbody> | ||
| <tr><td>Standalone table row</td></tr> | ||
| </tbody> | ||
| </table> | ||
| `; | ||
|
|
||
| globalThis.liquipedia.table2.init(); | ||
|
|
||
| const row = document.querySelector( '.table2__table tbody tr' ); | ||
| expect( row.getAttribute( 'data-group-id' ) ).toBeNull(); | ||
| } ); | ||
| } ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.