-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathUIButtonGroup.vue
More file actions
182 lines (172 loc) · 6.22 KB
/
UIButtonGroup.vue
File metadata and controls
182 lines (172 loc) · 6.22 KB
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
<template>
<div class="nrdb-ui-button-group-wrapper">
<!-- eslint-disable-next-line vue/no-v-html -->
<label v-if="label" class="v-label" v-html="label" />
<v-btn-toggle v-model="selection" mandatory divided :rounded="props.rounded ? 'xl' : ''" :color="selectedColor" :disabled="!state.enabled" @update:model-value="onChange(selection)">
<v-btn v-for="option in options" :key="option.value" :value="option.value">
<template v-if="option.icon && option.label !== undefined && option.label !== ''" #prepend>
<v-icon size="x-large" :icon="`mdi-${option.icon.replace(/^mdi-/, '')}`" />
</template>
<v-icon v-if="option.icon && !option.label" :icon="`mdi-${option.icon.replace(/^mdi-/, '')}`" size="x-large" />
{{ option.label }}
</v-btn>
</v-btn-toggle>
</div>
</template>
<script>
import DOMPurify from 'dompurify'
import { mapState } from 'vuex'
export default {
name: 'DBUIButtonGroup',
inject: ['$socket', '$dataTracker'],
props: {
id: { type: String, required: true },
props: { type: Object, default: () => ({}) },
state: { type: Object, default: () => ({}) }
},
data () {
return {
selection: null
}
},
computed: {
...mapState('data', ['messages']),
selectedColor: function () {
if (this.selection !== null && this.props.useThemeColors === false) {
return this.findOptionByValue(this.selection)?.color
} else {
return 'rgb(var(--v-theme-primary))'
}
},
variant: function () {
return this.look === 'default' ? null : this.look
},
label: function () {
// Sanetize the html to avoid XSS attacks
return DOMPurify.sanitize(this.getProperty('label'))
},
options: function () {
const options = this.getProperty('options')
if (options) {
return options.map(option => {
if (typeof option === 'string') {
return { label: option, value: option }
} else {
return option
}
})
}
return options
}
},
created () {
// can't do this in setup as we are using custom onInput function that needs access to 'this'
this.$dataTracker(this.id, this.onInput, this.onLoad, this.onDynamicProperty, this.onSync)
// let Node-RED know that this widget has loaded
this.$socket.emit('widget-load', this.id)
},
methods: {
onInput (msg) {
// update our vuex store with the value retrieved from Node-RED
this.$store.commit('data/bind', {
widgetId: this.id,
msg
})
// make sure our v-model is updated to reflect the value from Node-RED
if (msg.payload !== undefined) {
if (Array.isArray(msg.payload) && msg.payload.length === 0) {
this.selection = null
} else {
if (this.findOptionByValue(msg.payload) !== null) {
this.selection = msg.payload
}
}
}
},
onLoad (msg) {
if (msg) {
// update vuex store to reflect server-state
this.$store.commit('data/bind', {
widgetId: this.id,
msg
})
// make sure we've got the relevant option selected on load of the page
if (msg.payload !== undefined) {
if (Array.isArray(msg.payload) && msg.payload.length === 0) {
this.selection = null
} else {
if (this.findOptionByValue(msg.payload) !== null) {
this.selection = msg.payload
}
}
}
}
},
onDynamicProperty (msg) {
const updates = msg.ui_update
if (updates) {
this.updateDynamicProperty('label', updates.label)
this.updateDynamicProperty('options', updates.options)
}
},
onSync (msg) {
this.selection = msg.payload
},
onChange (value) {
if (value !== null && typeof value !== 'undefined') {
// Tell Node-RED a new value has been selected
this.$socket.emit('widget-change', this.id, value)
}
},
// Keep the code of this function in sync with the server-side function
findOptionByValue: function (val) {
const opt = this.options?.find(option => {
if (typeof (val) === 'object') {
return (JSON.stringify(val) === JSON.stringify(option.value))
} else {
return option.value === val
}
})
if (opt) {
return opt
}
return null
}
}
}
</script>
<style>
.nrdb-ui-button-group-wrapper {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
gap: 12px;
}
.nrdb-ui-button-group-wrapper .v-chip__content{
white-space: normal;
}
.nrdb-ui-button-group-wrapper .v-btn-group {
width: max-content;
border-width: thin;
border-color: rgba(var(--v-border-color), var(--v-border-opacity));
}
/* default styling for an unselected option */
.nrdb-ui-button-group-wrapper .v-btn--variant-elevated {
color: rgb(var(--v-theme-on-group-background));
background-color: rgb(var(--v-theme-group-background));
}
.nrdb-ui-button-group-wrapper .v-btn--variant-elevated {
--v-activated-opacity: 0;
}
.nrdb-ui-button-group-wrapper .icon-only .v-btn__prepend {
margin-inline: 0;
}
.nrdb-ui-button-group-wrapper .v-btn.v-btn--disabled .v-btn__overlay {
opacity: 0.1;
}
.nrdb-ui-button-group-wrapper .v-btn-group .v-btn--disabled .v-btn__content {
color: rgb(var(--v-theme-on-group-background), var(--v-disabled-opacity));
}
</style>