-
Notifications
You must be signed in to change notification settings - Fork 81
/
NcUserBubble.vue
348 lines (320 loc) · 7.18 KB
/
NcUserBubble.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<!--
- @copyright Copyright (c) 2019 Jonas Sulzer <jonas@violoncello.ch>
-
- @author Marco Ambrosini <marcoambrosini@icloud.com>
- @author Jonas Sulzer <jonas@violoncello.ch>
-
- @license GNU AGPL version 3 or any later version
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<docs>
### General description
This component displays a user together with a small avatar in a grey bubble.
It's possible to use an actual user's avatar, just an image/icon as a url or an icon-class,
to link the bubble to e.g. a users profile
and to show a popover on hover with e.g. the full user name handle / email address or something else.
This component has the following slot:
* a default slot which is for the content of the popover (this is passed to the popover component directly).
### Examples
```vue
<p>
Some text before <NcUserBubble user="admin" display-name="Admin Example" url="/test">@admin@foreign-host.com</NcUserBubble> and after the bubble.
<NcUserBubble avatar-image="icon-group" display-name="test group xyz" :primary="true">Hey there!</NcUserBubble>
</p>
```
### Example with name slot
```vue
<template>
<NcUserBubble
:margin="4"
:size="30"
display-name="Administrator"
user="admin">
<template #name>
<a href="#"
title="Remove user"
class="icon-close"
@click="alert" />
</template>
</NcUserBubble>
</template>
<script>
export default {
methods: {
alert() {
alert('Removed')
},
},
}
</script>
<style>
.icon-close {
display: block;
height: 100%;
}
</style>
```
</docs>
<template>
<component :is="isPopoverComponent"
trigger="hover focus"
:shown="open"
class="user-bubble__wrapper"
@update:open="onOpenChange">
<!-- Main userbubble structure -->
<template #trigger>
<component :is="isLinkComponent"
class="user-bubble__content"
:style="styles.content"
:href="hasUrl ? url : null"
:class="{ 'user-bubble__content--primary': primary }"
@click="onClick">
<!-- NcAvatar -->
<NcAvatar :url="isCustomAvatar && isAvatarUrl ? avatarImage : undefined"
:icon-class="isCustomAvatar && !isAvatarUrl ? avatarImage : undefined"
:user="user"
:display-name="displayName"
:size="size - (margin * 2)"
:style="styles.avatar"
:disable-tooltip="true"
:disable-menu="true"
:showUserStatus="showUserStatus"
class="user-bubble__avatar" />
<!-- Name -->
<span class="user-bubble__name">
{{ displayName || user }}
</span>
<!-- @slot Optional slot just after the name -->
<span v-if="$slots.name" class="user-bubble__secondary">
<slot name="name" />
</span>
</component>
</template>
<!-- @slot Main Popover content on userbubble hover/focus -->
<slot />
</component>
</template>
<script>
import NcUserBubbleDiv from './NcUserBubbleDiv.vue'
import NcAvatar from '../NcAvatar/index.js'
import NcPopover from '../NcPopover/index.js'
import Vue from 'vue'
export default {
name: 'NcUserBubble',
components: {
NcAvatar,
NcPopover,
NcUserBubbleDiv,
},
props: {
/**
* Override generated avatar, can be an url or an icon class
*/
avatarImage: {
type: String,
default: undefined,
},
/**
* Provide the user id if this is a user
*/
user: {
type: String,
default: undefined,
},
/**
* Displayed label
*/
displayName: {
type: String,
default: undefined,
},
/**
* Whether or not to display the user-status
*/
showUserStatus: {
type: Boolean,
default: false,
},
/**
* Define the whole bubble as a link
*/
url: {
type: String,
default: undefined,
validator: (url) => {
try {
url = new URL(url, url?.startsWith?.('/') ? window.location.href : undefined)
return true
} catch (error) {
return false
}
},
},
/**
* Default popover state. Requires the UserBubble
* to have some content to render inside the popover
*/
open: {
type: Boolean,
default: false,
},
/**
* Use the primary colour
*/
primary: {
type: Boolean,
default: false,
},
/**
* This is the height of the component
*/
size: {
type: Number,
default: 20,
},
/**
* This is the margin of the avatar (size - margin = avatar size)
*/
margin: {
type: Number,
default: 2,
},
},
emits: [
'click',
'update:open',
],
computed: {
/**
* If userbubble is empty, let's NOT
* use the Popover component
* We need a component instead of a simple div here,
* because otherwise the trigger template will not be shown.
*
* @return {string} 'Popover' or 'UserBubbleDiv'
*/
isPopoverComponent() {
return !this.popoverEmpty
? 'NcPopover'
: 'NcUserBubbleDiv'
},
/**
* Is the provided avatar url valid or not
*
* @return {boolean}
*/
isAvatarUrl() {
if (!this.avatarImage) {
return false
}
try {
const url = new URL(this.avatarImage)
return !!url
} catch (error) {
return false
}
},
/**
* Do we have a custom avatar or not
*
* @return {boolean}
*/
isCustomAvatar() {
return !!this.avatarImage
},
hasUrl() {
return this.url && this.url.trim() !== ''
},
isLinkComponent() {
return this.hasUrl ? 'a' : 'div'
},
popoverEmpty() {
if ('default' in this.$slots) {
return false
}
return true
},
styles() {
return {
content: {
height: this.size + 'px',
lineHeight: this.size + 'px',
borderRadius: this.size / 2 + 'px',
},
avatar: {
marginLeft: this.margin + 'px',
},
}
},
},
mounted() {
if (!this.displayName && !this.user) {
Vue.util.warn('[NcUserBubble] At least `displayName` or `user` property should be set.')
}
},
methods: {
onOpenChange(state) {
this.$emit('update:open', state)
},
/**
* Catch and forward click event to parent
*
* @param {Event} event the click event
*/
onClick(event) {
this.$emit('click', event)
},
},
}
</script>
<style lang="scss" scoped>
.user-bubble {
&__wrapper {
// align inline with text
display: inline-block;
vertical-align: middle;
// shrink and allow grow to fit
min-width: 0;
max-width: 100%;
}
&__content {
display: inline-flex;
max-width: 100%;
background-color: var(--color-background-dark);
&--primary {
color: var(--color-primary-element-text);
background-color: var(--color-primary-element);
}
> :last-child {
// border radius left padding
padding-right: 8px;
}
}
&__avatar {
align-self: center;
}
&__name {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
&__name,
&__secondary {
// proper spacing between avatar, name & slot
padding: 0;
padding-left: 4px;
}
}
</style>