"മീഡിയവിക്കി:Common.js" എന്ന താളിന്റെ പതിപ്പുകൾ തമ്മിലുള്ള വ്യത്യാസം
No edit summary |
optimize js |
||
| (2 ഉപയോക്താക്കൾ ചെയ്ത ഇടയ്ക്കുള്ള 17 നാൾപ്പതിപ്പുകൾ പ്രദർശിപ്പിക്കുന്നില്ല) | |||
| വരി 1: | വരി 1: | ||
//mw.loader.load('//en.wikipedia.org/w/index.php?title=User:Joeytje50/JWB.js/load.js&action=raw&ctype=text/javascript'); | |||
/* ============================================================ | /* ============================================================ | ||
OpenStreetMap marker URL fix | OpenStreetMap marker URL fix | ||
============================================================ */ | ============================================================ */ | ||
window.addEventListener('load', function () { | |||
document.querySelectorAll('img').forEach(function (img) { | |||
if (img.src && img.src.startsWith('https://tile.openstreetmap.org/v4/marker/')) { | |||
if (img.src && | |||
img.src = img.src.replace( | img.src = img.src.replace( | ||
'https://tile.openstreetmap.org/v4/marker/', | 'https://tile.openstreetmap.org/v4/marker/', | ||
| വരി 13: | വരി 12: | ||
); | ); | ||
} | } | ||
} | }); | ||
}); | }); | ||
/* ============================================================ | /* ============================================================ | ||
Wikimedia Commons Gallery Loader | Wikimedia Commons Gallery Loader | ||
============================================================ */ | ============================================================ */ | ||
mw.hook('wikipage.content').add(function ($content) { | mw.hook('wikipage.content').add(function ($content) { | ||
| വരി 23: | വരി 22: | ||
$content.find('.commons-gallery').each(function () { | $content.find('.commons-gallery').each(function () { | ||
var container = this; | var container = this; | ||
var sources = container.querySelectorAll('.commons-gallery-source'); | |||
if (!sources.length) return; | |||
var width = 300; | |||
var width = | |||
var grid = document.createElement('div'); | var grid = document.createElement('div'); | ||
grid.className = 'commons-gallery-grid'; | grid.className = 'commons-gallery-grid'; | ||
container.appendChild(grid); | container.appendChild(grid); | ||
sources.forEach(function (srcEl) { | |||
var filename = srcEl.dataset.file; | |||
var captionText = srcEl.dataset.caption || ''; | |||
if (!filename) return; | |||
var apiUrl = | var apiUrl = | ||
| വരി 59: | വരി 42: | ||
'&iiprop=url|extmetadata' + | '&iiprop=url|extmetadata' + | ||
'&iiurlwidth=' + width + | '&iiurlwidth=' + width + | ||
'&titles=File:' + encodeURIComponent( | '&titles=File:' + encodeURIComponent(filename) + | ||
'&origin=*'; | '&origin=*'; | ||
fetch(apiUrl) | fetch(apiUrl) | ||
.then(function (r) { return r.json(); }) | .then(function (r) { | ||
if (!r.ok) throw new Error('HTTP ' + r.status); | |||
return r.json(); | |||
}) | |||
.then(function (data) { | .then(function (data) { | ||
if (!data || !data.query || !data.query.pages) return; | |||
if (!data.query || !data.query.pages) return; | var page = data.query.pages[Object.keys(data.query.pages)[0]]; | ||
if (!page || !page.imageinfo) return; | |||
var page = | |||
if (!page.imageinfo) return; | |||
var info = page.imageinfo[0]; | var info = page.imageinfo[0]; | ||
var imgUrl = info.thumburl || info.url; | var imgUrl = info.thumburl || info.url; | ||
var meta = info.extmetadata || {}; | |||
var artist = (meta.Artist && meta.Artist.value) || 'Unknown'; | |||
var license = (meta.LicenseShortName && meta.LicenseShortName.value) || ''; | |||
var licenseUrl = (meta.LicenseUrl && meta.LicenseUrl.value) || ''; | |||
var commonsUrl = info.descriptionurl || ''; | |||
/* Prefer responsive URL for high-DPI screens */ | |||
var | var lightboxSrc = info.url; | ||
if (info.responsiveUrls) { | |||
if (window.devicePixelRatio > 1 && info.responsiveUrls['2']) { | |||
if (info. | lightboxSrc = info.responsiveUrls['2']; | ||
if ( | } else if (info.responsiveUrls['1.5']) { | ||
lightboxSrc = info.responsiveUrls['1.5']; | |||
} | |||
} | } | ||
} | } | ||
var captionHtml = | |||
(captionText ? captionText + '<br>' : '') + | |||
'© ' + artist + | |||
(licenseUrl | |||
? ' – <a href="' + licenseUrl + '" target="_blank" rel="noopener noreferrer">' + license + '</a>' | |||
: '') + | |||
(commonsUrl | |||
? '<br><a href="' + commonsUrl + '" target="_blank" rel="noopener noreferrer">View on Wikimedia Commons</a>' | |||
: ''); | |||
var figure = document.createElement('figure'); | var figure = document.createElement('figure'); | ||
| വരി 97: | വരി 88: | ||
var img = document.createElement('img'); | var img = document.createElement('img'); | ||
img.src = imgUrl; | img.src = imgUrl; | ||
img.alt = captionText || filename; | |||
img.alt = | |||
img.loading = 'lazy'; | img.loading = 'lazy'; | ||
img. | img.dataset.fullsrc = lightboxSrc; | ||
img. | img.dataset.caption = captionHtml; | ||
var figcaption = document.createElement('figcaption'); | var figcaption = document.createElement('figcaption'); | ||
figcaption.innerHTML = captionHtml; | |||
figcaption.innerHTML = | |||
figure.appendChild(img); | |||
figure.appendChild(figcaption); | figure.appendChild(figcaption); | ||
grid.appendChild(figure); | grid.appendChild(figure); | ||
}) | |||
.catch(function (err) { | |||
console.warn('Commons gallery image failed:', filename, err.message); | |||
}); | }); | ||
}); | }); | ||
sources.forEach(function (el) { el.style.display = 'none'; }); | |||
}); | }); | ||
}); | }); | ||
/* ============================================================ | /* ============================================================ | ||
Commons Gallery Lightbox | Commons Gallery Lightbox (keyboard + touch swipe) | ||
============================================================ */ | ============================================================ */ | ||
(function () { | (function () { | ||
var galleryImages = []; | |||
var currentIndex = -1; | |||
var touchStartX = 0; | |||
function collectImages() { | |||
galleryImages = Array.prototype.slice.call( | |||
document.querySelectorAll('.commons-gallery-item img') | |||
); | |||
} | |||
function createLightbox() { | function createLightbox() { | ||
| വരി 142: | വരി 129: | ||
var overlay = document.createElement('div'); | var overlay = document.createElement('div'); | ||
overlay.id = 'commons-lightbox'; | overlay.id = 'commons-lightbox'; | ||
overlay.innerHTML = | overlay.innerHTML = | ||
'<div class="clb-backdrop"></div>' + | '<div class="clb-backdrop"></div>' + | ||
'<div class="clb-content">' + | '<div class="clb-content">' + | ||
'<button class="clb-prev" aria-label="Previous">‹</button>' + | |||
'<button class="clb-next" aria-label="Next">›</button>' + | |||
'<button class="clb-close" aria-label="Close">×</button>' + | |||
'<img class="clb-image" alt="">' + | '<img class="clb-image" alt="">' + | ||
'<div class="clb-caption"></div>' + | '<div class="clb-caption"></div>' + | ||
'</div>'; | '</div>'; | ||
document.body.appendChild(overlay); | document.body.appendChild(overlay); | ||
overlay.querySelector('.clb-backdrop'). | overlay.querySelector('.clb-backdrop').addEventListener('click', closeLightbox); | ||
overlay.querySelector('.clb-close'). | overlay.querySelector('.clb-close').addEventListener('click', closeLightbox); | ||
overlay.querySelector('.clb-prev').addEventListener('click', showPrev); | |||
overlay.querySelector('.clb-next').addEventListener('click', showNext); | |||
/* Keyboard navigation */ | |||
document.addEventListener('keydown', function (e) { | document.addEventListener('keydown', function (e) { | ||
if (!document.getElementById('commons-lightbox').classList.contains('active')) return; | |||
if (e.key === 'Escape') closeLightbox(); | if (e.key === 'Escape') closeLightbox(); | ||
if (e.key === 'ArrowLeft') showPrev(); | |||
if (e.key === 'ArrowRight') showNext(); | |||
}); | }); | ||
/* Touch swipe navigation */ | |||
overlay.addEventListener('touchstart', function (e) { | |||
touchStartX = e.changedTouches[0].clientX; | |||
}, { passive: true }); | |||
overlay.addEventListener('touchend', function (e) { | |||
var dx = e.changedTouches[0].clientX - touchStartX; | |||
if (Math.abs(dx) > 50) { | |||
if (dx < 0) showNext(); else showPrev(); | |||
} | |||
}, { passive: true }); | |||
} | } | ||
function openLightbox(img) { | function openLightbox(img) { | ||
collectImages(); | |||
currentIndex = galleryImages.indexOf(img); | |||
if (currentIndex === -1) return; | |||
createLightbox(); | createLightbox(); | ||
showImage(currentIndex); | |||
document.getElementById('commons-lightbox').classList.add('active'); | |||
document.body.style.overflow = 'hidden'; | |||
} | |||
function showImage(index) { | |||
var overlay = document.getElementById('commons-lightbox'); | var overlay = document.getElementById('commons-lightbox'); | ||
var | var img = galleryImages[index]; | ||
if (!img) return; | |||
overlay.querySelector('.clb-image').src = img.dataset.fullsrc || img.src; | |||
overlay.querySelector('.clb-image').alt = img.alt || ''; | |||
overlay.querySelector('.clb-caption').innerHTML = img.dataset.caption || ''; | |||
/* Show/hide nav arrows at ends */ | |||
overlay.querySelector('.clb-prev').style.visibility = index > 0 ? '' : 'hidden'; | |||
overlay.querySelector('.clb-next').style.visibility = index < galleryImages.length - 1 ? '' : 'hidden'; | |||
currentIndex = index; | |||
} | } | ||
function showPrev() { if (currentIndex > 0) showImage(currentIndex - 1); } | |||
function showNext() { if (currentIndex < galleryImages.length - 1) showImage(currentIndex + 1); } | |||
function closeLightbox() { | function closeLightbox() { | ||
var overlay = document.getElementById('commons-lightbox'); | var overlay = document.getElementById('commons-lightbox'); | ||
if (!overlay) return; | if (!overlay) return; | ||
overlay.classList.remove('active'); | overlay.classList.remove('active'); | ||
document.body.style.overflow = ''; | document.body.style.overflow = ''; | ||
} | } | ||
document.addEventListener('click', function (e) { | |||
if (e.target && e.target.matches('.commons-gallery-item img')) { | |||
openLightbox(e.target); | |||
} | |||
} | |||
}); | }); | ||
})(); | })(); | ||
/* ============================================================ | /* ============================================================ | ||
Editing tools (load only on edit/upload pages) | |||
============================================================ */ | ============================================================ */ | ||
if ( | if ( | ||
| വരി 315: | വരി 224: | ||
); | ); | ||
} | } | ||
/* ============================================================ | /* ============================================================ | ||
| വരി 321: | വരി 229: | ||
============================================================ */ | ============================================================ */ | ||
function addCharSubsetMenu() { | function addCharSubsetMenu() { | ||
if ($('#editpage-specialchars').length | if ($('#editpage-specialchars').length === 0) return; | ||
var s = parseInt(mw.cookie.get('edittoolscharsubset'), 10); | |||
if (isNaN(s)) s = 0; | |||
var options = [ | |||
'ഫലകങ്ങൾ', 'വിക്കിവിന്യാസങ്ങൾ', 'അനുമതിപത്രങ്ങൾ', 'മലയാളം', | |||
'കൊറിയൻ', 'ലത്തീൻ', 'ഐ.പി.എ.', 'പലവക', | |||
'അറബി', 'ദേവനാഗരി', 'ഹിബ്രു', 'പഴയ ഇംഗ്ലീഷ്' | |||
]; | |||
var $menu = $('<select />') | |||
.attr('id', 'charSubsetControl') | |||
.css('display', 'inline') | |||
.change(chooseCharSubset) | |||
.data('previousSelectedIndex', s); | |||
options.forEach(function (label) { | |||
$menu.append($('<option />').text(label)); | |||
}); | |||
$('#editpage-specialchars').prepend($menu); | |||
$('#charSubsetControl')[0].selectedIndex = s; | |||
$('p', '#editpage-specialchars').each(function (index) { | |||
$(this).css({ | |||
display: index === s ? 'inline' : 'none', | |||
visibility: index === s ? 'visible' : 'hidden' | |||
}); | }); | ||
} | }); | ||
} | } | ||
| വരി 364: | വരി 269: | ||
}); | }); | ||
}); | }); | ||
mw.cookie.set('edittoolscharsubset', selectedIndex); | |||
} | } | ||
$(addCharSubsetMenu); | $(addCharSubsetMenu); | ||
/* ============================================================ | /* ============================================================ | ||
Navigation bars | Navigation bars (NavFrame show/hide) | ||
============================================================ */ | ============================================================ */ | ||
var NavigationBarHide = 'മറയ്ക്കുക'; | var NavigationBarHide = 'മറയ്ക്കുക'; | ||
| വരി 385: | വരി 289: | ||
NavToggle.firstChild.data = show ? NavigationBarHide : NavigationBarShow; | NavToggle.firstChild.data = show ? NavigationBarHide : NavigationBarShow; | ||
Array.prototype.forEach.call(NavFrame.children, function (child) { | |||
if (child.classList.contains('NavContent') || child.classList.contains('NavPic')) { | |||
if ( | child.style.display = show ? 'block' : 'none'; | ||
} | } | ||
} | }); | ||
event.preventDefault(); | event.preventDefault(); | ||
}; | }; | ||
| വരി 399: | വരി 301: | ||
indexNavigationBar++; | indexNavigationBar++; | ||
var NavFrame = this; | var NavFrame = this; | ||
var NavHead = $(NavFrame).find('.NavHead').first(); | var $NavHead = $(NavFrame).find('.NavHead').first(); | ||
if (!NavHead.length) return; | if (!$NavHead.length) return; | ||
var NavToggle = $('<a href="#" class="NavToggle"></a>') | var idx = indexNavigationBar; | ||
.attr('id', 'NavToggle' + | var $NavToggle = $('<a href="#" class="NavToggle"></a>') | ||
.attr('id', 'NavToggle' + idx) | |||
.text(NavigationBarHide) | .text(NavigationBarHide) | ||
.on('click', function (e) { | .on('click', function (e) { | ||
toggleNavigationBar( | toggleNavigationBar(idx, e); | ||
}); | }); | ||
NavFrame.id = 'NavFrame' + | NavFrame.id = 'NavFrame' + idx; | ||
NavHead.append(NavToggle); | $NavHead.append($NavToggle); | ||
}); | }); | ||
}); | }); | ||
/* ============================================================ | /* ============================================================ | ||
| വരി 419: | വരി 321: | ||
============================================================ */ | ============================================================ */ | ||
var autoCollapse = 2; | var autoCollapse = 2; | ||
var collapseCaption = | var collapseCaption = 'മറയ്ക്കുക'; | ||
var expandCaption = | var expandCaption = 'പ്രദർശിപ്പിക്കുക'; | ||
function collapseTable(tableIndex) { | function collapseTable(tableIndex) { | ||
var Button = document.getElementById( | var Button = document.getElementById('collapseButton' + tableIndex); | ||
var Table = document.getElementById( | var Table = document.getElementById('collapsibleTable' + tableIndex); | ||
if (!Table || !Button) return; | if (!Table || !Button) return; | ||
var hide = Button.firstChild.data === collapseCaption; | var hide = Button.firstChild.data === collapseCaption; | ||
Array.prototype.forEach.call(Table.rows, function (row, i) { | |||
if (i > 0) row.style.display = hide ? 'none' : ''; | |||
}); | |||
} | |||
Button.firstChild.data = hide ? expandCaption : collapseCaption; | Button.firstChild.data = hide ? expandCaption : collapseCaption; | ||
} | } | ||
| വരി 443: | വരി 343: | ||
if (!$header.length) return; | if (!$header.length) return; | ||
$table.attr('id', 'collapsibleTable' + | var idx = tableIndex; | ||
$table.attr('id', 'collapsibleTable' + idx); | |||
var $link = $('<a href="#"></a>') | var $link = $('<a href="#"></a>') | ||
.attr('id', 'collapseButton' + | .attr('id', 'collapseButton' + idx) | ||
.text(collapseCaption) | .text(collapseCaption) | ||
.on('click', function (e) { | .on('click', function (e) { | ||
e.preventDefault(); | e.preventDefault(); | ||
collapseTable( | collapseTable(idx); | ||
}); | }); | ||
$ | $header.prepend($('<span class="collapseButton">[</span>').append($link).append(']')); | ||
tableIndex++; | tableIndex++; | ||
}); | }); | ||
}); | }); | ||