-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMMM-CoupleDays.js
128 lines (113 loc) · 3.98 KB
/
MMM-CoupleDays.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
Module.register("MMM-CoupleDays", {
defaults: {
name1: "Partner 1",
name2: "Partner 2",
date: "2023-01-01",
transitionDuration: 7500,
language: "en",
textColor: "white"
},
start () {
this.currentIndex = 0;
this.views = [
{key: "days", label: this.translate("days")},
{key: "weeks", label: this.translate("weeks")},
{key: "months", label: this.translate("months")},
{key: "years", label: this.translate("years")},
{key: "total", label: this.translate("total")}
];
this.currentView = this.views[this.currentIndex];
this.startDate = moment(this.config.date);
this.endDate = moment();
this.headerWrapper = document.createElement("div");
this.headerWrapper.className = "couple-days-header";
this.wrapper = document.createElement("div");
this.wrapper.className = "couple-days-wrapper";
this.wrapper.style.color = this.config.textColor;
this.wrapper.appendChild(this.headerWrapper);
this.contentWrapper = document.createElement("div");
this.contentWrapper.className = "couple-days-content";
this.wrapper.appendChild(this.contentWrapper);
this.updateContent();
this.updateDom();
setInterval(() => {
this.currentIndex = (this.currentIndex + 1) % this.views.length;
this.currentView = this.views[this.currentIndex];
this.updateContent();
}, this.config.transitionDuration);
},
getStyles () {
return ["MMM-CoupleDays.css"];
},
getScripts () {
return ["moment.js"];
},
getTranslations () {
return {
[this.config.language]: `translations/${this.config.language}.json`
};
},
getHeader () {
if (this.config.name2 === "") {
return this.config.name1;
}
return `${this.config.name1} ${this.translate("and")} ${this.config.name2}`;
},
getDuration (unit) {
return this.endDate.diff(this.startDate, unit);
},
formatDuration (duration, unit) {
const translationKey = duration === 1
? unit.slice(0, -1)
: unit; // Entfernt das letzte Zeichen für Singularformen
return `${duration} ${this.translate(translationKey)}`;
},
updateContent () {
this.contentWrapper.innerHTML = this.getFormattedDuration();
},
getFormattedDuration () {
switch (this.currentView.key) {
case "days":
return this.formatDuration(this.getDuration("days"), "days");
case "weeks":
return this.formatDuration(this.getDuration("weeks"), "weeks");
case "months":
return this.formatDuration(this.getDuration("months"), "months");
case "years":
return this.formatYears();
case "total":
return this.formatTotal();
}
},
formatYears () {
const years = Math.floor(this.getDuration("years"));
const months = Math.floor(this.getDuration("months") % 12);
const days = Math.floor(this.getDuration("days") % 30);
if (years === 0) {
return `${this.formatDuration(months, this.translate("months"))} ${this.formatDuration(days, this.translate("days"))}`;
}
let formattedYears = this.formatDuration(years, this.currentView.label);
if (years === 1) {
formattedYears = `${years} ${this.translate("years")}`;
} else {
formattedYears = `${years} ${this.translate("years_plural")}`;
}
return formattedYears;
},
formatTotal () {
const years = Math.floor(this.getDuration("years"));
const months = Math.floor(this.getDuration("months") % 12);
const days = Math.floor(this.getDuration("days") % 30);
const formattedYears = this.formatDuration(years, this.translate("years"));
const formattedMonths = this.formatDuration(months, this.translate("months"));
const formattedDays = this.formatDuration(days, this.translate("days"));
const andTranslation = this.translate("and");
if (years === 0) {
return `${formattedMonths} ${andTranslation} ${formattedDays}`;
}
return `${formattedYears} ${formattedMonths} ${andTranslation} ${formattedDays}`;
},
getDom () {
return this.wrapper;
}
});