-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-Hevy.js
150 lines (117 loc) · 3.71 KB
/
MMM-Hevy.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
Module.register("MMM-Hevy", {
// Default module config.
defaults: {
updateInterval: 15 * 60 * 1000, // every 15 minutes
height: "200px",
heavyApiKey: "",
primaryColor: "rgba(194, 95, 96, 1)",
secondaryColor: "rgba(194, 95, 96, 0.5)"
},
start: function () {
this.workoutData = [];
this.getData();
this.scheduleUpdate();
this.updateDom();
},
// Override dom generator.
getDom: function () {
const wrapper = document.createElement("div");
const bodySvgWrapper = document.createElement("div");
// Make the wrapper flex,
bodySvgWrapper.style.display = "flex";
bodySvgWrapper.appendChild(this.getFrontSvgElement());
bodySvgWrapper.appendChild(this.getBottomSvgElement());
wrapper.appendChild(bodySvgWrapper);
wrapper.appendChild(this.getWeeklyWorkoutListElement());
return wrapper;
},
getStyles: function () {
return ["MMM-Hevy.css"];
},
getHeader: function () {
return this.data.header || 'Hevy';
},
getData: function () {
this.sendSocketNotification('GET_HEAVY_DATA', {heavyApiKey: this.config.heavyApiKey});
},
socketNotificationReceived: function (notification, payload) {
const self = this;
if (notification === "HEAVY_DATA_RES") {
console.log("MMM-Hevy received data from node_helper");
self.workoutData = payload;
self.updateDom(500);
}
},
scheduleUpdate: function () {
console.log("MMM-Hevy scheduling update with interval: ", this.config.updateInterval);
const nextLoad = this.config.updateInterval;
const self = this;
setInterval(function () {
self.getData();
}, nextLoad);
},
getFrontSvgElement: function () {
const svg = document.createElement("object");
svg.id = "body-front";
svg.type = "image/svg+xml";
svg.data = "/MMM-Hevy/body-front.svg";
svg.style.width = "100%";
svg.style.height = this.config.height;
const primary = this.workoutData.primary || [];
const secondary = this.workoutData.secondary || [];
const self = this;
svg.onload = function () {
const svgDoc = svg.contentDocument;
self.colorMuscles(svgDoc, secondary, self.config.secondaryColor);
self.colorMuscles(svgDoc, primary, self.config.primaryColor);
};
return svg;
},
colorMuscles: function (svgDoc, muscles, color) {
for (let muscle of muscles) {
const musclePaths = svgDoc.getElementsByClassName(muscle);
for (let i = 0; i < musclePaths.length; i++) {
musclePaths[i].style.fill = color;
}
}
},
getBottomSvgElement: function () {
const svg2 = document.createElement("object");
svg2.type = "image/svg+xml";
svg2.data = "/MMM-Hevy/body-back.svg";
svg2.style.width = "100%";
svg2.style.height = this.config.height;
const primary = this.workoutData.primary || [];
const secondary = this.workoutData.secondary || [];
const self = this;
svg2.onload = function () {
const svgDoc = svg2.contentDocument;
self.colorMuscles(svgDoc, secondary, self.config.secondaryColor);
self.colorMuscles(svgDoc, primary, self.config.primaryColor);
};
return svg2;
},
getWeeklyWorkoutListElement: function () {
const wrapper = document.createElement("div");
wrapper.className = "workout-list";
wrapper.style.display = "flex";
const workoutDays = this.workoutData.workoutDays || [];
// 7 days as M T W T F S S
const days = ["M", "T", "W", "T", "F", "S", "S"];
for (let i = 0; i < days.length; i++) {
const day = document.createElement("div");
// set class for styling
day.className = "day";
// check if the day is a workout day
if (workoutDays.includes(i + 1)) {
day.className = "day beasted";
}
day.innerHTML = days[i];
// day.style.flex = "1";
// day.style.textAlign = "center";
wrapper.appendChild(day);
}
return wrapper;
}
});
// https://www.figma.com/community/file/1320468164820924031