MediaWiki:Common.js: Difference between revisions

From Sanatan Hindu Dharma
No edit summary
No edit summary
Line 566: Line 566:
});
});


// === Auto Parent Page Logic for VisualEditor & Source Editor with Autocomplete ===
mw.loader.using(['mediawiki.util', 'mediawiki.api']).then(function () {
  console.log("✅ Common.js loaded safely.");


  const api = new mw.Api();


  // --- 1️⃣ Show Reminder Banner ---
mw.loader.using(['mediawiki.util', 'mediawiki.api']).then(function () {
  mw.hook('wikipage.content').add(function ($content) {
     console.log("✅ Common.js loaded safely.");
     if (mw.config.get('wgAction') === 'edit' || mw.config.get('wgAction') === 'submit') {
      const banner = $('<div>')
        .css({
          background: '#fff8c4',
          border: '1px solid #e0c14b',
          padding: '10px',
          marginBottom: '10px',
          borderRadius: '6px',
          textAlign: 'center',
          fontWeight: '600'
        })
        .text('🪶 Tip: Remember to set a parent page before saving.');
      $content.prepend(banner);
    }
  });


  // --- 2️⃣ Parent Page Prompt with Autocomplete ---
     const api = new mw.Api();
  function askForParent(title) {
    if (window.parentPromptShown) return;
    window.parentPromptShown = true;
 
    // Overlay UI
     const overlay = $('<div>').css({
      position: 'fixed',
      top: 0, left: 0, width: '100%', height: '100%',
      background: 'rgba(0,0,0,0.5)',
      display: 'flex', justifyContent: 'center', alignItems: 'center',
      zIndex: 999999
    });
 
    const box = $('<div>').css({
      background: '#fff', padding: '20px', borderRadius: '10px',
      width: '400px', textAlign: 'center', boxShadow: '0 4px 10px rgba(0,0,0,0.3)'
    });


     box.append('<h3>Select Parent Page</h3>');
     // ============================
    const input = $('<input type="text" placeholder="Type to search existing pages...">').css({
    // 1️⃣ Category Auto-Page Prompt
      width: '100%', padding: '8px', border: '1px solid #ccc', borderRadius: '5px'
     // ============================
     });
     (function categoryAutoPage() {
     const suggestionBox = $('<ul>').css({
        // Only logged-in users
      listStyle: 'none', margin: '10px 0 0', padding: 0,
        if (!mw.config.get('wgUserName')) return;
      maxHeight: '150px', overflowY: 'auto', border: '1px solid #ddd',
      borderRadius: '5px', display: 'none', textAlign: 'left'
    });


    const confirmBtn = $('<button>Confirm</button>').css({
        // Only for Category namespace (14)
      marginTop: '10px', background: '#007bff', color: '#fff',
        if (mw.config.get('wgNamespaceNumber') !== 14) return;
      border: 'none', padding: '8px 14px', borderRadius: '5px', cursor: 'pointer'
    });


    const skipBtn = $('<button>No Parent</button>').css({
        const catTitle = mw.config.get('wgTitle');
      marginLeft: '8px', marginTop: '10px', background: '#6c757d', color: '#fff',
        const storageKey = 'category_prompt_done_' + catTitle;
      border: 'none', padding: '8px 14px', borderRadius: '5px', cursor: 'pointer'
    });


    box.append(input, suggestionBox, $('<div>').append(confirmBtn, skipBtn));
        function askAndCreate() {
    overlay.append(box);
            if (sessionStorage.getItem(storageKey)) return;
    $('body').append(overlay);
            sessionStorage.setItem(storageKey, 'done');


    // --- Autocomplete Logic ---
            const ask = confirm(
    input.on('input', function () {
                '✅ You just created the new category "' + catTitle + '".\n\n' +
      const query = input.val().trim();
                'Do you want to create a normal page with the same name? It will automatically belong to this category.'
      suggestionBox.empty();
            );
      if (query.length < 2) {
        suggestionBox.hide();
        return;
      }


      api.get({
            if (ask) {
        action: 'opensearch',
                const editUrl = mw.util.getUrl(catTitle, {
        search: query,
                    action: 'edit',
        limit: 8,
                    preloadtext: '[[Category:' + catTitle + ']]\n\n'
        namespace: 0
                });
      }).done(function (data) {
                window.location.href = editUrl;
        const results = data[1] || [];
             }
        suggestionBox.empty();
        if (results.length) {
          suggestionBox.show();
          results.forEach(function (page) {
            const li = $('<li>').text(page).css({
              padding: '6px 10px', cursor: 'pointer'
            }).hover(
              function () { $(this).css('background', '#f0f0f0'); },
              function () { $(this).css('background', ''); }
            ).click(function () {
              input.val(page);
              suggestionBox.hide();
            });
             suggestionBox.append(li);
          });
        } else {
          suggestionBox.hide();
         }
         }
      });
    });


    // --- Confirm button handler ---
        // Trigger after save (VE and SourceEditor)
    confirmBtn.on('click', function () {
        mw.hook('postEdit').add(askAndCreate);
      const parent = input.val().trim();
      if (!parent) {
        alert('Please select or type a parent page name, or click "No Parent".');
        return;
      }


      const newTitle = parent + '/' + title;
        // Fallback for VE save via URL change
      if (confirm('Create under: ' + newTitle + '?')) {
        $(window).on('popstate', function () {
        const newUrl = mw.util.getUrl(newTitle) + '?veaction=edit';
            const params = new URLSearchParams(location.search);
        console.log("🟢 Redirecting to VisualEditor:", newUrl);
            if (params.has('action') || params.has('veaction')) return;
        window.location.href = newUrl;
            if (mw.config.get('wgAction') === 'view' && !sessionStorage.getItem(storageKey)) {
      }
                api.get({
     });
                    action: 'query',
                    titles: 'Category:' + catTitle,
                    prop: 'info',
                    format: 'json'
                }).done(function (data) {
                    const pageData = Object.values(data.query.pages)[0];
                    if (pageData && pageData.missing === undefined) {
                        askAndCreate();
                    }
                });
            }
        });
     })();


     // --- Skip button handler ---
     // ============================
     skipBtn.on('click', function () {
    // 2️⃣ Parent Page Prompt
      if (confirm('Proceed without a parent page?')) {
    // ============================
         const newUrl = mw.util.getUrl(title) + '?veaction=edit';
     (function parentPagePrompt() {
        console.log("🟢 No parent selected — opening VisualEditor:", newUrl);
        // Only for main namespace (0)
        window.location.href = newUrl;
         if (mw.config.get('wgNamespaceNumber') !== 0) return;
      }
    });


    // --- Prevent accidental close ---
        function askForParent(title) {
    overlay.on('click', function (e) {
            if (window.parentPromptShown) return;
      if (e.target === overlay[0]) {
            window.parentPromptShown = true;
        alert('Please choose a parent or click "No Parent" to continue.');
      }
    });
  }


// --- 3️⃣ Detect new page creation ---
            // Overlay
function handleParentPrompt() {
            const overlay = $('<div>').css({
  const title = mw.config.get('wgPageName');
                position: 'fixed',
  const action = mw.config.get('wgAction');
                top: 0, left: 0, width: '100%', height: '100%',
  const isNewPage = mw.config.get('wgCurRevisionId') === 0;
                background: 'rgba(0,0,0,0.5)',
                display: 'flex', justifyContent: 'center', alignItems: 'center',
                zIndex: 999999
            });


  // 🚫 Skip if this is a Category page or other non-main namespaces
            const box = $('<div>').css({
  const ns = mw.config.get('wgNamespaceNumber');
                background: '#fff', padding: '20px', borderRadius: '10px',
  if (ns !== 0) { // 0 = Main namespace
                width: '400px', textAlign: 'center', boxShadow: '0 4px 10px rgba(0,0,0,0.3)'
    console.log("⚪ Skipping parent selector for non-main namespace:", ns);
            });
    return;
  }


  if (action === 'edit' && isNewPage) {
            box.append('<h3>Select Parent Page</h3>');
    console.log("🟡 New page detected, showing parent selector...");
            const input = $('<input type="text" placeholder="Type to search existing pages...">').css({
    setTimeout(function () {
                width: '100%', padding: '8px', border: '1px solid #ccc', borderRadius: '5px'
      askForParent(title);
            });
    }, 1200);
            const suggestionBox = $('<ul>').css({
  }
                listStyle: 'none', margin: '10px 0 0', padding: 0,
}
                maxHeight: '150px', overflowY: 'auto', border: '1px solid #ddd',
                borderRadius: '5px', display: 'none', textAlign: 'left'
            });


            const confirmBtn = $('<button>Confirm</button>').css({
                marginTop: '10px', background: '#007bff', color: '#fff',
                border: 'none', padding: '8px 14px', borderRadius: '5px', cursor: 'pointer'
            });


  // --- 4️⃣ Handle both editors ---
            const skipBtn = $('<button>No Parent</button>').css({
  if (mw.config.get('wgAction') === 'edit') handleParentPrompt();
                marginLeft: '8px', marginTop: '10px', background: '#6c757d', color: '#fff',
                border: 'none', padding: '8px 14px', borderRadius: '5px', cursor: 'pointer'
            });


  mw.hook('ve.activationComplete').add(function () {
            box.append(input, suggestionBox, $('<div>').append(confirmBtn, skipBtn));
    console.log("✅ VisualEditor ready, checking for new page.");
            overlay.append(box);
    handleParentPrompt();
            $('body').append(overlay);
  });
});


            // Autocomplete
            input.on('input', function () {
                const query = input.val().trim();
                suggestionBox.empty();
                if (query.length < 2) {
                    suggestionBox.hide();
                    return;
                }


                api.get({
                    action: 'opensearch',
                    search: query,
                    limit: 8,
                    namespace: 0
                }).done(function (data) {
                    const results = data[1] || [];
                    suggestionBox.empty();
                    if (results.length) {
                        suggestionBox.show();
                        results.forEach(function (page) {
                            const li = $('<li>').text(page).css({
                                padding: '6px 10px', cursor: 'pointer'
                            }).hover(
                                function () { $(this).css('background', '#f0f0f0'); },
                                function () { $(this).css('background', ''); }
                            ).click(function () {
                                input.val(page);
                                suggestionBox.hide();
                            });
                            suggestionBox.append(li);
                        });
                    } else {
                        suggestionBox.hide();
                    }
                });
            });


/* === Create same name of pages when create new category === */
            // Confirm
mw.loader.using(['mediawiki.util', 'mediawiki.api']).then(function () {
            confirmBtn.on('click', function () {
                const parent = input.val().trim();
                if (!parent) {
                    alert('Please select or type a parent page name, or click "No Parent".');
                    return;
                }


    // Only logged-in users
                const newTitle = parent + '/' + title;
    if (!mw.config.get('wgUserName')) return;
                if (confirm('Create under: ' + newTitle + '?')) {
                    const newUrl = mw.util.getUrl(newTitle) + '?veaction=edit';
                    console.log("🟢 Redirecting to VisualEditor:", newUrl);
                    window.location.href = newUrl;
                }
            });


    // Only for Category pages (namespace 14)
            // Skip
    if (mw.config.get('wgNamespaceNumber') !== 14) return;
            skipBtn.on('click', function () {
                if (confirm('Proceed without a parent page?')) {
                    const newUrl = mw.util.getUrl(title) + '?veaction=edit';
                    console.log("🟢 No parent selected — opening VisualEditor:", newUrl);
                    window.location.href = newUrl;
                }
            });


    const catTitle = mw.config.get('wgTitle');
            // Prevent accidental close
    const storageKey = 'category_prompt_done_' + catTitle;
            overlay.on('click', function (e) {
    const api = new mw.Api();
                if (e.target === overlay[0]) {
                    alert('Please choose a parent or click "No Parent" to continue.');
                }
            });
        }


    function askAndCreate() {
        function handleParentPrompt() {
        if (sessionStorage.getItem(storageKey)) return;
            const title = mw.config.get('wgPageName');
        sessionStorage.setItem(storageKey, 'done');
            const action = mw.config.get('wgAction');
            const isNewPage = mw.config.get('wgCurRevisionId') === 0;


        const ask = confirm(
            if (action === 'edit' && isNewPage) {
            '✅ You just created the new category "' + catTitle + '".\n\n' +
                console.log("🟡 New page detected, showing parent selector...");
             'Do you want to create a normal page with the same name? It will automatically belong to this category.'
                setTimeout(function () {
         );
                    askForParent(title);
                }, 1200);
             }
         }


         if (ask) {
         if (mw.config.get('wgAction') === 'edit') handleParentPrompt();
            // Open edit page with category preloaded
            const editUrl = mw.util.getUrl(catTitle, {
                action: 'edit',
                preloadtext: '[[Category:' + catTitle + ']]\n\n'
            });
            window.location.href = editUrl;
        }
    }


    // Detect after save for both VE and SourceEditor
        mw.hook('ve.activationComplete').add(function () {
    mw.hook('postEdit').add(askAndCreate);
            console.log("✅ VisualEditor ready, checking for new page.");
            handleParentPrompt();
        });


     // Detect VE save via URL change (popstate)
     })();
    $(window).on('popstate', function () {
        const params = new URLSearchParams(location.search);
        if (params.has('action') || params.has('veaction')) return;
        if (mw.config.get('wgAction') === 'view' && !sessionStorage.getItem(storageKey)) {
            api.get({
                action: 'query',
                titles: 'Category:' + catTitle,
                prop: 'info',
                format: 'json'
            }).done(function (data) {
                const pageData = Object.values(data.query.pages)[0];
                if (pageData && pageData.missing === undefined) {
                    askAndCreate();
                }
            });
        }
    });


});
});

