MediaWiki:Common.js: Difference between revisions

From The Talos Principle Wiki
Previous code assumed wrong html structure for markup sublists
New social media thread attempt
Line 41: Line 41:


/* Custom collapsibles for dialogue (e.g., social media thread) */
/* Custom collapsibles for dialogue (e.g., social media thread) */
   $(document).ready(function () {
   $(document).ready(function() {
    // Hide immediate child sub-lists initially
    $('span.dialogue-choice').closest('li').next('ul').hide();


     // Add a click event to toggle the visibility of the sub-lists
     // Collapse all by default by running very similar code to the click event:
     $('span.dialogue-choice').click(function () {
     $('span.dialogue-choice').each(function() {
       // Find the closest ul following the span
       // Find the parent <li> of the clicked span
       var targetUl = $(this).closest('li').next('ul');
       var $parentLi = $(this).closest('li');
       // Toggle the visibility of the ul
      // Find all descendant <ul> elements of the parent <li> and toggle their visibility
       targetUl.slideToggle();
      $parentLi.find('ul').toggle(); // instant
       // Note that using .hide() here causes issues when expanding later. Something about the nested structure requires using .toggle() instead.
    });
 
    // Attach the click event to toggle visibility
    $('span.dialogue-choice').on('click', function() {
      var $parentLi = $(this).closest('li');
       $parentLi.find('ul').slideToggle(); // animated
     });
     });
   });
   });

Revision as of 04:34, 25 November 2023

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


/* Custom animated collapsibles: */
  var coll = document.getElementsByClassName("custom-collapsible");
  var i;
  
  for (i = 0; i < coll.length; i++) {
    coll[i].addEventListener("click", function() {
      this.classList.toggle("active");
      var content = this.nextElementSibling;
      if (content.style.maxHeight){
        content.style.maxHeight = null;
        content.classList.remove("custom-collapsible-content-visible");
      } else {
        content.style.maxHeight = content.scrollHeight + "px";
        content.classList.add("custom-collapsible-content-visible");
      } 
    });
  }


/* Custom hex strings */
  var hss = document.getElementsByClassName("hex-string");
  var i;

  for (i = 0; i < hss.length; i++) {
    hss[i].addEventListener("click", function() {
      if (this.dataset.state === 'hex') {
        // Switch to English
        this.querySelector('.hex-string-content').innerText = this.dataset.text;
        this.dataset.state = 'text';
      } else {
        // Switch to Hex
        this.querySelector('.hex-string-content').innerText = this.dataset.hex;
        this.dataset.state = 'hex';
      }
    });
  }


/* Custom collapsibles for dialogue (e.g., social media thread) */
  $(document).ready(function() {

    // Collapse all by default by running very similar code to the click event:
    $('span.dialogue-choice').each(function() {
      // Find the parent <li> of the clicked span
      var $parentLi = $(this).closest('li');
      // Find all descendant <ul> elements of the parent <li> and toggle their visibility
      $parentLi.find('ul').toggle(); // instant
      // Note that using .hide() here causes issues when expanding later. Something about the nested structure requires using .toggle() instead.
    });

    // Attach the click event to toggle visibility
    $('span.dialogue-choice').on('click', function() {
      var $parentLi = $(this).closest('li');
      $parentLi.find('ul').slideToggle(); // animated
    });
  });