import LayoutBuilder from './layout-builder' import { createWidget } from './widget-builder' import { WidgetBuilderContext } from './widgets-builder' import { getScriptPath } from '@dsf/helpers/script-helper' export type DialogHeaderOptions = { image?: Pixmap imagePath?: string imageWidth?: number height?: number html?: string text?: string } const escapeHtml = (value: string): string => value .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"') .replace(/'/g, ''') .replace(/\r?\n/g, '
') const resolveImagePath = (filePath: string): string => { const value = String(filePath ?? '').replace(/\\/g, '/') if (!value) return '' if (value.indexOf(':') >= 0 || value.charAt(0) === '/') return value return `${getScriptPath()}/${value.replace(/^\.\//, '')}` } export class DialogHeaderBuilder { constructor( private readonly context: WidgetBuilderContext, private readonly options: DialogHeaderOptions ) { } build(): DzWidget { const height = this.options.height ?? 96 const imageWidth = this.options.imageWidth ?? height const html = this.options.html ?? ( typeof this.options.text === 'string' ? escapeHtml(this.options.text) : '' ) return createWidget(this.context).build(DzWidget, (container) => { container.setFixedHeight(height) LayoutBuilder .create(this.context) .parent(container) .direction('horizontal') .build(() => { this.buildImage(height, imageWidth) this.buildText(html, height) }) }) } private buildImage(height: number, imageWidth: number): void { const pixmap = this.options.image ?? ( this.options.imagePath ? new Pixmap(resolveImagePath(this.options.imagePath)) : null ) if (!pixmap) return createWidget(this.context).build(DzLabel, (label) => { label.pixmap = pixmap label.scaledContents = true label.setFixedWidth(imageWidth) label.setFixedHeight(height) }) } private buildText(html: string, height: number): void { if (!html) return createWidget(this.context).build(DzTextBrowser, (textBrowser) => { textBrowser.html = html textBrowser.readOnly = true textBrowser.lineWrapMode = DzTextEdit.WidgetWidth textBrowser.wordWrapMode = DzTextEdit.WordWrap textBrowser.setFixedHeight(height) }) } }