MediaWiki:Timeless.js: Difference between revisions

From The HOA
No edit summary
mNo edit summary
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
mw.loader.using(['mediawiki.api']).then(function () {
<!---
    var category = "Resources"; // The name of the category
mw.loader.using(['mediawiki.api']).then(function () {
    var sidebar = $("div#site-navigation"); // Target the site-navigation container
var category = "Resources"; // The name of the category
var sidebar = $("div#site-navigation"); // Target the site-navigation container


    // Log to ensure we're targeting the correct container
// Log to ensure we're targeting the correct container
    if (sidebar.length === 0) {
if (sidebar.length === 0) {
        console.error("Sidebar container not found");
console.error("Sidebar container not found");
        return;
return;
    }
}


    console.log("Sidebar container found, loading menu...");
console.log("Sidebar container found, loading menu...");


    // Create a new wrapper for the custom menu
// Create a new wrapper for the custom menu
    var $menuWrapper = $("<div>").addClass("portal").attr("id", "custom-category-wrapper");
var $menuWrapper = $("<div>").addClass("portal").attr("id", "custom-category-wrapper");
      
      
    // Create a new block for the menu
// Create a new block for the menu
    var $menuBlock = $("<div>").addClass("portal-body").attr("id", "custom-category-menu");
var $menuBlock = $("<div>").addClass("portal-body").attr("id", "custom-category-menu");
    var $heading = $("<h3>").addClass("portal-heading").text("Pages in " + category);
var $heading = $("<h3>").addClass("portal-heading").text("" + category);
    var $list = $("<ul>").addClass("portal-body");
var $list = $("<ul>").addClass("portal-body");


    new mw.Api().get({
new mw.Api().get({
        action: "query",
action: "query",
        list: "categorymembers",
list: "categorymembers",
        cmtitle: "Category:" + category,
cmtitle: "Category:" + category,
        cmlimit: 10,
cmlimit: 10,
        format: "json"
format: "json"
    }).done(function (data) {
}).done(function (data) {
        if (data.query && data.query.categorymembers.length > 0) {
if (data.query && data.query.categorymembers.length > 0) {
            data.query.categorymembers.forEach(function (page) {
data.query.categorymembers.forEach(function (page) {
                $list.append($("<li>").append($("<a>").attr("href", mw.util.getUrl(page.title)).text(page.title)));
$list.append($("<li>").append($("<a>").attr("href", mw.util.getUrl(page.title)).text(page.title)));
            });
});


            // Append the heading and list to the new block
// Append the heading and list to the new block
            $menuBlock.append($heading).append($list);
$menuBlock.append($heading).append($list);


            // Append the new block to the wrapper
// Add a <br/> at the end of the list
            $menuWrapper.append($menuBlock);
$menuBlock.append("<br/>");


            // Insert the new wrapper into the site-navigation container
// Append the new block to the wrapper
            sidebar.append($menuWrapper); // Add the block to the sidebar
$menuWrapper.append($menuBlock);
            console.log("Menu added to sidebar.");
        } else {
            console.log("No pages found in the category.");
        }
    }).fail(function () {
        console.error("API request failed.");
    });


    // Optional: Add space at the bottom of the new menu block
// Insert the new wrapper into the site-navigation container
    $("#custom-category-wrapper").css("margin-bottom", "10px");
sidebar.append($menuWrapper); // Add the block to the sidebar
console.log("Menu added to sidebar.");
} else {
console.log("No pages found in the category.");
}
}).fail(function () {
console.error("API request failed.");
});
 
// Optional: Add space at the bottom of the new menu block
$("#custom-category-wrapper").css("margin-bottom", "10px");
});
});
--->

Latest revision as of 07:19, 13 March 2025

<!---
	mw.loader.using(['mediawiki.api']).then(function () {
	var category = "Resources"; // The name of the category
	var sidebar = $("div#site-navigation"); // Target the site-navigation container

	// Log to ensure we're targeting the correct container
	if (sidebar.length === 0) {
		console.error("Sidebar container not found");
		return;
	}

	console.log("Sidebar container found, loading menu...");

	// Create a new wrapper for the custom menu
	var $menuWrapper = $("<div>").addClass("portal").attr("id", "custom-category-wrapper");
    
	// Create a new block for the menu
	var $menuBlock = $("<div>").addClass("portal-body").attr("id", "custom-category-menu");
	var $heading = $("<h3>").addClass("portal-heading").text("" + category);
	var $list = $("<ul>").addClass("portal-body");

	new mw.Api().get({
		action: "query",
		list: "categorymembers",
		cmtitle: "Category:" + category,
		cmlimit: 10,
		format: "json"
	}).done(function (data) {
		if (data.query && data.query.categorymembers.length > 0) {
			data.query.categorymembers.forEach(function (page) {
				$list.append($("<li>").append($("<a>").attr("href", mw.util.getUrl(page.title)).text(page.title)));
			});

			// Append the heading and list to the new block
			$menuBlock.append($heading).append($list);

			// Add a <br/> at the end of the list
			$menuBlock.append("<br/>");

			// Append the new block to the wrapper
			$menuWrapper.append($menuBlock);

			// Insert the new wrapper into the site-navigation container
			sidebar.append($menuWrapper); // Add the block to the sidebar
			console.log("Menu added to sidebar.");
		} else {
			console.log("No pages found in the category.");
		}
	}).fail(function () {
		console.error("API request failed.");
	});

	// Optional: Add space at the bottom of the new menu block
	$("#custom-category-wrapper").css("margin-bottom", "10px");
});
--->