Skip to content

Commit

Permalink
Avoid direct use of e.type
Browse files Browse the repository at this point in the history
Some browsers need us to use window.event
  • Loading branch information
saivann committed Jul 26, 2015
1 parent 09acaaf commit 11c484c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
21 changes: 13 additions & 8 deletions js/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@ if (!e) var e = window.event;
(e.preventDefault) ? e.preventDefault() : e.returnValue = false;
}

function getEventTarget(e) {
// Return target DOM node on which the event is triggered.
// Ex. getEventTarget(event);
if (!e) var e = window.event;
return (e.target && e.target.nodeType == 3) ? e.target.parentNode : (e.target) ? e.target : e.srcElement;
function getEvent(e, a) {
// Return requested event property.
// Ex. var target = getEvent(event, 'target');
e = (e) ? e : window.event;
switch (a) {
case 'type':
return e.type;
case 'target':
return (e.target && e.target.nodeType == 3) ? e.target.parentNode : (e.target) ? e.target : e.srcElement;
}
}

function getStyle(a, b) {
Expand Down Expand Up @@ -99,7 +104,7 @@ var timeout = 1000,
// Cancel click events on different targets within timeframe.
// This avoids accidental clicks when the page is scrolled or updated due to the 300ms click event delay on mobiles.
removeEvent(document.body, 'click', wrongClickListener);
if (!clickReady() && getEventTarget(e) != t) cancelEvent(e);
if (!clickReady() && getEvent(e, 'target') != t) cancelEvent(e);
},
setClickTimeout = function() {
// Update timeout during which click events will be blocked.
Expand All @@ -111,7 +116,7 @@ var timeout = 1000,
return (ti === null || ti === '' || parseInt(ti, 10) < new Date().getTime());
};
// Apply appropriate actions according to each event type.
switch (e.type) {
switch (getEvent(e, 'type')) {
case 'touchstart':
// Save initial touchstart coordinates and listen for touchend events and accidental click events.
var x = e.changedTouches[0].pageX,
Expand Down Expand Up @@ -145,7 +150,7 @@ onTouchClick(e, show);

function mobileMenuHover(e) {
// Prevent mobile menu to shrink on hover to prevent accidental clicks on other entries.
var t = getEventTarget(e),
var t = getEvent(e, 'target'),
fn = (t.parentNode.className.indexOf('hover') === -1) ? addClass : removeClass,
initHover = function() {
if (t.nodeName != 'A') return;
Expand Down
22 changes: 11 additions & 11 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ return false;

function loadYoutubeVideo(e) {
// Load Youtube video on target node on click.
var t = getEventTarget(e),
var t = getEvent(e, 'target'),
nd = document.createElement('IFRAME');
while (t.getAttribute('data-youtubeurl') === null || t.getAttribute('data-youtubeurl') === '') t = t.parentNode;
nd.src = t.getAttribute('data-youtubeurl');
Expand Down Expand Up @@ -148,23 +148,23 @@ setTimeout(function() {

function boxShow(e) {
// Display the box content when the user click a box on the "Secure your wallet" page.
var t = getEventTarget(e);
var t = getEvent(e, 'target');
while (t.nodeName != 'DIV') t = t.parentNode;
expandBox(t);
cancelEvent(e);
}

function faqShow(e) {
// Display the content of a question in the FAQ at user request.
var t = getEventTarget(e);
var t = getEvent(e, 'target');
while (t.nodeType != 1 || t.nodeName != 'DIV') t = t.nextSibling;
expandBox(t);
cancelEvent(e);
}

function materialShow(e) {
// Display more materials on the "Press center" page at user request.
var t = getEventTarget(e),
var t = getEvent(e, 'target'),
p = t;
while (p.nodeType != 1 || p.nodeName != 'DIV') p = p.parentNode;
expandBox(p);
Expand All @@ -173,7 +173,7 @@ cancelEvent(e);

function librariesShow(e) {
// Display more open source projects on the "Development" page at user request.
var t = getEventTarget(e),
var t = getEvent(e, 'target'),
p = t;
while (p.nodeType != 1 || p.nodeName != 'UL') p = p.parentNode;
expandBox(p);
Expand Down Expand Up @@ -294,14 +294,14 @@ init();

function updateIssue(e) {
// Update GitHub issue link pre-filled with current page location.
var t = getEventTarget(e);
var t = getEvent(e, 'target');
t.href = 'https://github.com/bitcoin-dot-org/bitcoin.org/issues/new?body=' + encodeURIComponent('Location: ' + window.location.href.toString() + "\n\n");
}

function updateSource(e){
// Update GitHub source file link pre-filled with current page location.
if (!document.getElementsByClassName) return;
var t = getEventTarget(e),
var t = getEvent(e, 'target'),
nodes = document.getElementsByClassName('sourcefile'),
pageoffset = Math.max(0, getPageYOffset() + 100),
windowy = getWindowY(),
Expand Down Expand Up @@ -340,7 +340,7 @@ if (sessionStorage.getItem('develdocdisclaimerclose') === '1') disclaimerClose()

function walletMenuListener(e) {
// Listen for events on the wallet menu.
var t = getEventTarget(e),
var t = getEvent(e, 'target'),
walletSelectPlatform = function() {
if (t.nodeName != 'A') return;
if (t.parentNode.className.indexOf('active') !== -1) walletShowPlatform(t.getAttribute('data-walletcompat'));
Expand All @@ -352,7 +352,7 @@ onTouchClick(e, walletSelectPlatform);

function walletListener(e) {
// Listen for events on wallets.
var t = getEventTarget(e),
var t = getEvent(e, 'target'),
walletShow = function() {
// Show wallet on click on mobile or desktop.
if (t.id == 'wallets') return;
Expand Down Expand Up @@ -482,10 +482,10 @@ function makeEditable(e) {
// An easter egg that makes the page editable when user click on the page and hold their mouse button for one second.
// This trick allows translators and writers to preview their work.
if (!e) var e = window.event;
switch (e.type) {
switch (getEvent(e, 'type')) {
case 'mousedown':
if ((e.which && e.which == 3) || (e.button && e.button == 2)) return;
var t = getEventTarget(e);
var t = getEvent(e, 'target');
while (t.parentNode) {
if (getStyle(t, 'overflow') == 'auto' || getStyle(t, 'overflow-y') == 'auto' || getStyle(t, 'overflow-x') == 'auto') return;
t = t.parentNode;
Expand Down

0 comments on commit 11c484c

Please sign in to comment.