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

Make it possible to configure items per row for area cards in home view #84

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ class Helper {
*/
static #strategyOptions: generic.StrategyConfig;

/**
* Indicates if the display is narrow (i.e. mobile)
* @type {boolean}
*/
static #narrow: boolean;

/**
* Set to true for more verbose information in the console.
*
Expand Down Expand Up @@ -131,6 +137,14 @@ class Helper {
return this.#debug;
}

/**
* @returns {boolean}
* @static
*/
static get narrow(): boolean {
return this.#narrow;
}

/**
* Initialize this module.
*
Expand All @@ -141,6 +155,7 @@ class Helper {
static async initialize(info: generic.DashBoardInfo): Promise<void> {
// Initialize properties.
this.#hassStates = info.hass.states;
this.#narrow = window.matchMedia("(max-width: 870px)").matches;
this.#strategyOptions = deepmerge(configurationDefaults, info.config?.strategy?.options ?? {});
this.#debug = this.#strategyOptions.debug;

Expand Down
8 changes: 7 additions & 1 deletion src/types/strategy/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ export namespace generic {
extra_cards?: LovelaceCardConfig[];
extra_views?: ViewConfig[];
home_view: {
hidden: HiddenSectionType[]
hidden: HiddenSectionType[];
areas_per_row?: number | AreasPerRow;
}
quick_access_cards?: LovelaceCardConfig[];
views: { [k: string]: ViewConfig };
Expand All @@ -116,6 +117,11 @@ export namespace generic {
const hiddenSectionList = ["chips", "persons", "greeting", "areas", "areasTitle"] as const;
export type HiddenSectionType = typeof hiddenSectionList[number];

export interface AreasPerRow {
narrow?: number;
wide?: number;
}

/**
* Represents the default configuration for a strategy.
*/
Expand Down
11 changes: 9 additions & 2 deletions src/views/HomeView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {ActionConfig} from "../types/homeassistant/data/lovelace";
import {TitleCardConfig} from "../types/lovelace-mushroom/cards/title-card-config";
import {PersonCardConfig} from "../types/lovelace-mushroom/cards/person-card-config";

const DEFAULT_AREAS_PER_ROW = 2;

// noinspection JSUnusedGlobalSymbols Class is dynamically imported.
/**
Expand Down Expand Up @@ -263,10 +264,16 @@ class HomeView extends AbstractView {

// Horizontally group every two area cards if all cards are created.
if (i === Helper.areas.length - 1) {
for (let i = 0; i < areaCards.length; i += 2) {
let cardsPerRow = Helper.strategyOptions.home_view?.areas_per_row ?? DEFAULT_AREAS_PER_ROW;

if (typeof cardsPerRow === 'object') {
cardsPerRow = (Helper.narrow ? cardsPerRow.narrow : cardsPerRow.wide) ?? DEFAULT_AREAS_PER_ROW;
}

for (let i = 0; i < areaCards.length; i += cardsPerRow) {
groupedCards.push({
type: "horizontal-stack",
cards: areaCards.slice(i, i + 2),
cards: areaCards.slice(i, i + cardsPerRow),
} as StackCardConfig);
}
}
Expand Down