MediaWiki:Common.js: Difference between revisions

Jump to navigation Jump to search
no edit summary
No edit summary
No edit summary
Line 1,756: Line 1,756:


   function swBindChoiceAnchors() {
   function swBindChoiceAnchors() {
     var els = document.querySelectorAll("#print-with-border, #print-no-border");
     var sel = "#print-with-border, #print-no-border";
    var els = document.querySelectorAll(sel);
     for (var i = 0; i < els.length; i++) {
     for (var i = 0; i < els.length; i++) {
       (function (el) {
       (function (el) {
         if (el.__swChoiceBound) return; // bind once
         if (el.__swChoiceBound) return; // each node once
         el.__swChoiceBound = true;
         el.__swChoiceBound = true;


         function handler(ev) {
        // make sure node accepts clicks
        try {
          el.style.pointerEvents = el.style.pointerEvents || "auto";
          if (!el.getAttribute("role")) el.setAttribute("role", "button");
          if (!el.getAttribute("tabindex")) el.setAttribute("tabindex", "0");
        } catch (e) {}
 
         function fire(ev, where) {
           try {
           try {
             console.log("[swprint] ELEMENT click ->", el.id);
             console.log("[swprint] CHOICE fire (" + where + "):", el.id);
           } catch (e) {}
           } catch (e) {}
           ev.preventDefault();
           if (ev && ev.preventDefault) ev.preventDefault();
          // stop anything else from eating it
           if (ev && ev.stopImmediatePropagation) ev.stopImmediatePropagation();
           if (ev.stopImmediatePropagation) ev.stopImmediatePropagation();
           if (ev && ev.stopPropagation) ev.stopPropagation();
           if (ev.stopPropagation) ev.stopPropagation();
 
           var $a = (window.jQuery && jQuery(el)) || null;
           var $a = (window.jQuery && jQuery(el)) || null;
           swHandlePrintChoice(el.id, $a);
           swHandlePrintChoice(el.id, $a);
Line 1,776: Line 1,782:
         }
         }


         // bind in CAPTURE and BUBBLE, plus legacy onclick
         // bind multiple phases/types to dodge libraries that swallow 'click'
         el.addEventListener("click", handler, true); // capture
        el.addEventListener(
         el.addEventListener("click", handler, false); // bubble
          "pointerup",
         if (!el.onclick) el.onclick = handler;
          function (e) {
            fire(e, "pointerup");
          },
          true
        );
        el.addEventListener(
          "touchend",
          function (e) {
            fire(e, "touchend");
          },
          true
        );
        el.addEventListener(
          "mouseup",
          function (e) {
            fire(e, "mouseup");
          },
          true
        );
         el.addEventListener(
          "click",
          function (e) {
            fire(e, "click(capture)");
          },
          true
        );
         el.addEventListener(
          "click",
          function (e) {
            fire(e, "click(bubble)");
          },
          false
        );
         if (!el.onclick)
          el.onclick = function (e) {
            return fire(e, "onclick");
          };
 
        // keyboard
        el.addEventListener(
          "keydown",
          function (e) {
            var k = e.key || e.keyCode;
            if (k === "Enter" || k === 13 || k === " " || k === 32) {
              fire(e, "keydown");
            }
          },
          true
        );


         try {
         try {
Line 1,787: Line 1,841:
     }
     }
   }
   }
  (function () {
    if (window.__swprintHardCatcherV2) return;
    window.__swprintHardCatcherV2 = true;
    function route(ev, where) {
      var t = ev.target;
      var closest =
        t && t.closest
          ? t.closest("a#print-with-border, a#print-no-border")
          : null;
      if (!closest) return;
      try {
        console.log("[swprint] HARD-CAPTURE (" + where + "):", closest.id);
      } catch (e) {}
      ev.preventDefault();
      ev.stopImmediatePropagation && ev.stopImmediatePropagation();
      ev.stopPropagation && ev.stopPropagation();
      var $a = (window.jQuery && jQuery(closest)) || null;
      swHandlePrintChoice(closest.id, $a);
      return false;
    }
    // capture high in the tree
    window.addEventListener(
      "click",
      function (e) {
        route(e, "window");
      },
      true
    );
    document.addEventListener(
      "click",
      function (e) {
        route(e, "document");
      },
      true
    );
    document.documentElement.addEventListener(
      "click",
      function (e) {
        route(e, "documentElement");
      },
      true
    );
    // if something kills 'click', catch these too
    window.addEventListener(
      "pointerup",
      function (e) {
        route(e, "window:pointerup");
      },
      true
    );
    window.addEventListener(
      "touchend",
      function (e) {
        route(e, "window:touchend");
      },
      true
    );
  })();


   /* wiring (namespaced) */
   /* wiring (namespaced) */
Line 1,813: Line 1,929:
         jQuery("#show-article").toggleClass("print-opts-open", visible);
         jQuery("#show-article").toggleClass("print-opts-open", visible);


         // Bind directly on any choice anchors currently in the DOM
         // ensure the currently-rendered anchors are bound (important on Entry pages)
         swBindChoiceAnchors();
         swBindChoiceAnchors();


         // Bind native handlers to ALL nodes that currently exist with those IDs
         // (optional) quick count for sanity
         try {
         try {
          var nodes = Array.prototype.slice.call(
            document.querySelectorAll(
              '[id="print-with-border"], [id="print-no-border"]'
            )
          );
           console.log("[swprint] chooser toggled; visible=", visible, {
           console.log("[swprint] chooser toggled; visible=", visible, {
             withCount: document.querySelectorAll('[id="print-with-border"]')
             withCount: document.querySelectorAll('[id="print-with-border"]')
Line 1,828: Line 1,939:
             noCount: document.querySelectorAll('[id="print-no-border"]').length,
             noCount: document.querySelectorAll('[id="print-no-border"]').length,
           });
           });
        } catch (e) {}


          nodes.forEach(function (el) {
        // Bind directly on any choice anchors currently in the DOM
            if (el.__swprintBound) return;
        swBindChoiceAnchors();
            el.__swprintBound = true;
 
            var handler = function (ev) {
              ev.preventDefault();
              ev.stopPropagation();
              if (ev.stopImmediatePropagation) ev.stopImmediatePropagation();
              var id = el.getAttribute("id") || "";
              console.log("[swprint] NATIVE choice fired ->", id);
              swHandlePrintChoice(id, (window.jQuery && jQuery(el)) || null);
              return false;
            };
 
            // capture = see it before anything else
            el.addEventListener("click", handler, true);
            el.addEventListener("pointerup", handler, true);
          });
        } catch (err) {}


         // run visibility diagnostics on the two buttons
         // run visibility diagnostics on the two buttons

Navigation menu