MediaWiki:Common.js: Difference between revisions

MediaWiki interface page
No edit summary
No edit summary
Line 22: Line 22:
     images.forEach(function(image, index) {
     images.forEach(function(image, index) {
         image.addEventListener('click', function() {
         image.addEventListener('click', function() {
            clearTimeout(imageRotationTimeout); // Clear existing timeout
             setMainImage(this.innerHTML);
             setMainImage(this.innerHTML);
             lastSelectedIndex = currentIndex;
             lastSelectedIndex = currentIndex;
             currentIndex = index;
             currentIndex = index;
             resetAndStartLoadingBar();
             startImageRotation(); // Restart the rotation
         });
         });
     });
     });
Line 33: Line 34:
var currentIndex = 0;
var currentIndex = 0;
var rotationInterval = 10000; // 10 seconds
var rotationInterval = 10000; // 10 seconds
var imageRotationTimeout;
function getNextIndex(imagesLength) {
function getNextIndex(imagesLength) {
     var nextIndex;
     var nextIndex;
Line 41: Line 44:
}
}


function rotateImages() {
function startImageRotation() {
     var images = document.querySelectorAll('.carousel .dpImage');
     var images = document.querySelectorAll('.carousel .dpImage');
    currentIndex = getNextIndex(images.length);


     setInterval(function() {
     setMainImage(images[currentIndex].innerHTML);
        currentIndex = getNextIndex(images.length);
    lastSelectedIndex = currentIndex;


        setMainImage(images[currentIndex].innerHTML);
    resetAndStartLoadingBar();
        lastSelectedIndex = currentIndex;


        resetAndStartLoadingBar();
    // Schedule the next rotation
     }, rotationInterval);
     imageRotationTimeout = setTimeout(startImageRotation, rotationInterval);
}
}


Line 61: Line 64:
         loadingBar.style.transition = 'width ' + rotationInterval + 'ms linear';
         loadingBar.style.transition = 'width ' + rotationInterval + 'ms linear';
         loadingBar.style.width = '100%'; // Start the transition
         loadingBar.style.width = '100%'; // Start the transition
     }, 0); // Start on the next event loop
     }, 10); // Small delay to allow for width reset
}
}


Line 67: Line 70:
window.onload = function() {
window.onload = function() {
     addClickEventToImages();
     addClickEventToImages();
     rotateImages();
     startImageRotation(); // Start the initial rotation
    setMainImage(document.querySelector('.carousel .dpImage').innerHTML); // Set the first image as the default
    resetAndStartLoadingBar(); // Start the loading bar
};
};

Revision as of 14:10, 31 January 2024

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


var coll = document.getElementsByClassName("collapsible");
var i;

for (i = 0; i < coll.length; i++) {
  coll[i].addEventListener("click", function() {
    this.classList.toggle("active");
    var content = this.nextElementSibling;
    $(content).toggle('fast');
  });
}

function setMainImage(imageHtml) {
    var mainImageDiv = document.querySelector('.mainImage');
    mainImageDiv.innerHTML = imageHtml + '<div class="loadingBar"></div>';
}

function addClickEventToImages() {
    var images = document.querySelectorAll('.carousel .dpImage');
    images.forEach(function(image, index) {
        image.addEventListener('click', function() {
            clearTimeout(imageRotationTimeout); // Clear existing timeout
            setMainImage(this.innerHTML);
            lastSelectedIndex = currentIndex;
            currentIndex = index;
            startImageRotation(); // Restart the rotation
        });
    });
}

var lastSelectedIndex = -1;
var currentIndex = 0;
var rotationInterval = 10000; // 10 seconds
var imageRotationTimeout;

function getNextIndex(imagesLength) {
    var nextIndex;
    do {
        nextIndex = Math.floor(Math.random() * imagesLength);
    } while (nextIndex === lastSelectedIndex);
    return nextIndex;
}

function startImageRotation() {
    var images = document.querySelectorAll('.carousel .dpImage');
    currentIndex = getNextIndex(images.length);

    setMainImage(images[currentIndex].innerHTML);
    lastSelectedIndex = currentIndex;

    resetAndStartLoadingBar();

    // Schedule the next rotation
    imageRotationTimeout = setTimeout(startImageRotation, rotationInterval);
}

function resetAndStartLoadingBar() {
    var loadingBar = document.querySelector('.loadingBar');
    loadingBar.style.transition = 'none'; // Disable transition
    loadingBar.style.width = '0%'; // Reset the width immediately
    setTimeout(function() {
        loadingBar.style.transition = 'width ' + rotationInterval + 'ms linear';
        loadingBar.style.width = '100%'; // Start the transition
    }, 10); // Small delay to allow for width reset
}

// Initialize
window.onload = function() {
    addClickEventToImages();
    startImageRotation(); // Start the initial rotation
};