ഉള്ളടക്കത്തിലേക്ക് പോവുക

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

Schoolwiki സംരംഭത്തിൽ നിന്ന്
No edit summary
optimize js
 
(2 ഉപയോക്താക്കൾ ചെയ്ത ഇടയ്ക്കുള്ള 11 നാൾപ്പതിപ്പുകൾ പ്രദർശിപ്പിക്കുന്നില്ല)
വരി 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.addEventListener('load', function () {
     var images = document.querySelectorAll('img');
     document.querySelectorAll('img').forEach(function (img) {
    for (var i = 0; i < images.length; i++) {
         if (img.src && img.src.startsWith('https://tile.openstreetmap.org/v4/marker/')) {
        var img = images[i];
         if (
            img.src &&
            img.src.indexOf('https://tile.openstreetmap.org/v4/marker/') === 0
        ) {
             img.src = img.src.replace(
             img.src = img.src.replace(
                 'https://tile.openstreetmap.org/v4/marker/',
                 'https://tile.openstreetmap.org/v4/marker/',
വരി 15: വരി 12:
             );
             );
         }
         }
     }
     });
});
});


/* ============================================================
/* ============================================================
   Wikimedia Commons Gallery Loader (Template-based, MULTILINE SAFE)
   Wikimedia Commons Gallery Loader
   ============================================================ */
   ============================================================ */
mw.hook('wikipage.content').add(function ($content) {
mw.hook('wikipage.content').add(function ($content) {
വരി 25: വരി 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');
         var sources = container.querySelectorAll('.commons-gallery-source');
         if (!sources.length) return;
         if (!sources.length) return;


         var width = 300;
         var width = 300;
         var grid = document.createElement('div');
         var grid = document.createElement('div');
         grid.className = 'commons-gallery-grid';
         grid.className = 'commons-gallery-grid';
        grid.style.display = 'grid';
        grid.style.gridTemplateColumns =
            'repeat(auto-fill, minmax(' + width + 'px, 1fr))';
        grid.style.gap = '8px';
         container.appendChild(grid);
         container.appendChild(grid);


വരി 43: വരി 33:
             var filename = srcEl.dataset.file;
             var filename = srcEl.dataset.file;
             var captionText = srcEl.dataset.caption || '';
             var captionText = srcEl.dataset.caption || '';
             if (!filename) return;
             if (!filename) return;


വരി 63: വരി 52:
                 .then(function (data) {
                 .then(function (data) {
                     if (!data || !data.query || !data.query.pages) return;
                     if (!data || !data.query || !data.query.pages) return;
 
                     var page = data.query.pages[Object.keys(data.query.pages)[0]];
                     var page =
                        data.query.pages[Object.keys(data.query.pages)[0]];
                     if (!page || !page.imageinfo) return;
                     if (!page || !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 || '';


                    var artist = 'Unknown';
                     /* Prefer responsive URL for high-DPI screens */
                    var license = '';
                    var licenseUrl = '';
 
                    if (info.extmetadata) {
                        if (info.extmetadata.Artist) {
                            artist = info.extmetadata.Artist.value;
                        }
                        if (info.extmetadata.LicenseShortName) {
                            license = info.extmetadata.LicenseShortName.value;
                        }
                        if (info.extmetadata.LicenseUrl) {
                            licenseUrl = info.extmetadata.LicenseUrl.value;
                        }
                    }
 
                    var figure = document.createElement('figure');
                    figure.className = 'commons-gallery-item';
 
                    var img = document.createElement('img');
                    img.src = imgUrl;
                    img.alt = captionText || filename;
                    img.loading = 'lazy';
                    img.style.width = '100%';
 
                     /* Lightbox data (use responsive size, not original) */
                     var lightboxSrc = info.url;
                     var lightboxSrc = info.url;
                     if (info.responsiveUrls) {
                     if (info.responsiveUrls) {
വരി 106: വരി 73:
                     }
                     }


                     img.dataset.fullsrc = lightboxSrc;
                     var captionHtml =
                    img.dataset.caption =
                         (captionText ? captionText + '<br>' : '') +
                         (captionText ? captionText + '<br>' : '') +
                         '© ' + artist +
                         '© ' + artist +
                         (licenseUrl
                         (licenseUrl
                             ? ' – <a href="' + licenseUrl +
                             ? ' – <a href="' + licenseUrl + '" target="_blank" rel="noopener noreferrer">' + license + '</a>'
                              '" target="_blank">' + license + '</a>'
                            : '') +
                        (commonsUrl
                            ? '<br><a href="' + commonsUrl + '" target="_blank" rel="noopener noreferrer">View on Wikimedia Commons</a>'
                             : '');
                             : '');


                     figure.appendChild(img);
                     var figure = document.createElement('figure');
                    figure.className = 'commons-gallery-item';
 
                    var img = document.createElement('img');
                    img.src = imgUrl;
                    img.alt = captionText || filename;
                    img.loading = 'lazy';
                    img.dataset.fullsrc = lightboxSrc;
                    img.dataset.caption = captionHtml;


                     var figcaption = document.createElement('figcaption');
                     var figcaption = document.createElement('figcaption');
                    figcaption.style.fontSize = '0.75em';
                     figcaption.innerHTML = captionHtml;
                    figcaption.style.textAlign = 'center';
                     figcaption.innerHTML = img.dataset.caption;


                    figure.appendChild(img);
                     figure.appendChild(figcaption);
                     figure.appendChild(figcaption);
                     grid.appendChild(figure);
                     grid.appendChild(figure);
                 })
                 })
                 .catch(function (err) {
                 .catch(function (err) {
                     console.warn(
                     console.warn('Commons gallery image failed:', filename, err.message);
                        'Commons gallery image failed:',
                        filename,
                        err.message
                    );
                 });
                 });
         });
         });


        /* Hide placeholder spans */
         sources.forEach(function (el) { el.style.display = 'none'; });
         sources.forEach(function (el) {
            el.style.display = 'none';
        });
     });
     });
});
});


