Jump to content

MediaWiki:Common.js: Difference between revisions

From Costa Sano MediaWiki
No edit summary
No edit summary
Line 1: Line 1:
// The form is already present in the DOM, so we can run immediately
/************************************************************
* 1. AUTO‑PURGE FOR ALL DASHBOARD:* PAGES
************************************************************/
mw.hook('wikipage.content').add(function () {
    const title = mw.config.get('wgPageName');
 
    // Only apply to Dashboard:* pages
    if (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() {
$(function() {
     const addr = $('input[name="Place[address]"]');
     const addr = $('input[name="Place[address]"]');
Line 13: Line 32:


     addr.on('change', function() {
     addr.on('change', function() {
         console.log("Address changed:", addr.val());   // <— ADD THIS LINE
         console.log("Address changed:", addr.val());


         const q = encodeURIComponent(addr.val());
         const q = encodeURIComponent(addr.val());
Line 29: Line 48:
});
});


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

Revision as of 21:14, 14 February 2026

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

    // Only apply to Dashboard:* pages
    if (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();
});