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

[FEATURE] Add option to allow navigation between visible dates. #150

Open
wants to merge 1 commit 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Custom Home Assistant card displaying a responsive overview of multiple days wit
| `hidePastEvents` | boolean | false | `false` \| `true` | Do not show past events | 1.3.0 |
| `hideDaysWithoutEvents` | boolean | false | `false` \| `true` | Do not show days without events, except for today | 1.4.0 |
| `filter` | string | optional | Any regular expression | Remove events that match the regular expression | 1.7.0 |
| `showNavigation` | boolean | false | `false` \| `true` | Show navigational arrows to traverse additional dates on calendar. | 1.7.1 |

### Calendars

Expand Down
79 changes: 76 additions & 3 deletions src/card.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import mixed_rain from 'data-url:./icons/mixed_rain.png';
import sunny from 'data-url:./icons/sunny.png';
import windy from 'data-url:./icons/windy.svg';

const ICONS = {
const ICONS = {
'clear-day': sunny,
'clear-night': clear_night,
cloudy,
Expand Down Expand Up @@ -77,6 +77,8 @@ export class WeekPlannerCard extends LitElement {
_hideDaysWithoutEvents;
_filter;
_showLegend;
_nextOrPrevious;
_isMonthPresentaion;

/**
* Get properties
Expand Down Expand Up @@ -105,6 +107,7 @@ export class WeekPlannerCard extends LitElement {
throw new Error('No calendars are configured');
}

this._isMonthPresentaion = this._getIsMonthPresentaion(config.days ?? 7);
this._title = config.title ?? null;
this._calendars = config.calendars;
this._weather = this._getWeatherConfig(config.weather);
Expand All @@ -126,6 +129,7 @@ export class WeekPlannerCard extends LitElement {
this._hideDaysWithoutEvents = config.hideDaysWithoutEvents ?? false;
this._filter = config.filter ?? false;
this._showLegend = config.showLegend ?? false;
this._showNavigation = config.showNavigation ?? false;
if (config.locale) {
LuxonSettings.defaultLocale = config.locale;
}
Expand All @@ -147,6 +151,12 @@ export class WeekPlannerCard extends LitElement {
},
config.texts ?? {}
);
this._nextOrPrevious = null;

}

_getIsMonthPresentaion(numberOfDays) {
return numberOfDays === 'month';
}

_getWeatherConfig(weatherConfiguration) {
Expand Down Expand Up @@ -205,6 +215,7 @@ export class WeekPlannerCard extends LitElement {
}
<div class="container">
${this._renderLegend()}
${this._renderNavigation()}
${this._renderDays()}
</div>
${this._renderEventDetailsDialog()}
Expand All @@ -217,6 +228,24 @@ export class WeekPlannerCard extends LitElement {
`;
}

_renderNavigation() {
if (!this._showNavigation) {
return html``;
}

return html`
<div class="navigation">
<ul class="monthUl">
<li><span class="navMonth">${this._startDate.toFormat('MMMM')}</span></li>
</ul>
<ul class="navUl">
<li @click="${this._handleLeftArrowClick}"><ha-icon class="arrows" icon="mdi:arrow-left"></ha-icon></li>
<li @click="${this._handleRightArrowClick}"><ha-icon class="arrows" icon="mdi:arrow-right"></ha-icon></li>
</ul>
</div>
`;
}

_renderLegend() {
if (!this._showLegend) {
return html``;
Expand Down Expand Up @@ -708,6 +737,21 @@ export class WeekPlannerCard extends LitElement {
this._currentEventDetails = null;
}

_handleLeftArrowClick(event) {
this._nextOrPrevious = 'previous';
this._requestUpdate();
}

_handleRightArrowClick(event) {
this._nextOrPrevious = 'next';
this._requestUpdate();
}

_requestUpdate() {
this._initialized = false;
this.requestUpdate();
}

_handleWeatherClick(e) {
const event = new Event(
'hass-more-info', {
Expand All @@ -724,15 +768,20 @@ export class WeekPlannerCard extends LitElement {
}

_getNumberOfDays(numberOfDays) {
if (numberOfDays === 'month') {
if (this._isMonthPresentaion) {
numberOfDays = DateTime.now().daysInMonth;
}

return numberOfDays;
}

_getStartDate(alternativeStartingDay) {
let startDate = DateTime.now();
let startDate;
if (this._startDate) {
startDate = this._nextOrPrevious === null ? this._startDate : this._getNextOrPreviousStartDate(this._startDate);
} else {
startDate = DateTime.now();
}

switch (alternativeStartingDay ?? this._startingDay) {
case 'yesterday':
Expand Down Expand Up @@ -778,6 +827,30 @@ export class WeekPlannerCard extends LitElement {
return startDate.startOf('day');
}

_getNextOrPreviousStartDate(startDate) {
if (this._nextOrPrevious === 'next') {
if (this._isMonthPresentaion) {
let nextMonthId = startDate.month + 1;
let nextMonth = startDate.set({month: nextMonthId});
this._numberOfDays = nextMonth.daysInMonth
startDate = nextMonth.startOf('month');
} else {
startDate = startDate.plus({days: this._numberOfDays});
}
} else {
if (this._isMonthPresentaion) {
let prevMonthId = startDate.month - 1;
let prevMonth = startDate.set({month: prevMonthId});
this._numberOfDays = prevMonth.daysInMonth
startDate = prevMonth.startOf('month');
} else {
startDate = startDate.minus({days: this._numberOfDays});
}
}
this._nextOrPrevious = null;
return startDate;
}

_getWeekDayDate(currentDate, weekday) {
const currentWeekDay = currentDate.weekday;
if (currentWeekDay > weekday) {
Expand Down
34 changes: 34 additions & 0 deletions src/card.styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { css } from 'lit';

export default css`
ha-card {
--navigation-spacing: 15px;
--navigation-month-font-size: 2em;
--legend-spacing: 15px;
--legend-dot-size: 10px;
--days-spacing: 15px;
Expand Down Expand Up @@ -49,6 +51,38 @@ export default css`
gap: var(--days-spacing);
}

.container .navigation {
width: 100%;
display: flex;
}

.container .navigation ul {
display: flex;
flex-wrap: wrap;
gap: var(--navigation-spacing);
margin: 0;
padding: 0;
list-style: none;
width: 50%;
}

.monthUl {
justify-content: flex-start;
}

.navMonth {
font-size: var(--navigation-month-font-size);
color: var(--primary-text-color);
}

.navUl {
justify-content: flex-end;
}

.container .navigation ul li {
display: block;
}

.container .legend {
width: 100%;
}
Expand Down