|
3 | 3 | * @url stackoverflow.com/questions/65719387/ |
4 | 4 | */ |
5 | 5 | 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; |
25 | 7 |
|
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() { |
30 | 9 |
|
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(); |
49 | 10 | } |
50 | 11 |
|
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 = ""; |
63 | 14 | try { |
64 | | - this.faviconEL.setAttribute("href", this.dataURL); |
65 | | - } catch (e) { |
66 | | - console.log(e); |
67 | | - } |
68 | | - } |
| 15 | + title = document.title; |
69 | 16 |
|
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) { } |
76 | 22 |
|
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; |
94 | 26 | } |
95 | 27 |
|
96 | | - // Public functions / methods: |
97 | 28 | 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(); |
102 | 31 | } 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()}`; |
110 | 33 | } |
111 | 34 | } |
112 | 35 |
|
113 | | - get dataURL() { |
114 | | - return this.canvas.toDataURL(); |
115 | | - } |
116 | | - |
117 | 36 | get value() { |
118 | 37 | return this._value; |
119 | 38 | } |
|
0 commit comments