Skip to content
wiki.fftac.org

Theme - Source Excerpt 01

Back to Theme

Summary

This source excerpt preserves a bounded section of 2IA.org/wp-content/themes/twoia-intelligence/assets/js/theme.js so readers can inspect the evidence without opening the full source file.

**Source path:** 2IA.org/wp-content/themes/twoia-intelligence/assets/js/theme.js

/*
 * 2IA Intelligence theme JavaScript.
 * Vanilla JS only. No tracking, no external requests.
 */
(function () {
  'use strict';

  var prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;

  function ready(callback) {
    if (document.readyState !== 'loading') {
      callback();
      return;
    }
    document.addEventListener('DOMContentLoaded', callback);
  }

  function setupMobileMenu() {
    var toggle = document.querySelector('[data-menu-toggle]');
    var nav = document.getElementById('primary-navigation');

    if (!toggle || !nav) {
      return;
    }

    toggle.addEventListener('click', function () {
      var expanded = toggle.getAttribute('aria-expanded') === 'true';
      toggle.setAttribute('aria-expanded', expanded ? 'false' : 'true');
      document.body.classList.toggle('menu-is-open', !expanded);
    });

    nav.addEventListener('click', function (event) {
      if (event.target && event.target.tagName === 'A') {
        toggle.setAttribute('aria-expanded', 'false');
        document.body.classList.remove('menu-is-open');
      }
    });
  }

  function setupSearchOverlay() {
    var overlay = document.querySelector('[data-search-overlay]');
    var openers = document.querySelectorAll('[data-search-open]');
    var closer = document.querySelector('[data-search-close]');

    if (!overlay || !openers.length) {
      return;
    }

    var input = overlay.querySelector('input[type="search"]');
    var lastFocus = null;

    function openOverlay() {
      lastFocus = document.activeElement;
      overlay.hidden = false;
      document.body.classList.add('search-is-open');
      window.setTimeout(function () {
        if (input) {
          input.focus();
        }
      }, 20);
    }

    function closeOverlay() {
      overlay.hidden = true;
      document.body.classList.remove('search-is-open');
      if (lastFocus && typeof lastFocus.focus === 'function') {
        lastFocus.focus();
      }
    }

    openers.forEach(function (button) {
      button.addEventListener('click', openOverlay);
    });

    if (closer) {
      closer.addEventListener('click', closeOverlay);
    }

    overlay.addEventListener('click', function (event) {
      if (event.target === overlay) {
        closeOverlay();
      }
    });

    document.addEventListener('keydown', function (event) {
      if (event.key === 'Escape' && !overlay.hidden) {
        closeOverlay();
      }
    });
  }

  function setupAnnouncementBar() {
    var bar = document.querySelector('[data-announcement-bar]');

    if (!bar) {
      return;
    }

    var dismiss = bar.querySelector('[data-announcement-dismiss]');
    var id = bar.getAttribute('data-announcement-id') || 'default';
    var storageKey = 'twoia-announcement-dismissed-' + id;

    try {
      if (window.localStorage && window.localStorage.getItem(storageKey) === '1') {
        bar.hidden = true;
        return;
      }
    } catch (error) {
      // If localStorage is unavailable, the button still dismisses the banner for this page view.
    }

    if (!dismiss) {
      return;
    }

    dismiss.addEventListener('click', function () {
      bar.hidden = true;

      try {
        if (window.localStorage) {
          window.localStorage.setItem(storageKey, '1');
        }
      } catch (error) {
        return;
      }
    });
  }

  function setupCopyLink() {
    var buttons = document.querySelectorAll('[data-copy-link]');

    buttons.forEach(function (button) {
      var originalText = button.textContent;

      button.addEventListener('click', function () {
        var url = window.location.href;
        var success = button.getAttribute('data-copy-success') || 'Copied';

        if (!navigator.clipboard) {
          window.prompt('Copy this link:', url);
          return;
        }

        navigator.clipboard.writeText(url).then(function () {
          button.textContent = success;
          window.setTimeout(function () {
            button.textContent = originalText;
          }, 1600);
        }).catch(function () {
          window.prompt('Copy this link:', url);
        });
      });
    });
  }

  function setupCopyTextButtons() {
    var buttons = document.querySelectorAll('[data-copy-text]');

    buttons.forEach(function (button) {
      var originalText = button.textContent;

      button.addEventListener('click', function () {
        var text = button.getAttribute('data-copy-text') || '';
        var success = button.getAttribute('data-copy-success') || 'Copied';

        if (!text) {
          return;
        }

        if (!navigator.clipboard) {
          window.prompt('Copy this text:', text);
          return;
        }

        navigator.clipboard.writeText(text).then(function () {
          button.textContent = success;
          window.setTimeout(function () {
            button.textContent = originalText;
          }, 1600);
        }).catch(function () {
          window.prompt('Copy this text:', text);
        });
      });
    });
  }

  function uniqueId(base, index) {
    return base.toLowerCase()
      .replace(/[^a-z0-9\s-]/g, '')
      .trim()
      .replace(/\s+/g, '-') + '-' + index;
  }

  function setupTableOfContents() {
    var toc = document.querySelector('[data-toc]');

    if (!toc) {
      return;
    }

    var sourceSelector = toc.getAttribute('data-toc-source') || '.entry-content';
    var source = document.querySelector(sourceSelector);

    if (!source) {
      toc.hidden = true;
      return;
    }

    var headings = source.querySelectorAll('h2, h3');

    if (!headings.length) {
      toc.hidden = true;
      return;
    }

    var list = document.createElement('ol');
    headings.forEach(function (heading, index) {
      if (!heading.id) {
        heading.id = uniqueId(heading.textContent || 'section', index + 1);
      }

      var item = document.createElement('li');
      if (heading.tagName === 'H3') {
        item.className = 'toc-depth-3';
      }

      var link = document.createElement('a');
      link.href = '#' + heading.id;
      link.textContent = heading.textContent;
      item.appendChild(link);
      list.appendChild(item);
    });

    var placeholder = toc.querySelector('p');
    if (placeholder) {
      placeholder.remove();
    }
    toc.appendChild(list);
  }

  function setupSignalMotion() {
    if (prefersReducedMotion) {
      return;
    }

    document.documentElement.classList.add('signal-motion-enabled');
  }

  function setupWhoCaresWizard() {
    var wizards = document.querySelectorAll('[data-who-cares-wizard]');

    if (!wizards.length) {
      return;
    }

    function getChoice(data, type, value) {
      if (!data[type] || !data[type][value]) {
        return null;
      }

      return data[type][value];
    }

    function setText(root, selector, text) {
      var element = root.querySelector(selector);

      if (element) {
        element.textContent = text || '';
      }
    }

    function clearElement(element) {
      while (element.firstChild) {
        element.removeChild(element.firstChild);
      }
    }

    function renderTextList(root, selector, items) {
      var list = root.querySelector(selector);

      if (!list) {
        return;
      }

      clearElement(list);
      (items || []).forEach(function (item) {
        var listItem = document.createElement('li');
        listItem.textContent = item;
        list.appendChild(listItem);
      });
    }

    function renderLinkList(root, selector, items, external) {
      var list = root.querySelector(selector);

      if (!list) {
        return;
      }

      clearElement(list);
      (items || []).forEach(function (item) {
        var listItem = document.createElement('li');
        var link = document.createElement('a');
        link.href = item.url;
        link.textContent = item.label;

        if (external) {
          link.target = '_blank';
          link.rel = 'noopener noreferrer';
        }

        listItem.appendChild(link);
        list.appendChild(listItem);
      });
    }

    function buildPlanText(state, profile, goal, urgency, evidence) {
      var records = (profile.records || []).map(function (record) {
        return '- ' + record;
      }).join('\n');

      return [
        'Who Cares pressure map',
        'System: ' + profile.label,
        'Need: ' + goal.label,
        'Urgency: ' + urgency.label,
        'Proof: ' + evidence.label,
        '',
        'What is broken: ' + profile.broken,
        '',
        'Who cares: ' + profile.who,
        '',
        'Records to pull:',
        records,
        '',
        'Demand: ' + profile.demand,
        '',
        'First move: ' + goal.note + ' ' + urgency.note + ' ' + evidence.note,
        '',
        'Do not do this: ' + profile.avoid
      ].join('\n');
    }

    wizards.forEach(function (wizard) {
      var dataNode = wizard.querySelector('[data-who-cares-data]');
      var data;