Skip to content

Commit 8e9ce6a

Browse files
committed
Cleanup.
1 parent 4167fe9 commit 8e9ce6a

File tree

2 files changed

+17
-99
lines changed

2 files changed

+17
-99
lines changed

src/GleapNotificationBadge.js

Lines changed: 16 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -3,117 +3,36 @@
33
* @url stackoverflow.com/questions/65719387/
44
*/
55
export default class GleapNotificationBadge {
6-
constructor(options) {
7-
Object.assign(
8-
this, {
9-
backgroundColor: "#f00",
10-
color: "#fff",
11-
size: 0.6,
12-
position: "se",
13-
radius: 8,
14-
src: "",
15-
onChange() { },
16-
},
17-
options
18-
);
19-
this.canvas = document.createElement("canvas");
20-
this.src = this.src || this.faviconEL.getAttribute("href");
21-
this.ctx = this.canvas.getContext("2d");
22-
}
23-
24-
faviconEL = document.querySelector("link[rel$=icon]");
6+
_value = 0;
257

26-
_drawIcon() {
27-
this.ctx.clearRect(0, 0, this.faviconSize, this.faviconSize);
28-
this.ctx.drawImage(this.img, 0, 0, this.faviconSize, this.faviconSize);
29-
}
8+
constructor() {
309

31-
_drawShape() {
32-
const r = this.radius;
33-
const xa = this.offset.x;
34-
const ya = this.offset.y;
35-
const xb = this.offset.x + this.badgeSize;
36-
const yb = this.offset.y + this.badgeSize;
37-
this.ctx.beginPath();
38-
this.ctx.moveTo(xb - r, ya);
39-
this.ctx.quadraticCurveTo(xb, ya, xb, ya + r);
40-
this.ctx.lineTo(xb, yb - r);
41-
this.ctx.quadraticCurveTo(xb, yb, xb - r, yb);
42-
this.ctx.lineTo(xa + r, yb);
43-
this.ctx.quadraticCurveTo(xa, yb, xa, yb - r);
44-
this.ctx.lineTo(xa, ya + r);
45-
this.ctx.quadraticCurveTo(xa, ya, xa + r, ya);
46-
this.ctx.fillStyle = this.backgroundColor;
47-
this.ctx.fill();
48-
this.ctx.closePath();
4910
}
5011

51-
_drawVal() {
52-
const margin = (this.badgeSize * 0.18) / 2;
53-
this.ctx.beginPath();
54-
this.ctx.textBaseline = "middle";
55-
this.ctx.textAlign = "center";
56-
this.ctx.font = `bold ${this.badgeSize * 0.82}px Arial`;
57-
this.ctx.fillStyle = this.color;
58-
this.ctx.fillText(this.value, this.badgeSize / 2 + this.offset.x, this.badgeSize / 2 + this.offset.y + margin);
59-
this.ctx.closePath();
60-
}
61-
62-
_drawFavicon() {
12+
getCleanTitle() {
13+
var title = "";
6314
try {
64-
this.faviconEL.setAttribute("href", this.dataURL);
65-
} catch (e) {
66-
console.log(e);
67-
}
68-
}
15+
title = document.title;
6916

70-
_draw() {
71-
this._drawIcon();
72-
if (this.value) this._drawShape();
73-
if (this.value) this._drawVal();
74-
this._drawFavicon();
75-
}
17+
// Remove last value in title.
18+
if (title.indexOf("(") === 0 && title.indexOf(")") > 0) {
19+
title = title.substring(title.indexOf(") ") + 2, title.length);
20+
}
21+
} catch (e) { }
7622

77-
_setup() {
78-
this.faviconSize = this.img.naturalWidth;
79-
this.badgeSize = this.faviconSize * this.size;
80-
this.canvas.width = this.faviconSize;
81-
this.canvas.height = this.faviconSize;
82-
const sd = this.faviconSize - this.badgeSize;
83-
const sd2 = sd / 2;
84-
this.offset = {
85-
n: { x: sd2, y: 0 },
86-
e: { x: sd, y: sd2 },
87-
s: { x: sd2, y: sd },
88-
w: { x: 0, y: sd2 },
89-
nw: { x: 0, y: 0 },
90-
ne: { x: sd, y: 0 },
91-
sw: { x: 0, y: sd },
92-
se: { x: sd, y: sd },
93-
}[this.position];
23+
console.log("title", title);
24+
25+
return title;
9426
}
9527

96-
// Public functions / methods:
9728
update() {
98-
this._value = Math.min(99, parseInt(this._value, 10));
99-
if (this.img) {
100-
this._draw();
101-
if (this.onChange) this.onChange.call(this);
29+
if (this.value === 0) {
30+
document.title = this.getCleanTitle();
10231
} else {
103-
this.img = new Image();
104-
this.img.addEventListener("load", () => {
105-
this._setup();
106-
this._draw();
107-
if (this.onChange) this.onChange.call(this);
108-
});
109-
this.img.src = this.src;
32+
document.title = `(${this._value}) ${this.getCleanTitle()}`;
11033
}
11134
}
11235

113-
get dataURL() {
114-
return this.canvas.toDataURL();
115-
}
116-
11736
get value() {
11837
return this._value;
11938
}

src/GleapNotificationManager.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ export default class GleapNotificationManager {
2929
constructor() {
3030
if (typeof window !== "undefined") {
3131
try {
32-
this.badgeManager = new GleapNotificationBadge({});
32+
this.badgeManager = new GleapNotificationBadge();
3333
this.badgeManager.value = 0;
3434
} catch (exp) {
35-
console.log(exp);
3635
this.badgeManager = null;
3736
}
3837
}

0 commit comments

Comments
 (0)