Revision as of 14:48, 5 November 2025


mw.loader.using('mediawiki.util').done(function () {
    console.log("Common.js loaded safely");
});


/* Any JavaScript here will be loaded for all users on every page load. */

/*

document.addEventListener("DOMContentLoaded", function() {
  const btn = document.querySelector(".toggle-btn");
  const content = document.querySelector(".toggle-content");

  if (btn && content) {
    btn.addEventListener("click", function() {
      content.style.display = (content.style.display === "block") ? "none" : "block";
    });
  }
});


// Auto-add parent category when editing/creating a subpage
( function () {
    if ( mw.config.get('wgAction') !== 'edit' ) return;

    // wgTitle contains title without namespace, e.g. "Ancient-education/Subpage"
    var title = mw.config.get('wgTitle') || '';
    if ( title.indexOf('/') === -1 ) return; // not a subpage

    var parent = title.split('/')[0]; // "Ancient-education"

    // jQuery available
    $( function () {
        var textarea = $('#wpTextbox1');
        if ( !textarea.length ) return;

        // Only append if not present
        var current = textarea.val() || '';
        var catTag = '\n[[Category:' + parent + ']]';
        if ( current.indexOf(catTag.trim()) === -1 ) {
            // Insert the category at the end of the text area (preserve existing text)
            textarea.val(current + catTag);
        }
    } );
}() );

$(document).ready(function () {
  // Skip special pages
  if (mw.config.get('wgNamespaceNumber') < 0) return;

  var $content = $('#mw-content-text');

  // Fetch all page titles from the API (main namespace only)
  $.get(mw.util.wikiScript('api'), {
    action: 'query',
    list: 'allpages',
    aplimit: 'max',
    format: 'json'
  }).done(function (data) {
    var titles = data.query.allpages.map(function (p) { return p.title; });

    var html = $content.html();
    titles.forEach(function (title) {
      // Safe regex for whole words
      var safeTitle = title.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
      var regex = new RegExp('\\b(' + safeTitle + ')\\b', 'g');
      html = html.replace(regex, '<a href="/wiki/' + encodeURIComponent(title.replace(/ /g, '_')) + '">$1</a>');
    });

    $content.html(html);
  });
});






$(document).ready(function() {
    if (mw.config.get('wgNamespaceNumber') >= 0) { // Only show on normal pages
        var pageName = mw.config.get('wgPageName');
        var uploadUrl = mw.util.getUrl('Form:UploadVideo', { 'page': pageName });

        $('<div style="position:fixed; bottom:20px; right:20px; background:#007bff; color:white; padding:10px; border-radius:5px; cursor:pointer; font-weight:bold;">Upload a Video</div>')
        .click(function() {
            window.location.href = uploadUrl;
        }).appendTo('body');
    }
});
 */

