"മീഡിയവിക്കി:Common.js" എന്ന താളിന്റെ പതിപ്പുകൾ തമ്മിലുള്ള വ്യത്യാസം

No edit summary
No edit summary
 
(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
   ============================================================ */
   ============================================================ */
mw.hook('wikipage.content').add(function () {
window.onload = function () {
     var images = document.querySelectorAll('img');
     var images = document.querySelectorAll('img');
     for (var i = 0; i < images.length; i++) {
     for (var i = 0; i < images.length; i++) {
         var img = images[i];
         var img = images[i];
         if (img.src &&
         if (
             img.src.indexOf('https://tile.openstreetmap.org/v4/marker/') === 0) {
            img.src &&
             img.src.startsWith("https://tile.openstreetmap.org/v4/marker/")
        ) {
             img.src = img.src.replace(
             img.src = img.src.replace(
                 'https://tile.openstreetmap.org/v4/marker/',
                 'https://tile.openstreetmap.org/v4/marker/',
വരി 13: വരി 17:
             );
             );
         }
         }
    }
};
});
};


/* ============================================================
/* ============================================================
   Wikimedia Commons Gallery Loader (with per-image captions)
   Wikimedia Commons Gallery Loader (Template-based, MULTILINE SAFE)
   ============================================================ */
   ============================================================ */
mw.hook('wikipage.content').add(function ($content) {
mw.hook('wikipage.content').add(function ($content) {
വരി 24: വരി 28:
         var container = this;
         var container = this;


         var rawItems = container.dataset.items;
         var sources = container.querySelectorAll('.commons-gallery-source');
        if (!rawItems) return;
        if (!sources.length) return;
 
        var width = parseInt(container.dataset.imageWidth, 10) || 300;
 
        var items = rawItems.split('|').map(function (entry) {
            var parts = entry.split('::');
            return {
                filename: parts[0] ? parts[0].trim() : '',
                caption: parts[1] ? parts.slice(1).join('::').trim() : ''
            };
        }).filter(function (i) {
            return i.filename;
        });


         if (!items.length) return;
         var width = 300;


         var grid = document.createElement('div');
         var grid = document.createElement('div');
വരി 50: വരി 42:
         container.appendChild(grid);
         container.appendChild(grid);


         items.forEach(function (item) {
         sources.forEach(function (srcEl) {
            var filename = srcEl.dataset.file;
            var captionText = srcEl.dataset.caption || '';
 
            if (!filename) return;


             var apiUrl =
             var apiUrl =
വരി 59: വരി 55:
                 '&iiprop=url|extmetadata' +
                 '&iiprop=url|extmetadata' +
                 '&iiurlwidth=' + width +
                 '&iiurlwidth=' + width +
                 '&titles=File:' + encodeURIComponent(item.filename) +
                 '&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 =
                     var page =
                         data.query.pages[Object.keys(data.query.pages)[0]];
                         data.query.pages[Object.keys(data.query.pages)[0]];
 
                     if (!page || !page.imageinfo) return;
                     if (!page.imageinfo) return;


                     var info = page.imageinfo[0];
                     var info = page.imageinfo[0];
വരി 79: വരി 76:
                     var license = '';
                     var license = '';
                     var licenseUrl = '';
                     var licenseUrl = '';
                    var commonsUrl = info.descriptionurl || '';


                     if (info.extmetadata) {
                     if (info.extmetadata) {
വരി 97: വരി 95:
                     var img = document.createElement('img');
                     var img = document.createElement('img');
                     img.src = imgUrl;
                     img.src = imgUrl;
                     img.alt = item.caption || item.filename;
                     img.alt = captionText || filename;
                     img.loading = 'lazy';
                     img.loading = 'lazy';
                     img.style.width = '100%';
                     img.style.width = '100%';
                     img.style.display = 'block';
 
                    /* Lightbox source: responsive, not original */
                    var lightboxSrc = info.url;
                    if (info.responsiveUrls) {
                        if (window.devicePixelRatio > 1 && info.responsiveUrls['2']) {
                            lightboxSrc = info.responsiveUrls['2'];
                        } else if (info.responsiveUrls['1.5']) {
                            lightboxSrc = info.responsiveUrls['1.5'];
                        }
                    }
 
                     img.dataset.fullsrc = lightboxSrc;
                    img.dataset.caption =
                        (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>'
                            : '');


                     figure.appendChild(img);
                     figure.appendChild(img);
വരി 107: വരി 129:
                     figcaption.style.fontSize = '0.75em';
                     figcaption.style.fontSize = '0.75em';
                     figcaption.style.textAlign = 'center';
                     figcaption.style.textAlign = 'center';
 
                     figcaption.innerHTML = img.dataset.caption;
                     figcaption.innerHTML =
                        (item.caption ? item.caption + '<br>' : '') +
                        '© ' + artist +
                        (licenseUrl
                            ? ' – <a href="' + licenseUrl +
                              '" target="_blank">' + license + '</a>'
                            : '');


                     figure.appendChild(figcaption);
                     figure.appendChild(figcaption);
                     grid.appendChild(figure);
                     grid.appendChild(figure);
                })
                .catch(function (err) {
                    console.warn(
                        'Commons gallery image failed:',
                        filename,
                        err.message
                    );
                 });
                 });
        });
        /* Hide source placeholders */
        sources.forEach(function (el) {
            el.style.display = 'none';
         });
         });
     });
     });
});
});


