forked from thesoftwarejedi/MMM-YNAB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-YNAB.js
96 lines (80 loc) · 3.1 KB
/
MMM-YNAB.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
Module.register("MMM-YNAB", {
result: [],
defaults: {
token: "",
categories: [ "Household", "Pets", "Grocery", "Lunch", "Kids Clothes", "Restaurants", "Spontaneous Fun" ],
boldNegativeCategory: true,
redNegativeAmount: true,
flashNegativeCategory: true,
fontSize: "small",
multiline: false,
categoriesPerLine: 3
},
start: function () {
this.sendSocketNotification('YNAB_SET_CONFIG', this.config);
},
getDom: function () {
var wrapper = document.createElement("div");
wrapper.className = "ynab-wrapper";
if (this.config.multiline) {
wrapper.classList.add("multi-line");
} else {
wrapper.classList.add("single-line");
}
if (!this.result.items || this.result.items.length === 0) {
wrapper.innerHTML = "Loading YNAB";
return wrapper;
}
let rowWrapper = document.createElement("div");
rowWrapper.className = "ynab-row";
let itemCount = 0;
for (let item of this.result.items) {
if (this.config.multiline && itemCount % this.config.categoriesPerLine === 0 && itemCount !== 0) {
wrapper.appendChild(rowWrapper);
rowWrapper = document.createElement("div");
rowWrapper.className = "ynab-row";
}
let categoryWrapper = document.createElement("span");
categoryWrapper.className = "ynab-category";
let nameSpan = document.createElement("span");
nameSpan.className = "ynab-name";
nameSpan.textContent = item.name;
let balanceSpan = document.createElement("span");
balanceSpan.className = "ynab-balance";
balanceSpan.textContent = "$" + (item.balance/1000).toFixed(2);
if (item.balance < 0) {
nameSpan.classList.add("negative-category", this.config.fontSize);
if (this.config.boldNegativeCategory) {
nameSpan.classList.add("bold-negative");
}
if (this.config.redNegativeAmount) {
balanceSpan.classList.add("negative-amount");
}
if (this.config.flashNegativeCategory) {
nameSpan.classList.add("flashing");
}
}
categoryWrapper.appendChild(nameSpan);
categoryWrapper.appendChild(balanceSpan);
rowWrapper.appendChild(categoryWrapper);
itemCount++;
}
if (rowWrapper.childNodes.length > 0) {
wrapper.appendChild(rowWrapper);
}
return wrapper;
},
socketNotificationReceived: function (notification, payload) {
console.log("notification: " + notification);
console.log("payload: " + JSON.stringify(payload));
if (notification == "YNAB_UPDATE") {
this.result = payload;
this.updateDom(0);
}
},
getStyles: function() {
return [
this.file('MMM-YNAB.css')
]
}
});