summaryrefslogtreecommitdiffstats
path: root/src/components/ContactDetails/ContactDetailsProperty.vue
blob: 552629cf3c71953d241a0c214ef86d497a1b4149 (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
<!--
  - @copyright Copyright (c) 2018 John Molakvoæ <skjnldsv@protonmail.com>
  -
  - @author John Molakvoæ <skjnldsv@protonmail.com>
  - @author Richard Steinmetz <richard@steinmetz.cloud>
  -
  - @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>
	<!-- If not in the rfcProps then we don't want to display it -->
	<component :is="componentInstance"
		ref="component"
		:select-type.sync="selectType"
		:prop-model="propModel"
		:value.sync="value"
		:is-first-property="isFirstProperty"
		:property="property"
		:is-last-property="isLastProperty"
		:class="{
			'property--last': isLastProperty,
			[`property-${propName}`]: true,
		}"
		:local-contact="localContact"
		:prop-name="propName"
		:prop-type="propType"
		:options="sortedModelOptions"
		:is-read-only="isReadOnly"
		:bus="bus"
		:is-multiple="isMultiple"
		@delete="onDelete" />
</template>

<script>
import { Property } from 'ical.js'
import rfcProps from '../../models/rfcProps.js'
import Contact from '../../models/contact.js'

import OrgChartsMixin from '../../mixins/OrgChartsMixin.js'
import PropertyText from '../Properties/PropertyText.vue'
import PropertyMultipleText from '../Properties/PropertyMultipleText.vue'
import PropertyDateTime from '../Properties/PropertyDateTime.vue'
import PropertySelect from '../Properties/PropertySelect.vue'

