forked from forem/forem
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Conditionally include scripts (forem#21330)
* Conditionally include scripts * Adjust js loading * Remove console log * Adjust js loading
- Loading branch information
1 parent
5511dfb
commit 31a0b0e
Showing
8 changed files
with
119 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,22 @@ | ||
import { h, render } from 'preact'; | ||
import { SearchFormSync } from '../Search/SearchFormSync'; | ||
|
||
document.addEventListener('DOMContentLoaded', () => { | ||
if (document.readyState === "interactive" || document.readyState === "complete") { | ||
// DOMContentLoaded has already been triggered | ||
const root = document.getElementById('header-search'); | ||
|
||
render(<SearchFormSync />, root); | ||
window.InstantClick.on('change', () => { | ||
render(<SearchFormSync />, root); | ||
}); | ||
}); | ||
} else { | ||
// Add event listener for DOMContentLoaded | ||
document.addEventListener("DOMContentLoaded", function () { | ||
const root = document.getElementById('header-search'); | ||
|
||
render(<SearchFormSync />, root); | ||
window.InstantClick.on('change', () => { | ||
render(<SearchFormSync />, root); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const animatedImages = document.querySelectorAll('[data-animated="true"]'); | ||
if (animatedImages.length > 0) { | ||
import('@utilities/animatedImageUtils').then( | ||
({ initializePausableAnimatedImages }) => { | ||
initializePausableAnimatedImages(animatedImages); | ||
}, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { h, render } from 'preact'; | ||
import { Snackbar, addSnackbarItem } from '../Snackbar'; | ||
import { initializeUserSubscriptionLiquidTagContent } from '../liquidTags/userSubscriptionLiquidTag'; | ||
|
||
// 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; | ||
|
||
|
||
// Comment Subscription | ||
getCsrfToken().then(async () => { | ||
const { user = null, userStatus } = document.body.dataset; | ||
const root = document.getElementById('comment-subscription'); | ||
const isLoggedIn = userStatus === 'logged-in'; | ||
|
||
if (!root) { | ||
return; | ||
} | ||
try { | ||
const { | ||
getCommentSubscriptionStatus, | ||
setCommentSubscriptionStatus, | ||
CommentSubscription, | ||
} = await import('../CommentSubscription'); | ||
|
||
const { articleId } = document.getElementById('article-body').dataset; | ||
|
||
let subscriptionType = 'not_subscribed'; | ||
|
||
if (isLoggedIn && user !== null) { | ||
({ 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} | ||
isLoggedIn={isLoggedIn} | ||
/>, | ||
root, | ||
); | ||
} catch (e) { | ||
root.innerHTML = | ||
'<p className="color-accent-danger">Unable to load Comment Subscription component.<br />Try refreshing the page.</p>'; | ||
} | ||
}); | ||
|
||
initializeUserSubscriptionLiquidTagContent(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters