Jump to content

MediaWiki:Common.js: Difference between revisions

From Costa Sano MediaWiki
No edit summary
No edit summary
Line 2: Line 2:
  * 1. AUTO‑PURGE FOR ALL DASHBOARD:* PAGES
  * 1. AUTO‑PURGE FOR ALL DASHBOARD:* PAGES
  ************************************************************/
  ************************************************************/
mw.hook('wikipage.content').add(function () {
$(function () {
     const title = mw.config.get('wgPageName');
     const title = mw.config.get('wgPageName');


     // Only apply to Dashboard:* pages
     // Only apply to Dashboard:* pages
     if (title.startsWith('Dashboard:')) {
     if (title && title.startsWith('Dashboard:')) {


         // Avoid infinite loop: only purge if not already purged
         // Avoid infinite loop: only purge if not already purged
Line 14: Line 14:
     }
     }
});
});


/************************************************************
/************************************************************

Revision as of 21:20, 14 February 2026

/************************************************************
 * 1. AUTO‑PURGE FOR ALL DASHBOARD:* PAGES
 ************************************************************/
$(function () {
    const title = mw.config.get('wgPageName');

    // Only apply to Dashboard:* pages
    if (title && title.startsWith('Dashboard:')) {

        // Avoid infinite loop: only purge if not already purged
        if (!window.location.search.includes('action=purge')) {
            window.location.href = mw.util.getUrl(title, { action: 'purge' });
        }
    }
});

/************************************************************
 * 2. AUTOMATIC GEOCODING (your existing code, unchanged)
 ************************************************************/
$(function() {
    const addr = $('input[name="Place[address]"]');
    const lat  = $('input[name="Place[latitude]"]');
    const lon  = $('input[name="Place[longitude]"]');

    console.log("Address fields found:", addr.length);

    if (addr.length === 0) {
        console.log("Address field not found");
        return;
    }

    addr.on('change', function() {
        console.log("Address changed:", addr.val());

        const q = encodeURIComponent(addr.val());
        if (!q) return;

        const url = 'https://nominatim.openstreetmap.org/search?format=json&q=' + q;

        $.getJSON(url, function(data) {
            if (data && data.length > 0) {
                lat.val(data[0].lat);
                lon.val(data[0].lon);
            }
        });
    });
});


/************************************************************
 * 3. OSM LINK BELOW LONGITUDE FIELD (your existing code)
 ************************************************************/
$(function() {
    const lat = $('input[name="Place[latitude]"]');
    const lon = $('input[name="Place[longitude]"]');

    // Add a small container under the longitude field
    const linkBox = $('<div id="pf-osm-link" style="margin-top:4px;"></div>');
    lon.closest('td').append(linkBox);

    function updateOSMLink() {
        const la = lat.val();
        const lo = lon.val();
        if (la && lo) {
            const url = 'https://www.openstreetmap.org/?mlat=' + la + '&mlon=' + lo;
            linkBox.html('<a href="' + url + '" target="_blank">View on OpenStreetMap</a>');
        } else {
            linkBox.empty();
        }
    }

    // Update whenever coordinates change
    lat.on('change keyup', updateOSMLink);
    lon.on('change keyup', updateOSMLink);

    // Initial update (for editing existing pages)
    updateOSMLink();
});