/* Functions of Slider One */

(function () {
  function initSliders() {
    var sliders = document.querySelectorAll('.mw-slider');
    sliders.forEach(function (slider) {
      // Avoid double-init
      if (slider._mwSliderInited) return;
      slider._mwSliderInited = true;

      var viewport = slider.querySelector('.mw-slider-viewport');
      var track = slider.querySelector('.mw-slider-track');
      var items = Array.from(slider.querySelectorAll('.mw-slider-item'));
      var btnPrev = slider.querySelector('.mw-slider-btn.prev');
      var btnNext = slider.querySelector('.mw-slider-btn.next');

      var currentIndex = 0;
      var itemsToShow = getItemsToShow();
      var gap = parseFloat(getComputedStyle(track).columnGap || getComputedStyle(track).gap || 16);

      function getItemsToShow() {
        var w = window.innerWidth;
        if (w <= 600) return 1;
        if (w <= 900) return 2;
        return 3;
      }

      function updateSizes() {
        itemsToShow = getItemsToShow();
        // compute single item width including gap
        if (!items[0]) return;
        var itemRect = items[0].getBoundingClientRect();
        gap = parseFloat(getComputedStyle(track).columnGap || getComputedStyle(track).gap || 16);
        var single = itemRect.width + gap;
        // ensure currentIndex in range
        var maxIndex = Math.max(0, items.length - itemsToShow);
        currentIndex = Math.min(currentIndex, maxIndex);
        // apply transform
        var translateX = -currentIndex * single;
        track.style.transform = 'translateX(' + translateX + 'px)';
        updateButtons();
      }

      function updateButtons() {
        var maxIndex = Math.max(0, items.length - itemsToShow);
        if (btnPrev) btnPrev.disabled = currentIndex <= 0;
        if (btnNext) btnNext.disabled = currentIndex >= maxIndex;
      }

      function gotoIndex(index) {
        var maxIndex = Math.max(0, items.length - itemsToShow);
        currentIndex = Math.max(0, Math.min(maxIndex, index));
        updateSizes();
      }

      if (btnPrev) btnPrev.addEventListener('click', function () {
        gotoIndex(currentIndex - 1);
      });
      if (btnNext) btnNext.addEventListener('click', function () {
        gotoIndex(currentIndex + 1);
      });

      // Keyboard support
      slider.addEventListener('keydown', function (e) {
        if (e.key === 'ArrowLeft') gotoIndex(currentIndex - 1);
        if (e.key === 'ArrowRight') gotoIndex(currentIndex + 1);
      });

      // Resize handling
      var resizeTimeout;
      window.addEventListener('resize', function () {
        clearTimeout(resizeTimeout);
        resizeTimeout = setTimeout(updateSizes, 120);
      });

      // Touch / drag support
      (function () {
        var startX = 0;
        var currentTranslate = 0;
        var dragging = false;
        var pointerId = null;

        function pointerDown(e) {
          if (e.pointerType === 'mouse' && e.button !== 0) return;
          dragging = true;
          pointerId = e.pointerId;
          startX = e.clientX;
          track.style.transition = 'none';
          track.setPointerCapture && track.setPointerCapture(pointerId);
        }

        function pointerMove(e) {
          if (!dragging || e.pointerId !== pointerId) return;
          var dx = e.clientX - startX;
          var itemRect = items[0] && items[0].getBoundingClientRect();
          var single = itemRect ? (itemRect.width + gap) : 0;
          var baseTranslate = -currentIndex * single;
          track.style.transform = 'translateX(' + (baseTranslate + dx) + 'px)';
        }

        function pointerUp(e) {
          if (!dragging || e.pointerId !== pointerId) return;
          dragging = false;
          track.style.transition = '';
          var dx = e.clientX - startX;
          var threshold = Math.max(40, (items[0] ? items[0].getBoundingClientRect().width : 200) * 0.15);
          if (dx > threshold) {
            gotoIndex(currentIndex - 1);
          } else if (dx < -threshold) {
            gotoIndex(currentIndex + 1);
          } else {
            updateSizes();
          }
          try { track.releasePointerCapture(pointerId); } catch (err) {}
          pointerId = null;
        }

        track.addEventListener('pointerdown', pointerDown);
        window.addEventListener('pointermove', pointerMove);
        window.addEventListener('pointerup', pointerUp);
        track.addEventListener('pointercancel', pointerUp);
      })();

      // Initial sizes
      setTimeout(updateSizes, 60);
    });
  }

  // init on DOM ready
  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', initSliders);
  } else {
    initSliders();
  }

  // If new content is loaded via AJAX on the wiki, re-init
  if (window.mw && mw.hook) {
    mw.hook('wikipage.content').add(initSliders);
  }
})();

