-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathContactDetails.vue
1156 lines (1063 loc) Β· 30.9 KB
/
ContactDetails.vue
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!--
- SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<AppContentDetails>
<!-- nothing selected or contact not found -->
<EmptyContent v-if="!contact"
class="empty-content"
:name="t('contacts', 'No contact selected')"
:description="t('contacts', 'Select a contact on the list to begin')">
<template #icon>
<IconContact :size="20" />
</template>
</EmptyContent>
<!-- TODO: add empty content while this.loadingData === true -->
<template v-else>
<!-- contact header -->
<DetailsHeader>
<!-- avatar and upload photo -->
<template #avatar>
<ContactAvatar :contact="contact"
:is-read-only="isReadOnly"
:reload-bus="reloadBus"
@update-local-contact="updateLocalContact" />
</template>
<!-- fullname -->
<template #title>
<div v-if="isReadOnly" class="contact-title">
{{ contact.fullName }}
</div>
<input v-else
id="contact-fullname"
ref="fullname"
v-model="contact.fullName"
:placeholder="t('contacts', 'Name')"
type="text"
autocomplete="off"
autocorrect="off"
spellcheck="false"
name="fullname"
@click="selectInput">
</template>
<!-- org, title -->
<template #subtitle>
<template v-if="isReadOnly">
<span v-html="formattedSubtitle" />
</template>
<template v-else>
<input id="contact-title"
v-model="contact.title"
:placeholder="t('contacts', 'Title')"
type="text"
autocomplete="off"
autocorrect="off"
spellcheck="false"
name="title">
<input id="contact-org"
v-model="contact.org"
:placeholder="t('contacts', 'Company')"
type="text"
autocomplete="off"
autocorrect="off"
spellcheck="false"
name="org">
</template>
</template>
<template #quick-actions>
<div v-if="!editMode && !loadingData">
<Actions :inline="6"
type="secondary">
<ActionButton v-if="isTalkEnabled && isInSystemAddressBook"
:aria-label="(t('contacts', 'Go to talk conversation'))"
:name="(t('contacts', 'Go to talk conversation'))"
class="icon-talk quick-action"
:href="callUrl" />
<ActionButton v-if="profilePageLink"
class="quick-action"
:aria-label="(t('contacts','View profile'))"
:name="(t('contacts','View profile'))"
:href="profilePageLink">
<template #icon>
<IconAccount :size="20" />
</template>
</ActionButton>
<ActionLink v-for="emailAddress in emailAddressList"
:key="emailAddress"
class="quick-action"
:href="'mailto:' + emailAddress">
<template #icon>
<IconMail :size="20" />
</template>
{{ emailAddress }}
</ActionLink>
<ActionLink v-for="phoneNumber in phoneNumberList"
:key="phoneNumber"
class="quick-action"
:href="'tel:' + phoneNumber">
<template #icon>
<IconCall :size="20" />
</template>
{{ phoneNumber }}
</ActionLink>
</Actions>
</div>
</template>
<!-- actions -->
<template #actions>
<!-- warning message -->
<component :is="warning.icon"
v-if="warning"
v-tooltip.bottom="{
content: warning ? warning.msg : '',
trigger: 'hover focus'
}"
class="header-icon"
:classes="warning.classes" />
<!-- conflict message -->
<div v-if="conflict"
v-tooltip="{
content: conflict,
show: true,
trigger: 'manual',
}"
class="header-icon header-icon--pulse icon-history"
@click="refreshContact" />
<!-- repaired contact message -->
<div v-if="fixed"
v-tooltip="{
content: t('contacts', 'This contact was broken and received a fix. Please review the content and click here to save it.'),
show: true,
trigger: 'manual',
}"
class="header-icon header-icon--pulse icon-up"
@click="updateContact" />
<!-- edit and save buttons -->
<template v-if="!addressbookIsReadOnly">
<NcButton v-if="!editMode"
:type="isMobile ? 'secondary' : 'tertiary'"
@click="editMode = true">
<template #icon>
<PencilIcon :size="20" />
</template>
{{ t('contacts', 'Edit') }}
</NcButton>
<NcButton v-else
type="secondary"
:disabled="loadingUpdate"
@click="onSave">
<template #icon>
<IconLoading v-if="loadingUpdate" :size="20" />
<CheckIcon v-else :size="20" />
</template>
{{ t('contacts', 'Save') }}
</NcButton>
</template>
</template>
<!-- menu actions -->
<template #actions-menu>
<ActionLink :href="contact.url"
:download="`${contact.displayName}.vcf`">
<template #icon>
<IconDownload :size="20" />
</template>
{{ t('contacts', 'Download') }}
</ActionLink>
<!-- user can clone if there is at least one option available -->
<ActionButton v-if="isReadOnly && addressbooksOptions.length > 0"
ref="cloneAction"
:close-after-click="true"
@click="cloneContact">
<template #icon>
<IconCopy :size="20" />
</template>
{{ t('contacts', 'Clone contact') }}
</ActionButton>
<ActionButton :close-after-click="true" @click="showQRcode">
<template #icon>
<IconQr :size="20" />
</template>
{{ t('contacts', 'Generate QR Code') }}
</ActionButton>
<ActionButton v-if="enableToggleBirthdayExclusion"
:close-after-click="true"
@click="toggleBirthdayExclusionForContact">
<template #icon>
<CakeIcon :size="20" />
</template>
{{ excludeFromBirthdayLabel }}
</ActionButton>
<ActionButton v-if="!addressbookIsReadOnly"
@click="deleteContact">
<template #icon>
<IconDelete :size="20" />
</template>
{{ t('contacts', 'Delete') }}
</ActionButton>
</template>
</DetailsHeader>
<!-- qrcode -->
<Modal v-if="qrcode"
id="qrcode-modal"
size="small"
:clear-view-delay="-1"
:name="contact.displayName"
:close-button-contained="false"
@close="closeQrModal">
<img :src="`data:image/svg+xml;base64,${qrcode}`"
:alt="t('contacts', 'Contact vCard as QR code')"
class="qrcode"
width="400">
</Modal>
<!-- pick addressbook when cloning contact -->
<Modal v-if="showPickAddressbookModal"
id="pick-addressbook-modal"
:clear-view-delay="-1"
:name="t('contacts', 'Pick an address book')"
@close="closePickAddressbookModal">
<NcSelect ref="pickAddressbook"
v-model="pickedAddressbook"
class="address-book"
:allow-empty="false"
:options="copyableAddressbooksOptions"
:placeholder="t('contacts', 'Select address book')"
track-by="id"
label="name" />
<button @click="closePickAddressbookModal">
{{ t('contacts', 'Cancel') }}
</button>
<button class="primary" @click="cloneContact">
{{ t('contacts', 'Clone contact') }}
</button>
</Modal>
<!-- contact details loading -->
<IconLoading v-if="loadingData" :size="20" class="contact-details" />
<!-- quick actions -->
<div v-else-if="!loadingData" class="contact-details-wrapper">
<!-- contact details -->
<section class="contact-details">
<!-- properties iteration -->
<!-- using contact.key in the key and index as key to avoid conflicts between similar data and exact key -->
<div v-for="(properties, name) in groupedProperties"
:key="name">
<ContactDetailsProperty v-for="(property, index) in properties"
:key="`${index}-${contact.key}-${property.name}`"
:is-first-property="index===0"
:is-last-property="index === properties.length - 1"
:property="property"
:contact="contact"
:local-contact="localContact"
:contacts="contacts"
:bus="bus"
:is-read-only="isReadOnly" />
</div>
</section>
<!-- 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) -->
<PropertySelect :prop-model="addressbookModel"
:options="addressbooksOptions"
:value.sync="addressbook"
:is-first-property="true"
:is-last-property="true"
:property="{}"
:hide-actions="true"
:is-read-only="isReadOnly"
class="property--addressbooks property--last" />
<!-- Groups always visible -->
<PropertyGroups :prop-model="groupsModel"
:value.sync="localContact.groups"
:contact="contact"
:is-read-only="isReadOnly"
class="property--groups property--last"
@update:value="updateGroups" />
</div>
<div v-if="nextcloudVersionAtLeast28 && !editMode" class="related-resources">
<NcRelatedResourcesPanel v-if="!filesPanelHasError"
provider-id="account"
resource-type="files"
:description="desc"
:limit="5"
:header="t('contacts', 'Media shares with you')"
:item-id="contact.uid"
:primary="true"
@has-resources="value => hasFilesResources = value"
@has-error="value => filesPanelHasError = value" />
<NcRelatedResourcesPanel v-if="!talkPanelHasError"
provider-id="account"
resource-type="talk"
:description="desc"
:limit="5"
:header="t('contacts', 'Talk conversations with you')"
:item-id="contact.uid"
:primary="true"
@has-resources="value => hasTalkResources = value"
@has-error="value => talkPanelHasError = value" />
<NcRelatedResourcesPanel v-if="!calendarPanelHasError"
provider-id="account"
resource-type="calendar"
:description="desc"
:limit="5"
:header="t('contacts', 'Calendar events with you')"
:item-id="contact.uid"
:primary="true"
@has-resources="value => hasCalendarResources = value"
@has-error="value => calendarPanelHasError = value" />
<NcRelatedResourcesPanel v-if="!deckPanelHasError"
provider-id="account"
resource-type="deck"
:description="desc"
:limit="5"
:header="t('contacts', 'Deck cards with you')"
:item-id="contact.uid"
:primary="true"
@has-resources="value => hasDeckResources = value"
@has-error="value => deckPanelHasError = value" />
<NcEmptyContent v-if="!hasRelatedResources && !loadingData"
:name="t('contacts', 'No shared items with this contact')">
<template #icon>
<FolderMultipleImage :size="20" />
</template>
</NcEmptyContent>
</div>
<!-- new property select -->
<AddNewProp v-if="!isReadOnly"
class="last-edit"
:bus="bus"
:contact="contact" />
<!-- Last modified-->
<PropertyRev v-if="contact.rev" :value="contact.rev" class="last-edit" />
</template>
</AppContentDetails>
</template>
<script>
import { showError } from '@nextcloud/dialogs'
import ICAL from 'ical.js'
import { getSVG } from '@shortcm/qr-image/lib/svg'
import mitt from 'mitt'
import {
NcActions as Actions,
NcActionButton as ActionButton,
NcActionLink as ActionLink,
NcAppContentDetails as AppContentDetails,
NcEmptyContent as EmptyContent,
NcModal as Modal,
NcSelect,
NcLoadingIcon as IconLoading,
NcButton,
NcRelatedResourcesPanel,
isMobile,
NcEmptyContent,
} from '@nextcloud/vue'
import IconContact from 'vue-material-design-icons/AccountMultiple.vue'
import IconDownload from 'vue-material-design-icons/Download.vue'
import IconDelete from 'vue-material-design-icons/Delete.vue'
import IconQr from 'vue-material-design-icons/Qrcode.vue'
import CakeIcon from 'vue-material-design-icons/Cake.vue'
import IconMail from 'vue-material-design-icons/Email.vue'
import IconCall from 'vue-material-design-icons/Phone.vue'
import IconMessage from 'vue-material-design-icons/MessageProcessing.vue'
import IconAccount from 'vue-material-design-icons/Account.vue'
import IconCopy from 'vue-material-design-icons/ContentCopy.vue'
import PencilIcon from 'vue-material-design-icons/Pencil.vue'
import CheckIcon from 'vue-material-design-icons/Check.vue'
import EyeCircleIcon from 'vue-material-design-icons/EyeCircle.vue'
import FolderMultipleImage from 'vue-material-design-icons/FolderMultipleImage.vue'
import rfcProps from '../models/rfcProps.js'
import validate from '../services/validate.js'
import AddNewProp from './ContactDetails/ContactDetailsAddNewProp.vue'
import ContactAvatar from './ContactDetails/ContactDetailsAvatar.vue'
import ContactDetailsProperty from './ContactDetails/ContactDetailsProperty.vue'
import DetailsHeader from './DetailsHeader.vue'
import PropertyGroups from './Properties/PropertyGroups.vue'
import PropertyRev from './Properties/PropertyRev.vue'
import PropertySelect from './Properties/PropertySelect.vue'
import { generateUrl } from '@nextcloud/router'
import { loadState } from '@nextcloud/initial-state'
import isTalkEnabled from '../services/isTalkEnabled.js'
const { profileEnabled } = loadState('user_status', 'profileEnabled', false)
export default {
name: 'ContactDetails',
components: {
Actions,
ActionButton,
ActionLink,
AddNewProp,
AppContentDetails,
ContactAvatar,
ContactDetailsProperty,
DetailsHeader,
EmptyContent,
IconContact,
IconMail,
IconMessage,
IconCall,
IconAccount,
IconDownload,
IconDelete,
IconQr,
CakeIcon,
IconCopy,
IconLoading,
PencilIcon,
CheckIcon,
Modal,
NcSelect,
PropertyGroups,
PropertyRev,
PropertySelect,
NcButton,
NcRelatedResourcesPanel,
NcEmptyContent,
FolderMultipleImage,
},
mixins: [isMobile],
props: {
contactKey: {
type: String,
default: undefined,
},
contacts: {
type: Array,
default: () => [],
},
reloadBus: {
type: Object,
required: true,
},
desc: {
type: String,
required: false,
default: '',
},
},
data() {
return {
// if true, the local contact have been fixed and requires a push
fixed: false,
/**
* 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,
qrcode: '',
showPickAddressbookModal: false,
pickedAddressbook: null,
editMode: false,
newGroupsValue: [],
contactDetailsSelector: '.contact-details',
excludeFromBirthdayKey: 'x-nc-exclude-from-birthday-calendar',
// communication for ContactDetailsAddNewProp and ContactDetailsProperty
bus: mitt(),
showMenuPopover: false,
profileEnabled,
isTalkEnabled,
hasFilesResources: false,
hasTalkResources: false,
hasCalendarResources: false,
hasDeckResources: false,
deckPanelHasError: false,
filesPanelHasError: false,
talkPanelHasError: false,
calendarPanelHasError: false,
}
},
computed: {
hasRelatedResources() {
return this.hasFilesResources || this.hasTalkResources || this.hasCalendarResources || this.hasDeckResources
},
/**
* The address book is read-only (e.g. shared with me).
*
* @return {boolean}
*/
addressbookIsReadOnly() {
return this.contact.addressbook?.readOnly
},
/**
* The address book is read-only or the contact is in read-only mode.
*
* @return {boolean}
*/
isReadOnly() {
return this.addressbookIsReadOnly || !this.editMode
},
/**
* Warning messages
*
* @return {object | boolean}
*/
warning() {
if (this.addressbookIsReadOnly) {
return {
icon: EyeCircleIcon,
classes: [],
msg: t('contacts', 'This contact is in read-only mode. You do not have permission to edit this contact.'),
}
}
return false
},
/**
* Conflict message
*
* @return {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 properties copied and sorted by rfcProps.fieldOrder
*
* @return {Array}
*/
sortedProperties() {
return this.localContact.properties
.slice(0)
.sort((a, b) => {
const nameA = a.name.split('.').pop()
const nameB = b.name.split('.').pop()
return rfcProps.fieldOrder.indexOf(nameA) - rfcProps.fieldOrder.indexOf(nameB)
})
},
/**
* Contact properties filtered and grouped by rfcProps.fieldOrder
*
* @return {object}
*/
groupedProperties() {
return this.sortedProperties
.reduce((list, property) => {
// If there is no component to display this prop, ignore it
if (!this.canDisplay(property)) {
return list
}
// Init if needed
if (!list[property.name]) {
list[property.name] = []
}
list[property.name].push(property)
return list
}, {})
},
/**
* Fake model to use the propertySelect component
*
* @return {object}
*/
addressbookModel() {
return {
readableName: t('contacts', 'Address book'),
icon: 'icon-address-book',
options: this.addressbooksOptions,
}
},
/**
* Usable addressbook object linked to the local contact
*
* @param {string} [addressbookId] set the addressbook id
* @return {string}
*/
addressbook: {
get() {
return this.contact.addressbook.id
},
set(addressbookId) {
// Only move when the address book actually changed to prevent a conflict.
if (this.contact.addressbook.id !== addressbookId) {
this.moveContactToAddressbook(addressbookId)
}
},
},
/**
* Fake model to use the propertyGroups component
*
* @return {object}
*/
groupsModel() {
return {
readableName: t('contacts', 'Contact groups'),
icon: 'icon-contacts-dark',
}
},
/**
* Store getters filtered and mapped to usable object
* This is the list of addressbooks that are available
*
* @return {{id: string, name: string, readOnly: boolean}[]}
*/
addressbooksOptions() {
return this.addressbooks
.filter(addressbook => addressbook.enabled)
.map(addressbook => {
return {
id: addressbook.id,
name: addressbook.displayName,
readOnly: addressbook.readOnly,
}
})
},
/**
* Store getters filtered and mapped to usable object
* This is the list of addressbooks that are available to copy to
*
* @return {{id: string, name: string}[]}
*/
copyableAddressbooksOptions() {
return this.addressbooksOptions
.filter(option => !option.readOnly)
.filter(option => option.id !== this.contact.addressbook.id)
.map(addressbook => {
return {
id: addressbook.id,
name: addressbook.name,
}
})
},
// store getter
addressbooks() {
return this.$store.getters.getAddressbooks
},
contact() {
return this.$store.getters.getContact(this.contactKey)
},
excludeFromBirthdayLabel() {
return this.localContact.vCard.hasProperty(this.excludeFromBirthdayKey)
? t('contacts', 'Add contact to Birthday Calendar')
: t('contacts', 'Exclude contact from Birthday Calendar')
},
enableToggleBirthdayExclusion() {
return parseInt(window.OC.config.version.split('.')[0]) >= 26
&& this.localContact?.vCard // Wait until localContact was fetched
},
/**
* Read-only representation of the contact title and organization.
*
* @return {string}
*/
formattedSubtitle() {
const title = this.contact.title
const organization = this.contact.org
if (title && organization) {
return t('contacts', '{title} at {organization}', {
title,
organization,
})
} else if (title) {
return title
} else if (organization) {
return organization
}
return ''
},
profilePageLink() {
return this.contact.socialLink('NEXTCLOUD')
},
emailAddressProperties() {
return this.localContact.properties.find(property => property.name === 'email')
},
emailAddress() {
return this.emailAddressProperties?.getFirstValue()
},
phoneNumberProperties() {
return this.localContact.properties.find(property => property.name === 'tel')
},
phoneNumberList() {
return this.groupedProperties?.tel?.map(prop => prop.getFirstValue()).filter(tel => !!tel)
},
emailAddressList() {
return this.groupedProperties?.email?.map(prop => prop.getFirstValue()).filter(address => !!address)
},
callUrl() {
return generateUrl('/apps/spreed/?callUser={uid}', { uid: this.contact.uid })
},
isInSystemAddressBook() {
return this.contact.addressbook.id === 'z-server-generated--system'
},
nextcloudVersionAtLeast28() {
return parseInt(window.OC.config.version.split('.')[0]) >= 28
},
},
watch: {
contact(newContact, oldContact) {
if (this.contactKey && newContact !== oldContact) {
this.selectContact(this.contactKey)
}
},
},
beforeMount() {
// load the desired data if we already selected a contact
if (this.contactKey) {
this.selectContact(this.contactKey)
}
// capture ctrl+s
document.addEventListener('keydown', this.onCtrlSave)
},
beforeDestroy() {
// unbind capture ctrl+s
document.removeEventListener('keydown', this.onCtrlSave)
},
methods: {
updateGroups(value) {
this.newGroupsValue = value
},
/**
* Send the local clone of contact to the store
*/
async updateContact() {
this.fixed = false
this.loadingUpdate = true
try {
await this.$store.dispatch('updateContact', this.localContact)
} finally {
this.loadingUpdate = false
}
// if we just created the contact, we need to force update the
// localContact to match the proper store contact
if (!this.localContact.dav) {
this.logger.debug('New contact synced!', { localContact: this.localContact })
// fetching newly created & storred contact
const contact = this.$store.getters.getContact(this.localContact.key)
await this.updateLocalContact(contact)
}
},
/**
* Generate a qrcode for the contact
*/
async showQRcode() {
const jCal = this.contact.jCal.slice(0)
// do not encode photo
jCal[1] = jCal[1].filter(props => props[0] !== 'photo')
const data = ICAL.stringify(jCal)
if (data.length > 0) {
const svgBytes = await getSVG(data)
const svgString = new TextDecoder().decode(svgBytes)
this.qrcode = btoa(svgString)
}
},
async toggleBirthdayExclusionForContact() {
if (!this.localContact.vCard.hasProperty(this.excludeFromBirthdayKey)) {
this.localContact.vCard.addPropertyWithValue(this.excludeFromBirthdayKey, true)
} else {
this.localContact.vCard.removeProperty(this.excludeFromBirthdayKey)
}
await this.updateContact()
},
/**
* Select the text in the input if it is still set to 'Name'
*/
selectInput() {
if (this.$refs.fullname && this.$refs.fullname.value === t('contacts', 'Name')) {
this.$refs.fullname.select()
}
},
/**
* Select a contact, and update the localContact
* Fetch updated data if necessary
* Scroll to the selected contact if exists
*
* @param {string} key the contact key
*/
async selectContact(key) {
this.loadingData = true
this.editMode = false
// local version of the contact
const contact = this.$store.getters.getContact(key)
if (contact) {
// if contact exists AND if exists on server
if (contact.dav) {
try {
await this.$store.dispatch('fetchFullContact', { contact })
// clone to a local editable variable
await this.updateLocalContact(contact)
} catch (error) {
if (error.name === 'ParserError') {
showError(t('contacts', 'Syntax error. Cannot open the contact.'))
} else if (error?.status === 404) {
showError(t('contacts', 'The contact does not exist on the server anymore.'))
} else {
showError(t('contacts', 'Unable to retrieve the contact from the server, please check your network connection.'))
}
console.error(error)
// trigger a local deletion from the store only
this.$store.dispatch('deleteContact', { contact: this.contact, dav: false })
}
} else {
// clone to a local editable variable
await this.updateLocalContact(contact)
// enable edit mode by default when creating a new contact
this.editMode = true
}
}
this.loadingData = false
},
/**
* 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) {
const 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)
showError(t('contacts', 'An error occurred while trying to move the contact'))
} finally {
this.loadingUpdate = false
}
}
},
/**
* Copy contact to the specified addressbook
*
* @param {string} addressbookId the desired addressbook ID
*/
async copyContactToAddressbook(addressbookId) {
const addressbook = this.addressbooks.find(search => search.id === addressbookId)
this.loadingUpdate = true
if (addressbook) {
try {
const contact = await this.$store.dispatch('copyContactToAddressbook', {
// 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)
showError(t('contacts', 'An error occurred while trying to copy the contact'))
} 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
})
},
// reset the current qrcode
closeQrModal() {
this.qrcode = ''
},
/**
* Update this.localContact and set this.fixed
*
* @param {Contact} contact the contact to clone
*/
async updateLocalContact(contact) {
// create empty contact and copy inner data
const localContact = Object.assign(
Object.create(Object.getPrototypeOf(contact)),
contact,
)
this.fixed = validate(localContact)
this.localContact = localContact
this.newGroupsValue = [...this.localContact.groups]
},
onCtrlSave(e) {
if (!this.editMode) {
return
}
if (e.keyCode === 83 && (navigator.platform.match('Mac') ? e.metaKey : e.ctrlKey)) {
e.preventDefault()
this.onSave()
}
},
/**
* Clone the current contact to another addressbook
*/
async cloneContact() {
// only one addressbook, let's clone it there
if (this.pickedAddressbook && this.addressbooks.find(addressbook => addressbook.id === this.pickedAddressbook.id)) {
this.logger.debug('Cloning contact to', { name: this.pickedAddressbook.name })
await this.copyContactToAddressbook(this.pickedAddressbook.id)
this.closePickAddressbookModal()
} else if (this.addressbooksOptions.length === 1) {
this.logger.debug('Cloning contact to', { name: this.addressbooksOptions[0].name })
await this.copyContactToAddressbook(this.addressbooksOptions[0].id)
} else {
this.showPickAddressbookModal = true
}
},
closePickAddressbookModal() {