@enso-ui/notifications 3.2.10 → 3.2.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@enso-ui/notifications",
|
|
3
|
-
"version": "3.2.
|
|
3
|
+
"version": "3.2.11",
|
|
4
4
|
"description": "Notification center pages and navbar widgets for Enso UI",
|
|
5
5
|
"main": "src/bulma/pages/Index.vue",
|
|
6
6
|
"repository": {
|
|
@@ -21,7 +21,6 @@
|
|
|
21
21
|
"@fortawesome/fontawesome-svg-core": "^7.2.0",
|
|
22
22
|
"@fortawesome/free-solid-svg-icons": "^7.2.0",
|
|
23
23
|
"@fortawesome/vue-fontawesome": "3.1.3",
|
|
24
|
-
"favico.js": "^0.3.10",
|
|
25
24
|
"lodash": "^4.17.20"
|
|
26
25
|
},
|
|
27
26
|
"peerDependencies": {
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
<script>
|
|
2
2
|
import { debounce } from 'lodash';
|
|
3
|
-
import Favico from 'favico.js';
|
|
4
3
|
import eventBus from '@enso-ui/ui/src/core/services/eventBus';
|
|
5
4
|
import format from '@enso-ui/ui/src/modules/plugins/date-fns/format';
|
|
6
5
|
import formatDistance from '@enso-ui/ui/src/modules/plugins/date-fns/formatDistance';
|
|
7
6
|
import { app } from '@enso-ui/ui/src/pinia/app';
|
|
8
7
|
import { websockets } from '@enso-ui/ui/src/pinia/websockets';
|
|
8
|
+
import FaviconBadge from '../../services/FaviconBadge';
|
|
9
9
|
|
|
10
10
|
export default {
|
|
11
11
|
name: 'Notifications',
|
|
@@ -24,7 +24,7 @@ export default {
|
|
|
24
24
|
},
|
|
25
25
|
|
|
26
26
|
data: v => ({
|
|
27
|
-
favico: new
|
|
27
|
+
favico: new FaviconBadge(),
|
|
28
28
|
notifications: [],
|
|
29
29
|
unread: 0,
|
|
30
30
|
needsUpdate: true,
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
const favicon = () => {
|
|
2
|
+
const current = document.querySelector('link[rel~="icon"]');
|
|
3
|
+
|
|
4
|
+
if (current) {
|
|
5
|
+
return current;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const link = document.createElement('link');
|
|
9
|
+
link.rel = 'icon';
|
|
10
|
+
document.head.appendChild(link);
|
|
11
|
+
|
|
12
|
+
return link;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const label = value => (value > 99 ? '99+' : `${value}`);
|
|
16
|
+
|
|
17
|
+
const drawLabel = (context, value, size) => {
|
|
18
|
+
const radius = size * 0.34;
|
|
19
|
+
const center = size - radius;
|
|
20
|
+
|
|
21
|
+
context.fillStyle = '#d63638';
|
|
22
|
+
context.beginPath();
|
|
23
|
+
context.arc(center, center, radius, 0, Math.PI * 2);
|
|
24
|
+
context.fill();
|
|
25
|
+
|
|
26
|
+
context.fillStyle = '#fff';
|
|
27
|
+
context.font = `700 ${value > 99 ? size * 0.25 : size * 0.32}px sans-serif`;
|
|
28
|
+
context.textAlign = 'center';
|
|
29
|
+
context.textBaseline = 'middle';
|
|
30
|
+
context.fillText(label(value), center, center + 1);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export default class FaviconBadge {
|
|
34
|
+
constructor() {
|
|
35
|
+
this.link = typeof document === 'undefined' ? null : favicon();
|
|
36
|
+
this.originalHref = this.link?.href || null;
|
|
37
|
+
this.request = 0;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
badge(value) {
|
|
41
|
+
if (!this.link) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const unread = Number(value) || 0;
|
|
46
|
+
this.request++;
|
|
47
|
+
|
|
48
|
+
if (!unread) {
|
|
49
|
+
if (this.originalHref) {
|
|
50
|
+
this.link.href = this.originalHref;
|
|
51
|
+
} else {
|
|
52
|
+
this.link.removeAttribute('href');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
this.draw(unread, this.request);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
draw(value, request) {
|
|
62
|
+
if (!this.originalHref) {
|
|
63
|
+
this.updateFallback(value, request);
|
|
64
|
+
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const image = new Image();
|
|
69
|
+
image.crossOrigin = 'anonymous';
|
|
70
|
+
image.onload = () => this.updateWithImage(image, value, request);
|
|
71
|
+
image.onerror = () => this.updateFallback(value, request);
|
|
72
|
+
image.src = this.originalHref;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
updateWithImage(image, value, request) {
|
|
76
|
+
if (request !== this.request) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const size = 32;
|
|
81
|
+
const canvas = document.createElement('canvas');
|
|
82
|
+
canvas.width = size;
|
|
83
|
+
canvas.height = size;
|
|
84
|
+
const context = canvas.getContext('2d');
|
|
85
|
+
|
|
86
|
+
context.drawImage(image, 0, 0, size, size);
|
|
87
|
+
drawLabel(context, value, size);
|
|
88
|
+
this.link.href = canvas.toDataURL('image/png');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
updateFallback(value, request) {
|
|
92
|
+
if (request !== this.request) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const size = 32;
|
|
97
|
+
const canvas = document.createElement('canvas');
|
|
98
|
+
canvas.width = size;
|
|
99
|
+
canvas.height = size;
|
|
100
|
+
const context = canvas.getContext('2d');
|
|
101
|
+
|
|
102
|
+
drawLabel(context, value, size);
|
|
103
|
+
this.link.href = canvas.toDataURL('image/png');
|
|
104
|
+
}
|
|
105
|
+
}
|