-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcard.js
139 lines (114 loc) · 4.87 KB
/
card.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
function Card(node) {
var card = {
id: undefined,
badgesContainerNode: undefined,
node: node,
oldRealText: undefined,
initId: function() {
var cardText = this.realText();
var regexMatches = cardText.matchAll(new RegExp("#([0-9]+)", "g"));
if (!regexMatches || regexMatches.length < 1) {
return;
}
for (var i = 0; i < regexMatches.length; i++) {
var match = regexMatches[i];
var fullMatch = match[0];
var matchContents = match[1];
var matchContentsInt;
try {
matchContentsInt = Number.parseInt(matchContents, 10);
this.id = matchContentsInt;
this.node.setAttribute("id", matchContentsInt);
} catch (e) {
continue;
}
}
this.setVisibleText(cardText);
},
refresh: function() {
console.log('Refreshing card.');
// If the card has been updated it's real text will have changed.
if (this.oldRealText !== this.realText()) {
console.log('Text has changed.');
this.cleanBadges();
this.oldRealText = this.realText();
this.setVisibleText(this.oldRealText);
this.processBadges();
}
},
initFakeText: function() {
var realTextElem = this.node.getElementsByClassName("js-card-name")[0];
this.oldRealText = realTextElem.innerHTML;
var fakeTextElem = realTextElem.cloneNode();
fakeTextElem.className = "visible-text-override";
realTextElem.style.display = 'none';
realTextElem.parentNode.insertBefore(fakeTextElem, realTextElem.nextSibling);
},
initBadgesContainer: function() {
var labelsElem = this.node.getElementsByClassName("list-card-labels")[0];
var badgesContainer = document.createElement('div');
badgesContainer.classList.add("badger-badges");
labelsElem.parentNode.insertBefore(badgesContainer, labelsElem.nextSibling);
},
badgesContainer: function() {
return this.node.getElementsByClassName("badger-badges")[0];
},
realText: function() {
return this.node.getElementsByClassName("js-card-name")[0].innerHTML;
},
getVisibleText: function() {
return this.node.getElementsByClassName("visible-text-override")[0].innerHTML;
},
setVisibleText: function(text) {
this.node.getElementsByClassName("visible-text-override")[0].innerHTML = text;
},
addBadge: function(fieldName, badgeText, iconName, fontColor, badgeColor, fontSize, fontWeight) {
var badgesContainer = this.badgesContainer();
badge = newBadge(fieldName, badgeText, iconName, fontColor, badgeColor, fontSize, fontWeight);
badgesContainer.appendChild(badge);
},
addValue: function(field, value) {
if (field.indexOf("[]") != -1) {
fieldName = field.replace("[]", "");
if (!this[fieldName]) {
this[fieldName] = [];
}
this[fieldName].push(value);
} else {
this[field] = value;
}
},
processBadges: function() {
for (var j = 0; j < config.cardBadges.length; j++) {
var badgeConf = config.cardBadges[j];
this.processBadge(badgeConf.regex, badgeConf.field, badgeConf.icon,
badgeConf.textColor, badgeConf.bgColor,
badgeConf.fontSize, badgeConf.fontWeight);
}
},
processBadge: function(reg, fieldName, iconName, fontColor, badgeColor, fontSize, fontWeight) {
var cardText = this.getVisibleText();
var regexMatches = cardText.matchAll(reg);
if (!regexMatches || regexMatches.length < 1) {
return;
}
for (var i = 0; i < regexMatches.length; i++) {
var match = regexMatches[i];
var fullMatch = match[0];
var matchContents = match[1];
this.addValue(fieldName, matchContents);
cardText = cardText.replace(fullMatch, "");
this.addBadge(fieldName, matchContents, iconName, fontColor, badgeColor, fontSize, fontWeight);
}
this.setVisibleText(cardText);
},
cleanBadges: function() {
var badgesContainer = this.badgesContainer();
badgesContainer.innerHTML = "";
}
};
card.initBadgesContainer();
card.initFakeText();
card.initId();
return card;
}