mirror of
https://github.com/DigiLive/mushroom-strategy.git
synced 2025-08-01 02:44:27 +02:00
Refactor stackHorizontal method into a new module
- Cut the stackHorizontal method from the Registry class and added it to a new module for better organization and reusability.
This commit is contained in:
@@ -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.
|
||||
*
|
||||
|
@@ -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.<br>
|
||||
@@ -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;
|
||||
|
@@ -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 }),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user