Jump to content

MediaWiki:Common.js: Difference between revisions

From Costa Sano MediaWiki
No edit summary
No edit summary
(10 intermediate revisions by the same user not shown)
Line 3: Line 3:
  ************************************************************/
  ************************************************************/
$(function () {
$(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');
     const title = mw.config.get('wgPageName');


Line 33: Line 37:
});
});


/************************************************************
* 2. PAGE FORMS GEOCODING (ON BLUR ONLY – STABLE)
************************************************************/
mw.hook('wikipage.content').add(function () {


    function setupGeocoding(addressField, latField, lonField) {


/************************************************************
        const addr = $('input[name$="[' + addressField + ']"]');
* 2. AUTOMATIC GEOCODING (your existing code, unchanged)
        const lat  = $('input[name$="[' + latField + ']"]');
************************************************************/
        const lon  = $('input[name$="[' + lonField + ']"]');
$(function() {
 
    const addr = $('input[name="Place[address]"]');
        if (!addr.length || !lat.length || !lon.length) return;
    const lat  = $('input[name="Place[latitude]"]');
    const lon  = $('input[name="Place[longitude]"]');


    console.log("Address fields found:", addr.length);
        // Remove previous handlers to avoid duplicates
        addr.off('.pfGeocode');


    if (addr.length === 0) {
        addr.on('blur.pfGeocode', function () {
        console.log("Address field not found");
        return;
    }


    addr.on('change', function() {
            const q = addr.val().trim();
        console.log("Address changed:", addr.val());
            if (!q) return;


        const q = encodeURIComponent(addr.val());
            console.log('Geocoding on blur:', q);
        if (!q) return;


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


        $.getJSON(url, function(data) {
            $.getJSON(url, function (data) {
            if (data && data.length > 0) {
                if (data && data.length) {
                lat.val(data[0].lat);
                    lat.val(data[0].lat).trigger('change');
                lon.val(data[0].lon);
                    lon.val(data[0].lon).trigger('change');
             }
                }
             });
         });
         });
     });
     }
 
    // === ENABLE FOR PLACE ===
    setupGeocoding('Address', 'Latitude', 'Longitude');
});
});




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


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


     function updateOSMLink() {
     function setupOSMLink(latField, lonField) {
         const la = lat.val();
 
        const lo = lon.val();
        const lat = $('input[name$="[' + latField + ']"]');
        if (la && lo) {
        const lon = $('input[name$="[' + lonField + ']"]');
            const url = 'https://www.openstreetmap.org/?mlat=' + la + '&mlon=' + lo;
 
            linkBox.html('<a href="' + url + '" target="_blank">View on OpenStreetMap</a>');
        if (!lat.length || !lon.length) return;
        } else {
 
            linkBox.empty();
        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();
     }
     }


     // Update whenever coordinates change
     // === ENABLE FOR PLACE ===
     lat.on('change keyup', updateOSMLink);
     setupOSMLink('Latitude', 'Longitude');
    lon.on('change keyup', updateOSMLink);
});
 
/************************************************************
* 4. EMPTY MANDATORY FIELD CHECK (PAGE FORMS ONLY)
************************************************************/
mw.hook('wikipage.content').add(function () {


     // Initial update (for editing existing pages)
     // Only run on Page Forms edit pages
     updateOSMLink();
    if (mw.config.get('wgCanonicalSpecialPageName') !== 'FormEdit') {
        return;
    }
 
    // Save buttons used by Page Forms
    const saveButtons = $('input[name="wpSave"], button[name="wpSave"]');
    if (!saveButtons.length) return;
 
    // Avoid duplicate bindings
    saveButtons.off('.pfRequiredCheck');
 
     saveButtons.on('click.pfRequiredCheck', function (e) {
 
        const requiredFields = [
            { name: 'Label', label: 'Name' }
        ];
 
        let missing = [];
 
        requiredFields.forEach(function (f) {
            const input = $(
                'input[name$="[' + f.name + ']"], ' +
                'textarea[name$="[' + f.name + ']"], ' +
                'select[name$="[' + f.name + ']"]'
            );
 
            if (!input.length || !input.val().trim()) {
                missing.push(f.label);
            }
        });
 
        if (missing.length) {
            e.preventDefault();
            e.stopImmediatePropagation();
 
            alert(
                'Please fill in the following required field(s):\n\n' +
                missing.join('\n')
            );
 
            return false;
        }
    });
});
});

Revision as of 20:47, 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. PAGE FORMS GEOCODING (ON BLUR ONLY – STABLE)
 ************************************************************/

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 + ']"]');

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

        // Remove previous handlers to avoid duplicates
        addr.off('.pfGeocode');

        addr.on('blur.pfGeocode', function () {

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

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

    // === 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');
});

/************************************************************
 * 4. EMPTY MANDATORY FIELD CHECK (PAGE FORMS ONLY)
 ************************************************************/
mw.hook('wikipage.content').add(function () {

    // Only run on Page Forms edit pages
    if (mw.config.get('wgCanonicalSpecialPageName') !== 'FormEdit') {
        return;
    }

    // Save buttons used by Page Forms
    const saveButtons = $('input[name="wpSave"], button[name="wpSave"]');
    if (!saveButtons.length) return;

    // Avoid duplicate bindings
    saveButtons.off('.pfRequiredCheck');

    saveButtons.on('click.pfRequiredCheck', function (e) {

        const requiredFields = [
            { name: 'Label', label: 'Name' }
        ];

        let missing = [];

        requiredFields.forEach(function (f) {
            const input = $(
                'input[name$="[' + f.name + ']"], ' +
                'textarea[name$="[' + f.name + ']"], ' +
                'select[name$="[' + f.name + ']"]'
            );

            if (!input.length || !input.val().trim()) {
                missing.push(f.label);
            }
        });

        if (missing.length) {
            e.preventDefault();
            e.stopImmediatePropagation();

            alert(
                'Please fill in the following required field(s):\n\n' +
                missing.join('\n')
            );

            return false;
        }
    });
});