Files
dees-wcctools/ts_web/elements/wcc-properties.ts

557 lines
17 KiB
TypeScript
Raw Normal View History

2023-08-07 18:20:17 +02:00
import { DeesElement, property, html, customElement, type TemplateResult, state } from '@design.estate/dees-element';
2022-03-24 15:39:17 +01:00
import { WccDashboard } from './wcc-dashboard.js';
2020-05-11 00:36:58 +00:00
2021-04-01 19:09:57 +00:00
export type TPropertyType = 'String' | 'Number' | 'Boolean' | 'Object' | 'Enum' | 'Array';
2020-05-11 00:36:58 +00:00
export type TEnvironment = 'native' | 'desktop' | 'tablet' | 'phablet' | 'phone';
2020-11-26 02:28:17 +00:00
export type TTheme = 'bright' | 'dark';
2020-05-11 00:36:58 +00:00
let environment: TEnvironment = 'native';
2020-11-26 02:28:17 +00:00
export const setEnvironment = (envArg) => {
2020-05-11 00:36:58 +00:00
environment = envArg;
};
@customElement('wcc-properties')
2022-03-24 15:53:02 +01:00
export class WccProperties extends DeesElement {
@property({
type: WccDashboard
})
public dashboardRef: WccDashboard;
2020-11-26 02:28:17 +00:00
2020-05-11 00:36:58 +00:00
@property()
2022-03-24 15:53:02 +01:00
public selectedItem: (() => TemplateResult) | DeesElement;
2020-05-11 00:36:58 +00:00
@property()
2020-11-26 02:28:17 +00:00
public selectedViewport: TEnvironment = 'native';
@property()
public selectedTheme: TTheme = 'dark';
2020-05-11 00:36:58 +00:00
2020-05-24 19:37:43 +00:00
@property()
public warning: string = null;
2021-09-27 13:30:02 +02:00
@state()
2021-04-01 19:09:57 +00:00
propertyContent: TemplateResult[] = [];
2020-05-11 00:36:58 +00:00
public render(): TemplateResult {
return html`
<style>
:host {
2025-06-27 20:22:28 +00:00
/* CSS Variables - Always dark theme */
--background: #0a0a0a;
--foreground: #e5e5e5;
--card: #0f0f0f;
--card-foreground: #f0f0f0;
--muted: #1a1a1a;
--muted-foreground: #666;
--accent: #222;
--accent-foreground: #fff;
--border: rgba(255, 255, 255, 0.06);
--input: #141414;
--primary: #3b82f6;
--primary-foreground: #fff;
--ring: #3b82f6;
--radius: 4px;
--radius-sm: 2px;
--radius-md: 4px;
--radius-lg: 6px;
/* Base styles */
font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', sans-serif;
2020-05-11 00:36:58 +00:00
box-sizing: border-box;
position: absolute;
left: 200px;
2025-06-27 20:22:28 +00:00
height: 88px;
2020-05-11 00:36:58 +00:00
bottom: 0px;
right: 0px;
overflow: hidden;
2025-06-27 20:22:28 +00:00
background: var(--background);
color: var(--foreground);
border-top: 1px solid rgba(255, 255, 255, 0.08);
2020-05-11 00:36:58 +00:00
}
.grid {
display: grid;
2025-06-27 20:22:28 +00:00
grid-template-columns: 1fr 150px 300px 70px;
height: 100%;
2020-05-11 00:36:58 +00:00
}
.properties {
2025-06-27 20:22:28 +00:00
background: transparent;
2020-05-11 00:36:58 +00:00
overflow-y: auto;
display: grid;
2025-06-27 20:22:28 +00:00
grid-template-columns: repeat(3, 1fr);
border-right: 1px solid var(--border);
2020-05-11 00:36:58 +00:00
}
2023-01-07 14:05:25 +01:00
.material-symbols-outlined {
font-family: 'Material Symbols Outlined';
font-weight: normal;
font-style: normal;
font-size: 24px; /* Preferred icon size */
display: inline-block;
line-height: 1;
text-transform: none;
letter-spacing: normal;
word-wrap: normal;
white-space: nowrap;
direction: ltr;
2023-01-07 14:14:12 +01:00
font-variation-settings: 'FILL' 1, 'wght' 400, 'GRAD' 0, 'opsz' 48;
2023-01-07 14:05:25 +01:00
}
2020-05-11 00:36:58 +00:00
.properties .property {
2025-06-27 20:22:28 +00:00
padding: 0.4rem;
background: transparent;
border-right: 1px solid var(--border);
border-bottom: 1px solid var(--border);
transition: all 0.15s ease;
}
.properties .property:hover {
background: rgba(255, 255, 255, 0.02);
}
.properties .property-label {
font-size: 0.65rem;
font-weight: 400;
color: #888;
margin-bottom: 0.2rem;
text-transform: capitalize;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
2020-05-11 00:36:58 +00:00
}
2021-04-01 19:09:57 +00:00
2025-06-27 20:22:28 +00:00
.properties input[type="text"],
.properties input[type="number"],
2021-09-27 14:45:05 +02:00
.properties select {
2021-04-01 19:09:57 +00:00
display: block;
width: 100%;
2025-06-27 20:22:28 +00:00
padding: 0.25rem 0.4rem;
background: var(--input);
border: 1px solid transparent;
color: var(--foreground);
border-radius: var(--radius-sm);
font-size: 0.7rem;
transition: all 0.15s ease;
outline: none;
}
.properties input[type="text"]:focus,
.properties input[type="number"]:focus,
.properties select:focus {
border-color: var(--primary);
background: rgba(59, 130, 246, 0.1);
}
.properties input[type="checkbox"] {
width: 1rem;
height: 1rem;
cursor: pointer;
accent-color: var(--primary);
2021-04-01 19:09:57 +00:00
}
2020-11-26 02:28:17 +00:00
.viewportSelector,
.themeSelector {
2020-12-09 18:35:49 +00:00
user-select: none;
2025-06-27 20:22:28 +00:00
background: transparent;
display: flex;
flex-direction: column;
border-right: 1px solid var(--border);
2020-05-11 00:36:58 +00:00
}
2020-11-26 02:28:17 +00:00
.selectorButtons2 {
display: grid;
2025-06-27 20:22:28 +00:00
grid-template-columns: 1fr 1fr;
flex: 1;
2020-11-26 02:28:17 +00:00
}
.selectorButtons4 {
2020-05-11 00:36:58 +00:00
display: grid;
2025-06-27 20:22:28 +00:00
grid-template-columns: repeat(4, 1fr);
flex: 1;
2020-05-11 00:36:58 +00:00
}
2020-11-26 02:28:17 +00:00
.button {
2025-06-27 20:22:28 +00:00
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 0.5rem 0.25rem;
2020-05-11 00:36:58 +00:00
text-align: center;
2025-06-27 20:22:28 +00:00
background: transparent;
border: 1px solid var(--border);
transition: all 0.15s ease;
cursor: pointer;
font-size: 0.65rem;
gap: 0.2rem;
color: #999;
2020-05-11 00:36:58 +00:00
}
2025-06-27 20:22:28 +00:00
2020-11-26 02:28:17 +00:00
.button:hover {
2025-06-27 20:22:28 +00:00
background: rgba(59, 130, 246, 0.05);
color: #bbb;
2020-05-11 00:36:58 +00:00
}
2020-11-26 02:28:17 +00:00
.button.selected {
2025-06-27 20:22:28 +00:00
background: rgba(59, 130, 246, 0.15);
color: var(--primary);
border-color: rgba(59, 130, 246, 0.3);
2020-05-11 00:36:58 +00:00
}
2020-11-26 02:28:17 +00:00
.button.selected:hover {
2025-06-27 20:22:28 +00:00
background: rgba(59, 130, 246, 0.2);
}
.button .material-symbols-outlined {
font-size: 18px;
font-variation-settings: 'FILL' 0, 'wght' 300;
}
.button.selected .material-symbols-outlined {
font-variation-settings: 'FILL' 1, 'wght' 400;
2020-05-11 00:36:58 +00:00
}
.panelheading {
2025-06-27 20:22:28 +00:00
padding: 0.3rem 0.5rem;
font-weight: 500;
font-size: 0.65rem;
background: rgba(59, 130, 246, 0.03);
border-bottom: 1px solid var(--border);
color: #888;
text-transform: uppercase;
letter-spacing: 0.08em;
2020-05-11 00:36:58 +00:00
}
.docs {
2025-06-27 20:22:28 +00:00
display: flex;
align-items: center;
justify-content: center;
2020-05-11 00:36:58 +00:00
text-transform: uppercase;
2025-06-27 20:22:28 +00:00
background: transparent;
cursor: pointer;
font-size: 0.65rem;
font-weight: 500;
letter-spacing: 0.08em;
transition: all 0.15s ease;
color: #666;
2020-05-11 00:36:58 +00:00
}
.docs:hover {
2025-06-27 20:22:28 +00:00
background: rgba(59, 130, 246, 0.05);
color: #999;
2020-05-11 00:36:58 +00:00
}
2020-05-24 19:37:43 +00:00
.warning {
position: absolute;
2025-06-27 20:22:28 +00:00
background: rgba(20, 20, 20, 0.8);
color: #888;
top: 0.5rem;
bottom: 0.5rem;
left: 0.5rem;
right: calc(520px + 0.5rem);
display: flex;
align-items: center;
justify-content: center;
padding: 1.5rem;
font-size: 0.85rem;
border-radius: var(--radius-md);
border: 1px solid var(--border);
backdrop-filter: blur(8px);
2020-05-24 19:37:43 +00:00
}
2020-05-11 00:36:58 +00:00
</style>
<div class="grid">
<div class="properties">
<div class="panelheading">Properties</div>
2021-04-01 19:09:57 +00:00
${this.propertyContent}
2020-05-11 00:36:58 +00:00
</div>
2020-11-26 02:28:17 +00:00
<div class="themeSelector">
<div class="panelheading">Theme</div>
<div class="selectorButtons2">
<div
class="button ${this.selectedTheme === 'dark' ? 'selected' : null}"
@click=${() => {
this.selectTheme('dark');
}}
>
2025-06-27 20:22:28 +00:00
Dark<i class="material-symbols-outlined">brightness_3</i>
2020-11-26 02:28:17 +00:00
</div>
<div
class="button ${this.selectedTheme === 'bright' ? 'selected' : null}"
@click=${() => {
this.selectTheme('bright');
}}
>
2025-06-27 20:22:28 +00:00
Bright<i class="material-symbols-outlined">flare</i>
2020-11-26 02:28:17 +00:00
</div>
</div>
</div>
<div class="viewportSelector">
<div class="panelheading">Viewport</div>
<div class="selectorButtons4">
2020-05-11 00:36:58 +00:00
<div
2020-11-26 02:28:17 +00:00
class="button ${this.selectedViewport === 'phone' ? 'selected' : null}"
2020-05-11 00:36:58 +00:00
@click=${() => {
this.selectViewport('phone');
}}
>
2025-06-27 20:22:28 +00:00
Phone<i class="material-symbols-outlined">smartphone</i>
2020-05-11 00:36:58 +00:00
</div>
<div
2020-11-26 02:28:17 +00:00
class="button ${this.selectedViewport === 'phablet' ? 'selected' : null}"
2020-05-11 00:36:58 +00:00
@click=${() => {
this.selectViewport('phablet');
}}
>
2025-06-27 20:22:28 +00:00
Phablet<i class="material-symbols-outlined">smartphone</i>
2020-05-11 00:36:58 +00:00
</div>
<div
2020-11-26 02:28:17 +00:00
class="button ${this.selectedViewport === 'tablet' ? 'selected' : null}"
2020-05-11 00:36:58 +00:00
@click=${() => {
this.selectViewport('tablet');
}}
>
2025-06-27 20:22:28 +00:00
Tablet<i class="material-symbols-outlined">tablet</i>
2020-05-11 00:36:58 +00:00
</div>
<div
2020-11-26 02:28:17 +00:00
class="button ${this.selectedViewport === 'desktop' ||
this.selectedViewport === 'native'
? 'selected'
: null}"
2020-05-11 00:36:58 +00:00
@click=${() => {
this.selectViewport('native');
}}
>
2025-06-27 20:22:28 +00:00
Desktop<i class="material-symbols-outlined">desktop_windows</i>
2020-05-11 00:36:58 +00:00
</div>
</div>
</div>
<div class="docs">Docs</div>
</div>
2020-11-26 02:28:17 +00:00
${this.warning ? html`<div class="warning">${this.warning}</div>` : null}
2020-05-11 00:36:58 +00:00
`;
}
private async findElementRecursively(container: Element, elementClass: any, maxDepth: number = 5): Promise<HTMLElement | null> {
if (maxDepth <= 0) return null;
try {
// Check direct children
for (const child of Array.from(container.children)) {
if (child instanceof elementClass) {
return child as HTMLElement;
}
}
// Search in all children recursively
for (const child of Array.from(container.children)) {
// First, always check the light DOM children
const found = await this.findElementRecursively(child, elementClass, maxDepth - 1);
if (found) return found;
// Also check shadow root if it exists
if (child.shadowRoot) {
const shadowFound = await this.findElementRecursively(child.shadowRoot as any, elementClass, maxDepth - 1);
if (shadowFound) return shadowFound;
}
}
} catch (error) {
console.error('Error in findElementRecursively:', error);
}
return null;
}
2021-04-01 19:09:57 +00:00
public async createProperties() {
2021-09-27 14:45:05 +02:00
console.log('creating properties for:');
console.log(this.selectedItem);
// Clear any previous warnings
this.warning = null;
2021-04-01 19:09:57 +00:00
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));
2021-09-27 14:45:05 +02:00
const enumValues: any[] = [];
2021-04-01 19:09:57 +00:00
Object.keys(propertyArg.type).forEach((valueArg: string) => {
enumValues.push(valueArg);
});
return enumValues;
};
2022-04-14 16:56:04 +02:00
const determinePropertyType = async (propertyArg: any): Promise<TPropertyType> => {
2021-04-01 19:09:57 +00:00
const typeName: any | undefined = propertyArg.type.name;
if (typeName) {
return typeName;
} else {
return Array.isArray(propertyArg)
? 'Array'
: isEnumeration(propertyArg)
? 'Enum'
: 'Object';
}
};
2021-09-27 14:45:05 +02:00
if (this.selectedItem && (this.selectedItem as any).demo) {
console.log(`Got Dees-Element for property evaluation.`);
2021-04-01 19:09:57 +00:00
const anonItem: any = this.selectedItem;
2021-11-26 18:27:29 +01:00
if (!anonItem) {
2021-11-26 19:16:09 +01:00
this.warning = 'no element selected';
2021-11-26 18:27:29 +01:00
return;
}
2021-09-27 14:45:05 +02:00
console.log(anonItem.elementProperties);
2021-04-01 19:09:57 +00:00
const wccFrame = await this.dashboardRef.wccFrame;
// Wait for render to complete and any demo wrappers to run
await new Promise(resolve => setTimeout(resolve, 200));
// Try to find the element with recursive search
const viewport = await wccFrame.getViewportElement();
let firstFoundInstantiatedElement: HTMLElement = await this.findElementRecursively(
viewport,
this.selectedItem as any
);
// Retry logic if element not found
let retries = 0;
while (!firstFoundInstantiatedElement && retries < 5) {
await new Promise(resolve => setTimeout(resolve, 200));
try {
firstFoundInstantiatedElement = await this.findElementRecursively(
viewport,
this.selectedItem as any
);
} catch (error) {
console.error('Error during element search retry:', error);
}
retries++;
2021-04-01 19:09:57 +00:00
}
2021-11-26 19:05:04 +01:00
if (!firstFoundInstantiatedElement) {
this.warning = `no first instantiated element found for >>${anonItem.name}<< after ${retries} retries`;
this.propertyContent = [];
2021-11-26 19:05:04 +01:00
return;
}
2021-09-27 14:45:05 +02:00
const classProperties: Map<string, any> = anonItem.elementProperties;
2021-11-26 18:52:44 +01:00
if (!classProperties) {
2021-11-26 19:16:09 +01:00
this.warning = `selected element >>${anonItem.name}<< does not expose element properties`;
2021-11-26 18:52:44 +01:00
return;
}
2021-11-26 19:16:09 +01:00
this.warning = null;
2021-04-01 19:09:57 +00:00
const propertyArray: TemplateResult[] = [];
for (const key of classProperties.keys()) {
2021-09-27 14:45:05 +02:00
if (key === 'goBright' || key === 'domtools') {
2021-04-01 19:09:57 +00:00
continue;
}
try {
const property = classProperties.get(key);
const propertyTypeString = await determinePropertyType(property);
propertyArray.push(
2021-04-01 19:09:57 +00:00
html`
<div class="property">
2025-06-27 20:22:28 +00:00
<div class="property-label">${key} (${propertyTypeString})</div>
2021-04-01 19:09:57 +00:00
${(() => {
switch (propertyTypeString) {
case 'Boolean':
return html`<input
type="checkbox"
2021-11-26 19:05:04 +01:00
?checked=${firstFoundInstantiatedElement[key]}
2021-04-01 19:09:57 +00:00
@input="${(eventArg: any) => {
2021-11-26 19:05:04 +01:00
firstFoundInstantiatedElement[key] = eventArg.target.checked;
2021-04-01 19:09:57 +00:00
}}"
/>`;
case 'String':
return html`<input
type="text"
.value=${firstFoundInstantiatedElement[key] || ''}
2021-04-01 19:09:57 +00:00
@input="${(eventArg: any) => {
2021-11-26 19:05:04 +01:00
firstFoundInstantiatedElement[key] = eventArg.target.value;
2021-04-01 19:09:57 +00:00
}}"
/>`;
case 'Number':
return html`<input
type="number"
.value=${firstFoundInstantiatedElement[key] ?? ''}
2021-04-01 19:09:57 +00:00
@input="${(eventArg: any) => {
firstFoundInstantiatedElement[key] = parseFloat(eventArg.target.value) || 0;
2021-04-01 19:09:57 +00:00
}}"
/>`;
case 'Enum':
const enumValues: any[] = getEnumValues(property);
return html`<select
.value=${firstFoundInstantiatedElement[key] || ''}
2021-09-27 14:45:05 +02:00
@change="${(eventArg: any) => {
2021-11-26 19:05:04 +01:00
firstFoundInstantiatedElement[key] = eventArg.target.value;
2021-09-27 14:45:05 +02:00
}}"
>
${enumValues.map((valueArg) => {
return html`
<option
value="${valueArg}"
2021-09-27 14:45:05 +02:00
>
${valueArg}
</option>
`;
})}
2021-04-01 19:09:57 +00:00
</select>`;
}
})()}
</div>
`
);
} catch (error) {
console.error(`Error processing property ${key}:`, error);
// Continue with next property even if this one fails
}
2021-04-01 19:09:57 +00:00
}
this.propertyContent = propertyArray;
} else {
2021-11-26 19:16:09 +01:00
console.log(`Cannot extract properties of ${(this.selectedItem as any)?.name}`);
this.warning = `You selected a page.`;
2021-04-01 19:09:57 +00:00
return null;
}
}
2020-11-26 02:28:17 +00:00
public selectTheme(themeArg: TTheme) {
this.selectedTheme = themeArg;
this.dispatchEvent(
new CustomEvent('selectedTheme', {
detail: themeArg,
})
);
console.log(this.dashboardRef.selectedType);
this.dashboardRef.buildUrl();
}
2021-11-26 19:18:14 +01:00
public async scheduleUpdate() {
try {
await this.createProperties();
} catch (error) {
console.error('Error creating properties:', error);
// Clear property content on error to show clean state
this.propertyContent = [];
}
// Always call super.scheduleUpdate to ensure component updates
2021-11-26 19:18:14 +01:00
super.scheduleUpdate();
2021-04-01 19:09:57 +00:00
}
2020-05-11 00:36:58 +00:00
public selectViewport(viewport: TEnvironment) {
this.selectedViewport = viewport;
setEnvironment(viewport);
this.dispatchEvent(
new CustomEvent('selectedViewport', {
2020-11-26 02:28:17 +00:00
detail: viewport,
2020-05-11 00:36:58 +00:00
})
);
2020-11-26 02:28:17 +00:00
this.dashboardRef.buildUrl();
2020-05-11 00:36:58 +00:00
}
}