summaryrefslogtreecommitdiffstats
path: root/src/components/ContactsList/ContactsListItem.vue
blob: 2439a1177e2d831d6843706423e9f94f0236b769 (plain)
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
<template>
	<div class="contacts-list__item-wrapper"
		:draggable="isDraggable"
		@dragstart="startDrag($event, source)">
		<ListItem :id="id"
			:key="source.key"
			class="list-item-style envelope"
			:name="source.displayName"
			:to="{ name: 'contact', params: { selectedGroup: selectedGroup, selectedContact: source.key } }">
			<!-- @slot Icon slot -->

			<template #icon>
				<div class="app-content-list-item-icon">
					<BaseAvatar :display-name="source.displayName" :url="avatarUrl" :size="40" />
				</div>
			</template>
			<template #subtitle>
				<div class="envelope__subtitle">
					<span class="envelope__subtitle__subject">
						{{ source.email }}
					</span>
				</div>
			</template>
		</ListItem>
	</div>
</template>

<script>
import {
	NcListItem as ListItem,
	NcAvatar as BaseAvatar,
} from '@nextcloud/vue'

export default {
	name: 'ContactsListItem',

	components: {
		ListItem,
		BaseAvatar,
	},

	props: {
		index: {
			type: Number,
			required: true,
		},
		source: {
			type: Object,
			required: true,
		},
		reloadBus: {
			type: Object,
			required: true,
		},
	},
	data() {
		return {
			avatarUrl: undefined,
		}
	},

	computed: {
		selectedGroup() {
			return this.$route.params.selectedGroup
		},
		selectedContact() {
			return this.$route.params.selectedContact
		},
		// contact is not draggable when it has not been saved on server as it can't be added to groups/circles before
		isDraggable() {
			return !!this.source.dav && this.source.addressbook.id !== 'z-server-generated--system'
		},
		// usable and valid html id for scrollTo
		id() {
			return window.btoa(this.source.key).slice(0, -2)
		},
	},

	created() {
		this.reloadBus.on('reload-avatar', this.reloadAvatarUrl)
		this.reloadBus.on('delete-avatar', this.deleteAvatar)
	},
	destroyed() {
		this.reloadBus.off('reload-avatar', this.reloadAvatarUrl)
		this.reloadBus.off('delete-avatar', this.deleteAvatar)
	},
	async mounted() {
		await this.loadAvatarUrl()
	},
	methods: {
		startDrag(evt, item) {
			evt.dataTransfer.dropEffect = 'move'
			evt.dataTransfer.effectAllowed = 'move'
			evt.dataTransfer.setData('item', JSON.stringify({
				addressbookId: item.addressbook.id,
				displayName: item.displayName,
				groups: item.groups,
				url: item.url,
				uid: item.uid,
			}))
		},

		/**
		 * Is called on save in ContactDetails to reload Avatar,
		 * url does not change, so trigger on source change don't work
		 *
		 * @param {string} key from contact
		 */
		reloadAvatarUrl(key) {
			if (key === this.source.key) {
				this.loadAvatarUrl()
			}
		},

		/**
		 * Is called on save in ContactDetails to delete Avatar,
		 * somehow the avatarUrl is not unavailable immediately, so we just set undefined
		 *
		 * @param {string} key from contact
		 */
		deleteAvatar(key) {
			if (key === this.source.key) {
				this.avatarUrl = undefined
			}
		},

		async loadAvatarUrl() {
			this.avatarUrl = undefined
			if (this.source.photo) {
				const photoUrl = await this.source.getPhotoUrl()
				if (!photoUrl) {
					console.warn('contact has an invalid photo')
					// Invalid photo data
					return
				}
				this.avatarUrl = photoUrl
			} else if (this.source.url) {
				this.avatarUrl = `${this.source.url}?photo`
			}
		},

		/**
		 * Select this contact within the list
		 */
		selectContact() {
			// change url with router
			this.$router.push({
				name: 'contact',
				params: { selectedGroup: this.selectedGroup, selectedContact: this.source.key },
			})
		},
	},
}
</script>
<style lang="scss" scoped>

.envelope {
	.app-content-list-item-icon {
		height: 40px; // To prevent some unexpected spacing below the avatar
	}

	&__subtitle {
		display: flex;
		gap: 4px;

		&__subject {
			color: var(--color-main-text);
			line-height: 130%;
			overflow: hidden;
			text-overflow: ellipsis;
		}
	}
}

.list-item-style {
	list-style: none;
}

</style>
<style lang="scss">
.contacts-list__item-wrapper {
	&[draggable='true'] .avatardiv * {
		cursor: move !important;
	}

	&[draggable='false'] .avatardiv * {
		cursor: not-allowed !important;
	}
}
</style>