/* ============================================================
/* ============================================================
   Commons Gallery Lightbox Viewer (with navigation)
   Commons Gallery Lightbox (keyboard + touch swipe)
   ============================================================ */
   ============================================================ */
(function () {
(function () {
വരി 148: വരി 116:
     var galleryImages = [];
     var galleryImages = [];
     var currentIndex = -1;
     var currentIndex = -1;
    var touchStartX = 0;


     function collectGalleryImages() {
     function collectImages() {
         galleryImages = Array.prototype.slice.call(
         galleryImages = Array.prototype.slice.call(
             document.querySelectorAll('.commons-gallery-item img')
             document.querySelectorAll('.commons-gallery-item img')
വരി 160: വരി 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-prev" aria-label="Previous">&#8249;</button>' +
                 '<button class="clb-next" aria-label="Next"></button>' +
                 '<button class="clb-next" aria-label="Next">&#8250;</button>' +
                 '<button class="clb-close" aria-label="Close">×</button>' +
                 '<button class="clb-close" aria-label="Close">&times;</button>' +
                 '<img class="clb-image" alt="">' +
                 '<img class="clb-image" alt="">' +
                 '<div class="clb-caption"></div>' +
                 '<div class="clb-caption"></div>' +
വരി 173: വരി 141:
         document.body.appendChild(overlay);
         document.body.appendChild(overlay);


         overlay.querySelector('.clb-backdrop').onclick = closeLightbox;
         overlay.querySelector('.clb-backdrop').addEventListener('click', closeLightbox);
         overlay.querySelector('.clb-close').onclick = closeLightbox;
         overlay.querySelector('.clb-close').addEventListener('click', closeLightbox);
         overlay.querySelector('.clb-prev').onclick = showPrev;
         overlay.querySelector('.clb-prev').addEventListener('click', showPrev);
         overlay.querySelector('.clb-next').onclick = showNext;
         overlay.querySelector('.clb-next').addEventListener('click', showNext);


        /* Keyboard navigation */
         document.addEventListener('keydown', function (e) {
         document.addEventListener('keydown', function (e) {
             if (!overlay.classList.contains('active')) return;
             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 === 'ArrowLeft') showPrev();
             if (e.key === 'ArrowRight') showNext();
             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) {
         collectGalleryImages();
         collectImages();
         currentIndex = galleryImages.indexOf(img);
         currentIndex = galleryImages.indexOf(img);
         if (currentIndex === -1) return;
         if (currentIndex === -1) return;
വരി 193: വരി 174:
         createLightbox();
         createLightbox();
         showImage(currentIndex);
         showImage(currentIndex);
         document.getElementById('commons-lightbox').classList.add('active');
         document.getElementById('commons-lightbox').classList.add('active');
         document.body.style.overflow = 'hidden';
         document.body.style.overflow = 'hidden';
വരി 200: വരി 180:
     function showImage(index) {
     function showImage(index) {
         var overlay = document.getElementById('commons-lightbox');
         var overlay = document.getElementById('commons-lightbox');
        var lbImg = overlay.querySelector('.clb-image');
        var caption = overlay.querySelector('.clb-caption');
         var img = galleryImages[index];
         var img = galleryImages[index];
         if (!img) return;
         if (!img) return;


         lbImg.src = img.dataset.fullsrc || img.src;
         overlay.querySelector('.clb-image').src = img.dataset.fullsrc || img.src;
         lbImg.alt = img.alt || '';
         overlay.querySelector('.clb-image').alt = img.alt || '';
         caption.innerHTML = img.dataset.caption || '';
         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;
         currentIndex = index;
     }
     }


     function showPrev() {
     function showPrev() { if (currentIndex > 0) showImage(currentIndex - 1); }
        if (currentIndex > 0) showImage(currentIndex - 1);
     function showNext() { if (currentIndex < galleryImages.length - 1) showImage(currentIndex + 1); }
    }
 
     function showNext() {
        if (currentIndex < galleryImages.length - 1) {
            showImage(currentIndex + 1);
        }
    }


     function closeLightbox() {
     function closeLightbox() {
വരി 230: വരി 204:
     }
     }


    /* Event delegation (single, correct) */
     document.addEventListener('click', function (e) {
     document.addEventListener('click', function (e) {
         var img = e.target;
         if (e.target && e.target.matches('.commons-gallery-item img')) {
        if (img && img.matches('.commons-gallery-item img')) {
             openLightbox(e.target);
             openLightbox(img);
         }
         }
     });
     });
വരി 241: വരി 213:


/* ============================================================
/* ============================================================
   Wikimedia Commons Single Image Loader (CommonsImage)
   Editing tools (load only on edit/upload pages)
  ============================================================ */
mw.hook('wikipage.content').add(function ($content) {
 
    $content.find('.commons-image').each(function () {
        var el = this;
 
        var filename = el.dataset.filename;
        if (!filename) return;
 
        var width = parseInt(el.dataset.width, 10) || 300;
        var align = el.dataset.align || 'none';
        var captionText = el.dataset.caption || '';
 
        var apiUrl =
            'https://commons.wikimedia.org/w/api.php' +
            '?action=query' +
            '&format=json' +
            '&prop=imageinfo' +
            '&iiprop=url|extmetadata' +
            '&iiurlwidth=' + width +
            '&titles=File:' + encodeURIComponent(filename) +
            '&origin=*';
 
        fetch(apiUrl)
            .then(function (r) {
                if (!r.ok) throw new Error('HTTP ' + r.status);
                return r.json();
            })
            .then(function (data) {
                if (!data || !data.query || !data.query.pages) {
                    el.textContent = 'Error loading image';
                    return;
                }
 
                var page =
                    data.query.pages[Object.keys(data.query.pages)[0]];
 
                if (!page || !page.imageinfo) {
                    el.textContent = 'Image not found';
                    return;
                }
 
                var info = page.imageinfo[0];
                var imgUrl = info.thumburl || info.url;
 
                var artist = 'Unknown';
                var license = '';
                var licenseUrl = '';
 
                if (info.extmetadata) {
                    if (info.extmetadata.Artist) {
                        artist = info.extmetadata.Artist.value;
                    }
                    if (info.extmetadata.LicenseShortName) {
                        license = info.extmetadata.LicenseShortName.value;
                    }
                    if (info.extmetadata.LicenseUrl) {
                        licenseUrl = info.extmetadata.LicenseUrl.value;
                    }
                }
 
                var figure = document.createElement('figure');
                figure.style.maxWidth = width + 'px';
 
                if (align === 'right') figure.style.float = 'right';
                if (align === 'left') figure.style.float = 'left';
                if (align === 'center') {
                    figure.style.marginLeft = 'auto';
                    figure.style.marginRight = 'auto';
                }
 
                var img = document.createElement('img');
                img.src = imgUrl;
                img.alt = captionText || filename;
                img.loading = 'lazy';
                img.style.width = '100%';
 
                figure.appendChild(img);
 
                if (captionText || license) {
                    var figcaption = document.createElement('figcaption');
                    figcaption.style.fontSize = '0.8em';
                    figcaption.style.textAlign = 'center';
                    figcaption.innerHTML =
                        (captionText ? captionText + '<br>' : '') +
                        '© ' + artist +
                        (licenseUrl
                            ? ' – <a href="' + licenseUrl +
                              '" target="_blank">' + license + '</a>'
                            : '');
                    figure.appendChild(figcaption);
                }
 
                el.replaceWith(figure);
            })
            .catch(function () {
                el.textContent = 'Error loading image from Commons';
            });
    });
});
 
 
 
/* ============================================================
  Editing tools
   ============================================================ */
   ============================================================ */
if (
if (
വരി 357: വരി 224:
     );
     );
}
}


/* ============================================================
/* ============================================================
വരി 363: വരി 229:
   ============================================================ */
   ============================================================ */
function addCharSubsetMenu() {
function addCharSubsetMenu() {
     if ($('#editpage-specialchars').length > 0) {
     if ($('#editpage-specialchars').length === 0) return;
 
    var s = parseInt(mw.cookie.get('edittoolscharsubset'), 10);
    if (isNaN(s)) s = 0;
 
    var options = [
        'ഫലകങ്ങൾ', 'വിക്കിവിന്യാസങ്ങൾ', 'അനുമതിപത്രങ്ങൾ', 'മലയാളം',
        'കൊറിയൻ', 'ലത്തീൻ', 'ഐ.പി.എ.', 'പലവക',
        'അറബി', 'ദേവനാഗരി', 'ഹിബ്രു', 'പഴയ ഇംഗ്ലീഷ്'
    ];


        var s = parseInt($.cookie('edittoolscharsubset'), 10);
    var $menu = $('<select />')
         if (isNaN(s)) s = 0;
        .attr('id', 'charSubsetControl')
        .css('display', 'inline')
         .change(chooseCharSubset)
        .data('previousSelectedIndex', s);


        var $menu = $('<select />')
    options.forEach(function (label) {
            .attr('id', 'charSubsetControl')
        $menu.append($('<option />').text(label));
            .css('display', 'inline')
    });
            .change(chooseCharSubset)
            .data('previousSelectedIndex', s)
            .append($('<option />').text('ഫലകങ്ങൾ'))
            .append($('<option />').text('വിക്കിവിന്യാസങ്ങൾ'))
            .append($('<option />').text('അനുമതിപത്രങ്ങൾ'))
            .append($('<option />').text('മലയാളം'))
            .append($('<option />').text('കൊറിയൻ'))
            .append($('<option />').text('ലത്തീൻ'))
            .append($('<option />').text('ഐ.പി.എ.'))
            .append($('<option />').text('പലവക'))
            .append($('<option />').text('അറബി'))
            .append($('<option />').text('ദേവനാഗരി'))
            .append($('<option />').text('ഹിബ്രു'))
            .append($('<option />').text('പഴയ ഇംഗ്ലീഷ്'));


        $('#editpage-specialchars').prepend($menu);
    $('#editpage-specialchars').prepend($menu);
        $('#charSubsetControl')[0].selectedIndex = s;
    $('#charSubsetControl')[0].selectedIndex = s;


        $('p', '#editpage-specialchars').each(function (index) {
    $('p', '#editpage-specialchars').each(function (index) {
            $(this).css({
        $(this).css({
                display: index === s ? 'inline' : 'none',
            display: index === s ? 'inline' : 'none',
                visibility: index === s ? 'visible' : 'hidden'
            visibility: index === s ? 'visible' : 'hidden'
            });
         });
         });
     }
     });
}
}


വരി 406: വരി 269:
         });
         });
     });
     });
     $.cookie('edittoolscharsubset', selectedIndex);
     mw.cookie.set('edittoolscharsubset', selectedIndex);
}
}


