docbrown/app/javascript/packs/articlePage.jsx
rhymes f9506affb5
Use faster JS selection methods (#11409)
* Add JS tips section to frontend documentation

* Replace document.getElementsByTagName('body') with document.body

* Replace querySelectorAll with faster selecting methods where appropriate

* Replace querySelector with faster selecting methods where appropriate

* Fix typo

* Fix forEach and getElementsByClassName

* Change querySelector* to faster methods in erb files

* Change querySelector* to faster methods in ruby files

* Fix runkit tag

* Various fixes

* Update app/assets/javascripts/initializers/initializeEllipsisMenu.js

Co-authored-by: Ridhwana <Ridhwana.Khan16@gmail.com>

* Update app/assets/javascripts/utilities/slideSidebar.js

Co-authored-by: Ridhwana <Ridhwana.Khan16@gmail.com>

* Commenting out flaky spec

Co-authored-by: Ridhwana <Ridhwana.Khan16@gmail.com>
2020-11-16 17:35:50 +01:00

68 lines
2.1 KiB
JavaScript

import { h, render } from 'preact';
import { Snackbar, addSnackbarItem } from '../Snackbar';
import addFullScreenModeControl from '../utilities/codeFullscreenModeSwitcher';
const fullscreenActionElements = document.getElementsByClassName(
'js-fullscreen-code-action',
);
if (fullscreenActionElements) {
addFullScreenModeControl(fullscreenActionElements);
}
// The Snackbar for the article page
const snackZone = document.getElementById('snack-zone');
if (snackZone) {
render(<Snackbar lifespan="3" />, snackZone);
}
// eslint-disable-next-line no-restricted-globals
top.addSnackbarItem = addSnackbarItem;
const userDataIntervalID = setInterval(async () => {
const { user = null, userStatus } = document.body.dataset;
if (userStatus === 'logged-out') {
// User is not logged on so nothing dynamic to add to the page.
clearInterval(userDataIntervalID);
return;
}
if (userStatus === 'logged-in' && user !== null) {
// Load the comment subscription button for logged on users.
clearInterval(userDataIntervalID);
const root = document.getElementById('comment-subscription');
try {
const {
getCommentSubscriptionStatus,
setCommentSubscriptionStatus,
CommentSubscription,
} = await import('../CommentSubscription');
const { articleId } = document.getElementById('article-body').dataset;
const { config: subscriptionType } = await getCommentSubscriptionStatus(
articleId,
);
const subscriptionRequestHandler = async (type) => {
const message = await setCommentSubscriptionStatus(articleId, type);
addSnackbarItem({ message, addCloseButton: true });
};
render(
<CommentSubscription
subscriptionType={subscriptionType}
positionType="static"
onSubscribe={subscriptionRequestHandler}
onUnsubscribe={subscriptionRequestHandler}
/>,
root,
);
} catch (e) {
document.getElementById('comment-subscription').innerHTML =
'<p className="color-accent-danger">Unable to load Comment Subscription component.<br />Try refreshing the page.</p>';
}
}
});