view nickname.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 initNickname(connection) {
    const nick_input = document.getElementById('nick');
    const nick_access = document.getElementById('nick-access');
    const spinner_img = document.getElementById('nick-spinner');
    const access_spinner_img = document.getElementById('nick-access-spinner');

    const iq = $iq({type: 'get'})
        .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'})
            .c('items', {node: 'http://jabber.org/protocol/nick'});
    connection.sendIQ(iq, onNickname, onNicknameRetrievalError);
    displaySpinner(spinner_img);
    getAccessModel();

    function getAccessModel()
    {
        nick_access.disabled = true;
        displaySpinner(access_spinner_img);
        retrieveConfiguration(connection, 'http://jabber.org/protocol/nick').then((access_model) => {
            if (access_model !== null) {
                if (access_model === 'open')
                    nick_access.value = 'open';
                else if (access_model === 'presence')
                    nick_access.value = 'presence';
                else
                    console.log('Unsupported nickname access model: ' + access_model);
                nick_access.disabled = false;
            }
            hideSpinner(access_spinner_img);
        }, (reason) => {
            console.log('Failed to retrieve nickname configuration: ' + reason);
            hideSpinner(access_spinner_img);
        });
    }

    function onNickname(result_iq)
    {
        const item = parseXPath(result_iq, './pubsub:pubsub/pubsub:items/pubsub:item');
        if (item == null) {
            console.log('Failed to retrieve nickname: no item published.');
            hideSpinner(spinner_img);
            return;
        }

        const id = item.getAttributeNS(null, 'id');
        const nick = parseXPath(item, './nickname:nick');
        nick_input.value = nick.textContent;
        hideSpinner(spinner_img);
    }

    function onNicknameRetrievalError(iq)
    {
        console.log('Failed to retrieve nickname: ' + parseErrorIq(iq));
        hideSpinner(spinner_img);
        nick_access.disabled = true;
    }

    nick_input.addEventListener('change', function (evt) {
        const new_nickname = evt.target.value;
        let iq;
        let changed_callback;
        if (new_nickname) {
            iq = $iq({type: 'set'})
                .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'})
                    .c('publish', {node: 'http://jabber.org/protocol/nick'})
                        .c('item', {id: 'current'})
                            .c('nick', {xmlns: 'http://jabber.org/protocol/nick'})
                                .t(new_nickname);
            changed_callback = onNicknameChanged.bind(null, 'changed');
        } else {
            iq = $iq({type: 'set'})
                .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub#owner'})
                    .c('delete', {node: 'http://jabber.org/protocol/nick'});
            changed_callback = onNicknameChanged.bind(null, 'deleted');
        }
        connection.sendIQ(iq, changed_callback, onNicknameChangeError);
        displaySpinner(spinner_img);
        nick_access.disabled = true;
    });

    function onNicknameChanged(action, result_iq)
    {
        console.log('Nickname successfully ' + action + '.');
        nick_access.disabled = true;
        if (action === 'changed')
            getAccessModel();
        spinnerOk(spinner_img);
    }

    function onNicknameChangeError(iq)
    {
        console.log('onNicknameChangeError', iq);
        spinnerError(spinner_img);
        nick_access.disabled = true;
    }

    nick_access.addEventListener('change', function (evt) {
        const iq = configurePEPField('http://jabber.org/protocol/nick', 'pubsub#access_model', evt.target.value);
        connection.sendIQ(iq, onNicknameConfigured, onNicknameConfigureError);
        displaySpinner(access_spinner_img);
    });

    function onNicknameConfigured(iq)
    {
        console.log('Successfully set nickname access model.')
        spinnerOk(access_spinner_img);
    }

    function onNicknameConfigureError(iq)
    {
        const string = 'Failed to configure nick node: ' + parseErrorIq(iq);
        console.log(string);
        spinnerError(access_spinner_img, string);
    }
}