Mercurial > xmpp-account-manager
view vcard.js @ 63:ee1df80a1715 default tip
Nicer-looking input form
author | mathieui |
---|---|
date | Sun, 24 May 2020 14:19:29 +0200 |
parents | 6d861d881b96 |
children |
line wrap: on
line source
// SPDX-License-Identifier: AGPL-3.0-only /* * Copyright © 2018-2020 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> * Copyright © 2020 Mathieu Pasquet <mathieui@mathieui.net> * * Released under GNU AGPL v3 only, read the file 'LICENSE' for more information. */ 'use strict'; function initVCard(connection) { const vcard_access = document.getElementById('vcard-access'); const vcard_fn = document.getElementById('vcard-fn'); const vcard_email = document.getElementById('vcard-email'); const vcard_bday = document.getElementById('vcard-bday'); const spinner_img = document.getElementById('vcard-spinner'); const access_spinner_img = document.getElementById('vcard-access-spinner'); const vcard_data = {}; const iq = $iq({type: 'get'}) .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}) .c('items', {node: 'urn:xmpp:vcard4'}) .c('item', {id: 'current'}); connection.sendIQ(iq, onVCard4, onVCard4RetrievalError); displaySpinner(spinner_img); getAccessModel(); function getAccessModel() { vcard_access.disabled = true; displaySpinner(access_spinner_img); retrieveConfiguration(connection, 'urn:xmpp:vcard4').then((access_model) => { if (access_model !== null) { if (access_model === 'open') vcard_access.value = 'open'; else if (access_model === 'presence') vcard_access.value = 'presence'; else console.log('Unsupported vCard4 access model: ' + access_model); vcard_access.disabled = false; } hideSpinner(access_spinner_img); }, (reason) => { console.log('Failed to retrieve vCard4 configuration:', reason); hideSpinner(access_spinner_img); }); } function onVCard4(result_iq) { const item = parseXPath(result_iq, './pubsub:pubsub/pubsub:items/pubsub:item'); if (item === null) return onVCard4RetrievalError(null, 'no item found.'); const vcard = parseXPath(item, './vcard4:vcard'); if (vcard === null) return onVCard4RetrievalError(null, 'no vCard4 found, your PubSub node is broken.'); vcard_data.fn = parseXPathText(vcard, './vcard4:fn/vcard4:text'); vcard_data.email = parseXPathText(vcard, './vcard4:email/vcard4:text'); vcard_data.bday = parseXPathText(vcard, './vcard4:bday/vcard4:date'); vcard_fn.value = vcard_data.fn; vcard_fn.disabled = false; vcard_email.value = vcard_data.email; vcard_email.disabled = false; vcard_bday.value = vcard_data.bday; vcard_bday.disabled = false; hideSpinner(spinner_img); getAccessModel(); } function onVCard4RetrievalError(iq, string) { if (!string) { string = parseErrorIq(iq); } string = 'Failed to retrieve vCard4: ' + string; console.log(string); vcard_fn.disabled = false; vcard_email.disabled = false; vcard_bday.disabled = false; hideSpinner(spinner_img); vcard_access.disabled = true; } function setVCard4() { // TODO: avoid overriding fields we don’t understand. const iq = $iq({type: 'set'}) .c('vcard', {xmlns: 'urn:ietf:params:xml:ns:vcard-4.0'}); if (vcard_fn.value) iq.c('fn') .c('text').t(vcard_fn.value).up().up(); if (vcard_email.value) iq.c('email') .c('text').t(vcard_email.value).up().up(); if (vcard_bday.value) iq.c('bday') .c('date').t(vcard_bday.value).up().up(); connection.sendIQ(iq, onVCard4Changed, onVCard4ChangeError); displaySpinner(spinner_img); vcard_access.disabled = true; } vcard_fn.addEventListener('change', setVCard4); vcard_email.addEventListener('change', setVCard4); vcard_bday.addEventListener('change', setVCard4); function onVCard4Changed(result_iq) { console.log('Successfully set vCard4.') spinnerOk(spinner_img); getAccessModel(); } function onVCard4ChangeError(iq) { const string = 'Failed to set vCard4: ' + parseErrorIq(iq); console.log(string); spinnerError(spinner_img, string); vcard_access.disabled = true; } vcard_access.addEventListener('change', function (evt) { const iq = configurePEPField('urn:xmpp:vcard4', 'pubsub#access_model', evt.target.value); connection.sendIQ(iq, onAccessConfigured, onAccessConfigureError.bind(null, 'PubSub configuration failed.')); displaySpinner(access_spinner_img); }); function onAccessConfigured(result_iq) { console.log('Successfully set vCard4 access model.') spinnerOk(access_spinner_img); } function onAccessConfigureError(string) { console.log('Failed to configure vCard4 node: ' + string); spinnerError(access_spinner_img); } }