$(addCharSubsetMenu);
$(addCharSubsetMenu);


/* ============================================================
/* ============================================================
   Navigation bars
   Navigation bars (NavFrame show/hide)
   ============================================================ */
   ============================================================ */
var NavigationBarHide = 'മറയ്ക്കുക';
var NavigationBarHide = 'മറയ്ക്കുക';
വരി 427: വരി 289:
     NavToggle.firstChild.data = show ? NavigationBarHide : NavigationBarShow;
     NavToggle.firstChild.data = show ? NavigationBarHide : NavigationBarShow;


     var children = NavFrame.children;
     Array.prototype.forEach.call(NavFrame.children, function (child) {
    for (var i = 0; i < children.length; i++) {
         if (child.classList.contains('NavContent') || child.classList.contains('NavPic')) {
         if ($(children[i]).hasClass('NavContent') ||
             child.style.display = show ? 'block' : 'none';
            $(children[i]).hasClass('NavPic')) {
             children[i].style.display = show ? 'block' : 'none';
         }
         }
     }
     });
     event.preventDefault();
     event.preventDefault();
};
};
വരി 441: വരി 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' + indexNavigationBar)
        var $NavToggle = $('<a href="#" class="NavToggle"></a>')
             .attr('id', 'NavToggle' + idx)
             .text(NavigationBarHide)
             .text(NavigationBarHide)
             .on('click', function (e) {
             .on('click', function (e) {
                 toggleNavigationBar(indexNavigationBar, e);
                 toggleNavigationBar(idx, e);
             });
             });


         NavFrame.id = 'NavFrame' + indexNavigationBar;
         NavFrame.id = 'NavFrame' + idx;
         NavHead.append(NavToggle);
         $NavHead.append($NavToggle);
     });
     });
});
});


