Mercurial > xmpp-account-manager
annotate vcard.js @ 31:e561bdd81777
Display better vCard4 errors.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sun, 23 Dec 2018 18:20:17 +0100 |
parents | 9ba4f8cc32f1 |
children | b15e1581c3d4 |
rev | line source |
---|---|
25
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
1 'use strict'; |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
2 |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
3 function initVCard(connection) { |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
4 const vcard_access = document.getElementById('vcard-access'); |
27
02b5bceeca64
Add vCard birthday support, and only include complete values in the vCard.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
5 const vcard_fn = document.getElementById('vcard-fn'); |
25
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
6 const vcard_email = document.getElementById('vcard-email'); |
27
02b5bceeca64
Add vCard birthday support, and only include complete values in the vCard.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
7 const vcard_bday = document.getElementById('vcard-bday'); |
25
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
8 const spinner_img = document.getElementById('vcard-spinner'); |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
9 const access_spinner_img = document.getElementById('vcard-access-spinner'); |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
10 |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
11 const vcard_data = {}; |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
12 |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
13 const iq = $iq({type: 'get'}) |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
14 .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}) |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
15 .c('items', {node: 'urn:xmpp:vcard4'}) |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
16 .c('item', {id: 'current'}); |
31
e561bdd81777
Display better vCard4 errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
30
diff
changeset
|
17 connection.sendIQ(iq, onVCard4, onVCard4RetrievalError); |
25
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
18 displaySpinner(spinner_img); |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
19 |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
20 function onVCard4(result_iq) |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
21 { |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
22 const item = parseXPath(result_iq, './pubsub:pubsub/pubsub:items/pubsub:item'); |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
23 if (item === null) |
31
e561bdd81777
Display better vCard4 errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
30
diff
changeset
|
24 return onVCard4RetrievalError(null, 'no item found.'); |
25
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
25 const vcard = parseXPath(item, './vcard4:vcard'); |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
26 if (vcard === null) |
31
e561bdd81777
Display better vCard4 errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
30
diff
changeset
|
27 return onVCard4RetrievalError(null, 'no vCard4 found, your PubSub node is broken.'); |
25
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
28 |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
29 vcard_data.fn = parseXPathText(vcard, './vcard4:fn/vcard4:text'); |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
30 vcard_data.email = parseXPathText(vcard, './vcard4:email/vcard4:text'); |
27
02b5bceeca64
Add vCard birthday support, and only include complete values in the vCard.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
31 vcard_data.bday = parseXPathText(vcard, './vcard4:bday/vcard4:date'); |
25
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
32 |
27
02b5bceeca64
Add vCard birthday support, and only include complete values in the vCard.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
33 vcard_fn.value = vcard_data.fn; |
02b5bceeca64
Add vCard birthday support, and only include complete values in the vCard.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
34 vcard_fn.disabled = false; |
25
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
35 vcard_email.value = vcard_data.email; |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
36 vcard_email.disabled = false; |
27
02b5bceeca64
Add vCard birthday support, and only include complete values in the vCard.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
37 vcard_bday.value = vcard_data.bday; |
02b5bceeca64
Add vCard birthday support, and only include complete values in the vCard.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
38 vcard_bday.disabled = false; |
25
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
39 hideSpinner(spinner_img); |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
40 } |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
41 |
31
e561bdd81777
Display better vCard4 errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
30
diff
changeset
|
42 function onVCard4RetrievalError(iq, string) |
25
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
43 { |
31
e561bdd81777
Display better vCard4 errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
30
diff
changeset
|
44 if (!string) { |
e561bdd81777
Display better vCard4 errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
30
diff
changeset
|
45 const [condition, text] = parseErrorIq(iq); |
e561bdd81777
Display better vCard4 errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
30
diff
changeset
|
46 const string = condition + ': ' + text; |
e561bdd81777
Display better vCard4 errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
30
diff
changeset
|
47 } |
e561bdd81777
Display better vCard4 errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
30
diff
changeset
|
48 string = 'Failed to retrieve vCard4: ' + string; |
e561bdd81777
Display better vCard4 errors.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
30
diff
changeset
|
49 console.log(string); |
27
02b5bceeca64
Add vCard birthday support, and only include complete values in the vCard.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
50 vcard_fn.disabled = false; |
25
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
51 vcard_email.disabled = false; |
27
02b5bceeca64
Add vCard birthday support, and only include complete values in the vCard.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
52 vcard_bday.disabled = false; |
25
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
53 hideSpinner(spinner_img); |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
54 } |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
55 |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
56 function setVCard4() { |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
57 // TODO: avoid overriding fields we don’t understand. |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
58 const iq = $iq({type: 'set'}) |
27
02b5bceeca64
Add vCard birthday support, and only include complete values in the vCard.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
59 .c('vcard', {xmlns: 'urn:ietf:params:xml:ns:vcard-4.0'}); |
02b5bceeca64
Add vCard birthday support, and only include complete values in the vCard.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
60 if (vcard_fn.value) |
02b5bceeca64
Add vCard birthday support, and only include complete values in the vCard.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
61 iq.c('fn') |
02b5bceeca64
Add vCard birthday support, and only include complete values in the vCard.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
62 .c('text').t(vcard_fn.value).up().up(); |
02b5bceeca64
Add vCard birthday support, and only include complete values in the vCard.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
63 if (vcard_email.value) |
02b5bceeca64
Add vCard birthday support, and only include complete values in the vCard.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
64 iq.c('email') |
02b5bceeca64
Add vCard birthday support, and only include complete values in the vCard.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
65 .c('text').t(vcard_email.value).up().up(); |
02b5bceeca64
Add vCard birthday support, and only include complete values in the vCard.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
66 if (vcard_bday.value) |
02b5bceeca64
Add vCard birthday support, and only include complete values in the vCard.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
67 iq.c('bday') |
02b5bceeca64
Add vCard birthday support, and only include complete values in the vCard.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
68 .c('date').t(vcard_bday.value).up().up(); |
30
9ba4f8cc32f1
Display the returned error in the title of the spinner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
27
diff
changeset
|
69 connection.sendIQ(iq, onVCard4Changed, onVCard4ChangeError); |
25
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
70 displaySpinner(spinner_img); |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
71 } |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
72 |
27
02b5bceeca64
Add vCard birthday support, and only include complete values in the vCard.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
73 vcard_fn.addEventListener('blur', setVCard4); |
25
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
74 vcard_email.addEventListener('blur', setVCard4); |
27
02b5bceeca64
Add vCard birthday support, and only include complete values in the vCard.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
75 vcard_bday.addEventListener('change', setVCard4); |
25
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
76 |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
77 function onVCard4Changed(result_iq) |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
78 { |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
79 console.log('Successfully set vCard4.') |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
80 spinnerOk(spinner_img); |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
81 } |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
82 |
30
9ba4f8cc32f1
Display the returned error in the title of the spinner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
27
diff
changeset
|
83 function onVCard4ChangeError(iq) |
25
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
84 { |
30
9ba4f8cc32f1
Display the returned error in the title of the spinner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
27
diff
changeset
|
85 const [condition, text] = parseErrorIq(iq); |
9ba4f8cc32f1
Display the returned error in the title of the spinner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
27
diff
changeset
|
86 const string = 'Failed to set vCard4: ' + condition + ': ' + text; |
9ba4f8cc32f1
Display the returned error in the title of the spinner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
27
diff
changeset
|
87 console.log(string); |
9ba4f8cc32f1
Display the returned error in the title of the spinner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
27
diff
changeset
|
88 spinnerError(spinner_img, string); |
25
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
89 } |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
90 |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
91 vcard_access.addEventListener('change', function (evt) { |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
92 const iq = configurePEPField('urn:xmpp:vcard4', 'pubsub#access_model', evt.target.value); |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
93 connection.sendIQ(iq, onAccessConfigured, onAccessConfigureError.bind(null, 'PubSub configuration failed.')); |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
94 displaySpinner(access_spinner_img); |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
95 }); |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
96 |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
97 function onAccessConfigured(result_iq) |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
98 { |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
99 console.log('Successfully set vCard4 access model.') |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
100 spinnerOk(access_spinner_img); |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
101 } |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
102 |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
103 function onAccessConfigureError(string) |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
104 { |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
105 console.log('Failed to configure vCard4 node: ' + string); |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
106 spinnerError(access_spinner_img); |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
107 } |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
108 } |