MediaWiki:Common.js: Difference between revisions

MediaWiki interface page
No edit summary
No edit summary
Line 12: Line 12:
   });
   });
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
DONT PUKE IMAGE CAROUSEL //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


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


Line 25: Line 28:
             setMainImage(this.innerHTML);
             setMainImage(this.innerHTML);
             lastSelectedIndex = currentIndex;
             lastSelectedIndex = currentIndex;
             currentIndex = index; // Update the current index
             currentIndex = index;
             resetLoadingBar(); // Reset the loading bar on click
             resetAndStartLoadingBar();
         });
         });
     });
     });
Line 33: Line 36:
var lastSelectedIndex = -1;
var lastSelectedIndex = -1;
var currentIndex = 0;
var currentIndex = 0;
var rotationInterval = 10000; // 10 seconds
function getNextIndex(imagesLength) {
function getNextIndex(imagesLength) {
     var nextIndex;
     var nextIndex;
Line 50: Line 54:
         lastSelectedIndex = currentIndex;
         lastSelectedIndex = currentIndex;


         currentIndex = (currentIndex + 1) % images.length;
         resetAndStartLoadingBar();
     }, 10000); // Rotates every 10000 milliseconds (10 seconds)
     }, rotationInterval);
}
}


function resetLoadingBar() {
function resetAndStartLoadingBar() {
     var loadingBar = document.querySelector('.loadingBar');
     var loadingBar = document.querySelector('.loadingBar');
     loadingBar.style.width = '0%'; // Reset the width
    loadingBar.style.transition = 'none'; // Disable transition
    var intervalTime = 10000; // Same as image rotation interval
     loadingBar.style.width = '0%'; // Reset the width immediately
     setTimeout(function() { loadingBar.style.width = '100%'; }, 0);
     setTimeout(function() {
        loadingBar.style.transition = 'width ' + rotationInterval + 'ms linear';
        loadingBar.style.width = '100%'; // Start the transition
    }, 0); // Start on the next event loop
}
}


// Call the functions when the window loads
// Initialize
window.onload = function() {
window.onload = function() {
     addClickEventToImages();
     addClickEventToImages();
     rotateImages();
     rotateImages();
     setMainImage(document.querySelector('.carousel .dpImage').innerHTML); // Set the first image as the default
     setMainImage(document.querySelector('.carousel .dpImage').innerHTML); // Set the first image as the default
    resetAndStartLoadingBar(); // Start the loading bar
};
};

Revision as of 14:07, 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');
  });
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
DONT PUKE IMAGE CAROUSEL //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

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() {
            setMainImage(this.innerHTML);
            lastSelectedIndex = currentIndex;
            currentIndex = index;
            resetAndStartLoadingBar();
        });
    });
}

var lastSelectedIndex = -1;
var currentIndex = 0;
var rotationInterval = 10000; // 10 seconds
function getNextIndex(imagesLength) {
    var nextIndex;
    do {
        nextIndex = Math.floor(Math.random() * imagesLength);
    } while (nextIndex === lastSelectedIndex);
    return nextIndex;
}

function rotateImages() {
    var images = document.querySelectorAll('.carousel .dpImage');

    setInterval(function() {
        currentIndex = getNextIndex(images.length);

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

        resetAndStartLoadingBar();
    }, 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
    }, 0); // Start on the next event loop
}

// Initialize
window.onload = function() {
    addClickEventToImages();
    rotateImages();
    setMainImage(document.querySelector('.carousel .dpImage').innerHTML); // Set the first image as the default
    resetAndStartLoadingBar(); // Start the loading bar
};