Jump to content

MediaWiki:Common.js

From Grantha
Revision as of 08:53, 26 March 2026 by Chandrashekars (talk | contribs)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/* ── Verse Actions ─────────────────────────────────────────────────────── */
( function () {
    'use strict';

    /* =========================
   COPY FUNCTION + TOOLTIP
========================= */
function copyText(text, $btn) {
  function showTooltip() {
    // Remove any existing tip on this button first
    $btn.find('.copy-tooltip').remove();

    var $tip = $('<span class="copy-tooltip">Copied ✓</span>');
    $btn.css('position', 'relative').append($tip);

    // Trigger animation on next frame so transition fires
    requestAnimationFrame(function () {
      requestAnimationFrame(function () {
        $tip.addClass('copy-tooltip-visible');
      });
    });

    setTimeout(function () {
      $tip.removeClass('copy-tooltip-visible');
      // Remove after fade-out transition completes
      setTimeout(function () { $tip.remove(); }, 400);
    }, 1400);
  }

  if (navigator.clipboard && window.isSecureContext) {
    navigator.clipboard.writeText(text).then(showTooltip);
  } else {
    var $temp = $('<textarea>').val(text).appendTo('body');
    $temp[0].select();
    document.execCommand('copy');
    $temp.remove();
    showTooltip();
  }
}
    // ── Commentary toggle handler ─────────────────────────────────────────
    function handleCommentary( btn ) {
        var verseId = btn.getAttribute( 'data-verse' );
        if ( !verseId ) return;

        var block = document.getElementById( verseId );
        if ( !block ) return;

        // Toggle visibility of all commentary panels inside this verse block
        var panels = block.querySelectorAll( '.commentary-block' );
        panels.forEach( function ( panel ) {
            var isHidden = panel.style.display === 'none' || !panel.style.display;
            panel.style.display = isHidden ? '' : 'none';
        } );

        btn.classList.toggle( 'verse-action-active' );
    }

    // ── Single delegated listener on the document ─────────────────────────
    document.addEventListener( 'click', function ( e ) {
        var btn = e.target.closest( '.verse-action-btn' );
        if ( !btn ) return;

        if ( btn.classList.contains( 'verse-action-copy' ) ) {
            handleCopy( btn );
        } else if ( btn.classList.contains( 'verse-action-commentary' ) ) {
            handleCommentary( btn );
        }
    } );

}() );