-
Notifications
You must be signed in to change notification settings - Fork 1
/
MMM-Notion.js
executable file
·136 lines (122 loc) · 3.33 KB
/
MMM-Notion.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
/* global Module */
/* Magic Mirror
* Module: MMM-Notion
*
* By Cedrik Hoffmann
* MIT Licensed.
*/
Module.register("MMM-Notion", {
defaults: {
secret: "",
databases: [
{
showTitle: true,
title: "",
id: "",
layout: {
type: "listview",
showPersonWithNames: false,
displayElementTitle: true,
dateFormat: "full_date", // full_date, month_day_year, day_month_year, year_month_day, relative
properties: []
},
filter: {},
sorts: []
}
],
updateInterval: 60000
},
requiresVersion: "2.1.0", // Required version of MagicMirror
start: function () {
const self = this;
this.databases = [];
this.status = "loading"
this.error = ""
this.id = Date.now() + Math.floor(Math.random() * 100)
this.config = {...this.config, callId: this.id}
this.sendSocketNotification("MMM-NOTION-HERE_IS_YOUR_CONFIG", this.config);
self.sendSocketNotification("MMM-NOTION-UPDATE_PLEASE", self.id);
setInterval(function () {
self.sendSocketNotification("MMM-NOTION-UPDATE_PLEASE", self.id);
}, this.config.updateInterval);
},
getDom: function () {
// create element wrapper for show into the module
const wrapper = document.createElement("div");
wrapper.id = "mmm-notion";
switch (this.status) {
case "loading":
wrapper.innerText = "Loading..."
break;
case "success":
try {
this.databases.forEach(database => {
this.createDatabaseView(wrapper, database)
})
} catch (e) {
this.handleError(e)
}
break;
case "error":
wrapper.innerText = this.error
break
}
return wrapper;
},
handleError: function (error) {
this.status = "error"
console.error(error)
this.error = error
this.updateDom();
},
createDatabaseView: function (wrapper, properties) {
const container = document.createElement("div")
const databaseTitle = document.createElement("div")
container.id = "mmm-notion-database"
databaseTitle.id = "mmm-notion-database-title"
databaseTitle.innerText = `${properties.title}:`
if (properties.title !== undefined) {
if (properties.showTitle === undefined || properties.showTitle)
container.appendChild(databaseTitle)
}
this.createListView(container, properties)
wrapper.appendChild(container)
},
createListView: function (wrapper, properties) {
const listView = new ListView(properties)
wrapper.appendChild(listView.wrapper)
},
getScripts: function () {
return [
this.file('scripts/DateFormat.js'),
this.file('scripts/layout/listview/ListView.js'),
this.file('scripts/layout/listview/ListViewElement.js'),
this.file('scripts/layout/PropertiesView.js')
];
},
getStyles: function () {
return [
this.file("styles/MMM-Notion.css"),
this.file("styles/MMM-Notion-ListView.css"),
];
},
// Load translations files
getTranslations: function () {
//FIXME: This can be load a one file javascript definition
return {
en: "translations/en.json",
es: "translations/es.json"
};
},
// socketNotificationReceived from helper
socketNotificationReceived: function (notification, payload) {
if (notification === `MMM-Notion-DATABASE-DATA-${this.id}`) {
this.databases = payload;
console.log(payload)
this.status = "success"
this.updateDom();
} else if (notification === `MMM-Notion-DATABASE-ERROR-${this.id}`) {
this.handleError(JSON.parse(payload.body).message)
}
},
});