/* ============================================================
/* ============================================================
വരി 461: വരി 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("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;


    var Rows = Table.rows;
     var hide = Button.firstChild.data === collapseCaption;
     var hide = Button.firstChild.data === collapseCaption;
 
     Array.prototype.forEach.call(Table.rows, function (row, i) {
     for (var i = 1; i < Rows.length; i++) {
         if (i > 0) row.style.display = hide ? 'none' : '';
         Rows[i].style.display = hide ? "none" : "";
     });
     }
     Button.firstChild.data = hide ? expandCaption : collapseCaption;
     Button.firstChild.data = hide ? expandCaption : collapseCaption;
}
}
വരി 485: വരി 343:
         if (!$header.length) return;
         if (!$header.length) return;


         $table.attr('id', 'collapsibleTable' + tableIndex);
        var idx = tableIndex;
         $table.attr('id', 'collapsibleTable' + idx);


        var $button = $('<span class="collapseButton">[</span>');
         var $link = $('<a href="#"></a>')
         var $link = $('<a href="#"></a>')
             .attr('id', 'collapseButton' + tableIndex)
             .attr('id', 'collapseButton' + idx)
             .text(collapseCaption)
             .text(collapseCaption)
             .on('click', function (e) {
             .on('click', function (e) {
                 e.preventDefault();
                 e.preventDefault();
                 collapseTable(tableIndex);
                 collapseTable(idx);
             });
             });


         $button.append($link).append(']');
         $header.prepend($('<span class="collapseButton">[</span>').append($link).append(']'));
        $header.prepend($button);
 
         tableIndex++;
         tableIndex++;
     });
     });
});
});

23:26, 29 മേയ് 2026-നു നിലവിലുള്ള രൂപം

//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
   ============================================================ */
