annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 'use strict';
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 function initNickname(connection) {
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 const nick_input = document.getElementById('nick');
10
e47b9bd55192 Also use a spinner on nickname get/set.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7
diff changeset
5 const spinner_img = document.getElementById('nick-spinner');
0
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 const iq = $iq({type: 'get'})
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'})
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 .c('items', {node: 'http://jabber.org/protocol/nick'});
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 connection.sendIQ(iq, onNickname, onNicknameRetrievalError.bind(null, 'PubSub query failed.'));
15
3eed9fe0bd7c End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 11
diff changeset
11 displaySpinner(spinner_img);
0
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 function onNickname(result_iq)
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14 {
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15 const item = parseXPath(result_iq, './pubsub:pubsub/pubsub:items/pubsub:item');
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16 if (item == null)
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 return onNicknameRetrievalError('no item found.');
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18 const id = item.getAttributeNS(null, 'id');
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 const nick = parseXPath(item, './nickname:nick');
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20 nick_input.value = nick.textContent;
15
3eed9fe0bd7c End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 11
diff changeset
21 hideSpinner(spinner_img);
0
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22 }
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 function onNicknameRetrievalError(string)
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 {
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 console.log('Failed to retrieve nickname: ' + string);
15
3eed9fe0bd7c End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 11
diff changeset
27 hideSpinner(spinner_img);
0
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 }
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29
7
13e8e4ea53c0 Change nickname on blur, remove the button.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1
diff changeset
30 nick_input.addEventListener('blur', function (evt) {
0
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 const iq = $iq({type: 'set'})
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'})
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 .c('publish', {node: 'http://jabber.org/protocol/nick'})
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 .c('item', {id: 'current'})
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 .c('nick', {xmlns: 'http://jabber.org/protocol/nick'})
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 .t(nick_input.value);
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 connection.sendIQ(iq, onNicknameChanged, onNicknameChangeError);
15
3eed9fe0bd7c End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 11
diff changeset
38 displaySpinner(spinner_img);
0
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 });
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40
15
3eed9fe0bd7c End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 11
diff changeset
41 function onNicknameChanged(result_iq)
0
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 {
15
3eed9fe0bd7c End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 11
diff changeset
43 console.log("Nickname successfully changed.");
3eed9fe0bd7c End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 11
diff changeset
44 spinnerOk(spinner_img);
0
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 }
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 function onNicknameChangeError(iq)
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 {
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 console.log("onNicknameChangeError", iq);
15
3eed9fe0bd7c End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 11
diff changeset
50 spinnerError(spinner_img);
0
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51 }
2a8d4e8600d0 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 }