Jump to content

MediaWiki:Common.js

From Costa Sano MediaWiki
Revision as of 15:47, 16 February 2026 by Mngr (talk | contribs)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/************************************************************
 * 1. AUTO‑PURGE FOR ALL DASHBOARD:* PAGES
 ************************************************************/
$(function () {

    // Prevent purge logic from running on edit, history, diff, preview, etc.
    if (mw.config.get('wgAction') !== 'view') return;

    const title = mw.config.get('wgPageName');

    if (title && title.startsWith('Dashboard:')) {

        // Only purge once
        if (!location.search.includes('mw_purged=1')) {

            new mw.Api().get({
                action: 'query',
                meta: 'tokens',
                type: 'csrf'
            }).done(function (data) {

                const token = data.query.tokens.csrftoken;

                new mw.Api().post({
                    action: 'purge',
                    titles: title,
                    token: token
                }).always(function () {

                    // Reload the dashboard normally, marked as purged
                    const url = mw.util.getUrl(title, { mw_purged: 1 });
                    location.replace(url);
                });
            });
        }
    }
});

/************************************************************
 * 2. GENERIC PAGE FORMS GEOCODING (SAFE + DEBOUNCED)
 ************************************************************/

mw.hook('wikipage.content').add(function () {

    function setupGeocoding(addressField, latField, lonField) {

        const addr = $('input[name$="[' + addressField + ']"]');
        const lat  = $('input[name$="[' + latField + ']"]');
        const lon  = $('input[name$="[' + lonField + ']"]');

        console.log('Geocoding fields:',
            addr.length, lat.length, lon.length);

        if (!addr.length || !lat.length || !lon.length) return;

        let debounceTimer = null;

        addr.on('input change', function () {

            clearTimeout(debounceTimer);

            debounceTimer = setTimeout(function () {

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

                console.log('Geocoding:', q);

                const url =
                    'https://nominatim.openstreetmap.org/search' +
                    '?format=json&limit=1&q=' + encodeURIComponent(q);

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

            }, 500);
        });
    }

    // === ENABLE FOR PLACE ===
    setupGeocoding('Address', 'Latitude', 'Longitude');
});


/************************************************************
 * 3. GENERIC OSM LINK HELPER (SAFE)
 ************************************************************/

mw.hook('wikipage.content').add(function () {

    function setupOSMLink(latField, lonField) {

        const lat = $('input[name$="[' + latField + ']"]');
        const lon = $('input[name$="[' + lonField + ']"]');

        if (!lat.length || !lon.length) return;

        const linkBox =
            $('<div class="pf-osm-link" style="margin-top:4px;"></div>');
        lon.closest('td').append(linkBox);

        function update() {

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

        lat.on('change keyup', update);
        lon.on('change keyup', update);
        update();
    }

    // === ENABLE FOR PLACE ===
    setupOSMLink('Latitude', 'Longitude');
});