"മീഡിയവിക്കി:Common.js" എന്ന താളിന്റെ പതിപ്പുകൾ തമ്മിലുള്ള വ്യത്യാസം
No edit summary |
No edit summary |
||
| വരി 63: | വരി 63: | ||
fetch(apiUrl) | fetch(apiUrl) | ||
.then(function (r) { return r.json(); }) | .then(function (r) { | ||
if (!r.ok) { | |||
throw new Error('HTTP error ' + r.status); | |||
} | |||
return r.json(); | |||
}) | |||
.then(function (data) { | .then(function (data) { | ||
if (!data.query || !data.query.pages) | if (!data || !data.query || !data.query.pages) { | ||
throw new Error('Invalid API response'); | |||
} | |||
var page = | var page = | ||
data.query.pages[Object.keys(data.query.pages)[0]]; | data.query.pages[Object.keys(data.query.pages)[0]]; | ||
if (!page.imageinfo) | if (!page || !page.imageinfo || !page.imageinfo.length) { | ||
throw new Error('No imageinfo for ' + item.filename); | |||
} | |||
var info = page.imageinfo[0]; | var info = page.imageinfo[0]; | ||
| വരി 97: | വരി 106: | ||
var img = document.createElement('img'); | var img = document.createElement('img'); | ||
img.src = imgUrl; | img.src = imgUrl; | ||
img.alt = item.caption || item.filename; | |||
img.loading = 'lazy'; | |||
img.style.width = '100%'; | |||
img.style.display = 'block'; | |||
/* Data for lightbox */ | |||
img.dataset.fullsrc = info.url; | img.dataset.fullsrc = info.url; | ||
img.dataset.caption = | img.dataset.caption = | ||
| വരി 102: | വരി 117: | ||
'© ' + artist + | '© ' + artist + | ||
(licenseUrl | (licenseUrl | ||
? ' – <a href="' + licenseUrl + '" target="_blank">' + license + '</a>' | ? ' – <a href="' + licenseUrl + | ||
'" target="_blank">' + license + '</a>' | |||
: ''); | : ''); | ||
figure.appendChild(img); | figure.appendChild(img); | ||
var figcaption = document.createElement('figcaption'); | var figcaption = document.createElement('figcaption'); | ||
| വരി 125: | വരി 136: | ||
figure.appendChild(figcaption); | figure.appendChild(figcaption); | ||
grid.appendChild(figure); | grid.appendChild(figure); | ||
}) | |||
.catch(function (err) { | |||
console.warn( | |||
'Commons gallery image failed:', | |||
item.filename, | |||
err.message | |||
); | |||
}); | }); | ||
}); | }); | ||
}); | }); | ||
}); | }); | ||
/* ============================================================ | /* ============================================================ | ||
Commons Gallery Lightbox Viewer | Commons Gallery Lightbox Viewer (CORRECTED) | ||
============================================================ */ | ============================================================ */ | ||
(function () { | (function () { | ||
function createLightbox() { | function createLightbox() { | ||
if (document.getElementById('commons-lightbox')) return; | if (document.getElementById('commons-lightbox')) { | ||
return; | |||
} | |||
var overlay = document.createElement('div'); | var overlay = document.createElement('div'); | ||
| വരി 146: | വരി 166: | ||
'<div class="clb-backdrop"></div>' + | '<div class="clb-backdrop"></div>' + | ||
'<div class="clb-content">' + | '<div class="clb-content">' + | ||
'<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); | ||
document.addEventListener('keydown', function (e) { | document.addEventListener('keydown', function (e) { | ||
if (e.key === 'Escape') closeLightbox(); | if (e.key === 'Escape') { | ||
closeLightbox(); | |||
} | |||
}); | }); | ||
} | } | ||
| വരി 185: | വരി 207: | ||
} | } | ||
/* | /* ======================================================== | ||
Event delegation for dynamically loaded gallery images | |||
======================================================== */ | |||
document.addEventListener('click', function (e) { | |||
var img = e.target; | |||
if (img && img.matches('.commons-gallery-item img')) { | |||
img.style.cursor = 'zoom-in'; | img.style.cursor = 'zoom-in'; | ||
openLightbox(img); | |||
} | |||
} | |||
}); | }); | ||