MediaWiki:Common.js: Difference between revisions

From Makerpedia

EvaC (talk | contribs)
No edit summary
EvaC (talk | contribs)
No edit summary
Line 38: Line 38:
     '</div>');
     '</div>');


     // Handle button clicks for filtering
     // Function to filter by category
     $(".filter-btn").on("click", function () {
     function filterByCategory(selectedCategory) {
         var selectedCategory = $(this).data("filter");
         // Hide all items first
        $("#tool-gallery .gallerybox").hide();


         // Show all items if "Show All" is clicked
         // Show only matching category items
         if (selectedCategory === "Tools") {
         $("#tool-gallery .gallerybox").each(function () {
            $("#tool-gallery .gallerybox").show(); // Show all tools
             // Find all links within the item and check if they contain the selected category
        } else {
             var categories = $(this).find("a[href^='/wiki/Category:']");
             // Hide all items first
             $("#tool-gallery .gallerybox").hide();


             // Iterate over each tool entry in the gallery
            var matchFound = false;
             $("#tool-gallery .gallerybox").each(function () {
           
                 // Use the wgCategories variable (which is an array of categories for the current page)
             // Check if any of the categories match the selectedCategory
                var itemCategories = wgCategories || []; // Default to an empty array if wgCategories is undefined
             categories.each(function () {
                var matchesCategory = itemCategories.some(function (category) {
                 if ($(this).attr('href').includes(selectedCategory)) {
                    return category.includes(selectedCategory); // Check if the selected category matches
                     matchFound = true;
                });
 
                if (matchesCategory) {
                     $(this).show(); // Show the item if category matches
                 }
                 }
             });
             });
         }
 
            // If there's a match, show the tool
            if (matchFound || selectedCategory === "Tools") {
                $(this).show();
            }
         });
    }
 
    // Triggering the filter when a category button is clicked
    $(".filter-btn").on("click", function () {
        var selectedCategory = $(this).data("filter"); // Get the selected category
        filterByCategory(selectedCategory); // Call the filter function with the selected category
     });
     });
    // Optionally, trigger the "Show All" button by default
    $(".filter-btn[data-filter='Tools']").click(); // Trigger the "Show All" button on page load
});
});

Revision as of 02:05, 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>');

    // Function to filter by category
    function filterByCategory(selectedCategory) {
        // Hide all items first
        $("#tool-gallery .gallerybox").hide();

        // Show only matching category items
        $("#tool-gallery .gallerybox").each(function () {
            // Find all links within the item and check if they contain the selected category
            var categories = $(this).find("a[href^='/wiki/Category:']");

            var matchFound = false;
            
            // Check if any of the categories match the selectedCategory
            categories.each(function () {
                if ($(this).attr('href').includes(selectedCategory)) {
                    matchFound = true;
                }
            });

            // If there's a match, show the tool
            if (matchFound || selectedCategory === "Tools") {
                $(this).show();
            }
        });
    }

    // Triggering the filter when a category button is clicked
    $(".filter-btn").on("click", function () {
        var selectedCategory = $(this).data("filter"); // Get the selected category
        filterByCategory(selectedCategory); // Call the filter function with the selected category
    });

    // Optionally, trigger the "Show All" button by default
    $(".filter-btn[data-filter='Tools']").click(); // Trigger the "Show All" button on page load
});