Jump to content

MediaWiki:Common.js: Difference between revisions

From Costa Sano MediaWiki
No edit summary
No edit summary
Line 38: Line 38:


/************************************************************
/************************************************************
  * 2. AUTOMATIC GEOCODING (updated for current Form:Place)
  * 2. AUTOMATIC GEOCODING (Page Forms–safe)
  ************************************************************/
  ************************************************************/
$(function () {
$(function () {


     const addr = $('input[name="Address"]');
     const addr = $('input[name$="[Address]"]');
     const lat  = $('input[name="Latitude"]');
     const lat  = $('input[name$="[Latitude]"]');
     const lon  = $('input[name="Longitude"]');
     const lon  = $('input[name$="[Longitude]"]');


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


     if (!addr.length || !lat.length || !lon.length) {
     if (!addr.length || !lat.length || !lon.length) {
         console.log("Geocoding fields not found");
         console.log("Required fields not found for geocoding");
         return;
         return;
     }
     }
Line 75: Line 76:


/************************************************************
/************************************************************
  * 3. OSM LINK BELOW LONGITUDE FIELD (updated)
  * 3. OSM LINK BELOW LONGITUDE FIELD
  ************************************************************/
  ************************************************************/
$(function () {
$(function () {


     const lat = $('input[name="Latitude"]');
     const lat = $('input[name$="[Latitude]"]');
     const lon = $('input[name="Longitude"]');
     const lon = $('input[name$="[Longitude]"]');


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


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


Line 97: Line 99:
                 '&mlon=' + lo;
                 '&mlon=' + lo;
             linkBox.html(
             linkBox.html(
                 '<a href="' + url + '" target="_blank">View on OpenStreetMap</a>'
                 '<a href="' + url +
                '" target="_blank">View on OpenStreetMap</a>'
             );
             );
         } else {
         } else {

Revision as of 15:40, 16 February 2026

/************************************************************
 * 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. AUTOMATIC GEOCODING (Page Forms–safe)
 ************************************************************/
$(function () {

    const addr = $('input[name$="[Address]"]');
    const lat  = $('input[name$="[Latitude]"]');
    const lon  = $('input[name$="[Longitude]"]');

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

    if (!addr.length || !lat.length || !lon.length) {
        console.log("Required fields not found for geocoding");
        return;
    }

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

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

        console.log("Geocoding address:", 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');
            }
        });
    });
});


/************************************************************
 * 3. OSM LINK BELOW LONGITUDE FIELD
 ************************************************************/
$(function () {

    const lat = $('input[name$="[Latitude]"]');
    const lon = $('input[name$="[Longitude]"]');

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

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

    lat.on('change keyup', updateOSMLink);
    lon.on('change keyup', updateOSMLink);

    updateOSMLink();
});