MediaWiki:Common.js: Difference between revisions

From Makerpedia

EvaC (talk | contribs)
No edit summary
EvaC (talk | contribs)
No edit summary
Line 33: Line 33:
     $("#tool-gallery").before('<div id="category-filter">' +
     $("#tool-gallery").before('<div id="category-filter">' +
         '<button class="filter-btn" data-filter="Tools">Show All</button>' +
         '<button class="filter-btn" data-filter="Tools">Show All</button>' +
         '<button class="filter-btn" data-filter="Makerspace Tools">Makerspace Tools</button>' +
         '<button class="filter-btn" data-filter="Makerspace_Tools">Makerspace Tools</button>' +
         '<button class="filter-btn" data-filter="Wood Shop Tools">Wood Shop Tools</button>' +
         '<button class="filter-btn" data-filter="Wood_Shop_Tools">Wood Shop Tools</button>' +
         '<button class="filter-btn" data-filter="Machine Shop Tools">Machine Shop Tools</button>' +
         '<button class="filter-btn" data-filter="Machine_Shop_Tools">Machine Shop Tools</button>' +
     '</div>');
     '</div>');


Line 59: Line 59:
                     success: function (data) {
                     success: function (data) {
                         // Check if the page contains the selected category
                         // Check if the page contains the selected category
                         var pageCategories = $(data).find("a[href^='/wiki/Category:']").map(function() {
                         var pageCategories = $(data).find("a[href^='/wiki/index.php/Category:']").map(function() {
                             return $(this).attr("title").replace("Category:", "");
                             return $(this).attr("title").replace("Category:", "");
                         }).get();
                         }).get();


                         // If the tool's page contains the selected category, show the tool
                         // If the tool's page contains the selected category, show the tool

Revision as of 02:33, 4 February 2025

/* Any JavaScript here will be loaded for all users on every page load. */

if (document) window.µ = function (id, elem) {
  var ret;
  var root = ((elem) ? elem : document);
  switch (id.charAt(0)) {
    case '|':
      ret = root;
      break;
    case '+':
      ret = document.createElement(id.substring(1));
      if (elem) elem.appendChild(ret);
      break;
    case '#':
      ret = root.querySelector(id);
      break;
    default:
      ret = Array.prototype.slice.call(root.querySelectorAll(id));
      break;
  }

  return ret;
};

/* add additional edit button (prioritize 'Edit' over 'Edit Source' when available) -- styled in Medik.css */ 
if(document.getElementById("ca-edit") != null || document.getElementById("ca-ve-edit") != null) {
    let link = document.getElementById("ca-ve-edit") != null ? document.querySelector("#ca-ve-edit a").href : document.querySelector("#ca-edit a").href;
    document.getElementById("content").innerHTML += '<a href='+link+'><button class="big-edit-button"><p>EDIT</p></button></a>';
}

$(document).ready(function () {
    // Add filter buttons above the gallery
    $("#tool-gallery").before('<div id="category-filter">' +
        '<button class="filter-btn" data-filter="Tools">Show All</button>' +
        '<button class="filter-btn" data-filter="Makerspace_Tools">Makerspace Tools</button>' +
        '<button class="filter-btn" data-filter="Wood_Shop_Tools">Wood Shop Tools</button>' +
        '<button class="filter-btn" data-filter="Machine_Shop_Tools">Machine Shop Tools</button>' +
    '</div>');

    // Handle category filter button clicks
    $(".filter-btn").click(function () {
        var selectedCategory = $(this).attr("data-filter");

        // Show all tools if "Show All" is clicked
        if (selectedCategory === "Tools") {
            $("#tool-gallery .gallerybox").show();
        } else {
            // Hide all items first
            $("#tool-gallery .gallerybox").hide();

            // Show only the tools that belong to the selected category
            $("#tool-gallery .gallerybox").each(function () {
                var toolLink = $(this).find("a").attr("href"); // Get the link to the tool's individual page
                
                // Use AJAX to load the individual page content
                $.ajax({
                    url: toolLink,
                    method: "GET",
                    success: function (data) {
                        // Check if the page contains the selected category
                        var pageCategories = $(data).find("a[href^='/wiki/index.php/Category:']").map(function() {
                            return $(this).attr("title").replace("Category:", "");
                        }).get();


                        // If the tool's page contains the selected category, show the tool
                        if (pageCategories.includes(selectedCategory)) {
                            $(this).show();
                        }
                    }.bind(this) // Keep the context of the current tool
                });
            });
        }
    });
});