Skip to content

Commit

Permalink
feature: numberOfFavoritesToShow
Browse files Browse the repository at this point in the history
  • Loading branch information
punxaphil committed Sep 30, 2023
1 parent 93ddc1d commit d507432
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
5 changes: 5 additions & 0 deletions src/editor/advanced-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export const ADVANCED_SCHEMA = [
type: 'string',
name: 'topFavorites',
},
{
type: 'integer',
name: 'numberOfFavoritesToShow',
valueMin: 1,
},
];

class AdvancedEditor extends BaseEditor {
Expand Down
36 changes: 21 additions & 15 deletions src/sections/media-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,25 +140,31 @@ export class MediaBrowser extends LitElement {
this.mediaPlayers,
this.config.mediaBrowserTitlesToIgnore,
);
allFavorites = allFavorites.sort((a, b) => {
const topFavorites = this.config.topFavorites ?? [];
const aIndex = indexOfWithoutSpecialChars(topFavorites, a.title);
const bIndex = indexOfWithoutSpecialChars(topFavorites, b.title);
if (aIndex > -1 && bIndex > -1) {
return aIndex - bIndex;
} else {
let result = bIndex - aIndex;
if (result === 0) {
result = a.title.localeCompare(b.title, 'en', { sensitivity: 'base' });
}
return result;
}
});
return [
allFavorites.sort((a, b) => this.sortOnTopFavoritesThenAlphabetically(a.title, b.title));
allFavorites = [
...(this.config.customSources?.[this.activePlayer.id]?.map(MediaBrowser.createSource) || []),
...(this.config.customSources?.all?.map(MediaBrowser.createSource) || []),
...allFavorites,
];
allFavorites = this.config.numberOfFavoritesToShow
? allFavorites.slice(0, this.config.numberOfFavoritesToShow)
: allFavorites;
return allFavorites;
}

private sortOnTopFavoritesThenAlphabetically(a: string, b: string) {
const topFavorites = this.config.topFavorites ?? [];
const aIndex = indexOfWithoutSpecialChars(topFavorites, a);
const bIndex = indexOfWithoutSpecialChars(topFavorites, b);
if (aIndex > -1 && bIndex > -1) {
return aIndex - bIndex;
} else {
let result = bIndex - aIndex;
if (result === 0) {
result = a.localeCompare(b, 'en', { sensitivity: 'base' });
}
return result;
}
}

private static createSource(source: MediaPlayerItem) {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export interface CardConfig extends LovelaceCardConfig {
mediaBrowserItemsPerRow: number;
mediaBrowserShowTitleForThumbnailIcons?: boolean;
topFavorites?: string[];
numberOfFavoritesToShow?: number;
}

export interface MediaArtworkOverride {
Expand Down

0 comments on commit d507432

Please sign in to comment.