import { LitElement, property, html, customElement, TemplateResult, state } from 'lit-element'; import { WccDashboard } from './wcc-dashboard'; export type TPropertyType = 'String' | 'Number' | 'Boolean' | 'Object' | 'Enum' | 'Array'; export type TEnvironment = 'native' | 'desktop' | 'tablet' | 'phablet' | 'phone'; export type TTheme = 'bright' | 'dark'; let environment: TEnvironment = 'native'; export const setEnvironment = (envArg) => { environment = envArg; }; @customElement('wcc-properties') export class WccProperties extends LitElement { @property() dashboardRef: WccDashboard; @property() public selectedItem: (() => TemplateResult) | LitElement; @property() public selectedViewport: TEnvironment = 'native'; @property() public selectedTheme: TTheme = 'dark'; @property() public warning: string = null; @state() propertyContent: TemplateResult[] = []; public render(): TemplateResult { return html`
Properties
${this.propertyContent}
Theme
{ this.selectTheme('dark'); }} > Dark
brightness_3
{ this.selectTheme('bright'); }} > Bright
flare
Viewport
{ this.selectViewport('phone'); }} > Phone
smartphone
{ this.selectViewport('phablet'); }} > Phablet
smartphone
{ this.selectViewport('tablet'); }} > Tablet
tablet
{ this.selectViewport('native'); }} > Desktop
desktop_windows
Docs
${this.warning ? html`
${this.warning}
` : null} `; } public async createProperties() { console.log('creating properties for:'); console.log(this.selectedItem); const isEnumeration = (propertyArg): boolean => { const keys = Object.keys(propertyArg.type); const values = []; for (const key of keys) { let value = propertyArg.type[key]; if (typeof value === 'number') { value = value.toString(); } values.push(value); } for (const key of keys) { if (values.indexOf(key) < 0) { return false; } } return true; }; const getEnumValues = (propertyArg): any[] => { console.log(JSON.stringify(propertyArg)); const enumValues: any[] = []; Object.keys(propertyArg.type).forEach((valueArg: string) => { enumValues.push(valueArg); }); return enumValues; }; const determinePropertyType = (propertyArg: any): TPropertyType => { const typeName: any | undefined = propertyArg.type.name; if (typeName) { return typeName; } else { return Array.isArray(propertyArg) ? 'Array' : isEnumeration(propertyArg) ? 'Enum' : 'Object'; } }; if (this.selectedItem && (this.selectedItem as any).demo) { console.log(`Got Dees-Element for property evaluation.`); const anonItem: any = this.selectedItem; if (!anonItem) { this.warning = 'no element selected'; return; } console.log(anonItem.elementProperties); const wccFrame = await this.dashboardRef.wccFrame; let firstFoundInstantiatedElement: HTMLElement; for (const element of Array.from(wccFrame.children)) { if (element instanceof (this.selectedItem as any)) { firstFoundInstantiatedElement = element as HTMLElement; break; } } if (!firstFoundInstantiatedElement) { this.warning = `no first instantiated element found for >>${anonItem.name}<<`; return; } const classProperties: Map = anonItem.elementProperties; if (!classProperties) { this.warning = `selected element >>${anonItem.name}<< does not expose element properties`; return; } this.warning = null; const propertyArray: TemplateResult[] = []; for (const key of classProperties.keys()) { if (key === 'goBright' || key === 'domtools') { continue; } const property = classProperties.get(key); const propertyTypeString = determinePropertyType(property); propertyArray.push( html`
${key} / ${propertyTypeString}
${(() => { switch (propertyTypeString) { case 'Boolean': return html``; case 'String': return html``; case 'Number': return html``; case 'Enum': const enumValues: any[] = getEnumValues(property); return html``; } })()}
` ); } this.propertyContent = propertyArray; } else { console.log(`Cannot extract properties of ${(this.selectedItem as any)?.name}`); this.warning = `You selected a page.`; return null; } } public selectTheme(themeArg: TTheme) { this.selectedTheme = themeArg; this.dispatchEvent( new CustomEvent('selectedTheme', { detail: themeArg, }) ); console.log(this.dashboardRef.selectedType); this.dashboardRef.buildUrl(); } public async scheduleUpdate() { await this.createProperties(); super.scheduleUpdate(); } public selectViewport(viewport: TEnvironment) { this.selectedViewport = viewport; setEnvironment(viewport); this.dispatchEvent( new CustomEvent('selectedViewport', { detail: viewport, }) ); this.dashboardRef.buildUrl(); } }