MediaWiki:Common.js: Difference between revisions

From Emmy The Robot Fandom Wiki
Content added Content deleted
No edit summary
No edit summary
Tag: Manual revert
 
(41 intermediate revisions by the same user not shown)
Line 1: Line 1:
if ( mw.config.get( 'wgPageName' ) === 'Main_Page' ) {
/* Any JavaScript here will be loaded for all users on every page load.
document.getElementsByTagName('br')[0].remove();
$(function(){
}
importArticles({
type: "script",
articles: ["u:pad.wikia.com:MediaWiki:FilterTable.js"]
});
});
*/


if ( mw.config.get( 'wgPageName' ) === 'Main_Page' ) {
document.getElementsByTagName('h1')[0].remove();
}


// Function to initialize the text parsing and filtering functionality
function filterTable(){
function initializeTextParser() {
$("table.filterable").each(function(){
// Create a search input field
var i=0;
var searchInput = document.createElement('input');
var cols;
searchInput.setAttribute('type', 'text');
$(this).find("tr:first-child th, tr:first-child td").each(function(){
searchInput.setAttribute('id', 'mediawiki-text-parser-input');
if (!$(this).hasClass("unfilterable")){
searchInput.setAttribute('placeholder', 'Type to search');
cols=[];

$(this).closest("table").find("tr td:nth-child("+(i+1)+")").each(function(){
// Set the width of the search input field as a percentage
cols.push($(this).text());
searchInput.style.width = '100%'; // You can adjust the percentage here
});

cols = arrayUnique(cols).sort() ;
document.getElementById('mw-content-text').insertBefore(searchInput, document.getElementById('mw-content-text').firstChild);
l=0;

for (j=0; j<cols.length; j++){
// Get the table
t=charLength(cols[j]);
var table = document.querySelector('table.wikitable.sortable.mw-datatable.jquery-tablesorter');
if (l<t) l=t;

}
// Attach an event listener to the search input field
$(this).css("position","relative");
searchInput.addEventListener('input', function() {
$(this).html('<a href="javascript:void(0)" style="color:white;" class="showFilterMenu">'+$(this).html()+'▼</a>');
var searchText = this.value.toLowerCase();
$(this).append($('<div class="filterMenu" style="position:absolute;top:'+$(this).height()+'px;left:0;width:'+(22+l*7)+'px;text-align:left;padding:5px;border:1px #000 solid;background:#6C93B1;z-index:1;display:none"></div>'));

for (j=0; j<cols.length; j++){
// Get all rows in the table
$(this).find(".filterMenu").append('<div><input type="checkbox" value="'+cols[j]+'" col="'+(i+1)+'" class="filterOption" checked>'+cols[j]+'</div>')
var rows = table.querySelectorAll('tr');
}

}
// Loop through each row
i++;
rows.forEach(function(row, index) {
});
if (index === 0) {
$(this).find("tr:nth-child(n+1)").attr("condition", 0);
// Show the first row (headers) always
});
row.style.display = 'table-row';
$(".showFilterMenu").click(function(){
} else {
if ($(this).parent().find(".filterMenu:visible").length){
var rowCells = row.cells;
$(".filterMenu").slideUp(150);
var rowShouldBeVisible = false;
}else{

$(".filterMenu").slideUp(150);
// Loop through each cell in the row
$(this).parent().find(".filterMenu").slideDown(150);
Array.from(rowCells).forEach(function(cell) {
}
var cellText = cell.textContent.toLowerCase();
});
// Check if any cell contains the search text
$(document).mouseup(function(e){
if (cellText.includes(searchText)) {
var container = $(".filterMenu");
rowShouldBeVisible = true;
if (!container.is(e.target) && container.has(e.target).length === 0){
container.slideUp(150);
}
}
});

});
// Set the display property of the row and its cells
$(".filterOption").click(function(){
if (rowShouldBeVisible) {
col=$(this).attr("col");
row.style.display = 'table-row';
val=$(this).val();
Array.from(rowCells).forEach(function(cell) {
if ($(this).is(":checked")) chg=1; else chg=-1;
cell.style.display = 'table-cell';
$(this).closest("table").find("tr:nth-child(n+1)").each(function(){
});
if ($(this).find("td:nth-child("+col+")").text()==val){
} else {
var cond=$(this).attr("condition");
row.style.display = 'none';
cond=Number(cond)+chg;
}
$(this).attr("condition", cond);
}
if (cond==0) $(this).show();
});
else $(this).hide();
});
}
});
});
}
}

function arrayUnique(a) {
// Call the initializeTextParser function when the DOM is ready
return a.reduce(function(p, c) {
function initializeTextParserOnLoad() {
if (p.indexOf(c) < 0) p.push(c);
var pathname = window.location.pathname;
return p;
var pageNames = ['List_of_fan_Nandroids', 'List_of_fan_stories'];
}, []);
if (pageNames.includes(pathname.substring(pathname.lastIndexOf('/') + 1))) {
};
initializeTextParser();
function charLength(s){
}
return s.length+(encodeURI(s).split(/%..|./).length-1-s.length)/2;
}
}

filterTable();
// Call the initializeTextParserOnLoad function when the entire page is loaded
window.onload = initializeTextParserOnLoad;

Latest revision as of 07:52, 18 May 2024

if ( mw.config.get( 'wgPageName' ) === 'Main_Page' ) {
    document.getElementsByTagName('br')[0].remove();
}

if ( mw.config.get( 'wgPageName' ) === 'Main_Page' ) {
    document.getElementsByTagName('h1')[0].remove();
}

// Function to initialize the text parsing and filtering functionality
function initializeTextParser() {
    // Create a search input field
    var searchInput = document.createElement('input');
    searchInput.setAttribute('type', 'text');
    searchInput.setAttribute('id', 'mediawiki-text-parser-input');
    searchInput.setAttribute('placeholder', 'Type to search');

    // Set the width of the search input field as a percentage
    searchInput.style.width = '100%'; // You can adjust the percentage here

    document.getElementById('mw-content-text').insertBefore(searchInput, document.getElementById('mw-content-text').firstChild);

    // Get the table
    var table = document.querySelector('table.wikitable.sortable.mw-datatable.jquery-tablesorter');

    // Attach an event listener to the search input field
    searchInput.addEventListener('input', function() {
        var searchText = this.value.toLowerCase();

        // Get all rows in the table
        var rows = table.querySelectorAll('tr');

        // Loop through each row
        rows.forEach(function(row, index) {
            if (index === 0) {
                // Show the first row (headers) always
                row.style.display = 'table-row';
            } else {
                var rowCells = row.cells;
                var rowShouldBeVisible = false;

                // Loop through each cell in the row
                Array.from(rowCells).forEach(function(cell) {
                    var cellText = cell.textContent.toLowerCase();
                    // Check if any cell contains the search text
                    if (cellText.includes(searchText)) {
                        rowShouldBeVisible = true;
                    }
                });

                // Set the display property of the row and its cells
                if (rowShouldBeVisible) {
                    row.style.display = 'table-row';
                    Array.from(rowCells).forEach(function(cell) {
                        cell.style.display = 'table-cell';
                    });
                } else {
                    row.style.display = 'none';
                }
            }
        });
    });
}

// Call the initializeTextParser function when the DOM is ready
function initializeTextParserOnLoad() {
    var pathname = window.location.pathname;
    var pageNames = ['List_of_fan_Nandroids', 'List_of_fan_stories'];
    if (pageNames.includes(pathname.substring(pathname.lastIndexOf('/') + 1))) {
        initializeTextParser();
    }
}

// Call the initializeTextParserOnLoad function when the entire page is loaded
window.onload = initializeTextParserOnLoad;