forked from openWB/openwb-ui-settings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenwbPageMessages.vue
261 lines (252 loc) · 6.24 KB
/
OpenwbPageMessages.vue
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<template>
<teleport to="body">
<div
id="message-indicator"
class="text-light mt-1 p-2 mr-1 clickable"
:class="showAllMessages ? 'active' : ''"
@click="toggleAllMessages"
>
<font-awesome-layers full-width style="font-size: 175%">
<font-awesome-icon
fixed-width
:icon="showAllMessages ? ['fas', 'bell'] : ['far', 'bell']"
:class="messageIndicatorClass"
/>
<font-awesome-layers-text
v-if="messages.length > 0"
counter
:value="messages.length"
position="top-right"
class="message-counter bg-light text-dark"
/>
</font-awesome-layers>
</div>
</teleport>
<div
v-if="recentMessages.length > 0 || showAllMessages"
class="openwb-toast-container"
:class="showAllMessages ? 'full-height' : ''"
>
<openwb-base-alert v-if="recentMessages.length == 0" subtype="info">
Keine Nachrichten vorhanden.
</openwb-base-alert>
<openwb-base-click-button
v-else-if="showAllMessages"
class="btn-sm btn-secondary mb-1"
@buttonClicked="dismissAllMessages"
>
Alle Nachrichten löschen
</openwb-base-click-button>
<openwb-base-toast
v-for="message in recentMessages"
:key="message.topic"
:topic="message.topic"
:source="message.source"
:subtype="message.type"
:timestamp="message.timestamp"
@dismiss="dismissMessage"
@hide="hideMessage"
>
<span v-html="message.message"></span>
</openwb-base-toast>
</div>
</template>
<script>
import { library } from "@fortawesome/fontawesome-svg-core";
import { faBell as fasBell } from "@fortawesome/free-solid-svg-icons";
import { faBell as farBell } from "@fortawesome/free-regular-svg-icons";
import {
FontAwesomeIcon,
FontAwesomeLayers,
FontAwesomeLayersText,
} from "@fortawesome/vue-fontawesome";
library.add(fasBell, farBell);
import ComponentState from "./mixins/ComponentState.vue";
export default {
name: "OpenwbPageMessages",
mixins: [ComponentState],
components: {
FontAwesomeIcon,
FontAwesomeLayers,
FontAwesomeLayersText,
},
data() {
return {
mqttTopicsToSubscribe: [
"openWB/system/messages/+",
"openWB/command/" + this.$root.mqttClientId + "/messages/+",
"openWB/command/" + this.$root.mqttClientId + "/error",
],
showAllMessages: false,
hiddenMessages: [],
};
},
computed: {
alertLevel() {
let result = this.messages.reduce((total, currentMessage) => {
if (
(total == "light" &&
["info", "success", "warning", "danger"].includes(
currentMessage.type,
)) ||
(total == "info" &&
["success", "warning", "danger"].includes(
currentMessage.type,
)) ||
(total == "success" &&
["warning", "danger"].includes(currentMessage.type)) ||
(total == "warning" && currentMessage.type == "danger")
) {
total = currentMessage.type;
}
return total;
}, "light");
return result;
},
messages() {
const myMessages = [];
if (this.alertData) {
myMessages.push({
topic:
"openWB/command/" + this.$root.mqttClientId + "/error",
source: "command",
type: "danger",
message:
'Bei der Verarbeitung des Befehls \'<span class="font-weight-bold">' +
this.alertData.command +
"</span>' mit den Parametern '<span class=\"font-weight-bold\">" +
this.alertData.data +
"</span>' ist ein Fehler aufgetreten:<br />" +
"<pre class='font-weight-bold'>" +
this.alertData.error +
"</pre>",
});
}
this.systemMessages.forEach((message) => {
myMessages.push(message);
});
this.clientMessages.forEach((message) => {
myMessages.push(message);
});
myMessages.sort(this.compareMessagesByTimestamp);
return myMessages;
},
recentMessages() {
if (this.showAllMessages) {
return this.messages;
} else {
return this.messages.filter((message) => {
return !this.hiddenMessages.includes(message.topic);
});
}
},
messageIndicatorClass() {
return "text-" + this.alertLevel;
},
/**
* get initial error message
*/
alertData() {
return this.$store.state.mqtt[
"openWB/command/" + this.$root.mqttClientId + "/error"
];
},
systemMessages() {
let messageTopics = this.getWildcardTopics(
"openWB/system/messages/+",
);
var messageList = [];
for (const [key, element] of Object.entries(messageTopics)) {
messageList.push({ topic: key, ...element });
}
return messageList;
},
clientMessages() {
let messageTopics = this.getWildcardTopics(
"openWB/command/" + this.$root.mqttClientId + "/messages/+",
);
var messageList = [];
for (const [key, element] of Object.entries(messageTopics)) {
messageList.push({ topic: key, ...element });
}
return messageList;
},
},
methods: {
toggleAllMessages() {
this.showAllMessages = !this.showAllMessages;
},
compareMessagesByTimestamp(a, b) {
// last messages first
return b.timestamp - a.timestamp;
},
clearTopic(topic) {
this.$root.doPublish(topic, undefined);
},
/**
* Removes client specific error topic from broker
*/
dismissError() {
this.clearTopic(
"openWB/command/" + this.$root.mqttClientId + "/error",
);
},
/**
* Removes system or client specific topics from broker
*/
dismissMessage(event) {
this.clearTopic(event.topic);
var index = this.hiddenMessages.indexOf(event.topic);
if (index > -1) {
this.hiddenMessages.splice(index, 1);
}
},
/**
* Removes all received message topics from broker
*/
dismissAllMessages() {
this.messages.forEach((message) => {
this.clearTopic(message.topic);
});
this.hiddenMessages = [];
this.toggleAllMessages();
},
/**
* add message topic to list of hidden messages
*/
hideMessage(event) {
if (!this.hiddenMessages.includes(event.topic)) {
this.hiddenMessages.push(event.topic);
}
},
},
};
</script>
<style scoped>
#message-indicator {
position: fixed;
top: 0;
right: 0;
z-index: 2000; /* navbar: 1030 */
}
#message-indicator .message-counter {
font-weight: bolder;
}
.openwb-toast-container {
position: fixed !important;
z-index: 2000;
right: 0;
top: 55px;
padding: 0.25rem;
max-width: 275px;
}
.openwb-toast-container.full-height {
bottom: 30px;
overflow-y: auto;
background-color: var(--dark);
border: 1px solid var(--dark);
}
.clickable {
cursor: pointer;
}
</style>