/** * Header for Bryntum demos */ import React, { Component, createRef, Fragment, ReactNode, RefObject } from 'react'; import { BryntumFullscreenButton } from './BryntumFullscreenButton'; import { BryntumButton } from './BryntumButton'; import { BryntumThemeCombo } from './BryntumThemeCombo'; import { AjaxHelper, CSSHelper, DemoCodeEditor } from '@bryntum/gantt'; export type BryntumDemoHeaderProps = { /** * Insert children to demo header */ children?: ReactNode | ReactNode[] /** * Demo title for header */ title?: string /** * Display component when set to `true` */ themeCombo?: boolean } interface DemoCodeEditorState { hiddenEditor: boolean } export class BryntumDemoHeader extends Component { static defaultProps: BryntumDemoHeaderProps = { themeCombo : true }; codeButtonRef: RefObject = createRef(); downloadButtonRef: RefObject = createRef(); isTest = document.location.search.includes('test'); demoProduct = document.location.href.match(/\/(\w+)(-trial)?\/[-\w]*examples\//)?.[1].toLowerCase() || 'gantt'; downloadLink = `https://bryntum.com/download/?product=${this.demoProduct}`; appFolder = '../'; constructor(props: any) { super(props); this.state = { hiddenEditor : !Boolean(DemoCodeEditor.monacoCodePath) }; } codeEditor?: DemoCodeEditor; getLink(): string { // Convert: // https://bryntum.com/products/schedulerpro/examples-scheduler/frameworks/react/javascript/basic/build/ // https://bryntum.com/products/schedulerpro/examples/frameworks/react/javascript/basic/build/ // To: // https://bryntum.com/products/schedulerpro/examples/#example-examples-scheduler-frameworks-react-basic // https://bryntum.com/products/schedulerpro/examples/#example-frameworks-react-basic const match = /(.*?\/)(examples.*?\/frameworks\/.*?)\/(build|out|dist)/.exec(document.location.href); return match ? `${match[1]}examples/#example-${match[2].replace(/\//gm, '-').replace('examples-frameworks', 'frameworks')}` : '#'; } getTitle(): string { return this.props.title || document.title.split(' - ')[1] || document.title; } shouldComponentUpdate(nextProps: Readonly): boolean { return nextProps.title !== this.props.title; } toggleCodeEditor = async() => { this.codeEditor = await DemoCodeEditor.toggleCodeEditor( this.codeEditor, this.codeButtonRef!.current!.instance, { appFolder : this.appFolder, preferredSources : [ /App\.[jt]sx?/, /\w+Config\.[jt]sx?/ ] } ); }; async componentDidMount() { if (!this.state.hiddenEditor) { CSSHelper.insertRule('body { flex-direction : row !important }'); CSSHelper.insertRule('#root, #container { flex: 1 !important; overflow : hidden !important }'); CSSHelper.insertRule('#tools .b-widget { height: 2.5em !important }'); const appConfig = (await AjaxHelper.get(`${this.appFolder}app.config.json`, { parseJson : true })).parsedJson; if (appConfig.zip || this.isTest) { const downloadButton = this.downloadButtonRef!.current!.instance; downloadButton.hidden = false; downloadButton.href = `${this.appFolder}${appConfig.zip}`; } } // Enables special styling when generating screenshots const params = new URLSearchParams(window.location.search); if (params.has('screenshot')) { document.body.classList.add('b-screenshot'); if (window.bryntum) { (window.bryntum as any).noAnimations = true; } } } render(): ReactNode { return (

{this.getTitle()}

{this.props.children} {this.props.themeCombo && }
); } }