MediaWiki:Common.js: Difference between revisions

Jump to navigation Jump to search
no edit summary
No edit summary
No edit summary
Line 1,283: Line 1,283:
     $chooser = jQuery(
     $chooser = jQuery(
       '<div id="print-chooser" class="print-chooser" style="display:none;">' +
       '<div id="print-chooser" class="print-chooser" style="display:none;">' +
         '<a href="#" id="print-with-border" class="print-choice">border</a> ' +
         '<a href="#" id="sw-print-with" class="print-choice">border</a> ' +
         '<a href="#" id="print-no-border" class="print-choice">no border</a>' +
         '<a href="#" id="sw-print-no" class="print-choice">no border</a>' +
         "</div>"
         "</div>"
     );
     );
Line 1,829: Line 1,829:
   // --- extra safety bindings for Entry page / widgets ---
   // --- extra safety bindings for Entry page / widgets ---


   // 1) Directly bind the anchors (works even if container delegation misses)
   // Delegated: clicks on choices inside the chooser
   jQuery(document).on(
   jQuery(document).on(
     "click.swprintChoice",
     "click.swprintChoice",
     "a#print-with-border, a#print-no-border, #print-with-border, #print-no-border",
     "#print-chooser a#sw-print-with, #print-chooser a#sw-print-no",
     function (e) {
     function (e) {
      console.log("[print] direct anchor handler fired:", this.id);
       e.preventDefault();
       e.preventDefault();
       swHandlePrintChoice(this.id, jQuery(this));
      var id =
        this.id === "sw-print-no" ? "print-no-border" : "print-with-border";
       swHandlePrintChoice(id, jQuery(this));
     }
     }
   );
   );


   // 2) If using the Button widget (<a id=...><button>...</button></a>),
   // If the UI uses <button> inside the <a>, map button parent <a> (still scoped to chooser)
  //    capture clicks on the <button> and route via its parent <a>.
   jQuery(document).on(
   jQuery(document).on(
     "click.swprintChoiceBtn",
     "click.swprintChoiceBtn",
     "#print-options button",
     "#print-chooser button",
     function (e) {
     function (e) {
       var $a = jQuery(this).closest("a[id]");
       var host = this.closest(
      if (!$a.length) return;
         "#print-chooser a#sw-print-with, #print-chooser a#sw-print-no"
      console.log(
         "[print] widget button handler fired; parent anchor id:",
        $a.attr("id")
       );
       );
      if (!host) return;
       e.preventDefault();
       e.preventDefault();
       swHandlePrintChoice($a.attr("id"), $a);
      var id =
        host.id === "sw-print-no" ? "print-no-border" : "print-with-border";
       swHandlePrintChoice(id, (window.jQuery && jQuery(host)) || null);
     }
     }
   );
   );
  // ===== Ultra-hard native catcher (capture), no 'closest' dependency =====
  (function () {
    if (window.__swprintUltraCatcher) return;
    window.__swprintUltraCatcher = true;
    document.addEventListener(
      "click",
      function (ev) {
        try {
          var node = ev.target;
          var foundId = "";
          // manual ancestor walk (ES5-safe)
          while (node && node !== document) {
            if (node.nodeType === 1 && node.id) {
              if (
                node.id === "print-with-border" ||
                node.id === "print-no-border"
              ) {
                foundId = node.id;
                break;
              }
            }
            node = node.parentNode;
          }
          if (!foundId) return;
          // beat anything that tries to swallow the event
          ev.preventDefault();
          ev.stopPropagation();
          if (ev.stopImmediatePropagation) ev.stopImmediatePropagation();
          console.log("[swprint] ULTRA catcher routed:", foundId);
          var $btn = window.jQuery && node ? jQuery(node) : null;
          swHandlePrintChoice(foundId, $btn);
        } catch (e) {}
      },
      true // capture
    );
  })();
  // Window-level chooser catcher: any click inside #print-chooser is routed.
  (function () {
    if (window.__swprintWindowChooserCatch) return;
    window.__swprintWindowChooserCatch = true;
    window.addEventListener(
      "click",
      function (ev) {
        try {
          var chooser = document.getElementById("print-chooser");
          if (!chooser || chooser.style.display === "none") return;
          // Did we click a WITH / NO element (or inside it)?
          var btn = ev.target.closest
            ? ev.target.closest(
                '#print-with-border, #print-no-border, [id="print-with-border"], [id="print-no-border"]'
              )
            : null;
          if (!btn || !chooser.contains(btn)) return;
          ev.preventDefault();
          ev.stopPropagation();
          if (ev.stopImmediatePropagation) ev.stopImmediatePropagation();
          var id = btn.getAttribute("id") || "";
          console.log("[swprint] WINDOW chooser catcher ->", id);
          var $btn = (window.jQuery && jQuery(btn)) || null;
          swHandlePrintChoice(id, $btn);
        } catch (e) {}
      },
      true
    ); // capture
  })();


   // If a widget uses <button> inside the chooser, map it to the nearest id node
   // If a widget uses <button> inside the chooser, map it to the nearest id node

Navigation menu