MediaWiki:Common.js: Difference between revisions
From Sanatan Hindu Dharma
No edit summary Tag: Manual revert |
No edit summary |
||
| Line 14: | Line 14: | ||
}); | }); | ||
*/ | */ | ||
/* 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); | |||
} | |||
})(); | |||
Revision as of 09:53, 14 October 2025
/* Any JavaScript here will be loaded for all users on every page load. */
/*
$(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);
}
})();
