Jump to content

MediaWiki:Common.js: Difference between revisions

From Grantha
No edit summary
No edit summary
Tag: Reverted
Line 107: Line 107:
     }
     }
   }
   }
}() );
/* ════════════════════════════════════════════════
  Grantha — Site chrome (hide wiki UI, inject top bar)
  Edit at: yourdomain.com/wiki/MediaWiki:Common.js
  ════════════════════════════════════════════════ */
( function () {
'use strict';
/* Only run on NS_MAIN pages, skip Special pages */
if ( mw.config.get( 'wgNamespaceNumber' ) !== 0 ) return;
/* ── 1. Inject hide-chrome CSS immediately ── */
var css = [
/* Sidebar */
'#mw-navigation,#mw-panel,.mw-sidebar,.vector-sidebar-container,',
'.vector-sidebar-container-no-toc,#p-logo,.mw-portlet-logo{display:none!important}',
/* Page action tabs */
'#p-views,#p-cactions,.mw-portlet-views,.vector-page-tools,',
'.vector-page-tools-landmark,#ca-edit,#ca-ve-edit,#ca-history,',
'#ca-talk,#ca-nstab-main,.page-actions-menu,',
'.vector-page-titlebar-toc{display:none!important}',
/* User/personal tools */
'#p-personal,.vector-user-links,.vector-user-menu,',
'#pt-createaccount,#pt-login,#pt-logout,',
'.mw-portlet-personal,.vector-client-prefs-landmark{display:none!important}',
/* Header search */
'#p-search,.vector-search-box,.mw-header .cdx-search-input{display:none!important}',
/* Top header strip */
'.vector-header-start,.vector-header-end,.mw-header{display:none!important}',
/* Footer */
'#footer,.mw-footer,.vector-footer,.page-footer{display:none!important}',
/* Misc */
'#siteNotice,#localNotice,.usermessage,#catlinks,.catlinks,',
'.printfooter,.mw-indicators{display:none!important}',
/* Remove sidebar margin, full-width content */
'#mw-content-text,#content,.mw-body,.mw-body-content,',
'.vector-body,#bodyContent{margin-left:0!important;padding-left:0!important}',
'#content,#mw-content-container,.mw-page-container,',
'.vector-page-container,.mw-body{max-width:100%!important;padding:0!important;margin:0!important}',
'#mw-content-text,#bodyContent{margin-top:0!important}',
/* Top bar */
'#grantha-topbar{',
'  position:fixed;top:0;left:0;right:0;z-index:10000;height:50px;',
'  background:#1a2e40;display:flex;align-items:center;',
'  justify-content:space-between;padding:0 24px;',
'  box-shadow:0 1px 0 rgba(255,255,255,0.06);',
'  font-family:system-ui,sans-serif;',
'}',
'#grantha-topbar .gt-brand{',
'  display:flex;align-items:center;gap:10px;text-decoration:none;',
'}',
'#grantha-topbar .gt-favicon{',
'  width:22px;height:22px;border-radius:4px;object-fit:contain;',
'  filter:brightness(0) invert(1);opacity:.9;',
'}',
'#grantha-topbar .gt-name{',
'  color:#fff;font-size:15px;font-weight:700;letter-spacing:-.01em;',
'}',
'#grantha-topbar .gt-right{display:flex;align-items:center;gap:10px;}',
'#grantha-topbar .gt-btn{',
'  font-size:12.5px;padding:6px 16px;border-radius:20px;',
'  border:1.5px solid rgba(255,255,255,0.3);',
'  background:rgba(255,255,255,0.08);color:#fff;',
'  cursor:pointer;font-family:inherit;font-weight:600;',
'  transition:background .15s,border-color .15s;white-space:nowrap;',
'}',
'#grantha-topbar .gt-btn:hover{',
'  background:rgba(255,255,255,0.18);border-color:rgba(255,255,255,0.55);',
'}',
'body{padding-top:50px!important}',
'@media print{#grantha-topbar{display:none!important}body{padding-top:0!important}}'
].join( '' );
if ( !document.getElementById( 'grantha-chrome-css' ) ) {
var style = document.createElement( 'style' );
style.id = 'grantha-chrome-css';
style.textContent = css;
( document.head || document.documentElement ).appendChild( style );
}
/* ── 2. Build the top bar ── */
mw.hook( 'wikipage.content' ).add( function () {
if ( document.getElementById( 'grantha-topbar' ) ) return;
/* Hide topbar on edit pages — the editor toolbar takes over */
var action = mw.config.get( 'wgAction' );
if ( action === 'edit' || action === 'submit' ) return;
var mainPage = ( mw.config.get( 'wgArticlePath' ) || '/wiki/$1' )
.replace( '$1', encodeURIComponent(
mw.config.get( 'wgMainPageTitle' ) || 'Main_Page'
) );
var bar = document.createElement( 'div' );
bar.id = 'grantha-topbar';
bar.innerHTML =
'<a class="gt-brand" href="' + mainPage + '">' +
'  <img class="gt-favicon" src="/favicon.ico" alt="" />' +
'  <span class="gt-name">Grantha</span>' +
'</a>' +
'<div class="gt-right">' +
'  <button class="gt-btn" id="gt-newdoc">✚ New Document</button>' +
'</div>';
document.body.prepend( bar );
document.getElementById( 'gt-newdoc' ).addEventListener( 'click', function () {
/* NewPageDialog is provided by the Editor extension */
if ( window.NewPageDialog ) NewPageDialog.show();
} );
} );


}() );
}() );

