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

മീഡിയവിക്കി:Common.js

Schoolwiki സംരംഭത്തിൽ നിന്ന്
23:26, 29 മേയ് 2026-നു ഉണ്ടായിരുന്ന രൂപം സൃഷ്ടിച്ചത്:- Ranjithsiji (സംവാദം | സംഭാവനകൾ) (optimize js)
(മാറ്റം) ←പഴയ രൂപം | ഇപ്പോഴുള്ള രൂപം (മാറ്റം) | പുതിയ രൂപം→ (മാറ്റം)

ശ്രദ്ധിക്കുക: സേവ് ചെയ്തശേഷം മാറ്റങ്ങൾ കാണാനായി താങ്കൾക്ക് ബ്രൗസറിന്റെ കാഷെ ഒഴിവാക്കേണ്ടി വന്നേക്കാം.

  • ഫയർഫോക്സ് / സഫാരി: Reload ബട്ടൺ അമർത്തുമ്പോൾ Shift കീ അമർത്തി പിടിക്കുകയോ, Ctrl-F5 അല്ലെങ്കിൽ Ctrl-R (മാക്കിന്റോഷിൽ ⌘-R ) എന്ന് ഒരുമിച്ച് അമർത്തുകയോ ചെയ്യുക
  • ഗൂഗിൾ ക്രോം: Ctrl-Shift-R (മാക്കിന്റോഷിൽ ⌘-Shift-R ) അമർത്തുക
  • ഇന്റർനെറ്റ് എക്സ്പ്ലോറർ: Refresh ബട്ടൺ അമർത്തുമ്പോൾ Ctrl കീ അമർത്തിപിടിക്കുക. അല്ലെങ്കിൽ Ctrl-F5 അമർത്തുക
  • ഓപ്പറ: Menu → Settings എടുക്കുക (മാക്കിൽ Opera → Preferences) എന്നിട്ട് Privacy & security → Clear browsing data → Cached images and files ചെയ്യുക.
//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" എന്ന താളിൽനിന്ന് ശേഖരിച്ചത്