User:Suyash.dwivedi/common.js: Difference between revisions

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Content deleted Content added
No edit summary
No edit summary
Line 23: Line 23:
},
},
function(data) {
function(data) {
if (data.query && data.query.usercontribs) {
const totalItems = data.query.usercontribs.length;
const totalItems = data.query.usercontribs.length;
const totalPages = Math.ceil(totalItems / itemsPerPage);
const totalPages = Math.ceil(totalItems / itemsPerPage);
createPagination(currentPage, totalPages);
createPagination(totalItems, currentPage, totalPages);
} else {
// Handle case where no files are found
createPagination(0, currentPage, 0);
}
}
}
).fail(function() {
);
console.error('Error fetching data from MediaWiki API');
createPagination(0, currentPage, 0);
});
}
}


// Function to create and display pagination
// Function to create and display pagination
function createPagination(totalItems, currentPage, totalPages) {
function createPagination(currentPage, totalPages) {
const paginationContainer = $('#pagination-container');
const paginationContainer = $('#pagination-container');
paginationContainer.empty(); // Clear previous pagination
paginationContainer.empty(); // Clear previous pagination

// Display a message if no files are found
if (totalItems === 0) {
paginationContainer.append('<span>No files found.</span>');
return;
}


// Create Previous Page Link
// Create Previous Page Link
if (currentPage > 1) {
const prevPage = $('<a>', {
$('<a>', {
text: 'Previous',
text: 'Previous',
href: currentPage > 1 ? `?page=${currentPage - 1}` : '#',
href: `?page=${currentPage - 1}`,
class: currentPage > 1 ? '' : 'disabled',
class: '',
click: function(event) {
if (currentPage === 1) event.preventDefault(); // Disable link if on the first page
}).appendTo(paginationContainer);
}
}
}).appendTo(paginationContainer);


// Create Page Number List
// Create Page Number List
for (let i = 1; i <= totalPages; i++) {
for (let i = 1; i <= totalPages; i++) {
$('<a>', {
const pageLink = $('<a>', {
text: i,
text: i,
href: `?page=${i}`,
href: `?page=${i}`,
Line 68: Line 55:


// Create Next Page Link
// Create Next Page Link
if (currentPage < totalPages) {
const nextPage = $('<a>', {
$('<a>', {
text: 'Next',
text: 'Next',
href: currentPage < totalPages ? `?page=${currentPage + 1}` : '#',
href: `?page=${currentPage + 1}`,
class: currentPage < totalPages ? '' : 'disabled',
class: '',
click: function(event) {
if (currentPage === totalPages) event.preventDefault(); // Disable link if on the last page
}).appendTo(paginationContainer);
}
}
}).appendTo(paginationContainer);
}
}


// Example dynamic values (these should be set based on your actual pagination)
// Example dynamic values (these should be set based on your actual pagination)
const currentPage = parseInt(new URLSearchParams(window.location.search).get('page')) || 1; // Get the current page number from the URL
const currentPage = 1; // Replace with the actual current page number dynamically
const itemsPerPage = 20; // Set this based on user selection or default
const itemsPerPage = 20; // Set this based on user selection or default

// Extract the user ID from the URL
const title = decodeURIComponent(window.location.pathname.split('/').pop());
const username = title.split(':')[1]; // Get the username from the title


// Fetch total files for the specific user and create pagination
// Fetch total files for the specific user and create pagination
fetchTotalFiles('Suyash.dwivedi', itemsPerPage);
if (username) {
fetchTotalFiles(username, itemsPerPage);
}
});
});



Revision as of 20:30, 25 October 2024

importScript('User:Kanonkas/twinkle.js');
importScript('User:Majora/LicenseReview.js');
window.hotcat_single_changes_are_minor = false; // Or true
importScript('User:Perhelion/justReplace.js');
if (mw.config.get('wgNamespaceNumber') === 6 && /SVG/i.test(mw.config.get('wgTitle').slice(-3)))
	importScript('User:Perhelion/simpleSVGcheck.js');
mw.loader.load( 'ext.gadget.VisualFileChange' );
window.vFC_PortletText = 'Suyash-Visual File Change';
importScript('https://de.wikipedia.org/w/index.php?title=Benutzer:DerHexer/massendiskus.js');
////////////////////////
$(document).ready(function() {
    // Function to fetch total files by a specific user and calculate pagination
    function fetchTotalFiles(username, itemsPerPage) {
        $.getJSON(
            mw.util.wikiScript('api'), 
            {
                action: 'query',
                list: 'usercontribs',
                ucuser: username,
                uclimit: 'max', // Fetch max items to get the count
                ucnamespace: 6, // Namespace for files
                format: 'json'
            },
            function(data) {
                const totalItems = data.query.usercontribs.length;
                const totalPages = Math.ceil(totalItems / itemsPerPage);
                createPagination(currentPage, totalPages);
            }
        );
    }

    // Function to create and display pagination
    function createPagination(currentPage, totalPages) {
        const paginationContainer = $('#pagination-container');
        paginationContainer.empty(); // Clear previous pagination

        // Create Previous Page Link
        const prevPage = $('<a>', {
            text: 'Previous',
            href: currentPage > 1 ? `?page=${currentPage - 1}` : '#',
            class: currentPage > 1 ? '' : 'disabled',
            click: function(event) {
                if (currentPage === 1) event.preventDefault(); // Disable link if on the first page
            }
        }).appendTo(paginationContainer);

        // Create Page Number List
        for (let i = 1; i <= totalPages; i++) {
            const pageLink = $('<a>', {
                text: i,
                href: `?page=${i}`,
                class: i === currentPage ? 'active' : '',
            }).appendTo(paginationContainer);
        }

        // Create Next Page Link
        const nextPage = $('<a>', {
            text: 'Next',
            href: currentPage < totalPages ? `?page=${currentPage + 1}` : '#',
            class: currentPage < totalPages ? '' : 'disabled',
            click: function(event) {
                if (currentPage === totalPages) event.preventDefault(); // Disable link if on the last page
            }
        }).appendTo(paginationContainer);
    }

    // Example dynamic values (these should be set based on your actual pagination)
    const currentPage = 1; // Replace with the actual current page number dynamically
    const itemsPerPage = 20; // Set this based on user selection or default

    // Fetch total files for the specific user and create pagination
    fetchTotalFiles('Suyash.dwivedi', itemsPerPage);
});

////////////////////////////////////