Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring components etc into smaller one #226

Merged
merged 11 commits into from
Feb 27, 2024
342 changes: 227 additions & 115 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
},
"devDependencies": {
"@babel/core": "7.23.9",
"@fltri/eslint-config": "1.2.3",
"@fltri/eslint-config": "1.2.4",
"@material/mwc-ripple": "0.27.0",
"@material/tab-bar": "14.0.0",
"@rollup/plugin-babel": "6.0.4",
Expand All @@ -62,7 +62,7 @@
"@types/hammerjs": "2.0.45",
"@types/jest": "29.5.12",
"@typescript-eslint/parser": "5.62.0",
"eslint": "8.56.0",
"eslint": "8.57.0",
"eslint-import-resolver-typescript": "3.6.1",
"eslint-plugin-import": "2.29.1",
"eslint-plugin-jest": "27.9.0",
Expand Down
72 changes: 72 additions & 0 deletions src/cards/trash-card/container/cards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { LitElement, css, html, nothing } from 'lit';
import { styleMap } from 'lit/directives/style-map.js';
import { customElement, state } from 'lit/decorators.js';
import { TRASH_CARD_NAME } from '../const';

import '../items/card';

import type { TrashCardConfig } from '../trash-card-config';
import type { HassEntity } from 'home-assistant-js-websocket';
import type { CalendarItem } from '../../../utils/calendarItem';
import type { HomeAssistant } from '../../../utils/ha';

@customElement(`${TRASH_CARD_NAME}-cards-container`)
class Cards extends LitElement {
@state() private readonly items?: CalendarItem[];

@state() private readonly hass?: HomeAssistant;

@state() private readonly config?: TrashCardConfig;

public render () {
if (!this.config || !this.hass || !this.config.entity) {
return nothing;
}

const entityId = this.config.entity;
const stateObj = this.hass.states[entityId] as HassEntity | undefined;

if (!stateObj || !this.items || this.items.length === 0) {
return nothing;
}

const itemsPerRow = this.config.items_per_row ?? 1;

const cssStyleMap = styleMap({
// eslint-disable-next-line @typescript-eslint/naming-convention
'grid-template-columns': `repeat(${itemsPerRow}, calc(calc(100% - ${(itemsPerRow - 1) * 5}px) / ${itemsPerRow}))`
});

return html`
<div style=${cssStyleMap} class="card-container">
${this.items.map(item => html`
<trash-card-item-card
.item=${item}
.config=${this.config}
.hass=${this.hass}
>
</trash-card-item-card>
`)}
</div>
`;
}

public static get styles () {
return [
css`
.card-container {
display: grid;
grid-auto-rows: 1fr;
grid-gap: var(--grid-card-gap, 2px);
}
trash-card-item-card {
grid-row: auto / span 1;
}
`
];
}
}

export {
Cards
};
85 changes: 85 additions & 0 deletions src/cards/trash-card/container/chips.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { LitElement, css, html, nothing } from 'lit';
import { defaultHaCardStyle } from '../../../utils/defaultHaCardStyle';
import { customElement, state } from 'lit/decorators.js';
import { TRASH_CARD_NAME } from '../const';

import '../items/chip';

import type { HomeAssistant } from '../../../utils/ha';
import type { TrashCardConfig } from '../trash-card-config';
import type { HassEntity } from 'home-assistant-js-websocket';
import type { CalendarItem } from '../../../utils/calendarItem';

@customElement(`${TRASH_CARD_NAME}-chips-container`)
class Chips extends LitElement {
@state() private readonly items?: CalendarItem[];

@state() private readonly hass?: HomeAssistant;

@state() private readonly config?: TrashCardConfig;

public render () {
if (!this.config || !this.hass || !this.config.entity) {
return nothing;
}

const entityId = this.config.entity;
const stateObj = this.hass.states[entityId] as HassEntity | undefined;

if (!stateObj || !this.items || this.items.length === 0) {
return nothing;
}

return html`
<div class="chip-container">
${this.items.map(item => html`
<trash-card-item-chip
.item=${item}
.config=${this.config}
.hass=${this.hass}
>
</trash-card-item-card>
`)}
</div>
`;
}

public static get styles () {
return [
defaultHaCardStyle,
css`
.chip-container {
display: flex;
flex-direction: row;
align-items: flex-start;
justify-content: flex-start;
flex-wrap: wrap;
margin-bottom: calc(-1 * var(--chip-spacing));
}
.chip-container.align-end {
justify-content: flex-end;
}
.chip-container.align-center {
justify-content: center;
}
.chip-container.align-justify {
justify-content: space-between;
}
.chip-container * {
margin-bottom: var(--chip-spacing);
}
.chip-container *:not(:last-child) {
margin-right: var(--chip-spacing);
}
.chip-container[rtl] *:not(:last-child) {
margin-right: initial;
margin-left: var(--chip-spacing);
}
`
];
}
}

export {
Chips
};
69 changes: 69 additions & 0 deletions src/cards/trash-card/container/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { LitElement, css, html, nothing } from 'lit';
import { customElement, state } from 'lit/decorators.js';
import { TRASH_CARD_NAME } from '../const';
import { defaultHaCardStyle } from '../../../utils/defaultHaCardStyle';

import '../elements/title';
import '../items/logrow';

import type { Debugger } from '../../../utils/debugger';

@customElement(`${TRASH_CARD_NAME}-debug-container`)
class Debug extends LitElement {
@state() private readonly debugger?: Debugger;

protected copyDebugLogToClipboard (ev: CustomEvent): void {
ev.stopPropagation();
// eslint-disable-next-line @typescript-eslint/no-floating-promises
navigator.clipboard.writeText(JSON.stringify(this.debugger?.getLogs() ?? {})).
then(() => {
// eslint-disable-next-line no-alert
alert('Debug data copied to clipboard');
});
}

public render () {
if (!this.debugger) {
return nothing;
}

return html`<ha-card class="debug-container">
<trash-card-title>
<span slot="title">DEBUG LOG</span>
<ha-icon-button
.label="copy debug log to clipboard"
class="copy-to-clipboard-icon"
slot="title-icon"
@click=${this.copyDebugLogToClipboard.bind(this)}
>
<ha-icon icon="mdi:content-copy"></ha-icon>
</ha-icon-button>
</trash-card-title>

<div class="content">
${this.debugger.getLogs().map(item => html`
<trash-card-logrow
.item=${item}
></trash-card-logrow>
`)}
</div>
</ha-card>`;
}

public static get styles () {
return [
defaultHaCardStyle,
css`
.debug-container {
margin-bottom: 5px;
border: rgb(var(--rgb-pink)) dotted 1px;
opacity: 0.7;
}
`
];
}
}

export {
Debug
};
3 changes: 3 additions & 0 deletions src/cards/trash-card/container/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import './cards';
import './chips';
import './debug';
17 changes: 0 additions & 17 deletions src/cards/trash-card/elements/base.ts

This file was deleted.

Loading