comparison vcard.js @ 25:d9da5c3e305d

Add support for setting some vCard4 fields.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 23 Dec 2018 16:34:30 +0100
parents
children 02b5bceeca64
comparison
equal deleted inserted replaced
24:6c620e9f7d2c 25:d9da5c3e305d
1 'use strict';
2
3 function initVCard(connection) {
4 const vcard_access = document.getElementById('vcard-access');
5 const vcard_fullname = document.getElementById('vcard-fullname');
6 const vcard_email = document.getElementById('vcard-email');
7 const spinner_img = document.getElementById('vcard-spinner');
8 const access_spinner_img = document.getElementById('vcard-access-spinner');
9
10 const vcard_data = {};
11
12 const iq = $iq({type: 'get'})
13 .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'})
14 .c('items', {node: 'urn:xmpp:vcard4'})
15 .c('item', {id: 'current'});
16 connection.sendIQ(iq, onVCard4, onVCard4RetrievalError.bind(null, 'PubSub query failed.'));
17 displaySpinner(spinner_img);
18
19 function onVCard4(result_iq)
20 {
21 const item = parseXPath(result_iq, './pubsub:pubsub/pubsub:items/pubsub:item');
22 if (item === null)
23 return onVCard4RetrievalError('no item found.');
24 const vcard = parseXPath(item, './vcard4:vcard');
25 if (vcard === null)
26 return onVCard4RetrievalError('no vCard4 found, your PubSub node is broken.');
27
28 vcard_data.fn = parseXPathText(vcard, './vcard4:fn/vcard4:text');
29 vcard_data.email = parseXPathText(vcard, './vcard4:email/vcard4:text');
30
31 vcard_fullname.value = vcard_data.fn;
32 vcard_fullname.disabled = false;
33 vcard_email.value = vcard_data.email;
34 vcard_email.disabled = false;
35 hideSpinner(spinner_img);
36 }
37
38 function onVCard4RetrievalError(string)
39 {
40 console.log('Failed to retrieve vCard4: ' + string);
41 vcard_fullname.disabled = false;
42 vcard_email.disabled = false;
43 hideSpinner(spinner_img);
44 }
45
46 function setVCard4() {
47 // TODO: avoid overriding fields we don’t understand.
48 const iq = $iq({type: 'set'})
49 .c('vcard', {xmlns: 'urn:ietf:params:xml:ns:vcard-4.0'})
50 .c('fn')
51 .c('text').t(vcard_fullname.value).up().up()
52 .c('email')
53 .c('text').t(vcard_email.value).up().up()
54 connection.sendIQ(iq, onVCard4Changed, onVCard4ChangeError.bind(null, 'coucou'));
55 displaySpinner(spinner_img);
56 }
57
58 vcard_fullname.addEventListener('blur', setVCard4);
59 vcard_email.addEventListener('blur', setVCard4);
60
61 function onVCard4Changed(result_iq)
62 {
63 console.log('Successfully set vCard4.')
64 spinnerOk(spinner_img);
65 }
66
67 function onVCard4ChangeError(string)
68 {
69 console.log('Failed to set vCard4: ' + string);
70 spinnerError(spinner_img);
71 }
72
73 vcard_access.addEventListener('change', function (evt) {
74 const iq = configurePEPField('urn:xmpp:vcard4', 'pubsub#access_model', evt.target.value);
75 connection.sendIQ(iq, onAccessConfigured, onAccessConfigureError.bind(null, 'PubSub configuration failed.'));
76 displaySpinner(access_spinner_img);
77 });
78
79 function onAccessConfigured(result_iq)
80 {
81 console.log('Successfully set vCard4 access model.')
82 spinnerOk(access_spinner_img);
83 }
84
85 function onAccessConfigureError(string)
86 {
87 console.log('Failed to configure vCard4 node: ' + string);
88 spinnerError(access_spinner_img);
89 }
90 }