elRoot can be module global
All checks were successful
buildbot/Hondaforum Site Build done.

This commit is contained in:
László Károlyi 2023-12-23 14:28:21 +01:00
parent e9df2ae400
commit 832ebeecf6
Signed by: karolyi
GPG key ID: 2DCAF25E55735BFE

View file

@ -88,6 +88,7 @@ export let urlTemplates: {
} }
export let stringsPassed: PassedStringsType export let stringsPassed: PassedStringsType
let elRoot: ElTopicCommentListingType
function initializeUrls(options: TopicCommentListingOptionsType): void { function initializeUrls(options: TopicCommentListingOptionsType): void {
const urls = options.urls const urls = options.urls
@ -141,19 +142,15 @@ function initializeUrls(options: TopicCommentListingOptionsType): void {
} }
} }
type ElTopicCommentListingType = Document & { type ElTopicCommentListingType = HTMLElement & {
forumTopicCommentListing: TopicCommentListing forumTopicCommentListing: TopicCommentListing
} }
function getElRoot(): ElTopicCommentListingType {
return <ElTopicCommentListingType>document
}
function onClickJumpToComment(event: MouseEvent): void { function onClickJumpToComment(event: MouseEvent): void {
const commentLink = event.currentTarget const commentLink = event.currentTarget
if (!(commentLink instanceof HTMLAnchorElement)) return if (!(commentLink instanceof HTMLAnchorElement)) return
const commentPk = parseInt(commentLink.dataset.forumLinkTo ?? '-1') const commentPk = parseInt(commentLink.dataset.forumLinkTo ?? '-1')
const obj = getElRoot().forumTopicCommentListing const obj = elRoot.forumTopicCommentListing
const elOneComment = obj.comments.get(commentPk) const elOneComment = obj.comments.get(commentPk)
if (!elOneComment) return if (!elOneComment) return
event.preventDefault() event.preventDefault()
@ -176,12 +173,12 @@ function getFirstOrLastCommentPk(obj: TopicCommentListing): number {
} }
function onScrollFirst(): void { function onScrollFirst(): void {
const obj = getElRoot().forumTopicCommentListing const obj = elRoot.forumTopicCommentListing
obj.isPageScrolled = true obj.isPageScrolled = true
} }
function onScrollEnd(): void { function onScrollEnd(): void {
const obj = getElRoot().forumTopicCommentListing const obj = elRoot.forumTopicCommentListing
const navbarHeight = getNavbarHeight() const navbarHeight = getNavbarHeight()
const viewportHeight = document.documentElement.clientHeight const viewportHeight = document.documentElement.clientHeight
let selectedCommentPk = 0 let selectedCommentPk = 0
@ -208,7 +205,7 @@ async function onPaginate(
console.error('urlTemplates.commentListingPageNo is unset') console.error('urlTemplates.commentListingPageNo is unset')
return return
} }
const obj = getElRoot().forumTopicCommentListing const obj = elRoot.forumTopicCommentListing
obj.paginators.forEach(paginator => { obj.paginators.forEach(paginator => {
paginator.setLoading(elClicked) paginator.setLoading(elClicked)
}) })
@ -219,7 +216,7 @@ async function onPaginate(
} }
function onTeardownOnecomment(el: ElOnecommentType): void { function onTeardownOnecomment(el: ElOnecommentType): void {
const obj = getElRoot().forumTopicCommentListing const obj = elRoot.forumTopicCommentListing
obj.comments.delete(el.forumOneComment.commentPk) obj.comments.delete(el.forumOneComment.commentPk)
teardownOnecomment( teardownOnecomment(
el, onClickJumpToComment, commentHighlightOn, commentHighlightOff) el, onClickJumpToComment, commentHighlightOn, commentHighlightOff)
@ -262,7 +259,7 @@ function commentHighlightOn(event: Event): void {
const target = event.target const target = event.target
if (!(target instanceof HTMLAnchorElement)) return if (!(target instanceof HTMLAnchorElement)) return
const commentPk = parseInt(target.dataset.forumLinkTo ?? '-1') const commentPk = parseInt(target.dataset.forumLinkTo ?? '-1')
const obj = getElRoot().forumTopicCommentListing const obj = elRoot.forumTopicCommentListing
const elOnecomment = obj.comments.get(commentPk) const elOnecomment = obj.comments.get(commentPk)
if (!elOnecomment) return if (!elOnecomment) return
highlightOn(elOnecomment) highlightOn(elOnecomment)
@ -272,7 +269,7 @@ function commentHighlightOff(event: Event): void {
const target = event.target const target = event.target
if (!(target instanceof HTMLAnchorElement)) return if (!(target instanceof HTMLAnchorElement)) return
const commentPk = parseInt(target.dataset.forumLinkTo ?? '-1') const commentPk = parseInt(target.dataset.forumLinkTo ?? '-1')
const obj = getElRoot().forumTopicCommentListing const obj = elRoot.forumTopicCommentListing
const elOnecomment = obj.comments.get(commentPk) const elOnecomment = obj.comments.get(commentPk)
if (!elOnecomment) return if (!elOnecomment) return
highlightOff(elOnecomment) highlightOff(elOnecomment)
@ -311,18 +308,17 @@ class TopicCommentListing {
readonly paginators = new Array<Paginator>() readonly paginators = new Array<Paginator>()
constructor ( constructor (
root: ElTopicCommentListingType,
options: TopicCommentListingOptionsType options: TopicCommentListingOptionsType
) { ) {
this.options = options this.options = options
this.initializeElOnecomments(root) this.initializeElOnecomments()
this.initializePaginators(root) this.initializePaginators()
addEventListener('scrollend', onScrollEnd) addEventListener('scrollend', onScrollEnd)
} }
private initializeElOnecomments(root: ElTopicCommentListingType): void { private initializeElOnecomments(): void {
const commentWrappers = <HTMLCollectionOf<HTMLElement>> const commentWrappers = <HTMLCollectionOf<HTMLElement>>
root.getElementsByClassName(this.options.selectors.commentWrapper) elRoot.getElementsByClassName(this.options.selectors.commentWrapper)
for (const el of commentWrappers) { for (const el of commentWrappers) {
const commentPk = parseInt(el.dataset.forumCommentPk ?? '-1') const commentPk = parseInt(el.dataset.forumCommentPk ?? '-1')
if (commentPk === -1) throw new Error('forumLinkTo not found') if (commentPk === -1) throw new Error('forumLinkTo not found')
@ -335,14 +331,14 @@ class TopicCommentListing {
} }
} }
private initializePaginators(root: ElTopicCommentListingType): void { private initializePaginators(): void {
if (this.options.listingMode !== 'commentListing') return if (this.options.listingMode !== 'commentListing') return
const wrappers = const wrappers =
<HTMLCollectionOf<HTMLUListElement>> <HTMLCollectionOf<HTMLUListElement>>
root.getElementsByClassName('pagination-comments') elRoot.getElementsByClassName('pagination-comments')
for (const elRoot of wrappers) { for (const elWrapperRoot of wrappers) {
this.paginators.push(Paginator.add({ this.paginators.push(Paginator.add({
root: elRoot, callbacks: { load: onPaginate } root: elWrapperRoot, callbacks: { load: onPaginate }
})) }))
} }
} }
@ -350,12 +346,18 @@ class TopicCommentListing {
export function init(options: TopicCommentListingOptionsType): void { export function init(options: TopicCommentListingOptionsType): void {
stringsPassed = options.strings stringsPassed = options.strings
const elAssignedRoot =
<ElTopicCommentListingType | null>
document.getElementById(options.selectors.root)
if (!elAssignedRoot) {
throw new Error(`root selector '${options.selectors.root}' not found`)
}
elRoot = elAssignedRoot
oneCommentInit() oneCommentInit()
initializeUrls(options) initializeUrls(options)
const root = getElRoot() elRoot.forumTopicCommentListing = new TopicCommentListing(options)
root.forumTopicCommentListing = new TopicCommentListing(root, options)
addEventListener('scroll', onScrollFirst, { once: true }) addEventListener('scroll', onScrollFirst, { once: true })
initialJumpToComment(root.forumTopicCommentListing).catch((e) => { initialJumpToComment(elRoot.forumTopicCommentListing).catch((e) => {
console.error('initial scroll resulted in failure:', e) console.error('initial scroll resulted in failure:', e)
}) })
} }