Revision as of 08:55, 6 April 2026

/* =========================
   VERSE ACTIONS
========================= */
( function () {
  'use strict';

  /* ── Safe delegated listener ─────────────────────────────────────────── */
  function onDocClick( e ) {
    var target = e.target;

    // Walk up to find a matching action button
    function closest( el, cls ) {
      while ( el && el !== document ) {
        if ( el.classList && el.classList.contains( cls ) ) return el;
        el = el.parentNode;
      }
      return null;
    }

    /* ── Commentary toggle ── */
var commentBtn = closest( target, 'verse-action-commentary' );
if ( commentBtn ) {
  e.preventDefault();
  var verseId = commentBtn.getAttribute( 'data-verse' );
  if ( !verseId ) return;

  // Match all commentary bodies for this verse by id prefix
  var allBodies = document.querySelectorAll( '[id^="commentary-body-' + verseId + '"]' );
  var allBtns   = document.querySelectorAll( '.verse-action-commentary[data-verse="' + verseId + '"]' );

  var isOpen = allBodies.length && allBodies[0].classList.contains( 'open' );

  // Close all open commentaries on the page first
  document.querySelectorAll( '.commentary-body.open' ).forEach( function ( el ) {
    el.classList.remove( 'open' );
  } );
  document.querySelectorAll( '.verse-action-commentary.active' ).forEach( function ( el ) {
    el.classList.remove( 'active' );
  } );

  // If it was closed, open it
  if ( !isOpen ) {
    allBodies.forEach( function ( el ) { el.classList.add( 'open' ); } );
    allBtns.forEach(   function ( el ) { el.classList.add( 'active' ); } );
  }
  return;
}

    /* ── Copy verse — copies verse ID for crosslinking ── */
var copyBtn = closest( target, 'verse-action-copy' );
if ( copyBtn ) {
  e.preventDefault();
  var verseId = copyBtn.getAttribute( 'data-verse' );
  if ( !verseId ) return;
  copyText( verseId, copyBtn );
  return;
}
    /* ── Copy ID ── */
    var idBtn = closest( target, 'copy-id-btn' );
    if ( idBtn ) {
      e.preventDefault();
      var id = idBtn.getAttribute( 'data-copyid' );
      if ( !id ) return;
      copyText( id, idBtn );
      return;
    }
  }

  document.addEventListener( 'click', onDocClick );

  /* ── Copy helper + tooltip ───────────────────────────────────────────── */
  function copyText( text, btn ) {
    function showTooltip() {
      // Remove stale tooltip if double-clicked
      var old = btn.querySelector( '.copy-tooltip' );
      if ( old ) old.remove();

      btn.style.position = 'relative';
      var tip = document.createElement( 'span' );
      tip.className   = 'copy-tooltip';
      tip.textContent = 'Copied ✓';
      btn.appendChild( tip );

      requestAnimationFrame( function () {
        requestAnimationFrame( function () {
          tip.classList.add( 'copy-tooltip-visible' );
        } );
      } );

      setTimeout( function () {
        tip.classList.remove( 'copy-tooltip-visible' );
        setTimeout( function () { tip.remove(); }, 400 );
      }, 1400 );
    }

    if ( navigator.clipboard && window.isSecureContext ) {
      navigator.clipboard.writeText( text ).then( showTooltip );
    } else {
      var ta = document.createElement( 'textarea' );
      ta.value = text;
      ta.style.cssText = 'position:fixed;opacity:0;pointer-events:none;';
      document.body.appendChild( ta );
      ta.select();
      document.execCommand( 'copy' );
      document.body.removeChild( ta );
      showTooltip();
    }
  }

}() );
/* ════════════════════════════════════════════════
   Grantha — Site chrome (hide wiki UI, inject top bar)
   Edit at: yourdomain.com/wiki/MediaWiki:Common.js
   ════════════════════════════════════════════════ */

