MediaWiki:Mobile.js: Difference between revisions
Jump to navigation
Jump to search
No edit summary Tag: Reverted |
No edit summary Tags: Manual revert Reverted |
||
| 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 | |||
$( function(){ | $( function(){ | ||
| Line 50: | Line 189: | ||
}); | }); | ||
} | } | ||
// Initialize table sorting | |||
initMobileSorting(); | |||
// Also reinitialize when new content loads (like after editing) | |||
mw.hook('wikipage.content').add(function() { | |||
initMobileSorting(); | |||
}); | |||
}); | }); | ||
Revision as of 21:04, 26 August 2025
/* 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
$( function(){
// Mobile/Desktop view switching logic
if (window.matchMedia("(max-width: 768px)").matches) {
if( mw.config.get("skin") !== "minerva" ){
const currWikiUrl = new URL(window.location.href);
currWikiUrl.searchParams.set('mobileaction', 'toggle_view_mobile');
window.location.replace(currWikiUrl);
}
$("#footer-places-desktop-toggle").remove();
} else {
if( mw.config.get("skin") !== "vector" ){
const currWikiUrl = new URL(window.location.href);
currWikiUrl.searchParams.set('mobileaction', 'toggle_view_desktop');
window.location.replace(currWikiUrl);
}
}
// Poem formatting code
var countPoem2 = $(".Poem2-Ekatra");
if( countPoem2.length > 0 ){
var Poem2lenghtArray = [];
for (var k = 0; k <= countPoem2.length; k++) {
Poem2lenghtArray.push(k);
}
Poem2lenghtArray.forEach(function(j) {
var poemElement = $(".Poem2-Ekatra").eq(j);
if( poemElement.length ){
var poemText = poemElement.html();
var poemArray = poemText.split("\n");
poemElement.text("");
// First measure
poemArray.forEach( function(i) {
poemElement.append( '<p style="text-indent: 2em;">' + i + '</p>' );
});
// Second measure
poemElement.children('p').each(function () {
$(this).css('text-indent', '2em');
});
}
});
}
// Initialize table sorting
initMobileSorting();
// Also reinitialize when new content loads (like after editing)
mw.hook('wikipage.content').add(function() {
initMobileSorting();
});
});