/* ============================================================
/* ============================================================
   Load images from Wikimedia Commons using template
   Commons Gallery Lightbox Viewer (with navigation)
   ============================================================ */
   ============================================================ */
mw.hook('wikipage.content').add(function ($content) {
(function () {


     $content.find('.commons-image').each(function () {
     var galleryImages = [];
        var el = this;
    var currentIndex = -1;
        var filename = el.dataset.filename;
        if (!filename) return;


         var width = el.dataset.width || 300;
    function collectGalleryImages() {
        var align = el.dataset.align || 'none';
         galleryImages = Array.prototype.slice.call(
         var caption = el.dataset.caption || '';
            document.querySelectorAll('.commons-gallery-item img')
         );
    }


         var apiUrl =
    function createLightbox() {
            'https://commons.wikimedia.org/w/api.php' +
         if (document.getElementById('commons-lightbox')) return;
            '?action=query' +
            '&format=json' +
            '&prop=imageinfo' +
            '&iiprop=url|extmetadata' +
            '&iiurlwidth=' + width +
            '&titles=File:' + encodeURIComponent(filename) +
            '&origin=*';


         fetch(apiUrl)
         var overlay = document.createElement('div');
            .then(function (r) { return r.json(); })
        overlay.id = 'commons-lightbox';
            .then(function (data) {


                 if (!data.query || !data.query.pages) {
        overlay.innerHTML =
                    el.textContent = 'Error loading image';
            '<div class="clb-backdrop"></div>' +
                    return;
            '<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="">' +
                '<div class="clb-caption"></div>' +
            '</div>';


                var pages = data.query.pages;
        document.body.appendChild(overlay);
                var page = pages[Object.keys(pages)[0]];


                if (!page.imageinfo) {
        overlay.querySelector('.clb-backdrop').onclick = closeLightbox;
                    el.textContent = 'Image not found on Wikimedia Commons';
        overlay.querySelector('.clb-close').onclick = closeLightbox;
                    return;
        overlay.querySelector('.clb-prev').onclick = showPrev;
                }
        overlay.querySelector('.clb-next').onclick = showNext;


                var info = page.imageinfo[0];
        document.addEventListener('keydown', function (e) {
                var imgUrl = info.thumburl || info.url;
            if (!overlay.classList.contains('active')) return;
            if (e.key === 'Escape') closeLightbox();
            if (e.key === 'ArrowLeft') showPrev();
            if (e.key === 'ArrowRight') showNext();
        });
    }


                var artist = 'Unknown';
    function openLightbox(img) {
                var license = '';
        collectGalleryImages();
                var licenseUrl = '';
        currentIndex = galleryImages.indexOf(img);
        if (currentIndex === -1) return;


                if (info.extmetadata) {
        createLightbox();
                    if (info.extmetadata.Artist) {
        showImage(currentIndex);
                        artist = info.extmetadata.Artist.value;
 
                    }
        document.getElementById('commons-lightbox').classList.add('active');
                    if (info.extmetadata.LicenseShortName) {
        document.body.style.overflow = 'hidden';
                        license = info.extmetadata.LicenseShortName.value;
    }
                    }
                    if (info.extmetadata.LicenseUrl) {
                        licenseUrl = info.extmetadata.LicenseUrl.value;
                    }
                }


                var figure = document.createElement('figure');
    function showImage(index) {
                figure.style.maxWidth = width + 'px';
        var overlay = document.getElementById('commons-lightbox');
        var lbImg = overlay.querySelector('.clb-image');
        var caption = overlay.querySelector('.clb-caption');


                if (align === 'right') figure.style.float = 'right';
        var img = galleryImages[index];
                if (align === 'left') figure.style.float = 'left';
        if (!img) return;
                if (align === 'center') {
                    figure.style.marginLeft = 'auto';
                    figure.style.marginRight = 'auto';
                }


                var img = document.createElement('img');
        lbImg.src = img.dataset.fullsrc || img.src;
                img.src = imgUrl;
        lbImg.alt = img.alt || '';
                img.alt = caption || filename;
        caption.innerHTML = img.dataset.caption || '';
                img.loading = 'lazy';
                img.style.width = '100%';


                figure.appendChild(img);
        currentIndex = index;
    }


                if (caption || license) {
    function showPrev() {
                    var figcaption = document.createElement('figcaption');
        if (currentIndex > 0) showImage(currentIndex - 1);
                    figcaption.style.fontSize = '0.8em';
    }
                    figcaption.style.textAlign = 'center';


                    figcaption.innerHTML =
    function showNext() {
                        caption +
        if (currentIndex < galleryImages.length - 1) {
                        '<br>© ' + artist +
            showImage(currentIndex + 1);
                        (licenseUrl
        }
                            ? ' – <a href="' + licenseUrl +
    }
                              '" target="_blank">' + license + '</a>'
                            : '');


                    figure.appendChild(figcaption);
    function closeLightbox() {
                }
        var overlay = document.getElementById('commons-lightbox');
        if (!overlay) return;
        overlay.classList.remove('active');
        document.body.style.overflow = '';
    }


                el.replaceWith(figure);
    /* Event delegation (single, correct) */
            })
    document.addEventListener('click', function (e) {
            .catch(function () {
        var img = e.target;
                el.textContent = 'Error loading image from Wikimedia Commons';
        if (img && img.matches('.commons-gallery-item img')) {
             });
             openLightbox(img);
        }
     });
     });
});


})();


/* ============================================================
/* ============================================================
വരി 240: വരി 261:
     );
     );
}
}


/* ============================================================
/* ============================================================
വരി 293: വരി 313:


$(addCharSubsetMenu);
$(addCharSubsetMenu);


/* ============================================================
/* ============================================================
വരി 338: വരി 357:
     });
     });
});
});


/* ============================================================
/* ============================================================
വരി 344: വരി 362:
   ============================================================ */
   ============================================================ */
var autoCollapse = 2;
var autoCollapse = 2;
var collapseCaption = "മറയ്ക്കുക";
var collapseCaption = 'മറയ്ക്കുക';
var expandCaption = "പ്രദർശിപ്പിക്കുക";
var expandCaption = 'പ്രദർശിപ്പിക്കുക';


function collapseTable(tableIndex) {
function collapseTable(tableIndex) {
     var Button = document.getElementById("collapseButton" + tableIndex);
     var Button = document.getElementById('collapseButton' + tableIndex);
     var Table = document.getElementById("collapsibleTable" + tableIndex);
     var Table = document.getElementById('collapsibleTable' + tableIndex);
     if (!Table || !Button) return;
     if (!Table || !Button) return;


വരി 356: വരി 374:


     for (var i = 1; i < Rows.length; i++) {
     for (var i = 1; i < Rows.length; i++) {
         Rows[i].style.display = hide ? "none" : "";
         Rows[i].style.display = hide ? 'none' : '';
     }
     }
     Button.firstChild.data = hide ? expandCaption : collapseCaption;
     Button.firstChild.data = hide ? expandCaption : collapseCaption;
"https://schoolwiki.in/മീഡിയവിക്കി:Common.js" എന്ന താളിൽനിന്ന് ശേഖരിച്ചത്