( function () {
	'use strict';

	/* Only run on NS_MAIN pages, skip Special pages */
	if ( mw.config.get( 'wgNamespaceNumber' ) !== 0 ) return;

	/* ── 1. Inject hide-chrome CSS immediately ── */
	var css = [
		/* Sidebar */
		'#mw-navigation,#mw-panel,.mw-sidebar,.vector-sidebar-container,',
		'.vector-sidebar-container-no-toc,#p-logo,.mw-portlet-logo{display:none!important}',
		/* Page action tabs */
		'#p-views,#p-cactions,.mw-portlet-views,.vector-page-tools,',
		'.vector-page-tools-landmark,#ca-edit,#ca-ve-edit,#ca-history,',
		'#ca-talk,#ca-nstab-main,.page-actions-menu,',
		'.vector-page-titlebar-toc{display:none!important}',
		/* User/personal tools */
		'#p-personal,.vector-user-links,.vector-user-menu,',
		'#pt-createaccount,#pt-login,#pt-logout,',
		'.mw-portlet-personal,.vector-client-prefs-landmark{display:none!important}',
		/* Header search */
		'#p-search,.vector-search-box,.mw-header .cdx-search-input{display:none!important}',
		/* Top header strip */
		'.vector-header-start,.vector-header-end,.mw-header{display:none!important}',
		/* Footer */
		'#footer,.mw-footer,.vector-footer,.page-footer{display:none!important}',
		/* Misc */
		'#siteNotice,#localNotice,.usermessage,#catlinks,.catlinks,',
		'.printfooter,.mw-indicators{display:none!important}',
		/* Remove sidebar margin, full-width content */
		'#mw-content-text,#content,.mw-body,.mw-body-content,',
		'.vector-body,#bodyContent{margin-left:0!important;padding-left:0!important}',
		'#content,#mw-content-container,.mw-page-container,',
		'.vector-page-container,.mw-body{max-width:100%!important;padding:0!important;margin:0!important}',
		'#mw-content-text,#bodyContent{margin-top:0!important}',
		/* Top bar */
		'#grantha-topbar{',
		'  position:fixed;top:0;left:0;right:0;z-index:10000;height:50px;',
		'  background:#1a2e40;display:flex;align-items:center;',
		'  justify-content:space-between;padding:0 24px;',
		'  box-shadow:0 1px 0 rgba(255,255,255,0.06);',
		'  font-family:system-ui,sans-serif;',
		'}',
		'#grantha-topbar .gt-brand{',
		'  display:flex;align-items:center;gap:10px;text-decoration:none;',
		'}',
		'#grantha-topbar .gt-favicon{',
		'  width:22px;height:22px;border-radius:4px;object-fit:contain;',
		'  filter:brightness(0) invert(1);opacity:.9;',
		'}',
		'#grantha-topbar .gt-name{',
		'  color:#fff;font-size:15px;font-weight:700;letter-spacing:-.01em;',
		'}',
		'#grantha-topbar .gt-right{display:flex;align-items:center;gap:10px;}',
		'#grantha-topbar .gt-btn{',
		'  font-size:12.5px;padding:6px 16px;border-radius:20px;',
		'  border:1.5px solid rgba(255,255,255,0.3);',
		'  background:rgba(255,255,255,0.08);color:#fff;',
		'  cursor:pointer;font-family:inherit;font-weight:600;',
		'  transition:background .15s,border-color .15s;white-space:nowrap;',
		'}',
		'#grantha-topbar .gt-btn:hover{',
		'  background:rgba(255,255,255,0.18);border-color:rgba(255,255,255,0.55);',
		'}',
		'body{padding-top:50px!important}',
		'@media print{#grantha-topbar{display:none!important}body{padding-top:0!important}}'
	].join( '' );

	if ( !document.getElementById( 'grantha-chrome-css' ) ) {
		var style = document.createElement( 'style' );
		style.id = 'grantha-chrome-css';
		style.textContent = css;
		( document.head || document.documentElement ).appendChild( style );
	}

	/* ── 2. Build the top bar ── */
	mw.hook( 'wikipage.content' ).add( function () {
		if ( document.getElementById( 'grantha-topbar' ) ) return;

		/* Hide topbar on edit pages — the editor toolbar takes over */
		var action = mw.config.get( 'wgAction' );
		if ( action === 'edit' || action === 'submit' ) return;

		var mainPage = ( mw.config.get( 'wgArticlePath' ) || '/wiki/$1' )
			.replace( '$1', encodeURIComponent(
				mw.config.get( 'wgMainPageTitle' ) || 'Main_Page'
			) );

		var bar = document.createElement( 'div' );
		bar.id = 'grantha-topbar';
		bar.innerHTML =
			'<a class="gt-brand" href="' + mainPage + '">' +
			'  <img class="gt-favicon" src="/favicon.ico" alt="" />' +
			'  <span class="gt-name">Grantha</span>' +
			'</a>' +
			'<div class="gt-right">' +
			'  <button class="gt-btn" id="gt-newdoc">✚ New Document</button>' +
			'</div>';

		document.body.prepend( bar );

		document.getElementById( 'gt-newdoc' ).addEventListener( 'click', function () {
			/* NewPageDialog is provided by the Editor extension */
			if ( window.NewPageDialog ) NewPageDialog.show();
		} );
	} );

}() );