2024-05-23 09:42:14 +00:00
|
|
|
// SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-12-14 14:41:34 +00:00
|
|
|
import type { SVGButton } from './buttons'
|
2024-06-13 14:36:16 +00:00
|
|
|
import { logger } from '../../utils/logger'
|
2021-12-14 14:41:34 +00:00
|
|
|
|
2023-07-26 16:16:30 +00:00
|
|
|
interface displayButtonOptionsBase {
|
2021-12-14 14:41:34 +00:00
|
|
|
buttonContainer: HTMLElement
|
|
|
|
name: string
|
|
|
|
label: string
|
|
|
|
icon?: SVGButton
|
|
|
|
additionalClasses?: string[]
|
|
|
|
}
|
|
|
|
|
2023-07-26 16:16:30 +00:00
|
|
|
interface displayButtonOptionsCallback extends displayButtonOptionsBase {
|
2024-05-17 13:17:36 +00:00
|
|
|
callback: () => void | boolean | Promise<void>
|
2023-07-26 16:16:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface displayButtonOptionsHref extends displayButtonOptionsBase {
|
|
|
|
href: string
|
|
|
|
targetBlank?: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
type displayButtonOptions = displayButtonOptionsCallback | displayButtonOptionsHref
|
|
|
|
|
|
|
|
function displayButton (dbo: displayButtonOptions): void {
|
2021-12-14 14:41:34 +00:00
|
|
|
const button = document.createElement('a')
|
|
|
|
button.classList.add(
|
|
|
|
'orange-button', 'peertube-button-link',
|
|
|
|
'peertube-plugin-livechat-button',
|
2023-07-26 16:16:30 +00:00
|
|
|
'peertube-plugin-livechat-button-' + dbo.name
|
2021-12-14 14:41:34 +00:00
|
|
|
)
|
2023-07-26 16:16:30 +00:00
|
|
|
if (dbo.additionalClasses) {
|
|
|
|
for (let i = 0; i < dbo.additionalClasses.length; i++) {
|
|
|
|
button.classList.add(dbo.additionalClasses[i])
|
2021-12-14 14:41:34 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-26 16:16:30 +00:00
|
|
|
if ('callback' in dbo) {
|
|
|
|
button.onclick = dbo.callback
|
|
|
|
}
|
|
|
|
if ('href' in dbo) {
|
|
|
|
button.href = dbo.href
|
|
|
|
}
|
|
|
|
if (('targetBlank' in dbo) && dbo.targetBlank) {
|
|
|
|
button.target = '_blank'
|
|
|
|
}
|
|
|
|
if (dbo.icon) {
|
2021-12-14 14:41:34 +00:00
|
|
|
try {
|
2023-07-26 16:16:30 +00:00
|
|
|
const svg = dbo.icon()
|
2021-12-14 14:41:34 +00:00
|
|
|
const tmp = document.createElement('span')
|
|
|
|
tmp.innerHTML = svg.trim()
|
|
|
|
const svgDom = tmp.firstChild
|
|
|
|
if (svgDom) {
|
|
|
|
button.prepend(svgDom)
|
|
|
|
}
|
|
|
|
} catch (err) {
|
2023-07-26 16:16:30 +00:00
|
|
|
logger.error('Failed to generate the ' + dbo.name + ' button: ' + (err as string))
|
2021-12-14 14:41:34 +00:00
|
|
|
}
|
|
|
|
|
2023-07-26 16:16:30 +00:00
|
|
|
button.setAttribute('title', dbo.label)
|
2021-12-14 14:41:34 +00:00
|
|
|
} else {
|
2023-07-26 16:16:30 +00:00
|
|
|
button.textContent = dbo.label
|
2021-12-14 14:41:34 +00:00
|
|
|
}
|
2023-07-26 16:16:30 +00:00
|
|
|
dbo.buttonContainer.append(button)
|
2021-12-14 14:41:34 +00:00
|
|
|
}
|
|
|
|
|
2022-12-07 17:36:16 +00:00
|
|
|
export type {
|
|
|
|
displayButtonOptions
|
|
|
|
}
|
2021-12-14 14:41:34 +00:00
|
|
|
export {
|
|
|
|
displayButton
|
|
|
|
}
|