/* Full width Slider */
(function () {
  function initFullSliders() {
    var sliders = document.querySelectorAll('.mw-fullslider');
    sliders.forEach(function (slider) {
      if (slider._fullSliderInit) return;
      slider._fullSliderInit = true;

      var track = slider.querySelector('.mw-fullslider-track');
      var slides = Array.from(track.children);
      var btnPrev = slider.querySelector('.mw-fullslider-btn.prev');
      var btnNext = slider.querySelector('.mw-fullslider-btn.next');
      var dots = Array.from(slider.querySelectorAll('.mw-fullslider-dots .dot'));

      var current = 0;
      var slideCount = slides.length;

      function goTo(index, animate) {
        current = (index % slideCount + slideCount) % slideCount;
        var x = -current * slider.clientWidth;
        if (animate === false) track.style.transition = 'none';
        else track.style.transition = '';
        track.style.transform = 'translateX(' + x + 'px)';
        updateControls();
        if (animate === false) {
          // force reflow then restore
          void track.offsetWidth;
          track.style.transition = '';
        }
      }

      function updateControls() {
        if (btnPrev) btnPrev.disabled = false;
        if (btnNext) btnNext.disabled = false;
        // update dots
        dots.forEach(function (d) {
          d.classList.toggle('active', +d.getAttribute('data-index') === current);
          d.setAttribute('aria-pressed', (+d.getAttribute('data-index') === current).toString());
        });
      }

      if (btnPrev) btnPrev.addEventListener('click', function () { goTo(current - 1); });
      if (btnNext) btnNext.addEventListener('click', function () { goTo(current + 1); });

      dots.forEach(function (dot) {
        dot.addEventListener('click', function () {
          var idx = parseInt(this.getAttribute('data-index'), 10);
          goTo(idx);
        });
      });

      // Resize handling: ensure slide width matches viewport
      var resizeTimer;
      function onResize() {
        clearTimeout(resizeTimer);
        resizeTimer = setTimeout(function () {
          // Recompute translate in case width changed
          goTo(current, false);
        }, 80);
      }
      window.addEventListener('resize', onResize);

      // Touch / drag support
      (function () {
        var startX = 0, startTranslate = 0, dragging = false, pointerId = null;
        function down(e) {
          if (e.pointerType === 'mouse' && e.button !== 0) return;
          dragging = true;
          pointerId = e.pointerId;
          startX = e.clientX;
          var style = window.getComputedStyle(track);
          var matrix = new WebKitCSSMatrix(style.transform);
          startTranslate = matrix.m41 || 0;
          track.style.transition = 'none';
          e.target.setPointerCapture && e.target.setPointerCapture(pointerId);
        }
        function move(e) {
          if (!dragging || e.pointerId !== pointerId) return;
          var dx = e.clientX - startX;
          track.style.transform = 'translateX(' + (startTranslate + dx) + 'px)';
        }
        function up(e) {
          if (!dragging || e.pointerId !== pointerId) return;
          dragging = false;
          track.style.transition = '';
          var dx = e.clientX - startX;
          var threshold = Math.max(40, slider.clientWidth * 0.12);
          if (dx > threshold) goTo(current - 1);
          else if (dx < -threshold) goTo(current + 1);
          else goTo(current);
          try { e.target.releasePointerCapture(pointerId); } catch (err) {}
          pointerId = null;
        }
        track.addEventListener('pointerdown', down);
        window.addEventListener('pointermove', move);
        window.addEventListener('pointerup', up);
        track.addEventListener('pointercancel', up);
      })();

      // Autoplay (optional): change interval or set autoplay = false
      var autoplay = true;
      var autoplayInterval = 4500; // ms
      var autoplayTimer = null;
      function startAutoplay() {
        if (!autoplay) return;
        stopAutoplay();
        autoplayTimer = setInterval(function () { goTo(current + 1); }, autoplayInterval);
      }
      function stopAutoplay() {
        if (autoplayTimer) { clearInterval(autoplayTimer); autoplayTimer = null; }
      }
      slider.addEventListener('mouseenter', stopAutoplay);
      slider.addEventListener('mouseleave', startAutoplay);
      slider.addEventListener('focusin', stopAutoplay);
      slider.addEventListener('focusout', startAutoplay);

      // Respect prefers-reduced-motion
      var rmq = window.matchMedia('(prefers-reduced-motion: reduce)');
      if (rmq.matches) autoplay = false;

      // Ensure initial sizing: make each slide exactly slider.clientWidth
      function layoutSlides() {
        var w = slider.clientWidth;
        slides.forEach(function (s) { s.style.minWidth = w + 'px'; s.style.maxWidth = w + 'px'; });
        goTo(current, false);
      }

      // Wait for images then layout
      function imgsReady(cb) {
        var imgs = Array.from(slider.querySelectorAll('img'));
        var rem = imgs.length;
        if (!rem) return cb();
        imgs.forEach(function (img) {
          if (img.complete) { rem--; if (!rem) cb(); }
          else {
            img.addEventListener('load', function () { rem--; if (!rem) cb(); });
            img.addEventListener('error', function () { rem--; if (!rem) cb(); });
          }
        });
      }

      imgsReady(function () {
        layoutSlides();
        startAutoplay();
        window.addEventListener('resize', onResize);
      });

      // expose for debug (optional)
      slider._goTo = goTo;
    });
  }

  if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', initFullSliders);
  else initFullSliders();

  if (window.mw && mw.hook) mw.hook('wikipage.content').add(initFullSliders);
})();





 




