4,554
edits
No edit summary |
No edit summary |
||
Line 1,756: | Line 1,756: | ||
function swBindChoiceAnchors() { | function swBindChoiceAnchors() { | ||
var | 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; // | if (el.__swChoiceBound) return; // each node once | ||
el.__swChoiceBound = true; | el.__swChoiceBound = true; | ||
function | // 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] | console.log("[swprint] CHOICE fire (" + where + "):", el.id); | ||
} catch (e) {} | } catch (e) {} | ||
ev.preventDefault(); | if (ev && ev.preventDefault) ev.preventDefault(); | ||
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 | // bind multiple phases/types to dodge libraries that swallow 'click' | ||
el.addEventListener("click", | el.addEventListener( | ||
el.addEventListener("click", | "pointerup", | ||
if (!el.onclick) el.onclick = | 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); | ||
// | // ensure the currently-rendered anchors are bound (important on Entry pages) | ||
swBindChoiceAnchors(); | swBindChoiceAnchors(); | ||
// | // (optional) quick count for sanity | ||
try { | try { | ||
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) {} | |||
// Bind directly on any choice anchors currently in the DOM | |||
swBindChoiceAnchors(); | |||
// run visibility diagnostics on the two buttons | // run visibility diagnostics on the two buttons |