diff --git a/src/Registry.ts b/src/Registry.ts index 02e7c91..1c145d0 100644 --- a/src/Registry.ts +++ b/src/Registry.ts @@ -259,69 +259,6 @@ class Registry { }}`; } - /** - * Stacks an array of Lovelace card configurations into horizontal stacks based on their type. - * - * This method processes sequences of cards with the same type and applies a specified column count - * for each type of card. - * It returns a new array of stacked card configurations, preserving the original order of the cards. - * - * @param cardConfigurations - An array of Lovelace card configurations to be stacked. - * @param [columnCounts] - An object mapping card types to their respective column counts. - * If a type is not found in the mapping, it defaults to 2. - * @returns An array of stacked card configurations, where each configuration is a horizontal stack - * containing a specified number of cards. - * - * @example - * ```typescript - * stackedCards = stackHorizontal(card, {area: 1, "custom:card": 2}); - * ``` - */ - static stackHorizontal( - cardConfigurations: LovelaceCardConfig[], - columnCounts?: { - [key: string]: number; - }, - ): LovelaceCardConfig[] { - // Function to process a sequence of cards - const doStack = (cards: LovelaceCardConfig[], columnCount: number) => { - const stackedCardConfigurations: StackCardConfig[] = []; - - for (let i = 0; i < cards.length; i += columnCount) { - stackedCardConfigurations.push({ - type: 'horizontal-stack', - cards: cards.slice(i, i + columnCount), - } as StackCardConfig); - } - - return stackedCardConfigurations; - }; - - // Array to hold the processed cards - const processedConfigurations: LovelaceCardConfig[] = []; - - for (let i = 0; i < cardConfigurations.length; ) { - const currentCard = cardConfigurations[i]; - const currentType = currentCard.type; // Assuming each card has a 'type' property - - // Start a new sequence - const sequence: LovelaceCardConfig[] = []; - - // Collect all cards of the same type into the sequence - while (i < cardConfigurations.length && cardConfigurations[i].type === currentType) { - sequence.push(cardConfigurations[i]); - i++; // Move to the next card - } - - const columnCount = Math.max(columnCounts?.[currentType] || 2, 1); - - // Process the sequence and add the result to the processedConfigurations array - processedConfigurations.push(...doStack(sequence, columnCount)); - } - - return processedConfigurations; - } - /** * Get the names of the specified type which aren't set to hidden in the strategy options. * diff --git a/src/mushroom-strategy.ts b/src/mushroom-strategy.ts index 50573e9..d743a56 100644 --- a/src/mushroom-strategy.ts +++ b/src/mushroom-strategy.ts @@ -15,6 +15,7 @@ import { import { sanitizeClassName } from './utilities/auxiliaries'; import { logMessage, lvlError } from './utilities/debug'; import RegistryFilter from './utilities/RegistryFilter'; +import { stackHorizontal } from './utilities/cardStacking'; /** * Mushroom Dashboard Strategy.
@@ -152,7 +153,7 @@ class MushroomStrategy extends HTMLTemplateElement { }); if (domain === 'binary_sensor') { - domainCards = Registry.stackHorizontal(domainCards); + domainCards = stackHorizontal(domainCards); } return domainCards.length ? { type: 'vertical-stack', cards: [titleCard, ...domainCards] } : null; diff --git a/src/views/HomeView.ts b/src/views/HomeView.ts index ab23ee8..ce39b76 100644 --- a/src/views/HomeView.ts +++ b/src/views/HomeView.ts @@ -15,6 +15,7 @@ import { logMessage, lvlError, lvlInfo } from '../utilities/debug'; import { localize } from '../utilities/localize'; import AbstractView from './AbstractView'; import registryFilter from '../utilities/RegistryFilter'; +import { stackHorizontal } from '../utilities/cardStacking'; /** * Home View Class. @@ -209,7 +210,7 @@ class HomeView extends AbstractView { return { type: 'vertical-stack', - cards: Registry.stackHorizontal(cardConfigurations), + cards: stackHorizontal(cardConfigurations), }; } @@ -258,7 +259,7 @@ class HomeView extends AbstractView { title: (Registry.strategyOptions.home_view.hidden as HomeViewSections[]).includes('areasTitle') ? undefined : localize('generic.areas'), - cards: Registry.stackHorizontal(cardConfigurations, { area: 1, 'custom:mushroom-template-card': 2 }), + cards: stackHorizontal(cardConfigurations, { area: 1, 'custom:mushroom-template-card': 2 }), }; } }