dees-wcctools/ts_web/elements/wcc-dashboard.ts

178 lines
5.5 KiB
TypeScript
Raw Normal View History

2023-08-07 16:20:17 +00:00
import { DeesElement, property, html, customElement, type TemplateResult, queryAsync, render, domtools } from '@design.estate/dees-element';
2020-05-11 00:36:58 +00:00
2022-03-24 14:39:17 +00:00
import * as plugins from '../wcctools.plugins.js';
2020-05-27 22:13:50 +00:00
2020-05-11 00:36:58 +00:00
// wcc tools
2022-03-24 14:53:02 +00:00
import './wcc-frame.js';
import './wcc-sidebar.js';
import './wcc-properties.js';
2023-08-07 16:20:17 +00:00
import { type TTheme } from './wcc-properties.js';
import { type TElementType } from './wcc-sidebar.js';
import { breakpoints } from '@design.estate/dees-domtools';
2022-03-24 14:53:02 +00:00
import { WccFrame } from './wcc-frame.js';
2020-05-11 00:36:58 +00:00
@customElement('wcc-dashboard')
2022-03-24 14:39:17 +00:00
export class WccDashboard extends DeesElement {
2020-07-15 19:55:35 +00:00
2020-11-26 02:28:17 +00:00
@property()
public selectedType: TElementType;
2020-05-11 00:36:58 +00:00
@property()
2020-11-27 15:59:18 +00:00
public selectedItemName: string;
@property()
2022-03-24 14:39:17 +00:00
public selectedItem: (() => TemplateResult) | DeesElement;
2020-05-11 00:36:58 +00:00
@property()
2020-06-01 15:07:13 +00:00
public selectedViewport: plugins.deesDomtools.breakpoints.TViewport = 'desktop';
2020-05-11 00:36:58 +00:00
2020-11-26 02:28:17 +00:00
@property()
public selectedTheme: TTheme = 'dark';
2020-05-11 00:36:58 +00:00
@property()
2020-11-27 15:59:18 +00:00
public pages: { [key: string]: () => TemplateResult } = {};
2020-05-11 00:36:58 +00:00
@property()
2022-03-24 14:39:17 +00:00
public elements: { [key: string]: DeesElement } = {};
2020-05-11 00:36:58 +00:00
2020-05-24 19:37:43 +00:00
@property()
public warning: string = null;
2021-04-01 19:09:57 +00:00
@queryAsync('wcc-frame')
public wccFrame: Promise<WccFrame>;
2020-05-24 19:37:43 +00:00
2020-11-26 02:28:17 +00:00
constructor(
2022-03-24 14:39:17 +00:00
elementsArg?: { [key: string]: DeesElement },
2020-11-27 15:59:18 +00:00
pagesArg?: { [key: string]: () => TemplateResult }
2020-11-26 02:28:17 +00:00
) {
2020-05-11 00:36:58 +00:00
super();
2020-05-23 14:00:49 +00:00
if (elementsArg) {
this.elements = elementsArg;
2020-11-26 02:28:17 +00:00
console.log('got elements:');
console.log(this.elements);
2020-05-23 14:00:49 +00:00
}
if (pagesArg) {
this.pages = pagesArg;
}
2020-05-11 00:36:58 +00:00
}
public render(): TemplateResult {
return html`
<style>
:host {
background: #fcfcfc;
display: block;
box-sizing: border-box;
}
:host([hidden]) {
display: none;
}
</style>
2020-11-26 02:28:17 +00:00
<wcc-sidebar
.dashboardRef=${this}
.selectedItem=${this.selectedItem}
@selectedType=${(eventArg) => {
this.selectedType = eventArg.detail;
}}
2020-11-27 15:59:18 +00:00
@selectedItemName=${(eventArg) => {
2024-05-06 23:32:45 +00:00
document.title = eventArg.detail;
2020-11-27 15:59:18 +00:00
this.selectedItemName = eventArg.detail;
}}
@selectedItem=${(eventArg) => {
this.selectedItem = eventArg.detail;
}}
2020-11-26 02:28:17 +00:00
></wcc-sidebar>
<wcc-properties
.dashboardRef=${this}
.warning="${this.warning}"
.selectedItem=${this.selectedItem}
2020-11-26 02:50:50 +00:00
.selectedViewport=${this.selectedViewport}
.selectedTheme=${this.selectedTheme}
2020-11-26 02:28:17 +00:00
@selectedViewport=${(eventArg) => {
this.selectedViewport = eventArg.detail;
2021-11-26 16:47:08 +00:00
this.scheduleUpdate();
2020-11-26 02:28:17 +00:00
}}
@selectedTheme=${(eventArg) => {
this.selectedTheme = eventArg.detail;
}}
></wcc-properties>
2020-05-11 00:36:58 +00:00
<wcc-frame id="wccFrame" viewport=${this.selectedViewport}>
</wcc-frame>
`;
}
2020-05-24 19:37:43 +00:00
public setWarning(warningTextArg: string) {
if (this.warning !== warningTextArg) {
console.log(warningTextArg);
this.warning = warningTextArg;
setTimeout(() => {
2021-11-26 16:47:08 +00:00
this.scheduleUpdate();
2020-05-24 19:37:43 +00:00
}, 0);
}
}
2020-07-15 21:00:47 +00:00
public async firstUpdated() {
this.domtools = await plugins.deesDomtools.DomTools.setupDomTools();
2020-12-07 04:07:32 +00:00
this.domtools.router.on(
'/wcctools-route/:itemType/:itemName/:viewport/:theme',
async (routeInfo) => {
this.selectedType = routeInfo.params.itemType as TElementType;
this.selectedItemName = routeInfo.params.itemName;
2021-11-26 16:53:04 +00:00
this.selectedViewport = routeInfo.params.viewport as breakpoints.TViewport;
2020-12-07 04:07:32 +00:00
this.selectedTheme = routeInfo.params.theme as TTheme;
if (routeInfo.params.itemType === 'element') {
this.selectedItem = this.elements[routeInfo.params.itemName];
} else if (routeInfo.params.itemType === 'page') {
this.selectedItem = this.pages[routeInfo.params.itemName];
}
const domtoolsInstance = await plugins.deesDomtools.elementBasic.setup();
this.selectedTheme === 'bright'
? domtoolsInstance.themeManager.goBright()
: domtoolsInstance.themeManager.goDark();
2020-11-26 02:28:17 +00:00
}
2020-12-07 04:07:32 +00:00
);
2020-11-26 02:28:17 +00:00
}
2020-07-15 21:00:47 +00:00
2020-11-26 02:28:17 +00:00
public async updated() {
this.domtools = await plugins.deesDomtools.DomTools.setupDomTools();
await this.domtools.router._handleRouteState();
const storeElement = this.selectedItem;
2022-04-14 14:56:04 +00:00
const wccFrame: WccFrame = this.shadowRoot.querySelector('wcc-frame');
if (this.selectedType === 'page' && this.selectedItem) {
if (typeof this.selectedItem === 'function') {
console.log('slotting page.');
const viewport = await wccFrame.getViewportElement();
render(this.selectedItem(), viewport);
console.log('rendered page.');
} else {
console.error('The selected item looks strange:');
console.log(this.selectedItem);
}
} else if (this.selectedType === 'element' && this.selectedItem) {
console.log('slotting element.');
const anonItem: any = this.selectedItem;
if (!anonItem.demo) {
this.setWarning(`component ${anonItem.name} does not expose a demo property.`);
return;
}
if (!(typeof anonItem.demo === 'function')) {
this.setWarning(
`component ${anonItem.name} has demo property, but it is not of type function`
);
return;
}
this.setWarning(null);
const viewport = await wccFrame.getViewportElement();
render(anonItem.demo(), viewport);;
}
2020-11-26 02:28:17 +00:00
}
public buildUrl() {
this.domtools.router.pushUrl(
2020-12-07 04:07:32 +00:00
`/wcctools-route/${this.selectedType}/${this.selectedItemName}/${this.selectedViewport}/${this.selectedTheme}`
2020-11-26 02:28:17 +00:00
);
2020-07-15 21:00:47 +00:00
}
2020-05-11 00:36:58 +00:00
}