MediaWiki:Common.js: Difference between revisions

From The Talos Principle Wiki
Custom hex strings
Another dialogue attempt
 
(13 intermediate revisions by the same user not shown)
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/* Any JavaScript here will be loaded for all users on every page load. */


/* Custom animated hints: */
/* Custom hex strings */
   var coll = document.getElementsByClassName("custom-collapsible");
  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 hints (cannot be nested): */
   var coll = document.getElementsByClassName("collapsible-hint");
   var i;
   var i;
    
    
Line 11: Line 30:
       if (content.style.maxHeight){
       if (content.style.maxHeight){
         content.style.maxHeight = null;
         content.style.maxHeight = null;
         content.classList.remove("custom-collapsible-content-visible");
         content.classList.remove("collapsible-hint-content-visible");
       } else {
       } else {
         content.style.maxHeight = content.scrollHeight + "px";
         content.style.maxHeight = content.scrollHeight + "px";
         content.classList.add("custom-collapsible-content-visible");
         content.classList.add("collapsible-hint-content-visible");
       }  
       }  
     });
     });
Line 20: Line 39:




/* Custom hex strings */
/* Custom collapsibles for social media threads */
document.addEventListener('DOMContentLoaded', function () {
  $(document).ready(function() {
  var hexStringElements = document.getElementsByClassName('hex-string');
    $('.collapsible-dialogue-link').on('click', function() {
  for (var i = 0; i < hexStringElements.length; i++) {
        //alert("Click!");
     hexStringElements[i].addEventListener('click', toggleHexString);
        $parentElem = $(this).closest('.collapsible-dialogue');
  }
        if ($(this).text() == 'Collapse') {
});
            // hide
            $(this).text('Choose');
            $parentElem.children('.collapsible-dialogue-content').slideUp(200);
        } else {
            // show
            $(this).text('Collapse');
            $parentElem.children('.collapsible-dialogue-content').slideDown(200);
            $parentElem.siblings('.collapsible-dialogue').find('.collapsible-dialogue-content').slideUp(200);
            $parentElem.siblings('.collapsible-dialogue').find('.collapsible-dialogue-link').text('Choose');
        }
    });
  });
 
/* Custom collapsibles for social media threads METHOD 2 */
  $(document).ready(function() {
     var depth;
    var stillCollapsing, stillExpanding;
    // Attach the click event
    $('div.dialogue-thread span.dialogue-choice-button').on('click', chooseDialogue);
    // Hide initially
    $('div.dialogue-thread ul').each(initialDeepHide);


function toggleHexString(event) {
    function initialDeepHide() {
  var element = event.currentTarget;
      if ($(this).is('ul')) {
  var hexString = element.dataset.hex;
        $(this).children('li').each(initialDeepHide);
      }
      else if ($(this).is('li')) {
        if ($(this).children('ul').length) {
          //myDepth = 1 + $(this).parents('li').length;
          //if (myDepth > 1) { $(this).css('background','yellow'); }
          //$(this).children('ul').each(initialDeepHide);
        } else {
          myDepth = 1 + $(this).parents('li').length;
          //$(this).append(" - Depth "+myDepth);
          if (myDepth > 1) { $(this).hide(); }
        }
      }
    }


  if (element.dataset.state === 'hex') {
    function deepCollapse() {
    // Switch to English
      if ($(this).is('ul')) {
    element.querySelector('.hex-string-content').innerText = hexToString(hexString);
        $(this).children('li').each(deepCollapse);
     element.dataset.state = 'text';
      }
  } else {
      else if ($(this).is('li')) {
    // Switch to Hex
        if ($(this).children('ul').length) {
    element.querySelector('.hex-string-content').innerText = hexString;
          $(this).children('ul').each(deepCollapse);
    element.dataset.state = 'hex';
        } 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 hexToString(hex) {
    function chooseDialogue() {
  var str = '';
      var $topLevelUl = $(this).parents('li').last().parent('ul');
  for (var i = 0; i < hex.length; i += 2) {
      depth = 1 + $(this).parent('li').parents('li').length;
    str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
      //$topLevelUl.nextAll('ul').each(diveDeepUl);
  }
      if ($(this).data('collapsed') === true) {
   return str;
        $(this).data('collapsed', false);
}
        $(this).text('Collapse');
        stillExpanding = true;
        //$topLevelUl.css('background', 'lightgreen');
        $topLevelUl.nextAll('ul').each(deepExpand);
      } else {
        $(this).data('collapsed', true);
        $(this).text('Expand');
        stillCollapsing = true;
        //$topLevelUl.css('background', 'pink');
        $topLevelUl.nextAll('ul').each(deepCollapse);
      }
    }
   });

Latest revision as of 19:49, 2 December 2023

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

/* 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 hints (cannot be nested): */
  var coll = document.getElementsByClassName("collapsible-hint");
  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("collapsible-hint-content-visible");
      } else {
        content.style.maxHeight = content.scrollHeight + "px";
        content.classList.add("collapsible-hint-content-visible");
      } 
    });
  }


/* Custom collapsibles for social media threads */
  $(document).ready(function() {
    $('.collapsible-dialogue-link').on('click', function() {
        //alert("Click!");
        $parentElem = $(this).closest('.collapsible-dialogue');
        if ($(this).text() == 'Collapse') {
            // hide
            $(this).text('Choose');
            $parentElem.children('.collapsible-dialogue-content').slideUp(200);
        } else {
            // show
            $(this).text('Collapse');
            $parentElem.children('.collapsible-dialogue-content').slideDown(200);
            $parentElem.siblings('.collapsible-dialogue').find('.collapsible-dialogue-content').slideUp(200);
            $parentElem.siblings('.collapsible-dialogue').find('.collapsible-dialogue-link').text('Choose');
        }
    });
  });

/* Custom collapsibles for social media threads METHOD 2 */
  $(document).ready(function() {
    var depth;
    var stillCollapsing, stillExpanding;
    // Attach the click event
    $('div.dialogue-thread span.dialogue-choice-button').on('click', chooseDialogue);
    // Hide initially
    $('div.dialogue-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) {
          //myDepth = 1 + $(this).parents('li').length;
          //if (myDepth > 1) { $(this).css('background','yellow'); }
          //$(this).children('ul').each(initialDeepHide);
        } else {
          myDepth = 1 + $(this).parents('li').length;
          //$(this).append(" - Depth "+myDepth);
          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.css('background', 'lightgreen');
        $topLevelUl.nextAll('ul').each(deepExpand);
      } else {
        $(this).data('collapsed', true);
        $(this).text('Expand');
        stillCollapsing = true;
        //$topLevelUl.css('background', 'pink');
        $topLevelUl.nextAll('ul').each(deepCollapse);
      }
    }
  });