Difference between revisions of "MediaWiki:Timeless.js"

From Medivia Online Wiki
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
/* All JavaScript here will be loaded for users of the Timeless skin */
console.log("Timeless.js loaded");
(function () {
(function () {
   function normalizeToFileTitle(val) {
   function normalizeRoot(root) {
    if (!val) return "";
    if (root && (root.nodeType === 1 || root.nodeType === 9) && typeof root.querySelector === "function") {
     val = val.trim();
      return root;
     if (!val) return "";
    }
    return document;
  }
 
function normalizeToFileTitle(val) {
  if (!val) return "";
  val = ("" + val).trim();
  if (!val) return "";
 
  // Handle full wikilink formats:
  // [[File:Name.png]]
  // [[File:Name.png|...]]
  // [[Image:Name.png]] (alias)
 
    // Strip trailing ]] / leading [[ if someone pastes partial
    val = val.replace(/^\[\[\s*/, "").replace(/\s*\]\]$/, "");
 
     // If still contains pipe, keep left side
    if (val.includes("|")) val = val.split("|")[0].trim();
 
    // Convert Image: to File:
    val = val.replace(/^Image:/i, "File:");
 
    // Ensure File:
     if (!/^File:/i.test(val)) val = "File:" + val.trim();
 
  return val;
}


    // Allow "File:Something.png" or just "Something.png"
    if (!/^File:/i.test(val)) val = "File:" + val;
    return val;
  }


   function fileTitleToThumbUrl(fileTitle) {
   function fileTitleToThumbUrl(fileTitle) {
    // MediaWiki thumb URL via Special:Redirect (works without knowing upload path)
    // Example: /wiki/Special:Redirect/file/Some.png?width=64
     var name = fileTitle.replace(/^File:/i, "");
     var name = fileTitle.replace(/^File:/i, "");
     return mw.util.getUrl("Special:Redirect/file/" + name) + "?width=64";
     return mw.util.getUrl("Special:Redirect/file/" + name) + "?width=64";
   }
   }


   function hookPageFormsImagePreview() {
   function ensurePreviewDiv(input) {
     // Page Forms inputs usually end up as <input name="...">
     var existing = document.getElementById("pf-image-preview");
     var input =
    if (existing) return existing;
      document.querySelector('input[name="image"]') ||
 
      document.querySelector('input[name="Item[image]"]') ||
    var div = document.createElement("div");
      document.querySelector('input[name$="[image]"]');
    div.id = "pf-image-preview";
    div.style.width = "64px";
    div.style.height = "64px";
    div.style.border = "1px solid #333";
    div.style.marginTop = "6px";
    div.style.display = "flex";
    div.style.alignItems = "center";
    div.style.justifyContent = "center";
    div.style.fontSize = "11px";
    div.style.textAlign = "center";
    div.style.backgroundRepeat = "no-repeat";
    div.style.backgroundPosition = "center";
     div.style.backgroundSize = "contain";
    div.style.color = "#aaa";
    div.textContent = "No image";
 
    input.parentNode.appendChild(div);
    return div;
  }
 
  function hookItemImagePreview(root) {
    root = normalizeRoot(root);
 
    var input = root.querySelector("input.pf-item-image");
    if (!input) return;


     var img = document.getElementById("pf-image-preview");
     var preview = ensurePreviewDiv(input);
    var txt = document.getElementById("pf-image-preview-text");
    if (!input || !img || !txt) return;


     function update() {
     function update() {
       var fileTitle = normalizeToFileTitle(input.value);
       var fileTitle = normalizeToFileTitle(input.value);
       if (!fileTitle) {
       if (!fileTitle) {
         img.style.display = "none";
         preview.style.backgroundImage = "";
         txt.style.display = "";
         preview.textContent = "No image";
        txt.innerHTML = "<i>No image selected.</i>";
        img.removeAttribute("src");
         return;
         return;
       }
       }


       var url = fileTitleToThumbUrl(fileTitle);
       var url = fileTitleToThumbUrl(fileTitle);
       img.onload = function () {
 
         img.style.display = "";
      // Test image existence using background image load trick
         txt.style.display = "none";
       var test = new Image();
      test.onload = function () {
         preview.style.backgroundImage = "url('" + url + "')";
         preview.textContent = "";
       };
       };
       img.onerror = function () {
       test.onerror = function () {
         img.style.display = "none";
         preview.style.backgroundImage = "";
         txt.style.display = "";
         preview.textContent = "Not found";
        txt.innerHTML = "<i>Image not found.</i>";
       };
       };
       img.src = url;
       test.src = url;
     }
     }


    // Update while typing + when autocomplete fills
     input.addEventListener("input", update);
     input.addEventListener("input", update);
     input.addEventListener("change", update);
     input.addEventListener("change", update);
    update();
  }


     // Initial
  function run(root) {
     update();
     setTimeout(function () { hookItemImagePreview(root); }, 150);
     setTimeout(function () { hookItemImagePreview(root); }, 600);
   }
   }


   mw.hook("wikipage.content").add(function () {
   if (window.mw && mw.hook) {
     // Run on formedit pages
    mw.hook("wikipage.content").add(run);
    hookPageFormsImagePreview();
  }
  document.addEventListener("DOMContentLoaded", function () {
     run(document);
   });
   });
})();
})();

