-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-GoogleDocs-Notes.js
112 lines (92 loc) · 3.05 KB
/
MMM-GoogleDocs-Notes.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
/* global Module */
/* Magic Mirror
* Module: MMM-GoogleDocs-Notes
*
* By No3x
* MIT Licensed.
*/
Module.register('MMM-GoogleDocs-Notes', {
defaults: {
maxNotes: 2,
pollFrequency: 5 * 60 * 1000, //5 minutes;
showDatePosted: true,
dateFormatExact: 'YYYY-MM-DD HH:mm',
dateFormatShort: 'HH:mm',
notesPrefix: 'MMM'
},
start: function() {
this.loaded = false;
this.notesData = [];
this.sendSocketNotification("MMM-GOOGLEDOCS-NOTES-GET", this.config);
const self = this;
setInterval(function() {
self.sendSocketNotification("MMM-GOOGLEDOCS-NOTES-GET", self.config);
}, this.config.pollFrequency);
},
getTranslations: function() {
return {
en: "translations/en.json",
de: "translations/de.json"
}
},
getStyles: function () {
return ["MMM-GoogleDocs-Notes.css"];
},
getScripts: function() {
return ["moment.js"];
},
socketNotificationReceived: function(notification, payload) {
// only update if a data set is returned. Otherwise leave stale data on the screen.
if ( notification === 'MMM-GOOGLEDOCS-NOTES-RESPONSE') {
this.notesData = payload.data;
if (this.notesData.length === 0) {
this.loaded = true;
this.hide(1000, {lockString: this.identifier});
} else if (this.loaded) {
this.updateDom();
this.show(1000, {lockString: this.identifier});
} else {
this.loaded = true;
this.updateDom(2000);
}
}
},
formatDate: function(dateString) {
const self = this;
const d = moment(dateString);
const today = moment();
if (d.isSame(today, 'day')) {
return `${self.translate("TODAY").toLowerCase()} ${self.translate("AT").toLowerCase()} ${d.format(this.config.dateFormatShort)}`;
} else if (d.isSame(moment(today).subtract(1, 'days'), 'day')) {
return `${self.translate("YESTERDAY").toLowerCase()} ${self.translate("AT").toLowerCase()} ${d.format(this.config.dateFormatShort)}`;
} else {
return d.format(this.config.dateFormatExact);
}
},
getDom: function() {
const self = this;
var wrapper = document.createElement("div");
wrapper.classList.add("wrapper");
if (!this.loaded) {
wrapper.innerHTML = this.translate('LOADING');
wrapper.className = "dimmed light small";
return wrapper;
}
this.notesData.forEach(function(noteObj) {
var noteContainer = document.createElement("div");
noteContainer.classList.add("note-container");
var noteBody = document.createElement("span");
noteBody.classList.add("note-body", "bright");
noteBody.innerHTML = noteObj.noteText
noteContainer.appendChild(noteBody);
if (self.config.showDatePosted) {
var noteDateStamp = document.createElement("span");
noteDateStamp.classList.add("note-date-stamp");
noteDateStamp.innerHTML = `${self.translate("POSTED")} ${self.formatDate(noteObj.dateStamp)}`;
noteContainer.appendChild(noteDateStamp);
}
wrapper.appendChild(noteContainer);
});
return wrapper;
}
});