MediaWiki:Common.js: Difference between revisions

From The Talos Principle Wiki
New social media thread attempt
Custom collapsibles for social media thread
Line 40: Line 40:




/* Custom collapsibles for dialogue (e.g., social media thread) */
/* Custom collapsibles for social media thread */
   $(document).ready(function() {
   $(document).ready(function() {
 
    var depth;
     // Collapse all by default by running very similar code to the click event:
    var stillCollapsing, stillExpanding;
     $('span.dialogue-choice').each(function() {
     // Attach the click event
       // Find the parent <li> of the clicked span
     $('span.dialogue-choice-button').on('click', chooseDialogue);
       var $parentLi = $(this).closest('li');
    // Hide initially
       // Find all descendant <ul> elements of the parent <li> and toggle their visibility
    $('div.social-media-thread ul').each(initialDeepHide);
       $parentLi.find('ul').toggle(); // instant
 
       // Note that using .hide() here causes issues when expanding later. Something about the nested structure requires using .toggle() instead.
    function initialDeepHide() {
    });
       if ($(this).is('ul')) {
 
        $(this).children('li').each(initialDeepHide);
    // Attach the click event to toggle visibility
      }
    $('span.dialogue-choice').on('click', function() {
       else if ($(this).is('li')) {
       var $parentLi = $(this).closest('li');
        if ($(this).children('ul').length) {
       $parentLi.find('ul').slideToggle(); // animated
          $(this).children('ul').each(initialDeepHide);
    });
        } else {
          myDepth = 1 + $(this).parents('li').length;
          if (myDepth > 1) { $(this).hide(); }
        }
       }
    }
 
    function deepCollapse() {
       if ($(this).is('ul')) {
        $(this).children('li').each(deepCollapse);
       }
      else if ($(this).is('li')) {
        if ($(this).children('ul').length) {
          $(this).children('ul').each(deepCollapse);
        } else {
          myDepth = 1 + $(this).parents('li').length;
          if (myDepth <= depth) { stillCollapsing = false; }
          if (stillCollapsing) {
            $(this).slideUp();
            // Also set any sub-options to be collapsed
            $(this).find('span.dialogue-choice-button').data('collapsed', true).text('Expand');
          }
        }
      }
    }
    function deepExpand() {
      if ($(this).is('ul')) {
        $(this).children('li').each(deepExpand);
      }
      else if ($(this).is('li')) {
        if ($(this).children('ul').length) {
          $(this).children('ul').each(deepExpand);
        } else {
          myDepth = 1 + $(this).parents('li').length;
          if (myDepth <= depth) { stillExpanding = false; }
          if (stillExpanding && myDepth == depth+1) { $(this).slideDown(); }
        }
      }
    }
 
    function chooseDialogue() {
       var $topLevelUl = $(this).parents('li').last().parent('ul');
       depth = 1 + $(this).parent('li').parents('li').length;
      //$topLevelUl.nextAll('ul').each(diveDeepUl);
      if ($(this).data('collapsed') === true) {
        $(this).data('collapsed', false);
        $(this).text('Collapse');
        stillExpanding = true;
        $topLevelUl.nextAll('ul').each(deepExpand);
      } else {
        $(this).data('collapsed', true);
        $(this).text('Expand');
        stillCollapsing = true;
        $topLevelUl.nextAll('ul').each(deepCollapse);
      }
    }
   });
   });

Revision as of 09:38, 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 social media thread */
  $(document).ready(function() {
    var depth;
    var stillCollapsing, stillExpanding;
    // Attach the click event
    $('span.dialogue-choice-button').on('click', chooseDialogue);
    // Hide initially
    $('div.social-media-thread ul').each(initialDeepHide);
  
    function initialDeepHide() {
      if ($(this).is('ul')) {
        $(this).children('li').each(initialDeepHide);
      }
      else if ($(this).is('li')) {
        if ($(this).children('ul').length) {
          $(this).children('ul').each(initialDeepHide);
        } else {
          myDepth = 1 + $(this).parents('li').length;
          if (myDepth > 1) { $(this).hide(); }
        }
      }
    }
  
    function deepCollapse() {
      if ($(this).is('ul')) {
        $(this).children('li').each(deepCollapse);
      }
      else if ($(this).is('li')) {
        if ($(this).children('ul').length) {
          $(this).children('ul').each(deepCollapse);
        } else {
          myDepth = 1 + $(this).parents('li').length;
          if (myDepth <= depth) { stillCollapsing = false; }
          if (stillCollapsing) {
            $(this).slideUp();
            // Also set any sub-options to be collapsed
            $(this).find('span.dialogue-choice-button').data('collapsed', true).text('Expand');
          }
        }
      }
    }
    function deepExpand() {
      if ($(this).is('ul')) {
        $(this).children('li').each(deepExpand);
      }
      else if ($(this).is('li')) {
        if ($(this).children('ul').length) {
          $(this).children('ul').each(deepExpand);
        } else {
          myDepth = 1 + $(this).parents('li').length;
          if (myDepth <= depth) { stillExpanding = false; }
          if (stillExpanding && myDepth == depth+1) { $(this).slideDown(); }
        }
      }
    }
  
    function chooseDialogue() {
      var $topLevelUl = $(this).parents('li').last().parent('ul');
      depth = 1 + $(this).parent('li').parents('li').length;
      //$topLevelUl.nextAll('ul').each(diveDeepUl);
      if ($(this).data('collapsed') === true) {
        $(this).data('collapsed', false);
        $(this).text('Collapse');
        stillExpanding = true;
        $topLevelUl.nextAll('ul').each(deepExpand);
      } else {
        $(this).data('collapsed', true);
        $(this).text('Expand');
        stillCollapsing = true;
        $topLevelUl.nextAll('ul').each(deepCollapse);
      }
    }
  });