// --- Disable Tools Option for all users except admin ---

mw.loader.using(['mediawiki.user'], function () {
    $(function () {
        mw.user.getGroups().then(function (groups) {
            // If the user is NOT an admin (sysop)
            if (groups.indexOf('sysop') === -1) {

                // Disable Tools menu and its dropdown items
                $('a, span').filter(function () {
                    return $(this).text().trim() === 'Tools';
                }).each(function () {
                    const link = $(this);
                    link.css({
                        'pointer-events': 'none',
                        'opacity': '0.5',
                        'cursor': 'not-allowed'
                    });
                    link.attr('title', 'Restricted to admins');
                    link.closest('.dropdown').find('a').css({
                        'pointer-events': 'none',
                        'opacity': '0.5',
                        'cursor': 'not-allowed'
                    });
                });

                // Disable any link to Special:SpecialPages
                $('a[href*="Special:SpecialPages"]').css({
                    'pointer-events': 'none',
                    'opacity': '0.5',
                    'cursor': 'not-allowed'
                }).attr('title', 'Restricted to admins');

                // Disable MediaWiki namespace links
                $('a[href*="MediaWiki:"]').css({
                    'pointer-events': 'none',
                    'opacity': '0.5',
                    'cursor': 'not-allowed'
                }).attr('title', 'Restricted to admins');
            }
        });
    });
});