window.addEventListener('load', function () {
    document.querySelectorAll('img').forEach(function (img) {
        if (img.src && img.src.startsWith('https://tile.openstreetmap.org/v4/marker/')) {
            img.src = img.src.replace(
                'https://tile.openstreetmap.org/v4/marker/',
                'https://maps.wikimedia.org/v4/marker/'
            );
        }
    });
});

/* ============================================================
   Wikimedia Commons Gallery Loader
   ============================================================ */
mw.hook('wikipage.content').add(function ($content) {

    $content.find('.commons-gallery').each(function () {
        var container = this;
        var sources = container.querySelectorAll('.commons-gallery-source');
        if (!sources.length) return;

        var width = 300;
        var grid = document.createElement('div');
        grid.className = 'commons-gallery-grid';
        container.appendChild(grid);

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

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

            fetch(apiUrl)
                .then(function (r) {
                    if (!r.ok) throw new Error('HTTP ' + r.status);
                    return r.json();
                })
                .then(function (data) {
                    if (!data || !data.query || !data.query.pages) return;
                    var page = data.query.pages[Object.keys(data.query.pages)[0]];
                    if (!page || !page.imageinfo) return;

                    var info = page.imageinfo[0];
                    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 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'];
                        }
                    }

                    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');
                    figure.className = 'commons-gallery-item';

                    var img = document.createElement('img');
                    img.src = imgUrl;
                    img.alt = captionText || filename;
                    img.loading = 'lazy';
                    img.dataset.fullsrc = lightboxSrc;
                    img.dataset.caption = captionHtml;

                    var figcaption = document.createElement('figcaption');
                    figcaption.innerHTML = captionHtml;

                    figure.appendChild(img);
                    figure.appendChild(figcaption);
                    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 (keyboard + touch swipe)
   ============================================================ */
(function () {

    var galleryImages = [];
    var currentIndex = -1;
    var touchStartX = 0;

    function collectImages() {
        galleryImages = Array.prototype.slice.call(
            document.querySelectorAll('.commons-gallery-item img')
        );
    }

    function createLightbox() {
        if (document.getElementById('commons-lightbox')) return;

        var overlay = document.createElement('div');
        overlay.id = 'commons-lightbox';
        overlay.innerHTML =
            '<div class="clb-backdrop"></div>' +
            '<div class="clb-content">' +
                '<button class="clb-prev" aria-label="Previous">&#8249;</button>' +
                '<button class="clb-next" aria-label="Next">&#8250;</button>' +
                '<button class="clb-close" aria-label="Close">&times;</button>' +
                '<img class="clb-image" alt="">' +
                '<div class="clb-caption"></div>' +
            '</div>';

        document.body.appendChild(overlay);

        overlay.querySelector('.clb-backdrop').addEventListener('click', closeLightbox);
        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) {
            if (!document.getElementById('commons-lightbox').classList.contains('active')) return;
            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) {
        collectImages();
        currentIndex = galleryImages.indexOf(img);
        if (currentIndex === -1) return;

        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 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() {
        var overlay = document.getElementById('commons-lightbox');
        if (!overlay) return;
        overlay.classList.remove('active');
        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 (
    mw.config.get('wgAction') === 'edit' ||
    mw.config.get('wgAction') === 'submit' ||
    mw.config.get('wgCanonicalSpecialPageName') === 'Upload'
) {
    mw.loader.load(
        '/index.php?title=മീഡിയവിക്കി:Common.js/edit.js&action=raw&ctype=text/javascript'
    );
}

/* ============================================================
   Special characters subset menu
   ============================================================ */
function addCharSubsetMenu() {
    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'
        });
    });
}

function chooseCharSubset() {
    var selectedIndex = $(this).find(':selected').index();
    $('p', '#editpage-specialchars').each(function (index) {
        $(this).css({
            display: index === selectedIndex ? 'inline' : 'none',
            visibility: index === selectedIndex ? 'visible' : 'hidden'
        });
    });
    mw.cookie.set('edittoolscharsubset', selectedIndex);
}

$(addCharSubsetMenu);

/* ============================================================
   Navigation bars (NavFrame show/hide)
   ============================================================ */
var NavigationBarHide = 'മറയ്ക്കുക';
var NavigationBarShow = 'പ്രദർശിപ്പിക്കുക';
var indexNavigationBar = 0;

window.toggleNavigationBar = function (index, event) {
    var NavToggle = document.getElementById('NavToggle' + index);
    var NavFrame = document.getElementById('NavFrame' + index);
    if (!NavFrame || !NavToggle) return;

    var show = NavToggle.firstChild.data === NavigationBarShow;
    NavToggle.firstChild.data = show ? NavigationBarHide : NavigationBarShow;

    Array.prototype.forEach.call(NavFrame.children, function (child) {
        if (child.classList.contains('NavContent') || child.classList.contains('NavPic')) {
            child.style.display = show ? 'block' : 'none';
        }
    });
    event.preventDefault();
};

mw.hook('wikipage.content').add(function ($content) {
    $content.find('div.NavFrame').each(function () {
        indexNavigationBar++;
        var NavFrame = this;
        var $NavHead = $(NavFrame).find('.NavHead').first();
        if (!$NavHead.length) return;

        var idx = indexNavigationBar;
        var $NavToggle = $('<a href="#" class="NavToggle"></a>')
            .attr('id', 'NavToggle' + idx)
            .text(NavigationBarHide)
            .on('click', function (e) {
                toggleNavigationBar(idx, e);
            });

        NavFrame.id = 'NavFrame' + idx;
        $NavHead.append($NavToggle);
    });
});

/* ============================================================
   Collapsible tables
   ============================================================ */
var autoCollapse = 2;
var collapseCaption = 'മറയ്ക്കുക';
var expandCaption = 'പ്രദർശിപ്പിക്കുക';

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

    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;
}

$(function () {
    var tableIndex = 0;
    $('table.collapsible').each(function () {
        var $table = $(this);
        var $header = $table.find('tr:first th:first');
        if (!$header.length) return;

        var idx = tableIndex;
        $table.attr('id', 'collapsibleTable' + idx);

        var $link = $('<a href="#"></a>')
            .attr('id', 'collapseButton' + idx)
            .text(collapseCaption)
            .on('click', function (e) {
                e.preventDefault();
                collapseTable(idx);
            });

        $header.prepend($('<span class="collapseButton">[</span>').append($link).append(']'));
        tableIndex++;
    });
});
"https://schoolwiki.in/index.php?title=മീഡിയവിക്കി:Common.js&oldid=2989107" എന്ന താളിൽനിന്ന് ശേഖരിച്ചത്