Jump to content

User:Udays108/common.js: Difference between revisions

From BioMicro Center
Udays108 (talk | contribs)
 
Udays108 (talk | contribs)
No edit summary
Tag: Manual revert
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
/* ============================================================
/**
  MIT BioMicro Center — MediaWiki Custom Scripts
* BioMicro Center Wiki Full DOM Replacement
  Skin: Vector-2022
* Paste into: User:USERNAME/common.js
  Scope: User:USERNAME/common.js  (personal preview only)
*
  Promote to MediaWiki:Common.js for site-wide deployment
  * Strategy:
  ============================================================ */
* 1. Extract wiki article content from MediaWiki's DOM
* 2. Inject the complete about.html shell structure
* 3. Place article content inside the shell
* MediaWiki still runs underneath for editing, search, login, etc.
*/


mw.loader.using( [ 'mediawiki.util' ] ).then( function () {
( function () {
   'use strict';
   'use strict';


   /* ── Helpers ─────────────────────────────────────────────── */
   /* ── Nav definition (matches about.html exactly) ──────────── */
  var NAV = [
    {
      label: 'About',
      href: 'http://biomicro.mit.edu/about.html',
      match: [ 'BioMicroCenter', 'Main_Page' ]
    },
    {
      label: 'News',
      href: 'http://biomicro.mit.edu/news.html',
      match: [ 'News', 'Seminar' ],
      items: [
        { label: 'Latest News',        href: 'http://biomicro.mit.edu/news.html' },
        { label: 'Seminars',          href: 'http://biomicro.mit.edu/seminars.html' },
        { label: 'Classes & Training', href: 'https://igb.mit.edu/mini-courses', ext: true }
      ]
    },
    {
      label: 'Services',
      href: 'https://bmcwiki.mit.edu/index.php/BioMicroCenter:Assisted_Services',
      match: [ 'Assisted_Services', 'Walkup', 'Service', 'Consumable' ],
      items: [
        { label: 'Walkup',      href: 'https://bmcwiki.mit.edu/index.php/BioMicroCenter:Walkup_Instrumentation' },
        { label: 'Assisted',    href: 'https://bmcwiki.mit.edu/index.php/BioMicroCenter:Assisted_Services' },
        { label: 'Consumables', href: 'http://biomicro.mit.edu/consumables.html' },
        { label: 'Training',    href: 'https://igb.mit.edu/mini-courses', ext: true },
        { label: 'Informatics', href: 'https://igb.mit.edu/', ext: true }
      ]
    },
    {
      label: 'Submission',
      href: 'http://biomicro.mit.edu/submit.html',
      match: [ 'Submission', 'Submit', 'Pricing' ],
      items: [
        { groupLabel: 'MIT Users' },
        { label: 'Submit a Sample', href: 'http://biomicro.mit.edu/submit.html' },
        { label: 'MIT Pricing',    href: 'https://bmcwiki.mit.edu/index.php/MIT:Pricing' },
        { divider: true },
        { groupLabel: 'Non-MIT Users' },
        { label: 'External Submission', href: 'http://biomicro.mit.edu/submit.html' },
        { label: 'External Pricing',    href: 'http://biomicro.mit.edu/pricing_v2.html' }
      ]
    },
    {
      label: 'Staff',
      href: 'http://biomicro.mit.edu/staff.html',
      match: [ 'Staff' ]
    },
    {
      label: 'Resources',
      href: 'https://bmcwiki.mit.edu/index.php/BioMicroCenter:FAQ',
      match: [ 'FAQ', 'Resource', 'Form', 'Acknowledgement' ],
      items: [
        { label: 'FAQs',                    href: 'https://bmcwiki.mit.edu/index.php/BioMicroCenter:FAQ' },
        { label: 'Forms',                  href: 'http://biomicro.mit.edu/forms.html' },
        { label: 'Grant Support & Pricing', href: 'http://biomicro.mit.edu/pricing_v2.html' },
        { label: 'Acknowledgements',        href: 'https://bmcwiki.mit.edu/index.php/BioMicroCenter:Acknowledgement' }
      ]
    }
  ];
 
  /* ── Build topbar HTML ─────────────────────────────────────── */
  function buildTopbar() {
    var d = document.createElement( 'div' );
    d.className = 'bmc-topbar';
    d.innerHTML =
      '<div class="inner">' +
        '<a href="mailto:biomicro@mit.edu">biomicro@mit.edu</a>' +
        '<span>|</span>' +
        '617-715-4533' +
        '<span>|</span>' +
        'Building 68-322' +
      '</div>';
    return d;
  }


   function el( tag, props, children ) {
  /* ── Build nav dropdown item ───────────────────────────────── */
     var node = document.createElement( tag );
   function buildDropdownMenu( items ) {
     Object.keys( props || {} ).forEach( function ( k ) {
     var menu = document.createElement( 'div' );
       if ( k === 'style' && typeof props[ k ] === 'object' ) {
     menu.className = 'bmc-dropdown-menu';
         Object.assign( node.style, props[ k ] );
    items.forEach( function ( item ) {
      } else if ( k === 'className' ) {
       if ( item.divider ) {
         node.className = props[ k ];
         var div = document.createElement( 'div' );
      } else {
         div.className = 'bmc-divider';
         node.setAttribute( k, props[ k ] );
         menu.appendChild( div );
       }
       } else if ( item.groupLabel ) {
    } );
        var gl = document.createElement( 'div' );
    ( children || [] ).forEach( function ( c ) {
        gl.className = 'bmc-group-label';
      if ( typeof c === 'string' ) {
        gl.textContent = item.groupLabel;
         node.appendChild( document.createTextNode( c ) );
         menu.appendChild( gl );
       } else {
       } else {
         node.appendChild( c );
         var a = document.createElement( 'a' );
        a.href = item.href;
        a.textContent = item.label;
        if ( item.ext ) a.target = '_blank';
        menu.appendChild( a );
       }
       }
     } );
     } );
     return node;
     return menu;
   }
   }


  /* ── Build full header ─────────────────────────────────────── */
  function buildHeader( currentPage ) {
    /* Logo */
    var logo = document.createElement( 'a' );
    logo.href = 'https://bmcwiki.mit.edu/index.php/BioMicroCenter';
    logo.className = 'bmc-logo';
    logo.innerHTML =
      '<div class="bmc-logo-mark">BMC</div>' +
      '<div class="bmc-logo-text">' +
        '<span class="bmc-logo-name">MIT BioMicro Center</span>' +
        '<span class="bmc-logo-sub">Integrated Genomics Core Facility</span>' +
      '</div>';


  /* ── 1. Top Contact Bar ──────────────────────────────────── */
    /* Nav */
  /* Injects a crimson bar above the header with email/phone/location */
    var ul = document.createElement( 'ul' );
    NAV.forEach( function ( item ) {
      var li = document.createElement( 'li' );
      var isActive = ( item.match || [] ).some( function ( m ) {
        return currentPage.indexOf( m ) !== -1;
      } );
      if ( isActive ) li.classList.add( 'bmc-active' );
      if ( item.items ) li.classList.add( 'bmc-dropdown' );


  ( function injectTopbar() {
      var a = document.createElement( 'a' );
    var header = document.querySelector( '.vector-header' );
      a.href = item.href;
    if ( !header || document.querySelector( '.bmc-topbar' ) ) return;
      a.textContent = item.label;
      li.appendChild( a );


    var topbar = document.createElement( 'div' );
      if ( item.items ) {
    topbar.className = 'bmc-topbar';
        li.appendChild( buildDropdownMenu( item.items ) );
    topbar.innerHTML =
      }
       '<div class="bmc-topbar-inner">' +
       ul.appendChild( li );
        '<a href="mailto:biomicro@mit.edu">biomicro@mit.edu</a>' +
     } );
        '<span class="bmc-sep">|</span>' +
        '617-715-4533' +
        '<span class="bmc-sep">|</span>' +
        'Building 68-322' +
      '</div>';
     header.parentNode.insertBefore( topbar, header );


     /* Inject inline styles — common.css won't reach injected elements
     var nav = document.createElement( 'nav' );
      before the DOM settles on first load */
    nav.className = 'bmc-nav';
    mw.util.addCSS(
     nav.appendChild( ul );
      '.bmc-topbar{background:#a31f34;color:#fff;font-size:.75rem;' +
      'padding:6px 0;text-align:right;font-family:\'Helvetica Neue\',Arial,sans-serif}' +
      '.bmc-topbar-inner{max-width:1140px;margin:0 auto;padding:0 24px}' +
      '.bmc-topbar a{color:#fff;opacity:.85;text-decoration:none}' +
      '.bmc-topbar a:hover{opacity:1}' +
      '.bmc-sep{margin:0 6px;opacity:.5}'
     );
  }() );


    /* Search */
    var searchForm = document.createElement( 'form' );
    searchForm.className = 'bmc-search-form';
    searchForm.method = 'get';
    searchForm.action = '/index.php';
    searchForm.innerHTML =
      '<input type="hidden" name="title" value="Special:Search">' +
      '<input type="search" name="search" placeholder="Search wiki…" aria-label="Search">' +
      '<button type="submit">&#9906;</button>';


  /* ── 2. Active Navigation Highlight ─────────────────────── */
    var right = document.createElement( 'div' );
  /* Marks the sidebar nav link that matches the current wiki page */
    right.className = 'bmc-header-right';
    right.appendChild( nav );
    right.appendChild( searchForm );


  ( function markActiveNav() {
    /* Inner wrapper */
     var pageName = mw.config.get( 'wgPageName' );
     var inner = document.createElement( 'div' );
     if ( !pageName ) return;
    inner.className = 'bmc-header-inner';
    inner.appendChild( logo );
     inner.appendChild( right );


    /* Normalise: spaces → underscores, lower-case for comparison */
     var header = document.createElement( 'header' );
     var norm = pageName.replace( / /g, '_' ).toLowerCase();
     header.className = 'bmc-header';
 
    header.appendChild( inner );
     var links = document.querySelectorAll(
     return header;
      '#p-navigation .mw-list-item a, ' +
  }
      '.vector-main-menu .mw-list-item a'
     );


    links.forEach( function ( link ) {
  /* ── Build page hero ───────────────────────────────────────── */
      var href = ( link.getAttribute( 'href' ) || '' ).toLowerCase();
  function buildHero( titleText ) {
      /* Match on the last path segment (/wiki/PageName) */
    var crumb = document.createElement( 'div' );
      if ( href.indexOf( norm ) !== -1 ) {
    crumb.className = 'bmc-breadcrumb';
        var li = link.closest( 'li' );
    crumb.innerHTML =
        if ( li ) li.classList.add( 'bmc-active' );
       '<a href="http://biomicro.mit.edu">Home</a>' +
       }
      '<span class="sep">›</span>' +
    } );
      document.createTextNode( titleText ).textContent; // plain text, no XSS
  }() );


    var h1 = document.createElement( 'h1' );
    h1.textContent = titleText;


  /* ── 3. Scroll Offset Fix (sticky header) ────────────────── */
    var inner = document.createElement( 'div' );
  /* Vector-2022's sticky header is ~50–68px tall. Without this fix
    inner.className = 'inner';
    clicking a TOC anchor scrolls the heading behind the header. */
    inner.appendChild( crumb );
    inner.appendChild( h1 );


  ( function fixScrollOffset() {
    var hero = document.createElement( 'div' );
     var OFFSET = 88; /* header + topbar combined */
    hero.className = 'bmc-page-hero';
    hero.appendChild( inner );
     return hero;
  }


    document.addEventListener( 'click', function ( e ) {
  /* ── Build sidebar TOC from MediaWiki #toc ─────────────────── */
      var anchor = e.target.closest( 'a[href^="#"]' );
  function buildSidebarToc( mwToc ) {
      if ( !anchor ) return;
    var toc = document.createElement( 'div' );
    toc.className = 'bmc-toc';


      var targetId = anchor.getAttribute( 'href' ).slice( 1 );
    var h3 = document.createElement( 'h3' );
      if ( !targetId ) return;
    h3.textContent = 'On this page';
      var target = document.getElementById( targetId ) ||
    toc.appendChild( h3 );
                  document.querySelector( '[name="' + CSS.escape( targetId ) + '"]' );
      if ( !target ) return;


       e.preventDefault();
    if ( mwToc ) {
       var top = target.getBoundingClientRect().top + window.pageYOffset - OFFSET;
       /* Clone only the list from the MediaWiki TOC */
      window.scrollTo( { top: top, behavior: 'smooth' } );
      var mwList = mwToc.querySelector( 'ul' );
       if ( mwList ) {
        var cloned = mwList.cloneNode( true );
        /* Mark sub-items */
        cloned.querySelectorAll( 'li li' ).forEach( function ( li ) {
          li.classList.add( 'bmc-sub' );
        } );
        toc.appendChild( cloned );
      }
    } else {
      /* No TOC generated — leave the sidebar empty or show nothing */
      return null;
    }


      /* Update URL hash without triggering browser jump */
    var aside = document.createElement( 'aside' );
      if ( history.pushState ) {
    aside.className = 'bmc-sidebar';
        history.pushState( null, null, '#' + targetId );
    aside.appendChild( toc );
      }
     return aside;
     } );
   }
   }() );


  /* ── Build footer ──────────────────────────────────────────── */
  function buildFooter() {
    var inner = document.createElement( 'div' );
    inner.className = 'bmc-footer-inner';
    inner.innerHTML =
      '<div>' +
        '<h4>MIT BioMicro Center</h4>' +
        '<ul>' +
          '<li>Building 68-322</li>' +
          '<li>Cambridge, MA 02139</li>' +
          '<li><a href="mailto:biomicro@mit.edu">biomicro@mit.edu</a></li>' +
          '<li>617-715-4533</li>' +
        '</ul>' +
      '</div>' +
      '<div>' +
        '<h4>Services</h4>' +
        '<ul>' +
          '<li><a href="https://bmcwiki.mit.edu/index.php/BioMicroCenter:Assisted_Services">Bulk Sequencing</a></li>' +
          '<li><a href="https://bmcwiki.mit.edu/index.php/BioMicroCenter:Assisted_Services">Single Cell</a></li>' +
          '<li><a href="https://bmcwiki.mit.edu/index.php/BioMicroCenter:Walkup_Instrumentation">Spatial Genomics</a></li>' +
          '<li><a href="https://igb.mit.edu/" target="_blank">Informatics</a></li>' +
        '</ul>' +
      '</div>' +
      '<div>' +
        '<h4>Resources</h4>' +
        '<ul>' +
          '<li><a href="https://bmcwiki.mit.edu/index.php/BioMicroCenter:FAQ">FAQs</a></li>' +
          '<li><a href="http://biomicro.mit.edu/forms.html">Forms</a></li>' +
          '<li><a href="http://biomicro.mit.edu/pricing_v2.html">Grant Support &amp; Pricing</a></li>' +
          '<li><a href="https://bmcwiki.mit.edu/index.php/BioMicroCenter:Acknowledgement">Acknowledgements</a></li>' +
        '</ul>' +
      '</div>';


  /* ── 4. Tab Panel System ─────────────────────────────────── */
    var bottom = document.createElement( 'div' );
  /* Activates the .tab-btn / .tab-panel system used on the
    bottom.className = 'bmc-footer-bottom';
    pricing page. Does nothing if no .tab-btn elements exist. */
    bottom.innerHTML =
      '<span>&copy; ' + new Date().getFullYear() + ' MIT BioMicro Center</span>' +
      '<span>' +
        '<a href="https://bmcwiki.mit.edu/index.php/Special:UserLogin">Log in</a>' +
        ' &nbsp;|&nbsp; ' +
        '<a href="https://accessibility.mit.edu" target="_blank">Accessibility</a>' +
      '</span>';


  ( function initTabs() {
     var footer = document.createElement( 'footer' );
     var tabBtns = document.querySelectorAll( '.tab-btn' );
    footer.className = 'bmc-footer';
     if ( !tabBtns.length ) return;
    footer.appendChild( inner );
     footer.appendChild( bottom );
    return footer;
  }


     var tabPanels = document.querySelectorAll( '.tab-panel' );
  /* ── Smooth scroll for anchor links ───────────────────────── */
  function initSmoothScroll( container ) {
     container.addEventListener( 'click', function ( e ) {
      var a = e.target.closest( 'a[href^="#"]' );
      if ( !a ) return;
      var id = decodeURIComponent( a.getAttribute( 'href' ).slice( 1 ) );
      var target = document.getElementById( id );
      if ( target ) {
        e.preventDefault();
        target.scrollIntoView( { behavior: 'smooth', block: 'start' } );
        history.pushState( null, '', '#' + id );
      }
    } );
  }


    /* Activate first tab by default if none is active */
  /* ── Main: build and inject the shell ─────────────────────── */
    if ( !document.querySelector( '.tab-btn.active' ) ) {
  function buildShell() {
      tabBtns[ 0 ].classList.add( 'active' );
    var currentPage = mw.config.get( 'wgPageName' ) || '';
    }
    if ( !document.querySelector( '.tab-panel.active' ) ) {
      tabPanels[ 0 ] && tabPanels[ 0 ].classList.add( 'active' );
    }


     tabBtns.forEach( function ( btn ) {
     /* 1. Extract wiki content BEFORE any DOM manipulation */
      btn.addEventListener( 'click', function () {
    var mwContentText = document.getElementById( 'mw-content-text' );
        var target = btn.dataset.tab;
    if ( !mwContentText ) return; /* not a content page, bail */


        tabBtns.forEach( function ( t ) { t.classList.remove( 'active' ); } );
    var contentClone = mwContentText.cloneNode( true );
        tabPanels.forEach( function ( p ) { p.classList.remove( 'active' ); } );


        btn.classList.add( 'active' );
    /* 2. Extract page title */
        var panel = document.getElementById( 'tab-' + target );
    var titleEl = document.getElementById( 'firstHeading' ) ||
        if ( panel ) panel.classList.add( 'active' );
      document.querySelector( '.mw-first-heading' );
    var titleText = titleEl
      ? titleEl.textContent.trim()
      : ( mw.config.get( 'wgTitle' ) || 'BioMicro Center' );


        /* Scroll to tab bar, accounting for sticky header */
    /* 3. Extract MediaWiki TOC from the cloned content */
        var tabBarWrap = document.querySelector( '.tab-bar-wrap' );
    var mwToc = contentClone.querySelector( '#toc, .toc' );
        if ( tabBarWrap ) {
    if ( mwToc ) mwToc.parentNode.removeChild( mwToc ); /* remove from content body */
          var top = tabBarWrap.getBoundingClientRect().top + window.pageYOffset - 68;
          window.scrollTo( { top: top, behavior: 'smooth' } );
        }
      } );
    } );
  }() );


    /* 4. Build shell elements */
    var topbar = buildTopbar();
    var header = buildHeader( currentPage );
    var hero  = buildHero( titleText );


  /* ── 5. BMC Logo Mark ────────────────────────────────────── */
    /* Page layout grid */
  /* Injects the red square "BMC" mark next to the wiki wordmark
    var layout = document.createElement( 'div' );
    if the logo element is a plain text/image logo.             */
    layout.className = 'bmc-page-layout';


  ( function injectLogoMark() {
    /* Sidebar */
     var logoLink = document.querySelector( '.mw-logo' );
     var sidebar = buildSidebarToc( mwToc );
     if ( !logoLink || document.querySelector( '.bmc-logo-mark' ) ) return;
     if ( sidebar ) {
      layout.appendChild( sidebar );
    } else {
      /* No TOC — collapse to single-column via inline style */
      layout.style.gridTemplateColumns = '1fr';
    }


     var mark = document.createElement( 'div' );
    /* Main content area */
     mark.className = 'bmc-logo-mark';
     var main = document.createElement( 'main' );
     mark.textContent = 'BMC';
     main.className = 'bmc-content';
     main.appendChild( contentClone );
    layout.appendChild( main );


     mw.util.addCSS(
     var footer = buildFooter();
      '.bmc-logo-mark{' +
        'width:36px;height:36px;background:#a31f34;border-radius:4px;' +
        'display:flex;align-items:center;justify-content:center;' +
        'color:#fff;font-weight:700;font-size:.9rem;letter-spacing:-.5px;' +
        'flex-shrink:0;font-family:\'Helvetica Neue\',Arial,sans-serif' +
      '}' +
      '.mw-logo{display:flex!important;align-items:center!important;gap:10px!important}'
    );


     /* Prepend mark before the existing logo image/text */
     /* 5. Build wrapper */
     logoLink.insertBefore( mark, logoLink.firstChild );
     var wrapper = document.createElement( 'div' );
  }() );
    wrapper.id = 'bmc-wrapper';
    wrapper.appendChild( topbar );
    wrapper.appendChild( header );
    wrapper.appendChild( hero );
    wrapper.appendChild( layout );
    wrapper.appendChild( footer );


    /* 6. Inject into body (prepend so it appears first) */
    document.body.insertBefore( wrapper, document.body.firstChild );


  /* ── 6. Wrap Long Tables for Mobile Scroll ───────────────── */
    /* 7. Smooth scroll */
  /* Wraps .wikitable in a scrollable div so wide tables don't
    initSmoothScroll( main );
    break the layout on small screens.                        */
    initSmoothScroll( sidebar || main );


  ( function wrapTables() {
    /* 8. Auto-style plain tables in content */
     document.querySelectorAll( '.mw-parser-output .wikitable' ).forEach( function ( table ) {
     main.querySelectorAll(
       if ( table.closest( '.wikitable-wrapper' ) ) return;
      '.mw-parser-output table:not(.wikitable):not(.infobox):not(.navbox)'
      var wrapper = document.createElement( 'div' );
    ).forEach( function ( t ) {
      wrapper.className = 'wikitable-wrapper';
       t.classList.add( 'wikitable' );
      table.parentNode.insertBefore( wrapper, table );
      wrapper.appendChild( table );
     } );
     } );
  }
  /* ── Entry point ───────────────────────────────────────────── */
  mw.hook( 'wikipage.content' ).add( function () {
    /* Skip special pages (edit forms, history, etc.) to not break them */
    var ns = mw.config.get( 'wgNamespaceNumber' );
    var action = mw.config.get( 'wgAction' );
    if ( action !== 'view' ) return;
    if ( ns < 0 ) return; /* Special: pages */


     mw.util.addCSS(
     buildShell();
      '.wikitable-wrapper{overflow-x:auto;border-radius:8px;' +
   } );
      'border:1px solid #e4e4e4;margin-bottom:26px}'
    );
   }() );


} );
} )();