// Collapsible Related Pages on Mobile
mw.loader.using('jquery', function () {
  $(function () {
    var header = $('.page-links-header');
    var content = $('.page-links-content');

    if (header.length && content.length) {
      header.on('click', function () {
        $(this).toggleClass('active');
        content.toggleClass('open');
      });
    }
  });
});


/* === Auto-Link Page Titles in Paragraphs (exclude header/footer) === */
(function (mw, $) {
  'use strict';

  var MAX_TITLES = 1000;

  function escapeRegExp(s) {
    return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
  }

  function run(titles) {
    if (!titles || titles.length === 0) return;

    var filtered = titles
      .filter(function (t) {
        return t && t.length > 3;
      })
      .sort(function (a, b) {
        return b.length - a.length;
      })
      .slice(0, MAX_TITLES);

    // Select only main content paragraphs (not header/footer)
    $('#content p').each(function () {
      if ($(this).find('a, code, pre, .no-auto-link').length) return;

      var html = $(this).html();

      filtered.forEach(function (title) {
        var display = title.includes(':')
          ? title.split(':').pop()
          : title;

        var pattern = display.replace(/[_-]+/g, '[ _-]');
        var re = new RegExp('\\b' + escapeRegExp(pattern) + '\\b', 'gi');

        html = html.replace(re, function (match) {
          return (
            '<a href="' +
            mw.util.getUrl(title) +
            '" class="auto-linked-page">' +
            match +
            '</a>'
          );
        });
      });

      $(this).html(html);
    });
  }

  $(function () {
    var title = mw.config.get('wgPageName');
    if (!title) return;

    $.getJSON(
      mw.util.wikiScript() +
        '?title=Special:AutoLinkTitles&format=json&pageTitle=' +
        encodeURIComponent(title)
    )
      .done(function (data) {
        if (data && Array.isArray(data.titles)) {
          run(data.titles);
        }
      })
      .fail(function () {
        console.warn('AutoLinkTitles: failed to load title list.');
      });
  });
})(mediaWiki, jQuery);












