MediaWiki:Common.js: Difference between revisions

From Jcastle.info
No edit summary
No edit summary
 
(14 intermediate revisions by the same user not shown)
Line 2: Line 2:
mw.loader.load('//use.fontawesome.com/053a76b93c.js');
mw.loader.load('//use.fontawesome.com/053a76b93c.js');


$(function () {
setTimeout(function () {
   // Run only on Semantic Forms pages
   var $gpsInput = $('input[name="Castle Photo[GPSLocation]"]');
   if (mw.config.get('wgAction') === 'formedit') {
   var $orderInput = $('input[name="Castle Photo[order]"]');
    console.log("📄 GPS helper active on form edit page");
  var $imgEl = $('img[alt$=".jpg"], img[alt$=".jpeg"], img[alt$=".JPG"], img[alt$=".JPEG"]');


    setTimeout(function () {
  if ($gpsInput.length === 0 || $orderInput.length === 0 || $imgEl.length === 0) return;
      var latText = mw.util.$content.find('.mediaexif-gpslatitude').text().trim();
      var lonText = mw.util.$content.find('.mediaexif-gpslongitude').text().trim();


      if (!latText || !lonText) {
  var filename = $imgEl.attr('alt');  // e.g., yakami25.jpg
        console.log("❌ No GPSLatitude or GPSLongitude found.");
        return;
      }


      // Convert DMS to decimal
  // Try to extract GPS values (optional)
      function dmsToDecimal(dmsStr) {
  var gpsValue = null;
        var parts = dmsStr.match(/([\d.]+)[^\d]+([\d.]+)[^\d]+([\d.]+)?[^\d]+([NSEW])/);
  var $latEl = $('.mediaexif-gpslatitude');
        if (!parts) return null;
  var $lonEl = $('.mediaexif-gpslongitude');
        var deg = parseFloat(parts[1]);
  if ($latEl.length && $lonEl.length) {
        var min = parseFloat(parts[2]);
    var latDec = parseFloat($latEl.text().trim());
        var sec = parseFloat(parts[3] || "0");
    var lonDec = parseFloat($lonEl.text().trim());
        var ref = parts[4];
    if (!isNaN(latDec) && !isNaN(lonDec)) {
        var dec = deg + min / 60 + sec / 3600;
      gpsValue = latDec.toFixed(6) + ", " + lonDec.toFixed(6);
        if (ref === "S" || ref === "W") dec *= -1;
    }
        return dec.toFixed(6);
  }
      }


      var latDec = dmsToDecimal(latText);
  var $button = $('<button>')
      var lonDec = dmsToDecimal(lonText);
    .text("📋 Insert EXIF GPS & Order")
       if (!latDec || !lonDec) {
    .attr("type", "button")
         console.log("❌ GPS conversion failed.");
    .css({ marginLeft: "0.5em", fontSize: "90%" })
        return;
    .click(function () {
       if (gpsValue) {
         $gpsInput.val(gpsValue).focus().trigger('change');
       }
       }


       var gpsValue = latDec + ", " + lonDec;
       var match = filename.match(/\d+/);
 
       if (match) {
       // Add button next to GPSLocation input field
        var orderValue = parseInt(match[0], 10) * 10;
      var $input = $('input[name$="[GPSLocation]"], textarea[name$="[GPSLocation]"]');
        $orderInput.val(orderValue).focus().trigger('change');
 
      if ($input.length === 0) {
        console.log("⚠️ Could not find GPSLocation input field.");
        return;
       }
       }


      var $button = $('<button>')
//      alert("✅ Fields updated.");
        .text("📋 Use EXIF GPS: " + gpsValue)
    });
        .css({ marginLeft: "1em", fontSize: "90%" })
        .click(function (e) {
          e.preventDefault();
          $input.val(gpsValue).focus();
          alert("✅ GPS inserted into field.");
        });


      $input.after($button);
  $gpsInput.after($button);
 
}, 500);
    }, 500); // Wait for DOM and EXIF to fully render
  }
});

Latest revision as of 16:09, 18 May 2025

/* Any JavaScript here will be loaded for all users on every page load. */
mw.loader.load('//use.fontawesome.com/053a76b93c.js');

setTimeout(function () {
  var $gpsInput = $('input[name="Castle Photo[GPSLocation]"]');
  var $orderInput = $('input[name="Castle Photo[order]"]');
  var $imgEl = $('img[alt$=".jpg"], img[alt$=".jpeg"], img[alt$=".JPG"], img[alt$=".JPEG"]');

  if ($gpsInput.length === 0 || $orderInput.length === 0 || $imgEl.length === 0) return;

  var filename = $imgEl.attr('alt');  // e.g., yakami25.jpg

  // Try to extract GPS values (optional)
  var gpsValue = null;
  var $latEl = $('.mediaexif-gpslatitude');
  var $lonEl = $('.mediaexif-gpslongitude');
  if ($latEl.length && $lonEl.length) {
    var latDec = parseFloat($latEl.text().trim());
    var lonDec = parseFloat($lonEl.text().trim());
    if (!isNaN(latDec) && !isNaN(lonDec)) {
      gpsValue = latDec.toFixed(6) + ", " + lonDec.toFixed(6);
    }
  }

  var $button = $('<button>')
    .text("📋 Insert EXIF GPS & Order")
    .attr("type", "button")
    .css({ marginLeft: "0.5em", fontSize: "90%" })
    .click(function () {
      if (gpsValue) {
        $gpsInput.val(gpsValue).focus().trigger('change');
      }

      var match = filename.match(/\d+/);
      if (match) {
        var orderValue = parseInt(match[0], 10) * 10;
        $orderInput.val(orderValue).focus().trigger('change');
      }

//      alert("✅ Fields updated.");
    });

  $gpsInput.after($button);
}, 500);