-
Notifications
You must be signed in to change notification settings - Fork 1
/
MMM-Airtable.js
128 lines (111 loc) · 3.16 KB
/
MMM-Airtable.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
/**
* Magic Mirror
* Module: MMM-Airtable
*
* By https://github.com/yashatgit/MMM-Airtable
* MIT Licensed.
*/
//Module documentation: https://github.com/MichMich/MagicMirror/tree/master/modules
//Modules list: https://github.com/MichMich/MagicMirror/wiki/3rd-party-modules
const buildTableDOM = (tableData, tableConfig) => {
const renderColumns = columns =>
columns
.map(
columnName =>
`<th class="bright ${
tableConfig.rowBorder ? 'show-border' : ''
}">${columnName}</th>`,
)
.join('');
const renderRow = row =>
row
.map(
cell =>
`<td class="${
tableConfig.rowBorder ? 'show-border' : ''
}">${cell}</td>`,
)
.join('');
return `
<div class="mmt-atable-table-container">
<header>${tableConfig.tableTitle || tableConfig.workspaceName}</header>
<table class="mmt-atable-table">
<tr>${renderColumns(tableData.columns)}</tr>${tableData.rows
.map(row => `<tr>${renderRow(row)}</tr>`)
.join('')}
</table>
</div>`;
};
const buildTableConfig = moduleConfig => {
return moduleConfig.tables.reduce((acc, tableConfig, index) => {
const tableId = index + 1;
acc[tableId] = {
updateInterval: moduleConfig.updateInterval,
airtableAPIKey: moduleConfig.airtableAPIKey,
airtableBaseId: moduleConfig.airtableBaseId,
...tableDefaults,
...tableConfig,
tableId,
};
return acc;
}, {});
};
const tableDefaults = {
maxRows: 20,
rowBorder: false,
};
Module.register('MMM-Airtable', {
defaults: {
updateInterval: 1000 * 60,
animationSpeed: 1.5 * 1000,
},
getScripts: function() {
return ['lodash.core.min.js'];
},
getStyles: function() {
return ['MMM-airtable.css'];
},
start: function() {
const self = this;
Log.info('Now Starting module: ' + self.name);
const tableConfigs = buildTableConfig(self.config);
console.log({ tableConfigs });
self.tableConfigs = tableConfigs;
self.tableData = {};
_.forEach(tableConfigs, tableConfig => {
self.fetchTableData(tableConfig);
setInterval(() => {
self.fetchTableData(tableConfig);
}, tableConfig.updateInterval);
});
},
getDom: function() {
const self = this;
Log.info('Airtable getDom');
const wrapper = document.createElement('div');
const tableData = this.tableData;
if (_.isEmpty(tableData)) {
wrapper.className = 'small dimmed';
wrapper.innerHTML = `Loading...`; //TODO
} else {
wrapper.setAttribute('id', 'MMM-airtable');
wrapper.innerHTML = _.map(tableData, (data, tableId) =>
buildTableDOM(data, self.tableConfigs[tableId]),
).join('');
}
return wrapper;
},
fetchTableData: function(tableConfig) {
this.sendSocketNotification('MMM_AIRTABLE_DATA_FETCH', {
tableConfig,
});
},
socketNotificationReceived: function(notification, payload) {
var self = this;
if (notification === 'MMM_AIRTABLE_DATA_RECEIVED') {
const { tableConfig, tableData } = payload;
self.tableData[tableConfig.tableId] = tableData;
self.updateDom(self.config.animationSpeed);
}
},
});