summaryrefslogtreecommitdiffstats
path: root/src/components/ContactDetails.vue
blob: 95de110d0e8998ebf14cef39ac8663032fffff3c (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
<!--
  - @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 -->
				<div id="contact-header-avatar">
					<div class="contact-avatar-background" />
					<img v-if="contact.photo">
					<input id="contact-avatar-upload" type="file" class="hidden"
						accept="image/*">
					<label v-tooltip.auto="t('contacts', 'Upload a new picture')" for="contact-avatar-upload" class="icon-upload-white" />
				</div>

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

				<!-- actions -->
				<div id="contact-header-actions">
					<div v-click-outside="closeMenu" class="menu-icon icon-more-white" @click="toggleMenu" />
					<div :class="{ 'open': openedMenu }" class="popovermenu">
						<popover-menu :menu="contactActions" />
					</div>
				</div>
			</header>

			<!-- contact details -->
			<section class="contact-details">
				<contact-details-property v-for="(property, index) in sortedProperties" :key="index" :index="index"
					:sorted-properties="sortedProperties" :property="property" :contact="contact"
					@updatedcontact="updateContact" />
			</section>
		</template>
	</div>
</template>

<script>

import ICAL from 'ical.js'
import ClickOutside from 'vue-click-outside'
import Vue from 'vue'
import VTooltip from 'v-tooltip'
import debounce from 'debounce'
import Contact from '../models/contact'
import rfcProps from '../models/rfcProps.js'

import popoverMenu from './core/popoverMenu'
import contactDetailsProperty from './ContactDetails/ContactDetailsProperty'

Vue.use(VTooltip)

export default {
	name: 'ContactDetails',

	components: {
		popoverMenu,
		contactDetailsProperty
	},

	directives: {
		ClickOutside
	},

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

	data() {
		return {
			contact: undefined,
			openedMenu: false
		}
	},

	computed: {
		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
		 */
		contactActions() {
			let actions = [
				{
					icon: 'icon-download',
					text: t('contacts', 'Download'),
					href: this.contact.url
				}
			]
			if (this.contact.addressbook.enabled) {
				actions.push({
					icon: 'icon-delete',
					text: t('contacts', 'Delete'),
					action: this.deleteContact
				})
			}

			return actions
		},

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

	watch: {
		// url changed, get and show selected contact
		uid: function() {
			this.updateLocalContact()
		},
		// done loading, check if the provided uid is valid and open details
		loading: function() {
			if (this.uid) {
				this.updateLocalContact()
			}
		}
	},

	methods: {

		/**
		 * Fetch the selected contact from the store
		 * and store it as a local data for editing
		 */
		updateLocalContact() {
			// create new local instance of this contact
			let contact = this.$store.getters.getContact(this.uid)
			this.contact = new Contact(ICAL.stringify(contact.jCal), contact.addressbook)
		},

		/**
		 * Executed on the 'updatedcontact' event
		 * Send the local clone of contact to the store
		 */
		updateContact() {
			this.$store.dispatch('updateContact', this.contact)
		},
		/**
		 * 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
		}
	}

}
</script>