Skip to content

Commit

Permalink
More all day event fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gadgetchnnel committed Mar 23, 2020
1 parent 0e5db6d commit 594995b
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 22 deletions.
2 changes: 1 addition & 1 deletion custom_card.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"lovelace-home-feed-card": {
"version": "0.4.1b3",
"version": "0.4.1b4",
"remote_location": "https://raw.githubusercontent.com/gadgetchnnel/lovelace-home-feed-card/master/lovelace-home-feed-card.js",
"visit_repo": "https://github.com/gadgetchnnel/lovelace-home-feed-card",
"changelog": "https://github.com/gadgetchnnel/lovelace-home-feed-card"
Expand Down
14 changes: 7 additions & 7 deletions lovelace-home-feed-card.js

Large diffs are not rendered by default.

49 changes: 49 additions & 0 deletions src/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,52 @@ require('moment/locale/vi');
require('moment/locale/zh-cn');
require('moment/locale/zh-hk');
require('moment/locale/zh-tw');

export const getCalendarString = function(date){
var timeString = date.calendar();
switch(date.locale())
{
case "ca":
case "de":
case "de-ch":
case "es":
timeString = timeString.replace(` ${date.format("LT")}`, "");
timeString = timeString.substring(0, timeString.lastIndexOf(" ", timeString.lastIndexOf(" ")-1));
break;
case "et":
case "hi":
case "ta":
case "te":
timeString = timeString.replace(` ${date.format("LT")}`, "");
timeString = timeString.replace(",","");
break;
case "eu":
case "hu":
case "zh-cn":
case "zh-hk":
timeString = timeString.replace(`${date.format("LT")}`, "");
if(timeString.includes(" ")){
timeString = timeString.substring(0, timeString.lastIndexOf(" "));
}
break;
case "hy-am":
case "ar":
timeString = timeString.replace(` ${date.format("LT")}`, "");
if(timeString.includes(" ")){
timeString = timeString.substring(0, timeString.lastIndexOf(" ", timeString.lastIndexOf(" ")-1));
}
break;
case "ja":
case "ko":
case "lt":
case "sv":
timeString = timeString.replace(` ${date.format("LT")}`, "");
break;
default:
timeString = timeString.replace(` ${date.format("LT")}`, "");
if(timeString.includes(" ")){
timeString = timeString.substring(0, timeString.lastIndexOf(" "));
}
}
return timeString;
}
56 changes: 42 additions & 14 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { HomeFeedNotificationPopup } from "./popup.js";
import { computeStateDisplay as computeStateDisplayHelper, handleClick } from "custom-card-helpers";
import { createCard } from "card-tools/src/lovelace-element";

require('./locale.js');
import { getCalendarString } from "./locale.js";

class HomeFeedCard extends LitElement {
constructor() {
Expand All @@ -15,8 +15,9 @@ class HomeFeedCard extends LitElement {
this.refreshingNotifications = false;
this.feedContent = null;
this.moment = require('moment');

this.moment.locale(window.navigator.userLanguage || window.navigator.language);
let lang = window.navigator.userLanguage || window.navigator.language;
if(lang == "hy") lang = "hy-am"; // "hy" (Armenian) wrongly maps to zh-tw (Taiwan Chinese)
this.moment.locale(lang);
}

static get properties() { return {
Expand Down Expand Up @@ -897,22 +898,49 @@ class HomeFeedCard extends LitElement {
var allDay = (n.item_type == "calendar_event" && n.all_day);

var timeItem;

if(allDay){
let date = n.start.date ? this.moment(n.start.date) : this.moment(n.start).startOf('day');
let date = n.start.date ? this.moment(n.start.date).startOf('day') : this.moment(n.start).startOf('day');
let endDate = (n.end.date ? this.moment(n.end.date).startOf('day') : this.moment(n.end).startOf('day'));
// Fix end date for all day events
endDate = endDate.add(-1, 'hours').startOf('day');

if(date > this.moment().startOf('day')){
if(this.moment(n.start.date) > this.moment().startOf('day').add(1, 'days')){
var timeString = date.format("dddd");
}
else{
var timeString = "Tomorrow";
}
let days = Math.abs(date.diff(this.moment().startOf('day'),'days'));

if(days <= 7){
var timeString = getCalendarString(date);

if(endDate > date) {
let endTimeString = getCalendarString(endDate);
if(endTimeString == endDate.format("L")){
endTimeString = endDate.toDate().toLocaleDateString(this._hass.language, {
year: "numeric",
month: "long",
day: "numeric",
});
}
timeString = timeString + " - " + endTimeString;
}

timeItem = html`<div style="display:block; ${compact_mode ? "float:right" : "clear:both;"}" title="${date.toDate()} - ${endDate.toDate()}">${timeString}</div>`;
}
else{
var timeString = "Today";
var timeString = date.toDate().toLocaleDateString(this._hass.language, {
year: "numeric",
month: "long",
day: "numeric",
});

if(endDate > date) {
timeString = timeString + " - " + endDate.toDate().toLocaleDateString(this._hass.language, {
year: "numeric",
month: "long",
day: "numeric",
});
}

timeItem = html`<div style="display:block; ${compact_mode ? "float:right" : "clear:both;"}" title="${date.toDate()} - ${endDate().toDate()}">${timeString}</div>`;
}
timeItem = html`<div style="display:block; ${compact_mode ? "float:right" : "clear:both;"}">${timeString}</div>`;
}
else
{
Expand Down

0 comments on commit 594995b

Please sign in to comment.