comparison nickname.js @ 10:e47b9bd55192

Also use a spinner on nickname get/set.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 22 Dec 2018 16:13:01 +0100
parents 13e8e4ea53c0
children aedf80eefc19
comparison
equal deleted inserted replaced
9:b994c6c19f5c 10:e47b9bd55192
1 'use strict'; 1 'use strict';
2 2
3 function initNickname(connection) { 3 function initNickname(connection) {
4 const nick_input = document.getElementById('nick'); 4 const nick_input = document.getElementById('nick');
5 const spinner_img = document.getElementById('nick-spinner');
5 6
6 const iq = $iq({type: 'get'}) 7 const iq = $iq({type: 'get'})
7 .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}) 8 .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'})
8 .c('items', {node: 'http://jabber.org/protocol/nick'}); 9 .c('items', {node: 'http://jabber.org/protocol/nick'});
9 connection.sendIQ(iq, onNickname, onNicknameRetrievalError.bind(null, 'PubSub query failed.')); 10 connection.sendIQ(iq, onNickname, onNicknameRetrievalError.bind(null, 'PubSub query failed.'));
14 if (item == null) 15 if (item == null)
15 return onNicknameRetrievalError('no item found.'); 16 return onNicknameRetrievalError('no item found.');
16 const id = item.getAttributeNS(null, 'id'); 17 const id = item.getAttributeNS(null, 'id');
17 const nick = parseXPath(item, './nickname:nick'); 18 const nick = parseXPath(item, './nickname:nick');
18 nick_input.value = nick.textContent; 19 nick_input.value = nick.textContent;
20 spinner_img.hidden = true;
19 } 21 }
20 22
21 function onNicknameRetrievalError(string) 23 function onNicknameRetrievalError(string)
22 { 24 {
23 console.log('Failed to retrieve nickname: ' + string); 25 console.log('Failed to retrieve nickname: ' + string);
26 spinner_img.hidden = true;
24 } 27 }
25 28
26 nick_input.addEventListener('blur', function (evt) { 29 nick_input.addEventListener('blur', function (evt) {
27 const iq = $iq({type: 'set'}) 30 const iq = $iq({type: 'set'})
28 .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}) 31 .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'})
29 .c('publish', {node: 'http://jabber.org/protocol/nick'}) 32 .c('publish', {node: 'http://jabber.org/protocol/nick'})
30 .c('item', {id: 'current'}) 33 .c('item', {id: 'current'})
31 .c('nick', {xmlns: 'http://jabber.org/protocol/nick'}) 34 .c('nick', {xmlns: 'http://jabber.org/protocol/nick'})
32 .t(nick_input.value); 35 .t(nick_input.value);
33 connection.sendIQ(iq, onNicknameChanged, onNicknameChangeError); 36 connection.sendIQ(iq, onNicknameChanged, onNicknameChangeError);
37 spinner_img.hidden = false;
34 }); 38 });
35 39
36 function onNicknameChanged(iq) 40 function onNicknameChanged(iq)
37 { 41 {
38 console.log("onNicknameChanged", iq); 42 console.log("onNicknameChanged", iq);
43 spinner_img.hidden = true;
39 } 44 }
40 45
41 function onNicknameChangeError(iq) 46 function onNicknameChangeError(iq)
42 { 47 {
43 console.log("onNicknameChangeError", iq); 48 console.log("onNicknameChangeError", iq);
49 spinner_img.hidden = true;
44 } 50 }
45 } 51 }