Jump to content

User:Udays108/common.js: Difference between revisions

From BioMicro Center
Udays108 (talk | contribs)
No edit summary
Udays108 (talk | contribs)
No edit summary
Tag: Manual revert
 
(5 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 |  Scope: User:USERNAME/common.js
  * 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.
*/


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


  var WIKI = 'https://bmcwiki.mit.edu';
   /* ── Nav definition (matches about.html exactly) ──────────── */
 
   /* Navigation — matches the mockup exactly.
    Update hrefs if your wiki uses different page titles. */
   var NAV = [
   var NAV = [
     {
     {
       label: 'About',
       label: 'About',
       href: WIKI + '/index.php/BioMicroCenter:About'
       href: 'http://biomicro.mit.edu/about.html',
      match: [ 'BioMicroCenter', 'Main_Page' ]
     },
     },
     {
     {
       label: 'News',
       label: 'News',
       href: WIKI + '/index.php/BioMicroCenter:News',
       href: 'http://biomicro.mit.edu/news.html',
       children: [
      match: [ 'News', 'Seminar' ],
         { label: 'Latest News',        href: WIKI + '/index.php/BioMicroCenter:News' },
       items: [
         { label: 'Seminars',          href: WIKI + '/index.php/BioMicroCenter:Seminars' },
         { label: 'Latest News',        href: 'http://biomicro.mit.edu/news.html' },
         { label: 'Classes & Training', href: 'https://igb.mit.edu/mini-courses', external: true }
         { label: 'Seminars',          href: 'http://biomicro.mit.edu/seminars.html' },
         { label: 'Classes & Training', href: 'https://igb.mit.edu/mini-courses', ext: true }
       ]
       ]
     },
     },
     {
     {
       label: 'Services',
       label: 'Services',
       href: WIKI + '/index.php/BioMicroCenter:Assisted_Services',
       href: 'https://bmcwiki.mit.edu/index.php/BioMicroCenter:Assisted_Services',
       children: [
       match: [ 'Assisted_Services', 'Walkup', 'Service', 'Consumable' ],
         { label: 'Walkup',       href: WIKI + '/index.php/BioMicroCenter:Walk-up_Services' },
      items: [
         { label: 'Assisted',     href: WIKI + '/index.php/BioMicroCenter:Assisted_Services' },
         { label: 'Walkup',     href: 'https://bmcwiki.mit.edu/index.php/BioMicroCenter:Walkup_Instrumentation' },
         { label: 'Consumables', href: WIKI + '/index.php/BioMicroCenter:Consumables' },
         { label: 'Assisted',   href: 'https://bmcwiki.mit.edu/index.php/BioMicroCenter:Assisted_Services' },
         { label: 'Training',     href: 'https://igb.mit.edu/mini-courses', external: true },
         { label: 'Consumables', href: 'http://biomicro.mit.edu/consumables.html' },
         { label: 'Informatics', href: 'https://igb.mit.edu/', external: true }
         { label: 'Training',   href: 'https://igb.mit.edu/mini-courses', ext: true },
         { label: 'Informatics', href: 'https://igb.mit.edu/', ext: true }
       ]
       ]
     },
     },
     {
     {
       label: 'Submission',
       label: 'Submission',
       href: WIKI + '/index.php/BioMicroCenter:Submission',
       href: 'http://biomicro.mit.edu/submit.html',
       children: [
      match: [ 'Submission', 'Submit', 'Pricing' ],
         { group: 'MIT Users' },
       items: [
         { label: 'Submit a Sample',   href: WIKI + '/index.php/BioMicroCenter:Submission' },
         { groupLabel: 'MIT Users' },
         { label: 'MIT Pricing',       href: WIKI + '/index.php/MIT:Pricing', external: true },
         { 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 },
         { divider: true },
         { group: 'Non-MIT Users' },
         { groupLabel: 'Non-MIT Users' },
         { label: 'External Submission', href: WIKI + '/index.php/BioMicroCenter:Submission' },
         { label: 'External Submission', href: 'http://biomicro.mit.edu/submit.html' },
         { label: 'External Pricing',    href: WIKI + '/index.php/BioMicroCenter:Grant_Support_%26_Pricing', highlight: true }
         { label: 'External Pricing',    href: 'http://biomicro.mit.edu/pricing_v2.html' }
       ]
       ]
     },
     },
     {
     {
       label: 'Staff',
       label: 'Staff',
       href: WIKI + '/index.php/BioMicroCenter:Staff'
       href: 'http://biomicro.mit.edu/staff.html',
      match: [ 'Staff' ]
     },
     },
     {
     {
       label: 'Resources',
       label: 'Resources',
       href: WIKI + '/index.php/BioMicroCenter:FAQ',
       href: 'https://bmcwiki.mit.edu/index.php/BioMicroCenter:FAQ',
       children: [
       match: [ 'FAQ', 'Resource', 'Form', 'Acknowledgement' ],
         { label: 'FAQs',                    href: WIKI + '/index.php/BioMicroCenter:FAQ' },
      items: [
         { label: 'Forms',                  href: WIKI + '/index.php/BioMicroCenter:Forms' },
         { label: 'FAQs',                    href: 'https://bmcwiki.mit.edu/index.php/BioMicroCenter:FAQ' },
         { label: 'Grant Support & Pricing', href: WIKI + '/index.php/BioMicroCenter:Grant_Support_%26_Pricing' },
         { label: 'Forms',                  href: 'http://biomicro.mit.edu/forms.html' },
         { label: 'Acknowledgements',        href: WIKI + '/index.php/BioMicroCenter:Acknowledgement', external: true }
         { 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 ─────────────────────────────────────── */
   /* ── Helpers ── */
   function buildTopbar() {
   function make( tag, cls ) {
     var d = document.createElement( 'div' );
     var n = document.createElement( tag );
     d.className = 'bmc-topbar';
     if ( cls ) n.className = cls;
     d.innerHTML =
    return n;
       '<div class="inner">' +
  }
 
  function isCurrentPage( href ) {
    return window.location.href.indexOf( href ) !== -1;
  }
 
 
  /* ── 1. Inject Topbar ── */
  function injectTopbar() {
    if ( document.querySelector( '.bmc-topbar' ) ) return;
    var bar = make( 'div', 'bmc-topbar' );
     bar.innerHTML =
       '<div class="bmc-inner">' +
         '<a href="mailto:biomicro@mit.edu">biomicro@mit.edu</a>' +
         '<a href="mailto:biomicro@mit.edu">biomicro@mit.edu</a>' +
         '<span class="bmc-sep">|</span>617-715-4533' +
         '<span>|</span>' +
         '<span class="bmc-sep">|</span>Building 68-322' +
        '617-715-4533' +
         '<span>|</span>' +
        'Building 68-322' +
       '</div>';
       '</div>';
     document.body.insertBefore( bar, document.body.firstChild );
     return d;
   }
   }


 
   /* ── Build nav dropdown item ───────────────────────────────── */
   /* ── 2. Build Nav HTML ── */
   function buildDropdownMenu( items ) {
   function buildNavList() {
     var menu = document.createElement( 'div' );
     var ul = make( 'ul' );
     menu.className = 'bmc-dropdown-menu';
     NAV.forEach( function ( item ) {
    items.forEach( function ( item ) {
      var li = make( 'li' );
       if ( item.divider ) {
      var active = isCurrentPage( item.href );
         var div = document.createElement( 'div' );
 
         div.className = 'bmc-divider';
       if ( item.children ) {
         menu.appendChild( div );
        li.className = 'bmc-dropdown' + ( active ? ' bmc-active' : '' );
      } else if ( item.groupLabel ) {
         var topLink = make( 'a' );
        var gl = document.createElement( 'div' );
         topLink.href = item.href;
        gl.className = 'bmc-group-label';
        topLink.textContent = item.label;
        gl.textContent = item.groupLabel;
        li.appendChild( topLink );
        menu.appendChild( gl );
 
        var menu = make( 'div', 'bmc-dropdown-menu' );
         item.children.forEach( function ( c ) {
          if ( c.divider ) {
            menu.appendChild( make( 'div', 'bmc-divider' ) );
          } else if ( c.group ) {
            var g = make( 'div', 'bmc-group-label' );
            g.textContent = c.group;
            menu.appendChild( g );
          } else {
            var a = make( 'a' );
            a.href = c.href;
            a.textContent = c.label;
            if ( c.external ) a.setAttribute( 'target', '_blank' );
            if ( c.highlight ) { a.style.fontWeight = '600'; a.style.color = '#a31f34'; }
            menu.appendChild( a );
          }
        } );
        li.appendChild( menu );
       } else {
       } else {
        li.className = active ? 'bmc-active' : '';
         var a = document.createElement( 'a' );
         var a = make( 'a' );
         a.href = item.href;
         a.href = item.href;
         a.textContent = item.label;
         a.textContent = item.label;
         li.appendChild( a );
         if ( item.ext ) a.target = '_blank';
        menu.appendChild( a );
       }
       }
      ul.appendChild( li );
     } );
     } );
     return ul;
     return menu;
   }
   }


 
   /* ── Build full header ─────────────────────────────────────── */
   /* ── 3. Inject Header ── */
   function buildHeader( currentPage ) {
   function injectHeader() {
    if ( document.querySelector( '.bmc-header' ) ) return;
 
    var header = make( 'div', 'bmc-header' );
    var inner  = make( 'div', 'bmc-header-inner' );
 
     /* Logo */
     /* Logo */
     var logo = make( 'a', 'bmc-logo' );
     var logo = document.createElement( 'a' );
     logo.href = WIKI;
    logo.href = 'https://bmcwiki.mit.edu/index.php/BioMicroCenter';
     logo.className = 'bmc-logo';
     logo.innerHTML =
     logo.innerHTML =
       '<div class="bmc-logo-mark">BMC</div>' +
       '<div class="bmc-logo-mark">BMC</div>' +
Line 155: Line 128:


     /* Nav */
     /* Nav */
     var nav = make( 'nav', 'bmc-nav' );
     var ul = document.createElement( 'ul' );
    nav.appendChild( buildNavList() );
    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 );
    } );


    /* Right side: move Vector's search + user tools here */
     var nav = document.createElement( 'nav' );
     var right = make( 'div', 'bmc-header-right' );
    nav.className = 'bmc-nav';
    nav.appendChild( ul );


     /* Grab Vector's search box and user links — move into our header */
     /* Search */
     var vectorSearch = document.querySelector( '.vector-search-box, #p-search, .mw-search-container' );
     var searchForm = document.createElement( 'form' );
     if ( vectorSearch ) right.appendChild( vectorSearch );
    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 vectorUser = document.querySelector( '.vector-user-links, #p-personal, .mw-portlet-personal' );
     var right = document.createElement( 'div' );
     if ( vectorUser ) right.appendChild( vectorUser );
    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( logo );
    inner.appendChild( nav );
     inner.appendChild( right );
     inner.appendChild( right );
    var header = document.createElement( 'header' );
    header.className = 'bmc-header';
     header.appendChild( inner );
     header.appendChild( inner );
    return header;
  }


    /* Insert after topbar */
  /* ── Build page hero ───────────────────────────────────────── */
     var topbar = document.querySelector( '.bmc-topbar' );
  function buildHero( titleText ) {
     if ( topbar ) {
     var crumb = document.createElement( 'div' );
       topbar.insertAdjacentElement( 'afterend', header );
    crumb.className = 'bmc-breadcrumb';
    } else {
     crumb.innerHTML =
       document.body.insertBefore( header, document.body.firstChild );
       '<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;


  /* ── 4. Inject Hero Banner ── */
    var inner = document.createElement( 'div' );
  function injectHero() {
     inner.className = 'inner';
     if ( document.querySelector( '.bmc-hero' ) ) return;
    inner.appendChild( crumb );
    inner.appendChild( h1 );


    /* Skip Special: pages and edit forms */
     var hero = document.createElement( 'div' );
     var ns = mw.config.get( 'wgNamespaceNumber' );
     hero.className = 'bmc-page-hero';
     if ( ns < 0 ) return;
     hero.appendChild( inner );
     if ( document.querySelector( '#editform' ) ) return;
    return hero;
  }


    /* Read title from the hidden Vector h1 */
  /* ── Build sidebar TOC from MediaWiki #toc ─────────────────── */
     var h1El = document.querySelector( 'h1.firstHeading, .mw-first-heading' );
  function buildSidebarToc( mwToc ) {
     var title = h1El ? h1El.textContent.trim() : mw.config.get( 'wgTitle' );
     var toc = document.createElement( 'div' );
     toc.className = 'bmc-toc';


     var hero = make( 'div', 'bmc-hero' );
     var h3 = document.createElement( 'h3' );
     hero.innerHTML =
     h3.textContent = 'On this page';
      '<div class="bmc-inner">' +
    toc.appendChild( h3 );
        '<div class="bmc-breadcrumb">' +
          '<a href="' + WIKI + '">Home</a>' +
          '<span class="bmc-sep">›</span>' +
          mw.html.escape( title ) +
        '</div>' +
        '<div class="bmc-hero-title">' + mw.html.escape( title ) + '</div>' +
      '</div>';


     /* Insert immediately before the page content container — safe, no element moved */
     if ( mwToc ) {
    var target = document.querySelector( '.mw-content-container' );
      /* Clone only the list from the MediaWiki TOC */
    if ( target ) {
      var mwList = mwToc.querySelector( 'ul' );
      target.parentNode.insertBefore( hero, target );
      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 ──────────────────────────────────────────── */
   /* ── 5. Inject Footer ── */
   function buildFooter() {
   function injectFooter() {
     var inner = document.createElement( 'div' );
     if ( document.querySelector( '.bmc-footer' ) ) return;
     inner.className = 'bmc-footer-inner';
 
     inner.innerHTML =
     var footer = make( 'div', 'bmc-footer' );
       '<div>' +
     footer.innerHTML =
         '<h4>MIT BioMicro Center</h4>' +
       '<div class="bmc-footer-inner">' +
        '<ul>' +
         '<div>' +
          '<li>Building 68-322</li>' +
          '<h4>MIT BioMicro Center</h4>' +
          '<li>Cambridge, MA 02139</li>' +
          '<ul>' +
          '<li><a href="mailto:biomicro@mit.edu">biomicro@mit.edu</a></li>' +
            '<li>Building 68-322</li>' +
          '<li>617-715-4533</li>' +
            '<li>Cambridge, MA 02139</li>' +
        '</ul>' +
            '<li><a href="mailto:biomicro@mit.edu">biomicro@mit.edu</a></li>' +
      '</div>' +
            '<li>617-715-4533</li>' +
      '<div>' +
          '</ul>' +
        '<h4>Services</h4>' +
        '</div>' +
        '<ul>' +
        '<div>' +
          '<li><a href="https://bmcwiki.mit.edu/index.php/BioMicroCenter:Assisted_Services">Bulk Sequencing</a></li>' +
          '<h4>Services</h4>' +
          '<li><a href="https://bmcwiki.mit.edu/index.php/BioMicroCenter:Assisted_Services">Single Cell</a></li>' +
          '<ul>' +
          '<li><a href="https://bmcwiki.mit.edu/index.php/BioMicroCenter:Walkup_Instrumentation">Spatial Genomics</a></li>' +
            '<li><a href="' + WIKI + '/index.php/BioMicroCenter:Walk-up_Services">Walk-up</a></li>' +
          '<li><a href="https://igb.mit.edu/" target="_blank">Informatics</a></li>' +
            '<li><a href="' + WIKI + '/index.php/BioMicroCenter:Assisted_Services">Assisted</a></li>' +
        '</ul>' +
            '<li><a href="' + WIKI + '/index.php/BioMicroCenter:Consumables">Consumables</a></li>' +
            '<li><a href="https://igb.mit.edu/" target="_blank">Informatics</a></li>' +
          '</ul>' +
        '</div>' +
        '<div>' +
          '<h4>Resources</h4>' +
          '<ul>' +
            '<li><a href="' + WIKI + '/index.php/BioMicroCenter:FAQ">FAQs</a></li>' +
            '<li><a href="' + WIKI + '/index.php/BioMicroCenter:Forms">Forms</a></li>' +
            '<li><a href="' + WIKI + '/index.php/BioMicroCenter:Grant_Support_%26_Pricing">Grant Support &amp; Pricing</a></li>' +
            '<li><a href="' + WIKI + '/index.php/BioMicroCenter:Acknowledgement">Acknowledgements</a></li>' +
          '</ul>' +
        '</div>' +
       '</div>' +
       '</div>' +
       '<div class="bmc-footer-bottom">' +
       '<div>' +
         '<span>&copy; ' + new Date().getFullYear() + ' MIT BioMicro Center</span>' +
         '<h4>Resources</h4>' +
        '<span><a href="https://accessibility.mit.edu" target="_blank">Accessibility</a></span>' +
        '<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>';
       '</div>';


     document.body.appendChild( footer );
     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 ───────────────────────── */
   /* ── 6. Scroll Offset Fix ── */
   function initSmoothScroll( container ) {
   function fixScrollOffset() {
     container.addEventListener( 'click', function ( e ) {
     var OFFSET = 116; /* topbar 28px + header 68px + buffer 20px */
       var a = e.target.closest( 'a[href^="#"]' );
    document.addEventListener( 'click', function ( e ) {
       if ( !a ) return;
       var anchor = e.target.closest( 'a[href^="#"]' );
       var id = decodeURIComponent( a.getAttribute( 'href' ).slice( 1 ) );
       if ( !anchor ) return;
       var target = document.getElementById( id );
       var id = anchor.getAttribute( 'href' ).slice( 1 );
       if ( target ) {
      if ( !id ) return;
        e.preventDefault();
       var target = document.getElementById( id ) ||
         target.scrollIntoView( { behavior: 'smooth', block: 'start' } );
                  document.querySelector( '[name="' + CSS.escape( id ) + '"]' );
        history.pushState( null, '', '#' + id );
       if ( !target ) return;
      }
      e.preventDefault();
      window.scrollTo( {
         top: target.getBoundingClientRect().top + window.pageYOffset - OFFSET,
        behavior: 'smooth'
      } );
      if ( history.pushState ) history.pushState( null, 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 );


  /* ── 7. Tab Panel System ── */
     var footer = buildFooter();
  function initTabs() {
    var btns  = document.querySelectorAll( '.tab-btn' );
     var panels = document.querySelectorAll( '.tab-panel' );
    if ( !btns.length ) return;


     if ( !document.querySelector( '.tab-btn.active' ) ) btns[ 0 ].classList.add( 'active' );
     /* 5. Build wrapper */
     if ( !document.querySelector( '.tab-panel.active' ) && panels[ 0 ] ) panels[ 0 ].classList.add( 'active' );
    var wrapper = document.createElement( 'div' );
    wrapper.id = 'bmc-wrapper';
    wrapper.appendChild( topbar );
    wrapper.appendChild( header );
    wrapper.appendChild( hero );
     wrapper.appendChild( layout );
    wrapper.appendChild( footer );


     btns.forEach( function ( btn ) {
     /* 6. Inject into body (prepend so it appears first) */
      btn.addEventListener( 'click', function () {
    document.body.insertBefore( wrapper, document.body.firstChild );
        btns.forEach(  function ( t ) { t.classList.remove( 'active' ); } );
 
        panels.forEach( function ( p ) { p.classList.remove( 'active' ); } );
    /* 7. Smooth scroll */
        btn.classList.add( 'active' );
    initSmoothScroll( main );
        var panel = document.getElementById( 'tab-' + btn.dataset.tab );
    initSmoothScroll( sidebar || main );
        if ( panel ) panel.classList.add( 'active' );
 
        var wrap = document.querySelector( '.tab-bar-wrap' );
    /* 8. Auto-style plain tables in content */
        if ( wrap ) window.scrollTo( { top: wrap.getBoundingClientRect().top + window.pageYOffset - 96, behavior: 'smooth' } );
    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 */


  /* ── Run ── */
    buildShell();
  injectTopbar();
   } );
  injectHeader();
  injectHero();
  injectFooter();
  fixScrollOffset();
   initTabs();


} );
} )();

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();
  } );

} )();