// == Showing Template as default footer

$(document).ready(function () {

  // Remove Chameleon’s default footer
  $('.footercontainer, #footer, #mw-footer').remove();

  // Prevent duplicate inserts
  if ($('#mw-custom-footer').length) return;

  // Load your custom footer
  $.ajax({
    url: '/index.php?title=Template:Custom-footer&action=render',
    type: 'GET',
    success: function (data) {
      const footer = $('<div id="mw-custom-footer"></div>').html(data);
      $('body').append(footer);
    },
    error: function () {
      console.warn('⚠️ Custom footer could not be loaded.');
    }
  });

});



mw.loader.using(['mediawiki.util', 'mediawiki.api']).then(function () {
    console.log("✅ Common.js loaded safely.");

    const api = new mw.Api();

    // ============================
    // 1️⃣ Category Auto-Page Prompt
    // ============================
    (function categoryAutoPage() {
        // Only logged-in users
        if (!mw.config.get('wgUserName')) return;

        // Only for Category namespace (14)
        if (mw.config.get('wgNamespaceNumber') !== 14) return;

        const catTitle = mw.config.get('wgTitle');
        const storageKey = 'category_prompt_done_' + catTitle;

        function askAndCreate() {
            if (sessionStorage.getItem(storageKey)) return;
            sessionStorage.setItem(storageKey, 'done');

            const ask = confirm(
                '✅ You just created the new category "' + catTitle + '".\n\n' +
                'Do you want to create a normal page with the same name? It will automatically belong to this category.'
            );

            if (ask) {
                const editUrl = mw.util.getUrl(catTitle, {
                    action: 'edit',
                    preloadtext: '[[Category:' + catTitle + ']]\n\n'
                });
                window.location.href = editUrl;
            }
        }

        // Trigger after save (VE and SourceEditor)
        mw.hook('postEdit').add(askAndCreate);

        // Fallback for VE save via URL change
        $(window).on('popstate', function () {
            const params = new URLSearchParams(location.search);
            if (params.has('action') || params.has('veaction')) return;
            if (mw.config.get('wgAction') === 'view' && !sessionStorage.getItem(storageKey)) {
                api.get({
                    action: 'query',
                    titles: 'Category:' + catTitle,
                    prop: 'info',
                    format: 'json'
                }).done(function (data) {
                    const pageData = Object.values(data.query.pages)[0];
                    if (pageData && pageData.missing === undefined) {
                        askAndCreate();
                    }
                });
            }
        });
    })();

    // ============================
    // 2️⃣ Parent Page Prompt
    // ============================
    (function parentPagePrompt() {
        // Only for main namespace (0)
        if (mw.config.get('wgNamespaceNumber') !== 0) return;

        function askForParent(title) {
            if (window.parentPromptShown) return;
            window.parentPromptShown = true;

            // Overlay
            const overlay = $('<div>').css({
                position: 'fixed',
                top: 0, left: 0, width: '100%', height: '100%',
                background: 'rgba(0,0,0,0.5)',
                display: 'flex', justifyContent: 'center', alignItems: 'center',
                zIndex: 999999
            });

            const box = $('<div>').css({
                background: '#fff', padding: '20px', borderRadius: '10px',
                width: '400px', textAlign: 'center', boxShadow: '0 4px 10px rgba(0,0,0,0.3)'
            });

            box.append('<h3>Select Parent Page</h3>');
            const input = $('<input type="text" placeholder="Type to search existing pages...">').css({
                width: '100%', padding: '8px', border: '1px solid #ccc', borderRadius: '5px'
            });
            const suggestionBox = $('<ul>').css({
                listStyle: 'none', margin: '10px 0 0', padding: 0,
                maxHeight: '150px', overflowY: 'auto', border: '1px solid #ddd',
                borderRadius: '5px', display: 'none', textAlign: 'left'
            });

            const confirmBtn = $('<button>Confirm</button>').css({
                marginTop: '10px', background: '#007bff', color: '#fff',
                border: 'none', padding: '8px 14px', borderRadius: '5px', cursor: 'pointer'
            });

            const skipBtn = $('<button>No Parent</button>').css({
                marginLeft: '8px', marginTop: '10px', background: '#6c757d', color: '#fff',
                border: 'none', padding: '8px 14px', borderRadius: '5px', cursor: 'pointer'
            });

            box.append(input, suggestionBox, $('<div>').append(confirmBtn, skipBtn));
            overlay.append(box);
            $('body').append(overlay);

            // Autocomplete
            input.on('input', function () {
                const query = input.val().trim();
                suggestionBox.empty();
                if (query.length < 2) {
                    suggestionBox.hide();
                    return;
                }

                api.get({
                    action: 'opensearch',
                    search: query,
                    limit: 8,
                    namespace: 0
                }).done(function (data) {
                    const results = data[1] || [];
                    suggestionBox.empty();
                    if (results.length) {
                        suggestionBox.show();
                        results.forEach(function (page) {
                            const li = $('<li>').text(page).css({
                                padding: '6px 10px', cursor: 'pointer'
                            }).hover(
                                function () { $(this).css('background', '#f0f0f0'); },
                                function () { $(this).css('background', ''); }
                            ).click(function () {
                                input.val(page);
                                suggestionBox.hide();
                            });
                            suggestionBox.append(li);
                        });
                    } else {
                        suggestionBox.hide();
                    }
                });
            });

            // Confirm
            confirmBtn.on('click', function () {
                const parent = input.val().trim();
                if (!parent) {
                    alert('Please select or type a parent page name, or click "No Parent".');
                    return;
                }

                const newTitle = parent + '/' + title;
                if (confirm('Create under: ' + newTitle + '?')) {
                    const newUrl = mw.util.getUrl(newTitle) + '?veaction=edit';
                    console.log("🟢 Redirecting to VisualEditor:", newUrl);
                    window.location.href = newUrl;
                }
            });

            // Skip
            skipBtn.on('click', function () {
                if (confirm('Proceed without a parent page?')) {
                    const newUrl = mw.util.getUrl(title) + '?veaction=edit';
                    console.log("🟢 No parent selected — opening VisualEditor:", newUrl);
                    window.location.href = newUrl;
                }
            });

            // Prevent accidental close
            overlay.on('click', function (e) {
                if (e.target === overlay[0]) {
                    alert('Please choose a parent or click "No Parent" to continue.');
                }
            });
        }

        function handleParentPrompt() {
            const title = mw.config.get('wgPageName');
            const action = mw.config.get('wgAction');
            const isNewPage = mw.config.get('wgCurRevisionId') === 0;

            if (action === 'edit' && isNewPage) {
                console.log("🟡 New page detected, showing parent selector...");
                setTimeout(function () {
                    askForParent(title);
                }, 1200);
            }
        }

        if (mw.config.get('wgAction') === 'edit') handleParentPrompt();

        mw.hook('ve.activationComplete').add(function () {
            console.log("✅ VisualEditor ready, checking for new page.");
            handleParentPrompt();
        });

    })();

});