summaryrefslogtreecommitdiffstats
path: root/src/components/ContactDetails/ContactDetailsAddNewProp.vue
blob: 3d882ad65de2d754860bfa27125faa696e5de41b (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
<!--
  - @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>
	<div class="property__row">
		<!-- Dummy label to keep the layout -->
		<div class="property__label" />

		<!-- Extra div container to fix the poper position -->
		<div class="property__value">
			<Actions menu-align="right"
				event=""
				type="secondary"
				:menu-name="t('contacts', 'Add more info')"
				@click.native.prevent>
				<template #icon>
					<IconAdd :size="20" />
				</template>
				<template v-if="!moreActionsOpen">
					<ActionButton v-for="option in availablePrimaryProperties"
						:key="option.id"
						class="action--primary"
						:close-after-click="true"
						@click.prevent="addProp(option.id)">
						<template #icon>
							<PropertyTitleIcon :icon="option.icon" />
						</template>
						{{ option.name }}
					</ActionButton>
					<ActionButton :close-after-click="false"
						@click="moreActionsOpen=true">
						<template #icon>
							<DotsHorizontalIcon :title="t('contacts', 'More fields')"
								:size="20" />
						</template>
						{{ t('contacts', 'More fields') }}
					</ActionButton>
				</template>
				<template v-if="moreActionsOpen">
					<ActionButton :close-after-click="false"
						@click="moreActionsOpen=false">
						<template #icon>
							<ChevronLeft :title="t('contacts', 'More fields')"
								:size="20" />
							{{ t('contacts', 'More fields') }}
						</template>
					</ActionButton>
					<ActionButton v-for="option in availableSecondaryProperties"
						:key="option.id"
						class="action--primary"
						:close-after-click="true"
						@click.prevent="addProp(option.id)">
						<template #icon>
							<PropertyTitleIcon :icon="option.icon" />
						</template>
						{{ option.name }}
					</ActionButton>
				</template>
			</Actions>
		</div>

		<!-- Dummy actions to keep the layout -->
		<div class="property__actions" />
	</div>
</template>

<script>
import OrgChartsMixin from '../../mixins/OrgChartsMixin.js'
import { NcActions as Actions, NcActionButton as ActionButton } from '@nextcloud/vue'
import Contact from '../../models/contact.js'
import rfcProps from '../../models/rfcProps.js'
import ICAL from 'ical.js'
import PropertyTitleIcon from '../Properties/PropertyTitleIcon.vue'
import ChevronLeft from 'vue-material-design-icons/ChevronLeft.vue'
import DotsHorizontalIcon from 'vue-material-design-icons/DotsHorizontal.vue'
import IconAdd from 'vue-material-design-icons/Plus.vue'

export default {
	name: 'ContactDetailsAddNewProp',

	components: {
		IconAdd,
		PropertyTitleIcon,
		Actions,
		ActionButton,
		ChevronLeft,
		DotsHorizontalIcon,
	},

	mixins: [
		OrgChartsMixin,
	],

	props: {
		contact: {
			type: Contact,
			default: null,
		},
		bus: {
			type: Object,
			required: true,
		},
	},
	data() {
		return {
			moreActionsOpen: false,
		}
	},
	computed: {
		/**
		 * Rfc props
		 *
		 * @return {object}
		 */
		properties() {
			return rfcProps.properties
		},

		/**
		 * Rfc props
		 *
		 * @return string[]
		 */
		propertiesOrder() {
			return rfcProps.fieldOrder
		},

		/**
		 * List of properties that the contact already have
		 *
		 * @return {string[]}
		 */
		usedProperties() {
			return this.contact.jCal[1].map(prop => prop[0])
		},

		/**
		 * List of every primary properties you are allowed to add
		 * on this contact
		 *
		 * @return {object[]}
		 */
		availablePrimaryProperties() {
			return Object.keys(this.properties)
				// show primary or secondary props
				.filter(key => this.properties[key].primary)
				// usable array of objects
				.map(key => {
					return {
						id: key,
						name: this.properties[key].readableName,
						icon: this.properties[key].icon,
					}
				}).sort((a, b) => this.propertiesOrder.indexOf(a.id) - this.propertiesOrder.indexOf(b.id))
		},
		/**
		 * List of every secondary properties you are allowed to add
		 * on this contact
		 *
		 * @return {object[]}
		 */
		availableSecondaryProperties() {
			return Object.keys(this.properties)
				// show primary or secondary props
				.filter(key => !this.properties[key].primary)
				// usable array of objects
				.map(key => {
					return {
						id: key,
						name: this.properties[key].readableName,
						icon: this.properties[key].icon,
					}
				}).sort((a, b) => a.name.localeCompare(b.name))
		},
	},

	created() {
		this.bus.$on('add-prop', this.addProp)
	},

	destroyed() {
		this.bus.$off('add-prop', this.addProp)
	},

	methods: {
		/**
		 * Add a new prop to the contact
		 *
		 * @param {string} id the id of the property. e.g fn
		 */
		async addProp(id) {
			if (this.usedProperties.includes(id) && !this.properties[id].multiple) {
				this.bus.$emit('focus-prop', id)
				return
			}

			if (id === 'x-managersname') {
				const others = this.otherContacts(this.contact)
				if (others.length === 1) {
					// Pick the first and only other contact
					await this.contact.vCard.addPropertyWithValue(id, others[0].key)
					await this.$store.dispatch('updateContact', this.contact)
				} else {
					await this.contact.vCard.addPropertyWithValue(id, '')
				}
			} else if (this.properties[id] && this.properties[id].defaultjCal
				&& this.properties[id].defaultjCal[this.contact.version]) {
				const defaultjCal = this.properties[id].defaultjCal[this.contact.version]
				const property = new ICAL.Property([id, ...defaultjCal])
				await this.contact.vCard.addProperty(property)
			} else {
				const defaultData = this.properties[id].defaultValue
				let defaultValue = defaultData ? defaultData.value : ''
				if (Array.isArray(defaultValue)) {
					defaultValue = [...defaultValue]
				}
				const property = await this.contact.vCard.addPropertyWithValue(id, defaultValue)
				if (defaultData && defaultData.type) {
					property.setParameter('type', defaultData.type)
				}
			}
			this.moreActionsOpen = false
			this.bus.$emit('focus-prop', id)
		},
	},
}
</script>