Latest revision as of 20:33, 17 January 2026

(function () {
  function normalizeRoot(root) {
    if (root && (root.nodeType === 1 || root.nodeType === 9) && typeof root.querySelector === "function") {
      return root;
    }
    return document;
  }

function normalizeToFileTitle(val) {
  if (!val) return "";
  val = ("" + val).trim();
  if (!val) return "";

  // Handle full wikilink formats:
  // [[File:Name.png]]
  // [[File:Name.png|...]]
  // [[Image:Name.png]] (alias)

    // Strip trailing ]] / leading [[ if someone pastes partial
    val = val.replace(/^\[\[\s*/, "").replace(/\s*\]\]$/, "");

    // If still contains pipe, keep left side
    if (val.includes("|")) val = val.split("|")[0].trim();

    // Convert Image: to File:
    val = val.replace(/^Image:/i, "File:");

    // Ensure File:
    if (!/^File:/i.test(val)) val = "File:" + val.trim();

  return val;
}


  function fileTitleToThumbUrl(fileTitle) {
    var name = fileTitle.replace(/^File:/i, "");
    return mw.util.getUrl("Special:Redirect/file/" + name) + "?width=64";
  }

  function ensurePreviewDiv(input) {
    var existing = document.getElementById("pf-image-preview");
    if (existing) return existing;

    var div = document.createElement("div");
    div.id = "pf-image-preview";
    div.style.width = "64px";
    div.style.height = "64px";
    div.style.border = "1px solid #333";
    div.style.marginTop = "6px";
    div.style.display = "flex";
    div.style.alignItems = "center";
    div.style.justifyContent = "center";
    div.style.fontSize = "11px";
    div.style.textAlign = "center";
    div.style.backgroundRepeat = "no-repeat";
    div.style.backgroundPosition = "center";
    div.style.backgroundSize = "contain";
    div.style.color = "#aaa";
    div.textContent = "No image";

    input.parentNode.appendChild(div);
    return div;
  }

  function hookItemImagePreview(root) {
    root = normalizeRoot(root);

    var input = root.querySelector("input.pf-item-image");
    if (!input) return;

    var preview = ensurePreviewDiv(input);

    function update() {
      var fileTitle = normalizeToFileTitle(input.value);
      if (!fileTitle) {
        preview.style.backgroundImage = "";
        preview.textContent = "No image";
        return;
      }

      var url = fileTitleToThumbUrl(fileTitle);

      // Test image existence using background image load trick
      var test = new Image();
      test.onload = function () {
        preview.style.backgroundImage = "url('" + url + "')";
        preview.textContent = "";
      };
      test.onerror = function () {
        preview.style.backgroundImage = "";
        preview.textContent = "Not found";
      };
      test.src = url;
    }

    input.addEventListener("input", update);
    input.addEventListener("change", update);
    update();
  }

  function run(root) {
    setTimeout(function () { hookItemImagePreview(root); }, 150);
    setTimeout(function () { hookItemImagePreview(root); }, 600);
  }

  if (window.mw && mw.hook) {
    mw.hook("wikipage.content").add(run);
  }
  document.addEventListener("DOMContentLoaded", function () {
    run(document);
  });
})();