export default {
	name: 'ContactDetailsProperty',

	mixins: [
		OrgChartsMixin,
	],

	props: {
		property: {
			type: Property,
			default: true,
		},

		/**
		 * Is it the first property of its kind
		 */
		isFirstProperty: {
			type: Boolean,
			default: false,
		},
		/**
		 * Is it the last property of its kind
		 */
		isLastProperty: {
			type: Boolean,
			default: false,
		},

		contact: {
			type: Contact,
			default: null,
		},
		localContact: {
			type: Contact,
			default: null,
		},
		contacts: {
			type: Array,
			default: () => [],
		},
		bus: {
			type: Object,
			required: true,
		},
		isReadOnly: {
			type: Boolean,
			required: true,
		},
	},

	computed: {
		// dynamically load component based on property type
		componentInstance() {
			// dynamic matching
			if (this.property.isMultiValue && this.propType === 'text') {
				return PropertyMultipleText
			} else if (this.propType && ['date-and-or-time', 'date-time', 'time', 'date'].indexOf(this.propType) > -1) {
				return PropertyDateTime
			} else if (this.propType && this.propType === 'select') {
				return PropertySelect
			} else if (this.propType && this.propType !== 'unknown') {
				return PropertyText
			}
			return PropertyText
		},

		// rfc properties list
		properties() {
			return rfcProps.properties
		},

		/**
		 * Return if property is multiple
		 *
		 * @return {boolean}
		 */
		isMultiple() {
			return this.properties[this.property.name].multiple
		},

		/**
		 * Return the type of the prop e.g. FN
		 *
		 * @return {string}
		 */
		propName() {
			// ! is this a ITEMXX.XXX property??
			if (this.propGroup[1]) {
				return this.propGroup[1]
			}

			return this.property.name
		},

		/**
		 * Return the type or property
		 *
		 * @see src/models/rfcProps
		 * @return {string}
		 */
		propType() {
			// if we have a force type set, use it!
			if (this.propModel && this.propModel.force) {
				return this.propModel.force
			}

			return this.property.getDefaultType()
		},

		/**
		 * RFC template matching this property
		 *
		 * @see src/models/rfcProps
		 * @return {object}
		 */
		propModel() {
			return this.properties[this.propName]
		},

		/**
		 * Remove duplicate name amongst options
		 * but make sure to include the selected one
		 * in the final list
		 *
		 * @return {object[]}
		 */
		sortedModelOptions() {
			if (!this.propModel.options) {
				return []
			}

			if (typeof this.propModel.options === 'function') {
				return this.propModel.options({
					contact: this.contact,
					$store: this.$store,
					selectType: this.selectType,
				})
			} else {
				return this.propModel.options.reduce((list, option) => {
					if (!list.find(search => search.name === option.name)) {
						list.push(option)
					}
					return list
				}, this.selectType ? [this.selectType] : [])
			}
		},

		/**
		 * Return the id and type of a property group
		 * e.g ITEMXX.tel => ['ITEMXX', 'tel']
		 *
		 * @return {Array}
		 */
		propGroup() {
			return this.property.name.split('.')
		},

		/**
		 * Return the associated X-ABLABEL if any
		 *
		 * @return {Property}
		 */
		propLabel() {
			return this.localContact.vCard.getFirstProperty(`${this.propGroup[0]}.x-ablabel`)
		},

		/**
		 * Returns the closest match to the selected type
		 * or return the default selected as a new object if
		 * none exists
		 *
		 * @return Object|null
		 */
		selectType: {
			get() {
				// ! if ABLABEL is present, this is a priority
				if (this.propLabel) {
					return {
						id: this.propLabel.name,
						name: this.propLabel.getFirstValue(),
					}
				}
				if (this.propModel && this.propModel.options && this.type) {

					const selectedType = this.type
						// vcard 3.0 save pref alongside TYPE
						.filter(type => type !== 'pref')
						// we only use uppercase strings
						.map(str => str.toUpperCase())

					// Compare array and score them by how many matches they have to the selected type
					// sorting directly is cleaner but slower
					// https://jsperf.com/array-map-and-intersection-perf
					const matchingTypes = this.propModel.options
						.map(type => {
							let score = 0
							const types = type.id.split(',') // "WORK,HOME" => ['WORK', 'HOME']

							if (types.length === selectedType.length) {
								// additional point for same length
								score++
							}

							const intersection = types.filter(value => selectedType.includes(value))
							score = score + intersection.length

							return { type, score }
						})

					// Sort by score, filtering out the null score and selecting the first match
					const matchingType = matchingTypes
						.sort((a, b) => b.score - a.score)
						.filter(type => type.score > 0)[0]

					if (matchingType) {
						return matchingType.type
					}
				}
				if (this.type) {
					// vcard 3.0 save pref alongside TYPE
					const selectedType = this.type
						.filter(type => type !== 'pref')
						.join(',')
					if (selectedType.trim() !== '') {
						return {
							id: selectedType,
							name: selectedType,
						}
					}
				}
				return null
			},
			set(data) {
				// Skip setting type if select is cleared
				if (!data) {
					return
				}

				// if a custom label exists and this is the one we selected
				if (this.propLabel && data.id === this.propLabel.name) {
					this.propLabel.setValue(data.name)
					// only one can coexist
					this.type = []
				} else {
					// ical.js take types as arrays
					this.type = data.id.split(',')
					// only one can coexist
					this.localContact.vCard.removeProperty(`${this.propGroup[0]}.x-ablabel`)

					// checking if there is any other property in this group
					const groups = this.localContact.jCal[1]
						.map(prop => prop[0])
						.filter(name => name.startsWith(`${this.propGroup[0]}.`))
					if (groups.length === 1) {
						// then this prop is the latest of its group
						// -> converting back to simple prop
						// eslint-disable-next-line vue/no-mutating-props
						this.property.jCal[0] = this.propGroup[1]
					}
				}
			},

		},

		// property value(s)
		value: {
			get() {
				if (this.property.isMultiValue) {
					// differences between values types :x;x;x;x;x and x,x,x,x,x
					return this.property.isStructuredValue
						? this.property.getValues()[0]
						: this.property.getValues()
				}
				if (this.propName === 'x-managersname') {
					if (this.property.getParameter('uid')) {
						return this.property.getParameter('uid') + '~' + this.contact.addressbook.id
					}
					// Try to find the matching contact by display name
					// TODO: this only *shows* the display name but doesn't assign the missing UID
					const displayName = this.property.getFirstValue()
					const other = this.otherContacts(this.contact).find(contact => contact.displayName === displayName)
					return other?.key
				}
				return this.property.getFirstValue()
			},
			set(data) {
				if (this.property.isMultiValue) {
					// differences between values types :x;x;x;x;x and x,x,x,x,x
					this.property.isStructuredValue
						? this.property.setValues([data])
						: this.property.setValues(data)
				} else {
					if (this.propName === 'x-managersname') {
						const manager = this.$store.getters.getContact(data)
						this.property.setValue(manager.displayName)
						this.property.setParameter('uid', manager.uid)
					} else {
						this.property.setValue(data)
					}
				}
			},
		},

		// property meta type
		type: {
			get() {
				const type = this.property.getParameter('type')
				// ensure we have an array
				if (type) {
					return Array.isArray(type) ? type : [type]
				}
				return null
			},
			set(data) {
				this.property.setParameter('type', data)
			},
		},

		// property meta pref
		pref: {
			get() {
				return this.property.getParameter('pref')
			},
			set(data) {
				this.property.setParameter('pref', data)
			},
		},
	},

	created() {
		this.bus.$on('focus-prop', this.onFocusProp)
	},

	destroyed() {
		this.bus.$off('focus-prop', this.onFocusProp)
	},

	methods: {
		/**
		 * Focus first input element of the new prop
		 *
		 * @param {string} id the id of the property
		 */
		onFocusProp(id) {
			if (id === this.propName && this.isLastProperty) {
				this.$nextTick(() => {
					const inputs = this.$refs.component.$el.querySelectorAll('input, textarea')
					if (inputs === undefined || inputs.length === 0) {
						console.warn('no input to focus found')
					} else {
						inputs[0].focus()
					}
				})
			}
		},

		/**
		 * Delete this property
		 */
		onDelete() {
			this.localContact.vCard.removeProperty(this.property)
		},
	},
}
</script>