view nickname.js @ 30:9ba4f8cc32f1

Display the returned error in the title of the spinner.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 23 Dec 2018 17:15:38 +0100
parents 3eed9fe0bd7c
children 45bd945c5722
line wrap: on
line source

'use strict';

function initNickname(connection) {
    const nick_input = document.getElementById('nick');
    const spinner_img = document.getElementById('nick-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.bind(null, 'PubSub query failed.'));
    displaySpinner(spinner_img);

    function onNickname(result_iq)
    {
        const item = parseXPath(result_iq, './pubsub:pubsub/pubsub:items/pubsub:item');
        if (item == null)
            return onNicknameRetrievalError('no item found.');
        const id = item.getAttributeNS(null, 'id');
        const nick = parseXPath(item, './nickname:nick');
        nick_input.value = nick.textContent;
        hideSpinner(spinner_img);
    }

    function onNicknameRetrievalError(string)
    {
        console.log('Failed to retrieve nickname: ' + string);
        hideSpinner(spinner_img);
    }

    nick_input.addEventListener('blur', function (evt) {
        const 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(nick_input.value);
        connection.sendIQ(iq, onNicknameChanged, onNicknameChangeError);
        displaySpinner(spinner_img);
    });

    function onNicknameChanged(result_iq)
    {
        console.log("Nickname successfully changed.");
        spinnerOk(spinner_img);
    }

    function onNicknameChangeError(iq)
    {
        console.log("onNicknameChangeError", iq);
        spinnerError(spinner_img);
    }
}