557
edits
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
/* All JavaScript here will be loaded for users of the mobile site */ | /* All JavaScript here will be loaded for users of the mobile site */ | ||
// | // Simple custom table sorting that doesn't rely on MediaWiki modules | ||
function initMobileSorting() { | function initMobileSorting() { | ||
console.log('Initializing mobile sorting...' | console.log('Initializing simple mobile sorting...'); | ||
if (mw.config.get('skin') === 'minerva') { | // Check if we're on mobile (Minerva skin or small screen) | ||
console.log(' | var isMobile = (typeof mw !== 'undefined' && mw.config && mw.config.get('skin') === 'minerva') || | ||
window.matchMedia("(max-width: 768px)").matches; | |||
if (isMobile) { | |||
console.log('Mobile detected, applying custom table sorting'); | |||
var tables = document.querySelectorAll('.wikitable, table.sortable'); | var tables = document.querySelectorAll('.wikitable, table.sortable'); | ||
console.log('Found tables:', tables.length); | console.log('Found tables:', tables.length); | ||
if ( | $(tables).each(function(index, table) { | ||
var $table = $(table); | |||
var $headers = $table.find('th'); | |||
console.log(' | |||
if ($headers.length > 0) { | |||
console.log('Processing table', index, 'with', $headers.length, 'headers'); | |||
// Style headers to look clickable | |||
$headers.css({ | |||
'cursor': 'pointer', | |||
'user-select': 'none', | |||
'background-color': '#f8f9fa', | |||
'border-bottom': '2px solid #a2a9b1' | |||
}); | |||
// Add sort indicators | |||
$headers.each(function() { | |||
var $header = $(this); | |||
if (!$header.find('.sort-arrow').length) { | |||
$header.append('<span class="sort-arrow" style="margin-left: 5px; color: #666;">⇅</span>'); | |||
} | |||
}); | |||
// Add click handlers | |||
$headers.off('click.tablesort').on('click.tablesort', function() { | |||
var $header = $(this); | |||
var columnIndex = $header.index(); | |||
var $rows = $table.find('tbody tr'); | |||
// If no tbody, get all rows except the header row | |||
if ($rows.length === 0) { | |||
$rows = $table.find('tr:not(:first)'); | |||
} | |||
if ($rows.length === 0) return; | |||
console.log('Sorting column', columnIndex, 'with', $rows.length, 'rows'); | |||
// Determine sort direction | |||
var isAscending = !$header.hasClass('sort-desc'); | |||
// Reset all headers | |||
$headers.removeClass('sort-asc sort-desc'); | |||
$headers.find('.sort-arrow').text('⇅').css('color', '#666'); | |||
// Mark current header | |||
$header.addClass(isAscending ? 'sort-asc' : 'sort-desc'); | |||
$header.find('.sort-arrow').text(isAscending ? '⇑' : '⇓').css('color', '#000'); | |||
$ | // Sort rows | ||
var $ | var sortedRows = Array.from($rows).sort(function(a, b) { | ||
var aText = $(a).find('td').eq(columnIndex).text().trim(); | |||
var bText = $(b).find('td').eq(columnIndex).text().trim(); | |||
// Try numeric comparison first | |||
var aNum = parseFloat(aText.replace(/[,$%]/g, '')); | |||
var bNum = parseFloat(bText.replace(/[,$%]/g, '')); | |||
if (! | if (!isNaN(aNum) && !isNaN(bNum)) { | ||
return isAscending ? aNum - bNum : bNum - aNum; | |||
} | } | ||
// String comparison | |||
if (isAscending) { | |||
return aText.localeCompare(bText); | |||
} | } else { | ||
return bText.localeCompare(aText); | |||
} | } | ||
}); | }); | ||
console. | // Re-append sorted rows | ||
var $tbody = $table.find('tbody'); | |||
if ($tbody.length > 0) { | |||
$tbody.empty().append(sortedRows); | |||
} else { | |||
// If no tbody, append after first row (header) | |||
$table.find('tr:not(:first)').remove(); | |||
$table.append(sortedRows); | |||
} | |||
console.log('Sorting completed'); | |||
}); | }); | ||
console.log('Table', index, 'sorting initialized'); | |||
} | } | ||
} | }); | ||
} | } | ||
} | } | ||