|
|
| (Una versione intermedia di uno stesso utente non è mostrata) |
| Riga 1: |
Riga 1: |
|
| |
|
|
| |
|
| |
| /* WIKINESS_DYNAMIC_GALLERY_START */
| |
| (function () {
| |
| function escapeHtml(text) {
| |
| return String(text || '')
| |
| .replace(/&/g, '&')
| |
| .replace(/</g, '<')
| |
| .replace(/>/g, '>')
| |
| .replace(/"/g, '"');
| |
| }
| |
|
| |
| function normalizeFileName(name) {
| |
| return String(name || '')
| |
| .replace(/^File:/i, '')
| |
| .replace(/ /g, '_');
| |
| }
| |
|
| |
| function getPagesFromCategory(categoryTitle) {
| |
| var apiUrl =
| |
| '/api.php?action=query' +
| |
| '&generator=categorymembers' +
| |
| '&gcmtitle=' + encodeURIComponent(categoryTitle) +
| |
| '&gcmlimit=50' +
| |
| '&gcmsort=sortkey' +
| |
| '&gcmdir=ascending' +
| |
| '&prop=pageimages|info' +
| |
| '&piprop=name' +
| |
| '&inprop=url' +
| |
| '&format=json';
| |
|
| |
| return fetch(apiUrl, { credentials: 'same-origin' })
| |
| .then(function (response) {
| |
| return response.json();
| |
| })
| |
| .then(function (data) {
| |
| var pagesObj = data && data.query && data.query.pages ? data.query.pages : {};
| |
| return Object.keys(pagesObj).map(function (key) {
| |
| return pagesObj[key];
| |
| });
| |
| });
| |
| }
| |
|
| |
| function getImageUrls(fileNames) {
| |
| var cleanNames = fileNames
| |
| .filter(Boolean)
| |
| .map(function (name) {
| |
| return 'File:' + normalizeFileName(name);
| |
| });
| |
|
| |
| if (!cleanNames.length) {
| |
| return Promise.resolve({});
| |
| }
| |
|
| |
| var apiUrl =
| |
| '/api.php?action=query' +
| |
| '&prop=imageinfo' +
| |
| '&iiprop=url' +
| |
| '&iiurlwidth=140' +
| |
| '&format=json' +
| |
| '&titles=' + encodeURIComponent(cleanNames.join('|'));
| |
|
| |
| return fetch(apiUrl, { credentials: 'same-origin' })
| |
| .then(function (response) {
| |
| return response.json();
| |
| })
| |
| .then(function (data) {
| |
| var map = {};
| |
| var pagesObj = data && data.query && data.query.pages ? data.query.pages : {};
| |
|
| |
| Object.keys(pagesObj).forEach(function (key) {
| |
| var filePage = pagesObj[key];
| |
| var info = filePage.imageinfo && filePage.imageinfo[0] ? filePage.imageinfo[0] : null;
| |
|
| |
| if (!info) {
| |
| return;
| |
| }
| |
|
| |
| var cleanTitle = normalizeFileName(filePage.title);
| |
| map[cleanTitle] = info.thumburl || info.url || '';
| |
| });
| |
|
| |
| return map;
| |
| });
| |
| }
| |
|
| |
| function buildGallery(targetId, categoryTitle, emptyText) {
| |
| var target = document.getElementById(targetId);
| |
|
| |
| if (!target) {
| |
| return;
| |
| }
| |
|
| |
| target.innerHTML = '<div class="wikiness-gallery-loading">Caricamento immagini...</div>';
| |
|
| |
| getPagesFromCategory(categoryTitle)
| |
| .then(function (pages) {
| |
| pages.sort(function (a, b) {
| |
| return String(a.title || '').localeCompare(String(b.title || ''), 'it');
| |
| });
| |
|
| |
| var pagesWithPageImage = pages.filter(function (page) {
| |
| return page.pageimage && page.fullurl;
| |
| }).slice(0, 10);
| |
|
| |
| return getImageUrls(pagesWithPageImage.map(function (page) {
| |
| return page.pageimage;
| |
| })).then(function (imageMap) {
| |
| return {
| |
| pages: pagesWithPageImage,
| |
| imageMap: imageMap
| |
| };
| |
| });
| |
| })
| |
| .then(function (result) {
| |
| var pages = result.pages;
| |
| var imageMap = result.imageMap;
| |
|
| |
| var finalPages = pages.filter(function (page) {
| |
| return imageMap[normalizeFileName(page.pageimage)];
| |
| });
| |
|
| |
| if (!finalPages.length) {
| |
| target.innerHTML = '<div class="wikiness-gallery-empty">' + emptyText + '</div>';
| |
| return;
| |
| }
| |
|
| |
| var html = '<div class="wikiness-dynamic-gallery-grid">';
| |
|
| |
| finalPages.forEach(function (page) {
| |
| var title = page.title || '';
| |
| var img = imageMap[normalizeFileName(page.pageimage)];
| |
| var url = page.fullurl;
| |
|
| |
| html += ''
| |
| + '<a class="wikiness-dynamic-gallery-card" href="' + escapeHtml(url) + '">'
| |
| + '<span class="wikiness-dynamic-gallery-thumb">'
| |
| + '<img src="' + escapeHtml(img) + '" alt="' + escapeHtml(title) + '" loading="lazy">'
| |
| + '</span>'
| |
| + '<span class="wikiness-dynamic-gallery-title">' + escapeHtml(title) + '</span>'
| |
| + '</a>';
| |
| });
| |
|
| |
| html += '</div>';
| |
| target.innerHTML = html;
| |
| })
| |
| .catch(function () {
| |
| target.innerHTML = '<div class="wikiness-gallery-empty">' + emptyText + '</div>';
| |
| });
| |
| }
| |
|
| |
| document.addEventListener('DOMContentLoaded', function () {
| |
| buildGallery(
| |
| 'wikiness-gallery-professionisti',
| |
| 'Categoria:- Pagine con immagini',
| |
| 'Nessuna immagine disponibile.'
| |
| );
| |
|
| |
| buildGallery(
| |
| 'wikiness-gallery-loghi',
| |
| 'Categoria:- Pagine con loghi',
| |
| 'Nessun logo disponibile.'
| |
| );
| |
| });
| |
| }());
| |
| /* WIKINESS_DYNAMIC_GALLERY_END */
| |