MediaWiki:Common.js: Difference between revisions

mNo edit summary
mNo edit summary
 
(5 intermediate revisions by the same user not shown)
Line 116: Line 116:
mw.hook('wikipage.content').add(function () {
mw.hook('wikipage.content').add(function () {


   const hash = location.hash.replace('#','');
   if (!location.hash) return;
   if (!hash) return;
 
   const id = location.hash.slice(1);


   setTimeout(function(){
   setTimeout(function(){


     const item = document.querySelector('.ooo-browser__item[data-target="'+hash+'"]');
     const item = document.querySelector('.ooo-browser__item[data-target="'+id+'"]');
     const panel = document.getElementById(hash);
     const panel = document.getElementById(id);


     if (!item || !panel) return;
     if (!item || !panel) return;
Line 129: Line 130:
     if (!browser) return;
     if (!browser) return;


    // activate correct item
     browser.querySelectorAll('.ooo-browser__item')
     browser.querySelectorAll('.ooo-browser__item')
       .forEach(el => el.classList.remove('is-active'));
       .forEach(el => el.classList.remove('is-active'));


    item.classList.add('is-active');
    // activate correct panel
     browser.querySelectorAll('.ooo-browser__info')
     browser.querySelectorAll('.ooo-browser__info')
       .forEach(el => el.classList.remove('is-active'));
       .forEach(el => el.classList.remove('is-active'));


    item.classList.add('is-active');
     panel.classList.add('is-active');
     panel.classList.add('is-active');


    // scroll to panel
  },150);
    panel.scrollIntoView({ block:'start', behavior:'instant' });
 
});
 
/* =========================================================
  OOO Browser controller (click switching)
  ========================================================= */
 
document.addEventListener("click", function (e) {
 
  const item = e.target.closest(".ooo-browser__item");
  if (!item) return;
 
  const browser = item.closest(".ooo-browser");
  if (!browser) return;
 
  const target = item.getAttribute("data-target");
  if (!target) return;
 
  e.preventDefault();
 
  browser.querySelectorAll(".ooo-browser__item")
    .forEach(el => el.classList.remove("is-active"));
 
  browser.querySelectorAll(".ooo-browser__info")
    .forEach(el => el.classList.remove("is-active"));
 
  item.classList.add("is-active");
 
const panel = browser.querySelector("#" + CSS.escape(target));
if (panel) panel.classList.add("is-active");


  }, 200);
/* update browser URL */
history.replaceState(null, null, "#" + target);


});
});