summaryrefslogtreecommitdiffstats
path: root/src/components/ContactDetails.vue
blob: 3033f270ad2846dd54c893c6825eec5c24584a33 (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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
<!--
  - @copyright Copyright (c) 2018 John Molakvoæ <skjnldsv@protonmail.com>
  -
  - @author John Molakvoæ <skjnldsv@protonmail.com>
  -
  - @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/>.
  -
  -->

<template>
	<div id="contact-details" class="app-content-details">
		<!-- nothing selected or contact not found -->
		<div v-if="!contact && !loading" id="emptycontent">
			<div class="icon-contacts" />
			<h2>{{ t('contacts', 'No contact selected') }}</h2>
			<p>{{ t('contacts', 'Select a contact on the list to begin') }}</p>
		</div>

		<!-- loading -->
		<div v-else-if="!contact && loading" id="emptycontent">
			<div class="icon-contacts" />
			<h2>{{ t('contacts', 'Loading') }}</h2>
		</div>

		<template v-else>
			<!-- contact header -->
			<header :style="{ 'backgroundColor': colorAvatar }">
				<!-- avatar and upload photo -->
				<contact-avatar :contact="contact" />
				<!-- QUESTION: is it better to pass contact as a prop or get it from the store inside
				contact-avatar ?  :avatar="contact.photo"-->

				<!-- fullname, org, title -->
				<div id="contact-header-infos">
					<h2>
						<input id="contact-fullname" ref="fullname" v-model="contact.fullName"
							:readonly="contact.addressbook.readOnly" :placeholder="t('contacts', 'Name')" type="text"
							autocomplete="off" autocorrect="off" spellcheck="false"
							name="fullname"
							@input="debounceUpdateContact" @click="selectInput">
					</h2>
					<div id="details-org-container">
						<input id="contact-org" v-model="contact.org" :readonly="contact.addressbook.readOnly"
							:placeholder="t('contacts', 'Company')" type="text" autocomplete="off"
							autocorrect="off" spellcheck="false" name="org"
							@input="debounceUpdateContact">
						<input id="contact-title" v-model="contact.title" :readonly="contact.addressbook.readOnly"
							:placeholder="t('contacts', 'Title')" type="text" autocomplete="off"
							autocorrect="off" spellcheck="false" name="title"
							@input="debounceUpdateContact">
					</div>
				</div>

				<!-- actions -->
				<div id="contact-header-actions">
					<a v-if="loadingUpdate || warning"
						v-tooltip.bottom="{
							content: warning ? warning.msg : '',
							trigger: 'hover focus'
						}"
						:class="{'icon-loading-small': loadingUpdate,
							[`${warning.icon}`]: warning}" class="header-icon" href="#" />
					<div v-if="conflict" v-tooltip="{
							content: conflict,
							show: true,
							trigger: 'manual',
						}" class="header-icon header-icon--pulse icon-history-white"
						@click="refreshContact" />
					<div class="menu-icon">
						<div v-click-outside="closeMenu" class="header-icon icon-more-white" @click="toggleMenu" />
						<div :class="{ 'open': openedMenu }" class="popovermenu">
							<popover-menu :menu="contactActions" />
						</div>
					</div>
				</div>
			</header>

			<!-- contact details loading -->
			<section v-if="loadingData" class="icon-loading contact-details" />

			<!-- contact details -->
			<section v-else class="contact-details">
				<!-- properties iteration -->
				<!-- using contact.key in the key and index as key to avoid conflicts between similar data and exact key -->
				<contact-property v-for="(property, index) in sortedProperties" :key="`${index}-${contact.key}-${property.name}`" :index="index"
					:sorted-properties="sortedProperties" :property="property" :contact="contact"
					@updatedcontact="updateContact" />

				<!-- addressbook change select - no last property because class is not applied here,
					empty property because this is a required prop on regular property-select. But since
					we are hijacking this... (this is supposed to be used with a ICAL.property, but to avoid code
					duplication, we created a fake propModel and property with our own options here) -->
				<property-select :prop-model="addressbookModel" :select-type.sync="addressbook" value=""
					:is-first-property="true"
					:is-last-property="true" :property="{}" class="property--addressbooks property--last" />

				<!-- Groups always visible -->
				<property-groups :prop-model="groupsModel" :value.sync="groups" :contact="contact"
					:is-read-only="isReadOnly" class="property--groups property--last" />

				<!-- new property select -->
				<add-new-prop v-if="!isReadOnly" :contact="contact" />
			</section>
		</template>
	</div>
