|
|
| 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() {
| |
| console.log('Initializing simple mobile sorting...');
| |
|
| |
| // Check if we're on mobile (Minerva skin or small screen)
| |
| 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');
| |
| console.log('Found tables:', tables.length);
| |
|
| |
| $(tables).each(function(index, table) {
| |
| setupTableSorting($(table), index);
| |
| });
| |
| }
| |
| }
| |
|
| |
| // Separate function to setup sorting for a single table
| |
| function setupTableSorting($table, tableIndex) {
| |
| var $headers = $table.find('th');
| |
|
| |
| if ($headers.length === 0) return;
| |
|
| |
| console.log('Setting up table', tableIndex, 'with', $headers.length, 'headers');
| |
|
| |
| // Remove any existing event handlers to avoid duplicates
| |
| $table.off('click.tablesort');
| |
|
| |
| // Style headers and add sort indicators
| |
| $headers.each(function() {
| |
| var $header = $(this);
| |
|
| |
| // Style header
| |
| $header.css({
| |
| 'cursor': 'pointer',
| |
| 'user-select': 'none',
| |
| 'background-color': '#f8f9fa',
| |
| 'border-bottom': '2px solid #a2a9b1',
| |
| 'position': 'relative'
| |
| });
| |
|
| |
| // Remove existing arrows and add new ones
| |
| $header.find('.sort-arrow').remove();
| |
| $header.append('<span class="sort-arrow" style="margin-left: 5px; color: #666; font-size: 12px;">⇅</span>');
| |
| });
| |
|
| |
| // Use event delegation on the table
| |
| $table.on('click.tablesort', 'th', function(e) {
| |
| e.preventDefault();
| |
| e.stopPropagation();
| |
|
| |
| var $clickedHeader = $(this);
| |
| var columnIndex = $clickedHeader.index();
| |
|
| |
| console.log('Header clicked - Column:', columnIndex);
| |
|
| |
| // Get fresh references
| |
| var $allHeaders = $table.find('th');
| |
| var $dataRows = $table.find('tbody tr');
| |
|
| |
| // If no tbody, get all rows except header
| |
| if ($dataRows.length === 0) {
| |
| $dataRows = $table.find('tr').not($table.find('tr').first());
| |
| }
| |
|
| |
| if ($dataRows.length === 0) {
| |
| console.log('No data rows found');
| |
| return;
| |
| }
| |
|
| |
| console.log('Found', $dataRows.length, 'data rows to sort');
| |
|
| |
| // Determine sort direction based on current state
| |
| var currentSortClass = $clickedHeader.hasClass('sort-asc') ? 'asc' :
| |
| $clickedHeader.hasClass('sort-desc') ? 'desc' : 'none';
| |
| var newSortDirection = currentSortClass === 'asc' ? 'desc' : 'asc';
| |
|
| |
| console.log('Current sort:', currentSortClass, '-> New sort:', newSortDirection);
| |
|
| |
| // Reset all headers
| |
| $allHeaders.removeClass('sort-asc sort-desc');
| |
| $allHeaders.find('.sort-arrow').text('⇅').css('color', '#666');
| |
|
| |
| // Set new sort state
| |
| $clickedHeader.addClass('sort-' + newSortDirection);
| |
| var arrow = newSortDirection === 'asc' ? '⇑' : '⇓';
| |
| $clickedHeader.find('.sort-arrow').text(arrow).css('color', '#000');
| |
|
| |
| // Convert to array and sort
| |
| var rowsArray = $dataRows.toArray();
| |
|
| |
| rowsArray.sort(function(rowA, rowB) {
| |
| var $cellA = $(rowA).find('td').eq(columnIndex);
| |
| var $cellB = $(rowB).find('td').eq(columnIndex);
| |
|
| |
| if ($cellA.length === 0 || $cellB.length === 0) return 0;
| |
|
| |
| var textA = $cellA.text().trim();
| |
| var textB = $cellB.text().trim();
| |
|
| |
| // Try numeric comparison
| |
| var numA = parseFloat(textA.replace(/[,$%\s]/g, ''));
| |
| var numB = parseFloat(textB.replace(/[,$%\s]/g, ''));
| |
|
| |
| var result;
| |
| if (!isNaN(numA) && !isNaN(numB)) {
| |
| result = numA - numB;
| |
| } else {
| |
| result = textA.localeCompare(textB);
| |
| }
| |
|
| |
| return newSortDirection === 'desc' ? -result : result;
| |
| });
| |
|
| |
| // Reinsert sorted rows
| |
| var $tbody = $table.find('tbody');
| |
| if ($tbody.length > 0) {
| |
| $tbody.empty().append(rowsArray);
| |
| } else {
| |
| // Remove existing data rows and append sorted ones
| |
| $table.find('tr').not($table.find('tr').first()).remove();
| |
| $table.append(rowsArray);
| |
| }
| |
|
| |
| console.log('Sorting completed successfully');
| |
|
| |
| // Re-setup the table to ensure it stays sortable
| |
| setTimeout(function() {
| |
| setupTableSorting($table, tableIndex);
| |
| }, 10);
| |
| });
| |
|
| |
| console.log('Table', tableIndex, 'sorting setup completed');
| |
| }
| |
|
| |
|
| // Main initialization function | | // Main initialization function |