Latest revision as of 23:19, 8 May 2026

/**
 * BioMicro Center Wiki — Full DOM Replacement
 * Paste into: User:USERNAME/common.js
 *
 * Strategy:
 * 1. Extract wiki article content from MediaWiki's DOM
 * 2. Inject the complete about.html shell structure
 * 3. Place article content inside the shell
 * MediaWiki still runs underneath for editing, search, login, etc.
 */

( function () {
  'use strict';

  /* ── Nav definition (matches about.html exactly) ──────────── */
  var NAV = [
    {
      label: 'About',
      href: 'http://biomicro.mit.edu/about.html',
      match: [ 'BioMicroCenter', 'Main_Page' ]
    },
    {
      label: 'News',
      href: 'http://biomicro.mit.edu/news.html',
      match: [ 'News', 'Seminar' ],
      items: [
        { label: 'Latest News',        href: 'http://biomicro.mit.edu/news.html' },
        { label: 'Seminars',           href: 'http://biomicro.mit.edu/seminars.html' },
        { label: 'Classes & Training', href: 'https://igb.mit.edu/mini-courses', ext: true }
      ]
    },
    {
      label: 'Services',
      href: 'https://bmcwiki.mit.edu/index.php/BioMicroCenter:Assisted_Services',
      match: [ 'Assisted_Services', 'Walkup', 'Service', 'Consumable' ],
      items: [
        { label: 'Walkup',      href: 'https://bmcwiki.mit.edu/index.php/BioMicroCenter:Walkup_Instrumentation' },
        { label: 'Assisted',    href: 'https://bmcwiki.mit.edu/index.php/BioMicroCenter:Assisted_Services' },
        { label: 'Consumables', href: 'http://biomicro.mit.edu/consumables.html' },
        { label: 'Training',    href: 'https://igb.mit.edu/mini-courses', ext: true },
        { label: 'Informatics', href: 'https://igb.mit.edu/', ext: true }
      ]
    },
    {
      label: 'Submission',
      href: 'http://biomicro.mit.edu/submit.html',
      match: [ 'Submission', 'Submit', 'Pricing' ],
      items: [
        { groupLabel: 'MIT Users' },
        { label: 'Submit a Sample', href: 'http://biomicro.mit.edu/submit.html' },
        { label: 'MIT Pricing',     href: 'https://bmcwiki.mit.edu/index.php/MIT:Pricing' },
        { divider: true },
        { groupLabel: 'Non-MIT Users' },
        { label: 'External Submission', href: 'http://biomicro.mit.edu/submit.html' },
        { label: 'External Pricing',    href: 'http://biomicro.mit.edu/pricing_v2.html' }
      ]
    },
    {
      label: 'Staff',
      href: 'http://biomicro.mit.edu/staff.html',
      match: [ 'Staff' ]
    },
    {
      label: 'Resources',
      href: 'https://bmcwiki.mit.edu/index.php/BioMicroCenter:FAQ',
      match: [ 'FAQ', 'Resource', 'Form', 'Acknowledgement' ],
      items: [
        { label: 'FAQs',                    href: 'https://bmcwiki.mit.edu/index.php/BioMicroCenter:FAQ' },
        { label: 'Forms',                   href: 'http://biomicro.mit.edu/forms.html' },
        { label: 'Grant Support & Pricing', href: 'http://biomicro.mit.edu/pricing_v2.html' },
        { label: 'Acknowledgements',        href: 'https://bmcwiki.mit.edu/index.php/BioMicroCenter:Acknowledgement' }
      ]
    }
  ];

  /* ── Build topbar HTML ─────────────────────────────────────── */
  function buildTopbar() {
    var d = document.createElement( 'div' );
    d.className = 'bmc-topbar';
    d.innerHTML =
      '<div class="inner">' +
        '<a href="mailto:biomicro@mit.edu">biomicro@mit.edu</a>' +
        '<span>|</span>' +
        '617-715-4533' +
        '<span>|</span>' +
        'Building 68-322' +
      '</div>';
    return d;
  }

  /* ── Build nav dropdown item ───────────────────────────────── */
  function buildDropdownMenu( items ) {
    var menu = document.createElement( 'div' );
    menu.className = 'bmc-dropdown-menu';
    items.forEach( function ( item ) {
      if ( item.divider ) {
        var div = document.createElement( 'div' );
        div.className = 'bmc-divider';
        menu.appendChild( div );
      } else if ( item.groupLabel ) {
        var gl = document.createElement( 'div' );
        gl.className = 'bmc-group-label';
        gl.textContent = item.groupLabel;
        menu.appendChild( gl );
      } else {
        var a = document.createElement( 'a' );
        a.href = item.href;
        a.textContent = item.label;
        if ( item.ext ) a.target = '_blank';
        menu.appendChild( a );
      }
    } );
    return menu;
  }

  /* ── Build full header ─────────────────────────────────────── */
  function buildHeader( currentPage ) {
    /* Logo */
    var logo = document.createElement( 'a' );
    logo.href = 'https://bmcwiki.mit.edu/index.php/BioMicroCenter';
    logo.className = 'bmc-logo';
    logo.innerHTML =
      '<div class="bmc-logo-mark">BMC</div>' +
      '<div class="bmc-logo-text">' +
        '<span class="bmc-logo-name">MIT BioMicro Center</span>' +
        '<span class="bmc-logo-sub">Integrated Genomics Core Facility</span>' +
      '</div>';

    /* Nav */
    var ul = document.createElement( 'ul' );
    NAV.forEach( function ( item ) {
      var li = document.createElement( 'li' );
      var isActive = ( item.match || [] ).some( function ( m ) {
        return currentPage.indexOf( m ) !== -1;
      } );
      if ( isActive ) li.classList.add( 'bmc-active' );
      if ( item.items ) li.classList.add( 'bmc-dropdown' );

      var a = document.createElement( 'a' );
      a.href = item.href;
      a.textContent = item.label;
      li.appendChild( a );

      if ( item.items ) {
        li.appendChild( buildDropdownMenu( item.items ) );
      }
      ul.appendChild( li );
    } );

    var nav = document.createElement( 'nav' );
    nav.className = 'bmc-nav';
    nav.appendChild( ul );

    /* Search */
    var searchForm = document.createElement( 'form' );
    searchForm.className = 'bmc-search-form';
    searchForm.method = 'get';
    searchForm.action = '/index.php';
    searchForm.innerHTML =
      '<input type="hidden" name="title" value="Special:Search">' +
      '<input type="search" name="search" placeholder="Search wiki…" aria-label="Search">' +
      '<button type="submit">&#9906;</button>';

    var right = document.createElement( 'div' );
    right.className = 'bmc-header-right';
    right.appendChild( nav );
    right.appendChild( searchForm );

    /* Inner wrapper */
    var inner = document.createElement( 'div' );
    inner.className = 'bmc-header-inner';
    inner.appendChild( logo );
    inner.appendChild( right );

    var header = document.createElement( 'header' );
    header.className = 'bmc-header';
    header.appendChild( inner );
    return header;
  }

  /* ── Build page hero ───────────────────────────────────────── */
  function buildHero( titleText ) {
    var crumb = document.createElement( 'div' );
    crumb.className = 'bmc-breadcrumb';
    crumb.innerHTML =
      '<a href="http://biomicro.mit.edu">Home</a>' +
      '<span class="sep">›</span>' +
      document.createTextNode( titleText ).textContent; // plain text, no XSS

    var h1 = document.createElement( 'h1' );
    h1.textContent = titleText;

    var inner = document.createElement( 'div' );
    inner.className = 'inner';
    inner.appendChild( crumb );
    inner.appendChild( h1 );

    var hero = document.createElement( 'div' );
    hero.className = 'bmc-page-hero';
    hero.appendChild( inner );
    return hero;
  }

  /* ── Build sidebar TOC from MediaWiki #toc ─────────────────── */
  function buildSidebarToc( mwToc ) {
    var toc = document.createElement( 'div' );
    toc.className = 'bmc-toc';

    var h3 = document.createElement( 'h3' );
    h3.textContent = 'On this page';
    toc.appendChild( h3 );

    if ( mwToc ) {
      /* Clone only the list from the MediaWiki TOC */
      var mwList = mwToc.querySelector( 'ul' );
      if ( mwList ) {
        var cloned = mwList.cloneNode( true );
        /* Mark sub-items */
        cloned.querySelectorAll( 'li li' ).forEach( function ( li ) {
          li.classList.add( 'bmc-sub' );
        } );
        toc.appendChild( cloned );
      }
    } else {
      /* No TOC generated — leave the sidebar empty or show nothing */
      return null;
    }

    var aside = document.createElement( 'aside' );
    aside.className = 'bmc-sidebar';
    aside.appendChild( toc );
    return aside;
  }

  /* ── Build footer ──────────────────────────────────────────── */
  function buildFooter() {
    var inner = document.createElement( 'div' );
    inner.className = 'bmc-footer-inner';
    inner.innerHTML =
      '<div>' +
        '<h4>MIT BioMicro Center</h4>' +
        '<ul>' +
          '<li>Building 68-322</li>' +
          '<li>Cambridge, MA 02139</li>' +
          '<li><a href="mailto:biomicro@mit.edu">biomicro@mit.edu</a></li>' +
          '<li>617-715-4533</li>' +
        '</ul>' +
      '</div>' +
      '<div>' +
        '<h4>Services</h4>' +
        '<ul>' +
          '<li><a href="https://bmcwiki.mit.edu/index.php/BioMicroCenter:Assisted_Services">Bulk Sequencing</a></li>' +
          '<li><a href="https://bmcwiki.mit.edu/index.php/BioMicroCenter:Assisted_Services">Single Cell</a></li>' +
          '<li><a href="https://bmcwiki.mit.edu/index.php/BioMicroCenter:Walkup_Instrumentation">Spatial Genomics</a></li>' +
          '<li><a href="https://igb.mit.edu/" target="_blank">Informatics</a></li>' +
        '</ul>' +
      '</div>' +
      '<div>' +
        '<h4>Resources</h4>' +
        '<ul>' +
          '<li><a href="https://bmcwiki.mit.edu/index.php/BioMicroCenter:FAQ">FAQs</a></li>' +
          '<li><a href="http://biomicro.mit.edu/forms.html">Forms</a></li>' +
          '<li><a href="http://biomicro.mit.edu/pricing_v2.html">Grant Support &amp; Pricing</a></li>' +
          '<li><a href="https://bmcwiki.mit.edu/index.php/BioMicroCenter:Acknowledgement">Acknowledgements</a></li>' +
        '</ul>' +
      '</div>';

    var bottom = document.createElement( 'div' );
    bottom.className = 'bmc-footer-bottom';
    bottom.innerHTML =
      '<span>&copy; ' + new Date().getFullYear() + ' MIT BioMicro Center</span>' +
      '<span>' +
        '<a href="https://bmcwiki.mit.edu/index.php/Special:UserLogin">Log in</a>' +
        ' &nbsp;|&nbsp; ' +
        '<a href="https://accessibility.mit.edu" target="_blank">Accessibility</a>' +
      '</span>';

    var footer = document.createElement( 'footer' );
    footer.className = 'bmc-footer';
    footer.appendChild( inner );
    footer.appendChild( bottom );
    return footer;
  }

  /* ── Smooth scroll for anchor links ───────────────────────── */
  function initSmoothScroll( container ) {
    container.addEventListener( 'click', function ( e ) {
      var a = e.target.closest( 'a[href^="#"]' );
      if ( !a ) return;
      var id = decodeURIComponent( a.getAttribute( 'href' ).slice( 1 ) );
      var target = document.getElementById( id );
      if ( target ) {
        e.preventDefault();
        target.scrollIntoView( { behavior: 'smooth', block: 'start' } );
        history.pushState( null, '', '#' + id );
      }
    } );
  }

  /* ── Main: build and inject the shell ─────────────────────── */
  function buildShell() {
    var currentPage = mw.config.get( 'wgPageName' ) || '';

    /* 1. Extract wiki content BEFORE any DOM manipulation */
    var mwContentText = document.getElementById( 'mw-content-text' );
    if ( !mwContentText ) return; /* not a content page, bail */

    var contentClone = mwContentText.cloneNode( true );

    /* 2. Extract page title */
    var titleEl = document.getElementById( 'firstHeading' ) ||
      document.querySelector( '.mw-first-heading' );
    var titleText = titleEl
      ? titleEl.textContent.trim()
      : ( mw.config.get( 'wgTitle' ) || 'BioMicro Center' );

    /* 3. Extract MediaWiki TOC from the cloned content */
    var mwToc = contentClone.querySelector( '#toc, .toc' );
    if ( mwToc ) mwToc.parentNode.removeChild( mwToc ); /* remove from content body */

    /* 4. Build shell elements */
    var topbar = buildTopbar();
    var header = buildHeader( currentPage );
    var hero   = buildHero( titleText );

    /* Page layout grid */
    var layout = document.createElement( 'div' );
    layout.className = 'bmc-page-layout';

    /* Sidebar */
    var sidebar = buildSidebarToc( mwToc );
    if ( sidebar ) {
      layout.appendChild( sidebar );
    } else {
      /* No TOC — collapse to single-column via inline style */
      layout.style.gridTemplateColumns = '1fr';
    }

    /* Main content area */
    var main = document.createElement( 'main' );
    main.className = 'bmc-content';
    main.appendChild( contentClone );
    layout.appendChild( main );

    var footer = buildFooter();

    /* 5. Build wrapper */
    var wrapper = document.createElement( 'div' );
    wrapper.id = 'bmc-wrapper';
    wrapper.appendChild( topbar );
    wrapper.appendChild( header );
    wrapper.appendChild( hero );
    wrapper.appendChild( layout );
    wrapper.appendChild( footer );

    /* 6. Inject into body (prepend so it appears first) */
    document.body.insertBefore( wrapper, document.body.firstChild );

    /* 7. Smooth scroll */
    initSmoothScroll( main );
    initSmoothScroll( sidebar || main );

    /* 8. Auto-style plain tables in content */
    main.querySelectorAll(
      '.mw-parser-output table:not(.wikitable):not(.infobox):not(.navbox)'
    ).forEach( function ( t ) {
      t.classList.add( 'wikitable' );
    } );
  }

  /* ── Entry point ───────────────────────────────────────────── */
  mw.hook( 'wikipage.content' ).add( function () {
    /* Skip special pages (edit forms, history, etc.) to not break them */
    var ns = mw.config.get( 'wgNamespaceNumber' );
    var action = mw.config.get( 'wgAction' );
    if ( action !== 'view' ) return;
    if ( ns < 0 ) return; /* Special: pages */

    buildShell();
  } );

} )();