</template>

<script>
import debounce from 'debounce'

import rfcProps from 'Models/rfcProps'

import ContactProperty from './ContactDetails/ContactDetailsProperty'
import AddNewProp from './ContactDetails/ContactDetailsAddNewProp'
import PropertySelect from './Properties/PropertySelect'
import PropertyGroups from './Properties/PropertyGroups'
import ContactAvatar from './ContactDetails/ContactDetailsAvatar'

export default {
	name: 'ContactDetails',

	components: {
		ContactProperty,
		PropertySelect,
		PropertyGroups,
		AddNewProp,
		ContactAvatar
	},

	props: {
		loading: {
			type: Boolean,
			default: true
		},
		contactKey: {
			type: String,
			default: undefined
		}
	},

	data() {
		return {
			/**
			 * Local off-store clone of the selected contact for edition
			 * because we can't edit contacts data outside the store.
			 * Every change will be dispatched and updated on the real
			 * store contact after a debounce.
			 */
			localContact: undefined,
			loadingData: true,
			loadingUpdate: false,
			openedMenu: false
		}
	},

	computed: {
		isReadOnly() {
			if (this.contact.addressbook) {
				return this.contact.addressbook.readOnly
			}
			return false
		},

		/**
		 * Warning messages
		 *
		 * @returns {Object|boolean}
		 */
		warning() {
			if (!this.contact.dav) {
				return {
					icon: 'icon-error-white header-icon--pulse',
					msg: t('contacts', 'This contact is not yet synced. Edit it to save it to the server.')
				}
			} else if (this.isReadOnly) {
				return {
					icon: 'icon-eye-white',
					msg: t('contacts', 'This contact is in read-only mode. You do not have permission to edit this contact.')
				}
			}
			return false
		},

		/**
		 * Conflict message
		 *
		 * @returns {string|boolean}
		 */
		conflict() {
			if (this.contact.conflict) {
				return t('contacts', 'The contact you were trying to edit has changed. Please manually refresh the contact. Any further edits will be discarded.')
			}
			return false
		},

		/**
		 * Contact color based on uid
		 *
		 * @returns {string}
		 */
		colorAvatar() {
			try {
				let color = this.contact.uid.toRgb()
				return `rgb(${color.r}, ${color.g}, ${color.b})`
			} catch (e) {
				return 'grey'
			}
		},

		/**
		 * Header actions for the contact
		 *
		 * @returns {Array}
		 */
		contactActions() {
			let actions = [
				{
					icon: 'icon-download',
					text: t('contacts', 'Download'),
					href: this.contact.url
				}
			]
			if (!this.contact.addressbook.readOnly) {
				actions.push({
					icon: 'icon-delete',
					text: t('contacts', 'Delete'),
					action: this.deleteContact
				})
			}

			return actions
		},

		/**
		 * Contact properties copied and sorted by rfcProps.fieldOrder
		 *
		 * @returns {Array}
		 */
		sortedProperties() {
			return this.contact.properties.slice(0).sort((a, b) => {
				return (
					rfcProps.fieldOrder.indexOf(a.name) - rfcProps.fieldOrder.indexOf(b.name)
				)
			})
		},

		/**
		 * Fake model to use the propertySelect component
		 *
		 * @returns {Object}
		 */
		addressbookModel() {
			return {
				readableName: t('contacts', 'Addressbook'),
				icon: 'icon-address-book',
				options: this.addressbooksOptions
			}
		},

		/**
		 * Usable addressbook object linked to the local contact
		 *
		 * @param {string} [addressbook] set the addressbook object
		 * @returns {string}
		 */
		addressbook: {
			get: function() {
				return this.addressbooksOptions
					.find(addressbook => addressbook.id === this.contact.addressbook.id)
			},
			set: function(addressbook) {
				this.moveContactToAddressbook(addressbook.id)
			}
		},

		/**
		 * Fake model to use the propertyGroups component
		 *
		 * @returns {Object}
		 */
		groupsModel() {
			return {
				readableName: t('contacts', 'Groups'),
				icon: 'icon-contacts'
			}
		},

		/**
		 * Usable groups object linked to the local contact
		 *
		 * @param {string[]} data An array of groups
		 * @returns {Array}
		 */
		groups: {
			get: function() {
				return this.contact.groups
			},
			set: function(data) {
				this.contact.groups = data
				this.updateContact()
			}
		},

		/**
		 * Store getters filtered and mapped to usable object
		 *
		 * @returns {Array}
		 */
		addressbooksOptions() {
			return this.addressbooks
				.filter(addressbook => !addressbook.readOnly && addressbook.enabled)
				.map(addressbook => {
					return {
						id: addressbook.id,
						name: addressbook.displayName
					}
				})
		},

		// store getter
		addressbooks() {
			return this.$store.getters.getAddressbooks
		},
		contact() {
			return this.$store.getters.getContact(this.contactKey)
		}
	},

	watch: {
		contact: function() {
			if (this.contactKey) {
				this.selectContact(this.contactKey)
			}
		}
	},

	beforeMount() {
		// load the desired data if we already selected a contact
		if (this.contactKey) {
			this.selectContact(this.contactKey)
		}
	},

	methods: {
		/**
		 * Executed on the 'updatedcontact' event
		 * Send the local clone of contact to the store
		 */
		async updateContact() {
			this.loadingUpdate = true
			await this.$store.dispatch('updateContact', this.contact)
			this.loadingUpdate = false
		},

		/**
		 * Debounce the contact update for the header props
		 * photo, fn, org, title
		 */
		debounceUpdateContact: debounce(function(e) {
			this.updateContact()
		}, 500),

		// menu handling
		closeMenu() {
			this.openedMenu = false
		},
		toggleMenu() {
			this.openedMenu = !this.openedMenu
		},

		/**
		 * Select the text in the input if it is still set to 'new Contact'
		 */
		selectInput() {
			if (this.$refs.fullname && this.$refs.fullname.value === t('contacts', 'New contact')) {
				this.$refs.fullname.select()
			}
		},

		/**
		 * Select a contac, and update the localContact
		 * Fetch updated data if necessary
		 * Scroll to the selected contact if exists
		 *
		 * @param {string} key the contact key
		 */
		selectContact(key) {
			// local version of the contact
			this.loadingData = true
			let contact = this.$store.getters.getContact(key)

			if (contact) {
				// if contact exists AND if exists on server
				if (contact.dav) {
					this.$store.dispatch('fetchFullContact', { contact })
						.then(() => {
							// create empty contact and copy inner data
							let localContact = Object.assign(
								Object.create(Object.getPrototypeOf(contact)),
								contact
							)
							this.localContact = localContact
							this.loadingData = false
						})
						.catch((error) => {
							OC.Notification.showTemporary(t('contacts', 'The contact doesn\'t exists anymore on the server.'))
							console.error(error)
							// trigger a local deletion from the store only
							this.$store.dispatch('deleteContact', { contact: this.contact, dav: false })
						})
				} else {
					// create empty contact and copy inner data
					// wait for an update to really push the contact on the server!
					this.localContact = Object.assign(
						Object.create(Object.getPrototypeOf(contact)),
						contact
					)
					this.loadingData = false
				}

				// scroll to selected contact if any
				let list = document.getElementById('contacts-list')
				let item = document.querySelector('#' + btoa(contact.key).slice(0, -2))
				let isAbove = list.scrollTop > item.offsetTop
				let isUnder = item.offsetTop + item.offsetHeight > list.scrollTop + list.offsetHeight
				// check if contact outside visible list area
				if (item && (isAbove || isUnder)) {
					list.scrollTo(0, item.offsetTop - item.offsetHeight / 2)
				}
			}
		},

		/**
		 * Dispatch contact deletion request
		 */
		deleteContact() {
			this.$store.dispatch('deleteContact', { contact: this.contact })
		},

		/**
		 * Move contact to the specified addressbook
		 *
		 * @param {string} addressbookId the desired addressbook ID
		 */
		async moveContactToAddressbook(addressbookId) {
			let addressbook = this.addressbooks.find(search => search.id === addressbookId)
			this.loadingUpdate = true
			if (addressbook) {
				try {
					const contact = await this.$store.dispatch('moveContactToAddressbook', {
						// we need to use the store contact, not the local contact
						// using this.contact and not this.localContact
						contact: this.contact,
						addressbook
					})
					// select the contact again
					this.$router.push({
						name: 'contact',
						params: {
							selectedGroup: this.$route.params.selectedGroup,
							selectedContact: contact.key
						}
					})
				} catch (error) {
					console.error(error)
				} finally {
					this.loadingUpdate = false
				}
			}
		},

		/**
		 * Refresh the data of a contact
		 */
		refreshContact() {
			this.$store.dispatch('fetchFullContact', { contact: this.contact, etag: this.conflict })
				.then(() => {
					this.contact.conflict = false
				})
		}
	}
}
</script>