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

Remove Array.from where possible #4262

Merged
merged 3 commits into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// start of function

const profileCache = {};
const filters = document.querySelectorAll(
const labels = document.querySelectorAll(
`.profile-directory .fellowships-directory-filter .filter-option button`
);
const profileContainer = document.querySelector(`.profiles .row`);
Expand Down Expand Up @@ -124,7 +124,6 @@
* associated year (for now)
*/
function bindEventsToLabels() {
let labels = Array.from(filters);
labels.forEach(label => {
label.addEventListener("click", evt => {
// the label text content is, itself, the filter:
Expand Down
176 changes: 78 additions & 98 deletions source/js/buyers-guide/bg-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,116 +98,96 @@ let main = {
},

enableCopyLinks() {
if (document.querySelectorAll(`.copy-link`)) {
Array.from(document.querySelectorAll(`.copy-link`)).forEach(element => {
element.addEventListener(`click`, event => {
event.preventDefault();

let productBox = document.querySelector(
`.product-detail .h1-heading`
);
let productTitle = productBox
? productBox.textContent
: `unknown product`;

ReactGA.event({
category: `product`,
action: `copy link tap`,
label: `copy link ${productTitle}`
});

copyToClipboard(event.target, window.location.href);
document.querySelectorAll(`.copy-link`).forEach(element => {
element.addEventListener(`click`, event => {
event.preventDefault();

let productBox = document.querySelector(`.product-detail .h1-heading`);
let productTitle = productBox
? productBox.textContent
: `unknown product`;

ReactGA.event({
category: `product`,
action: `copy link tap`,
label: `copy link ${productTitle}`
});

copyToClipboard(event.target, window.location.href);
});
}
});
},

// Embed various React components based on the existence of containers within the current page
injectReactComponents() {
let creepVoteTargets = document.querySelectorAll(`.creep-vote-target`);

if (creepVoteTargets.length > 0) {
Array.from(creepVoteTargets).forEach(element => {
let csrf = element.querySelector(`input[name=csrfmiddlewaretoken]`);
let productName = element.dataset.productName;
let productID = element.querySelector(`input[name=productID]`).value;
let votes = element.querySelector(`input[name=votes]`).value;

try {
votes = JSON.parse(votes.replace(/'/g, `"`));
} catch (e) {
votes = {
creepiness: {
average: 50,
vote_breakdown: { "0": 0, "1": 0, "2": 0, "3": 0, "4": 0 }
},
confidence: { "0": 0, "1": 0 }
};
}

apps.push(
new Promise(resolve => {
ReactDOM.render(
<CreepVote
csrf={csrf.value}
productName={productName}
productID={parseInt(productID, 10)}
votes={votes}
whenLoaded={() => resolve()}
joinUsCSRF={csrfToken}
joinUsApiUrl={`${networkSiteURL}/api/campaign/signups/0/`}
/>,
element
);
})
);
});
}

let creepometerTargets = document.querySelectorAll(`.creepometer-target`);

if (creepometerTargets.length > 0) {
Array.from(creepometerTargets).forEach(element => {
let initialValue = element.dataset.initialValue;

apps.push(
new Promise(resolve => {
ReactDOM.render(
<Creepometer
initialValue={initialValue}
whenLoaded={() => resolve()}
/>,
element
);
})
);
});
}
document.querySelectorAll(`.creep-vote-target`).forEach(element => {
let csrf = element.querySelector(`input[name=csrfmiddlewaretoken]`);
let productName = element.dataset.productName;
let productID = element.querySelector(`input[name=productID]`).value;
let votes = element.querySelector(`input[name=votes]`).value;

try {
votes = JSON.parse(votes.replace(/'/g, `"`));
} catch (e) {
votes = {
creepiness: {
average: 50,
vote_breakdown: { "0": 0, "1": 0, "2": 0, "3": 0, "4": 0 }
},
confidence: { "0": 0, "1": 0 }
};
}

if (document.querySelectorAll(`.join-us`)) {
var elements = Array.from(document.querySelectorAll(`.join-us`));
apps.push(
new Promise(resolve => {
ReactDOM.render(
<CreepVote
csrf={csrf.value}
productName={productName}
productID={parseInt(productID, 10)}
votes={votes}
whenLoaded={() => resolve()}
joinUsCSRF={csrfToken}
joinUsApiUrl={`${networkSiteURL}/api/campaign/signups/0/`}
/>,
element
);
})
);
});

if (elements.length) {
elements.forEach(element => {
var props = element.dataset;
document.querySelectorAll(`.creepometer-target`).forEach(element => {
let initialValue = element.dataset.initialValue;
apps.push(
new Promise(resolve => {
ReactDOM.render(
<Creepometer
initialValue={initialValue}
whenLoaded={() => resolve()}
/>,
element
);
})
);
});

props.apiUrl = `${networkSiteURL}/api/campaign/signups/${props.signupId ||
0}/`;
document.querySelectorAll(`.join-us`).forEach(element => {
const props = element.dataset;
const sid = props.signupId || 0;
props.apiUrl = `${networkSiteURL}/api/campaign/signups/${sid}/`;

props.csrfToken = props.csrfToken || csrfToken;
props.isHidden = false;
props.csrfToken = props.csrfToken || csrfToken;
props.isHidden = false;

apps.push(
new Promise(resolve => {
ReactDOM.render(
<JoinUs {...props} whenLoaded={() => resolve()} />,
element
);
})
apps.push(
new Promise(resolve => {
ReactDOM.render(
<JoinUs {...props} whenLoaded={() => resolve()} />,
element
);
});
}
}
})
);
});
}
};

Expand Down
69 changes: 31 additions & 38 deletions source/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,16 +301,13 @@ let main = {
});

// petition elements
var petitionElements = Array.from(
document.querySelectorAll(`.sign-petition`)
);
var subscribed = false;

if (window.location.search.indexOf(`subscribed=1`) !== -1) {
subscribed = true;
}

petitionElements.forEach(element => {
document.querySelectorAll(`.sign-petition`).forEach(element => {
var props = element.dataset;

props.apiUrl = `${networkSiteURL}/api/campaign/petitions/${props.petitionId}/`;
Expand Down Expand Up @@ -370,11 +367,7 @@ let main = {
}

// Pulse project lists
let pulseProjectList = Array.from(
document.querySelectorAll(`.pulse-project-list`)
);

pulseProjectList.forEach(target => {
document.querySelectorAll(`.pulse-project-list`).forEach(target => {
apps.push(
new Promise(resolve => {
ReactDOM.render(
Expand All @@ -396,11 +389,9 @@ let main = {
});

// Share button group
let shareButtonGroups = document.querySelectorAll(
`.share-button-group-wrapper`
);
if (shareButtonGroups) {
shareButtonGroups.forEach(element => {
document
.querySelectorAll(`.share-button-group-wrapper`)
.forEach(element => {
var props = element.dataset;

apps.push(
Expand All @@ -412,24 +403,23 @@ let main = {
})
);
});
}

//Profile Directory Filter-Bar GA

const filters = document.querySelectorAll(
`.profile-directory .fellowships-directory-filter .filter-option button`
);

filters.forEach(filter => {
let year = filter.textContent.trim();
filter.addEventListener(`click`, () => {
ReactGA.event({
category: `profiles`,
action: `directory filter`,
label: `${document.title} ${year}`
document
.querySelectorAll(
`.profile-directory .fellowships-directory-filter .filter-option button`
)
.forEach(filter => {
let year = filter.textContent.trim();
filter.addEventListener(`click`, () => {
ReactGA.event({
category: `profiles`,
action: `directory filter`,
label: `${document.title} ${year}`
});
});
});
});

//Profile Directory Cards Social Media GA

Expand Down Expand Up @@ -493,19 +483,22 @@ let main = {
}

// store profile cards
let profileCards = document.querySelectorAll(`.profiles .person-card`);
function getProfileCards() {
return document.querySelectorAll(`.profiles .person-card`);
}

// checks for profile cards in the initial page load
if (profileCards.length > 0) {
bindProfileCardAnalytics(profileCards);
function updateProfileList() {
let profileCards = getProfileCards();
if (profileCards.length > 0) {
bindProfileCardAnalytics(profileCards);
}
}
// And start listening for profile filter events,
// in case profile cards get updated.
document.addEventListener(`profiles:list-updated`, () => {
// Refetch the profile cards, because they'll have gone stale.
profileCards = document.querySelectorAll(`.profiles .person-card`);
bindProfileCardAnalytics(profileCards);
});

document.addEventListener(`profiles:list-updated`, () =>
updateProfileList()
);

updateProfileList();

// Enable the "load more results" button on index pages
let loadMoreButton = document.querySelector(`.load-more-index-entries`);
Expand Down