Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4176 passive listeners #4504

Merged
merged 11 commits into from
Apr 6, 2021
31 changes: 30 additions & 1 deletion fec/fec/static/js/modules/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,34 @@ function getCookie(name) {
return cookieValue;
}

/**
* Passive listeners on touchstart and scroll events improve page performance
* but could crash non-supportive browsers.
* This function will return either an object to be used in addEventListener,
* or will return null if the browser doesn't support `passive()`
* @returns Object or false depending on whether the browser supports passive listeners
*/
function passiveListenerIfSupported() {
var supported = false;
/**
* Let's check whether the browser supports passive event listeners
*/
try {
let options = {
get passive() {
supported = true;
return false;
}
};
window.addEventListener('test', null, options);
window.removeEventListener('test', null, options);
} catch (err) {
supported = false;
}

return supported ? { passive: true } : false;
}

module.exports = {
anchorify: anchorify,
scrollAnchor: scrollAnchor,
Expand Down Expand Up @@ -594,5 +622,6 @@ module.exports = {
getWindowWidth: getWindowWidth,
sanitizeValue: sanitizeValue,
sanitizeQueryParams: sanitizeQueryParams,
getCookie: getCookie
getCookie: getCookie,
passiveListener: passiveListenerIfSupported
};
8 changes: 5 additions & 3 deletions fec/fec/static/js/widgets/contributions-by-state-box.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const breakpointToXL = 860;
const rootPathToIndividualContributions =
'/data/receipts/individual-contributions/';

import { buildUrl } from '../modules/helpers';
import { buildUrl, passiveListener } from '../modules/helpers';
import typeahead from '../modules/typeahead';
import { defaultElectionYear } from './widget-vars';
import 'abortcontroller-polyfill/dist/polyfill-patch-fetch';
Expand Down Expand Up @@ -230,11 +230,13 @@ ContributionsByState.prototype.init = function() {
);
theTypeaheadElement.addEventListener(
'mousedown',
this.handleTypeaheadFocus.bind(this)
this.handleTypeaheadFocus.bind(this),
passiveListener()
);
theTypeaheadElement.addEventListener(
'touchstart',
this.handleTypeaheadFocus.bind(this)
this.handleTypeaheadFocus.bind(this),
passiveListener()
);

// Listen for any field updates, looking for errors
Expand Down
58 changes: 24 additions & 34 deletions fec/fec/static/js/widgets/pres-finance-map-box.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const selector_candidateListDisclaimer = '.js-cand-list-note';

// Imports, etc
// const $ = jquery;
import { buildUrl } from '../modules/helpers';
import { buildUrl, passiveListener } from '../modules/helpers';
// import { defaultElectionYear } from './widget-vars';
import 'abortcontroller-polyfill/dist/polyfill-patch-fetch';
import analytics from '../modules/analytics';
Expand Down Expand Up @@ -1257,49 +1257,39 @@ PresidentialFundsMap.prototype.openDownloads = function() {
PresidentialFundsMap.prototype.handleExportRaisingClick = function(e) {
e.preventDefault();

//scroll downloads area into view
// scroll downloads area into view
this.downloadsWrapper.scrollIntoView();

// Wait until the downloadsWrapper is in view before opening (if not already open)
let instance = this;
//'this' refers to the main protoype here
window.onscroll = function() {
//'this' is window inside the context of this function
let theWindow = this;
let windowScroll = theWindow.scrollY,
downloadsScrollPosition =
instance.downloadsWrapper.getBoundingClientRect().top + windowScroll,
downloadsHeight = instance.downloadsWrapper.offsetHeight,
windowHeight = window.innerHeight;
if (
windowScroll >
downloadsScrollPosition + downloadsHeight - windowHeight
) {
{
instance.openDownloads();
window.onscroll = null; // remove listener
}
}
};
document.addEventListener(
'scroll',
this.handleDocScrolling.bind(this),
passiveListener()
);
};

/**
* TODO -
* Fired on each tick/update of the scroll position. Removes its own listener when scroll is complete
Copy link
Contributor

@johnnyporkchops johnnyporkchops Apr 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The open/close toggle does not seem to work when scrolling away and coming back to it. We could simplify this interaction if it solves this problem by, for example, opening the it on-click and then not offering an option to toggle it open/close it once its open

* @param {UiEvent} e
*/
PresidentialFundsMap.prototype.handleToggleRaisingExports = function(e) {
e.preventDefault();

//toggle export area
if (this.downloadsLinksWrapper.style.height > '0px') {
this.raisingExportsToggle.classList.toggle('button--close', false);
this.downloadsLinksWrapper.style.height = 0;
} else {
this.raisingExportsToggle.classList.toggle('button--close', true);
this.downloadsLinksWrapper.style.height = 'auto';
PresidentialFundsMap.prototype.handleDocScrolling = function() {
let windowScroll = window.scrollY;
let downloadsScrollPosition =
this.downloadsWrapper.getBoundingClientRect().top + windowScroll;
let downloadsHeight = this.downloadsWrapper.offsetHeight;
let windowHeight = window.innerHeight;

if (windowScroll > downloadsScrollPosition + downloadsHeight - windowHeight) {
{
this.openDownloads();
window.removeEventListener('scroll', this.handleDocScrolling.bind(this));
}
}
};

/**
* TODO -
* Handles when 'Export raising data' is clicked, toggles the list of state abbreviations
* @param {MouseEvent} e
*/
PresidentialFundsMap.prototype.handleToggleRaisingExports = function(e) {
e.preventDefault();
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
"karma-coverage-istanbul-reporter": "^2.0.5",
"karma-mocha": "^0.2.2",
"karma-webpack": "^3.0.5",
"leaflet": "^1.4.0",
"leaflet": "^1.7.1",
"mocha": "^5.2.0",
"mockdate": "^2.0.2",
"natives": "^1.1.6",
Expand Down