-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-TimeTreeEvent.js
189 lines (168 loc) · 4.78 KB
/
MMM-TimeTreeEvent.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* Magic Mirror
* Module: MMM-TimeTreeEvent
*
* By sabo10o29 https://github.com/sabo10o29
* MIT Licensed.
*/
Module.register("MMM-TimeTreeEvent",{
// Module config defaults.
defaults: {
apiBase: "https://timetreeapis.com/",
calendars: "calendars",
endpoint: "upcoming_events",
timezone: "Asia/Tokyo",
include: "creator,label,attendees",
animationSpeed: 1000,
upadteInterval: 3 * 60 * 60 *1000, //msec
timeFormat: "HH:mm",
dateFormat: "MM/DD",
showDate: false,
eventWordCount: 10,
days: 1,
title: "Today's event ",
},
todayEvents: [],
// Define required scripts.
getScripts: function() {
return ["moment.js", "eventobject.js"];
},
// Define styles.
getStyles: function() {
return ["timetree_styles.css"];
},
// Define start sequence.
start: function() {
var self = this;
Log.info("Starting module: " + self.name);
setInterval(function() {
self.sendSocketNotification("GET-TIMETREE-EVENT", self.getOptions());
}, self.config.upadteInterval);
this.sendSocketNotification("GET-TIMETREE-EVENT", this.getOptions());
},
getDom: function() {
var wrapper = document.createElement("div");
if(this.todayEvents.length == 0){
Log.info("There are no delay line to your target.");
return wrapper;
}
var title = document.createElement("text");
title.innerHTML = this.config.title;
var table = document.createElement("table");
table.className = "timetree";
if(this.data.position == "top_right" || this.data.position == "bottom_right" ){
table.style.marginLeft = "auto";
}
for(var i = 0; i < this.todayEvents.length; i++){
var event = this.todayEvents[i];
var eventItem = document.createElement("tr");
eventItem.className = "bright";
//Add date
if(this.config.showDate){
var date = document.createElement("td");
date.className = "date";
date.innerHTML = moment(event.start_at).format(this.config.dateFormat);
eventItem.appendChild(date);
}
// Add time
var time = document.createElement("td");
time.className = "time";
if(event.all_day){
var timeList = document.createElement("ul");
timeList.className = "time";
var st = document.createElement("li");
var to = document.createElement("li");
to.innerHTML = "All-day";
var end = document.createElement("li");
timeList.appendChild(st);
timeList.appendChild(to);
timeList.appendChild(end);
time.appendChild(timeList);
}else{
var timeList = document.createElement("ul");
timeList.className = "time";
var st = document.createElement("li");
st.innerHTML = moment(event.start_at).format(this.config.timeFormat);
var to = document.createElement("li");
to.innerHTML = "-";
var end = document.createElement("li");
end.innerHTML = moment(event.end_at).format(this.config.timeFormat);
timeList.appendChild(st);
timeList.appendChild(to);
timeList.appendChild(end);
time.appendChild(timeList);
}
eventItem.appendChild(time);
//Add event content
var content = document.createElement("td");
content.className = "content";
content.innerHTML = this.getContentText(event);
eventItem.appendChild(content);
table.appendChild(eventItem);
}
wrapper.appendChild(title);
wrapper.appendChild(table);
return wrapper;
},
getContentText: function(event) {
var content = this.multByteStringSlice(event.title, this.config.eventWordCount);
return content;
},
strLength: function( strSrc ){
len = 0;
strSrc = escape(strSrc);
for(i = 0; i < strSrc.length; i++, len++){
if(strSrc.charAt(i) == "%"){
if(strSrc.charAt(++i) == "u"){
i += 3;
len++;
}
i++;
}
}
return len;
},
multByteStringSlice: function(str , strLimit ){
var isSlice = false;
while( this.strLength(str) > strLimit ){
str = str.slice(0, str.length-1);
isSlice = true;
}
if( isSlice ){
str += "...";
}
return str;
},
getUrl() {
return this.config.apiBase + this.config.calendars + "/" +this.config.calenderid + "/" +this.config.endpoint + this.getParams();
},
getParams() {
let params = "?";
params += "timezone=" + this.config.timezone;
params += "&days=" + this.config.days;
params += "&include=" + this.config.include;
return params;
},
getHeaders() {
return headers = {
"Authorization": "Bearer " + this.config.appid
};
},
getOptions() {
return options = {
url: this.getUrl(),
method: "GET",
headers: this.getHeaders()
};
},
socketNotificationReceived: function(notification, payload) {
if (notification === "GOT-TIMETREE-EVENT" && payload != null) {
Log.info("Success to get timetree event!!");
this.todayEvents = [];
for (var i = 0; i < payload.result.data.length; i ++) {
const o = new EventObject(payload.result.data[i]);
this.todayEvents.push(o);
}
this.updateDom(this.config.animationSpeed);
}
}
});