Mercurial > xmpp-account-manager
annotate nickname.js @ 63:ee1df80a1715 default tip
Nicer-looking input form
author | mathieui |
---|---|
date | Sun, 24 May 2020 14:19:29 +0200 |
parents | 6d861d881b96 |
children |
rev | line source |
---|---|
60
6d861d881b96
Add license headers to all source files.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
54
diff
changeset
|
1 // SPDX-License-Identifier: AGPL-3.0-only |
6d861d881b96
Add license headers to all source files.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
54
diff
changeset
|
2 /* |
6d861d881b96
Add license headers to all source files.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
54
diff
changeset
|
3 * Copyright © 2018-2020 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
6d861d881b96
Add license headers to all source files.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
54
diff
changeset
|
4 * Copyright © 2020 Mathieu Pasquet <mathieui@mathieui.net> |
6d861d881b96
Add license headers to all source files.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
54
diff
changeset
|
5 * |
6d861d881b96
Add license headers to all source files.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
54
diff
changeset
|
6 * Released under GNU AGPL v3 only, read the file 'LICENSE' for more information. |
6d861d881b96
Add license headers to all source files.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
54
diff
changeset
|
7 */ |
6d861d881b96
Add license headers to all source files.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
54
diff
changeset
|
8 |
0 | 9 'use strict'; |
10 | |
11 function initNickname(connection) { | |
12 const nick_input = document.getElementById('nick'); | |
35
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
13 const nick_access = document.getElementById('nick-access'); |
10
e47b9bd55192
Also use a spinner on nickname get/set.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
14 const spinner_img = document.getElementById('nick-spinner'); |
35
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
15 const access_spinner_img = document.getElementById('nick-access-spinner'); |
0 | 16 |
17 const iq = $iq({type: 'get'}) | |
18 .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}) | |
19 .c('items', {node: 'http://jabber.org/protocol/nick'}); | |
47
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
20 connection.sendIQ(iq, onNickname, onNicknameRetrievalError); |
15
3eed9fe0bd7c
End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
11
diff
changeset
|
21 displaySpinner(spinner_img); |
52
2f45bee88b47
Add pubsub#access_model retrieval for the avatar node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
48
diff
changeset
|
22 getAccessModel(); |
47
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
23 |
52
2f45bee88b47
Add pubsub#access_model retrieval for the avatar node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
48
diff
changeset
|
24 function getAccessModel() |
47
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
25 { |
52
2f45bee88b47
Add pubsub#access_model retrieval for the avatar node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
48
diff
changeset
|
26 nick_access.disabled = true; |
2f45bee88b47
Add pubsub#access_model retrieval for the avatar node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
48
diff
changeset
|
27 displaySpinner(access_spinner_img); |
2f45bee88b47
Add pubsub#access_model retrieval for the avatar node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
48
diff
changeset
|
28 retrieveConfiguration(connection, 'http://jabber.org/protocol/nick').then((access_model) => { |
48
021185105e2f
Move the node configuration retrieval to a util promise.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
29 if (access_model !== null) { |
021185105e2f
Move the node configuration retrieval to a util promise.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
30 if (access_model === 'open') |
021185105e2f
Move the node configuration retrieval to a util promise.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
31 nick_access.value = 'open'; |
021185105e2f
Move the node configuration retrieval to a util promise.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
32 else if (access_model === 'presence') |
021185105e2f
Move the node configuration retrieval to a util promise.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
33 nick_access.value = 'presence'; |
021185105e2f
Move the node configuration retrieval to a util promise.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
34 else |
021185105e2f
Move the node configuration retrieval to a util promise.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
35 console.log('Unsupported nickname access model: ' + access_model); |
021185105e2f
Move the node configuration retrieval to a util promise.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
36 nick_access.disabled = false; |
47
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
37 } |
48
021185105e2f
Move the node configuration retrieval to a util promise.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
38 hideSpinner(access_spinner_img); |
021185105e2f
Move the node configuration retrieval to a util promise.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
39 }, (reason) => { |
021185105e2f
Move the node configuration retrieval to a util promise.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
40 console.log('Failed to retrieve nickname configuration: ' + reason); |
021185105e2f
Move the node configuration retrieval to a util promise.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
41 hideSpinner(access_spinner_img); |
021185105e2f
Move the node configuration retrieval to a util promise.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
47
diff
changeset
|
42 }); |
47
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
43 } |
0 | 44 |
45 function onNickname(result_iq) | |
46 { | |
47 const item = parseXPath(result_iq, './pubsub:pubsub/pubsub:items/pubsub:item'); | |
47
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
48 if (item == null) { |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
49 console.log('Failed to retrieve nickname: no item published.'); |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
50 hideSpinner(spinner_img); |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
51 return; |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
52 } |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
53 |
0 | 54 const id = item.getAttributeNS(null, 'id'); |
55 const nick = parseXPath(item, './nickname:nick'); | |
56 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
|
57 hideSpinner(spinner_img); |
0 | 58 } |
59 | |
47
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
60 function onNicknameRetrievalError(iq) |
0 | 61 { |
47
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
62 console.log('Failed to retrieve nickname: ' + parseErrorIq(iq)); |
15
3eed9fe0bd7c
End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
11
diff
changeset
|
63 hideSpinner(spinner_img); |
54
9ada1963558c
Disable the avatar access_model in more cases.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
52
diff
changeset
|
64 nick_access.disabled = true; |
0 | 65 } |
66 | |
34
45bd945c5722
Change all blur events to change, prevents a stanza when no change occured.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
15
diff
changeset
|
67 nick_input.addEventListener('change', function (evt) { |
47
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
68 const new_nickname = evt.target.value; |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
69 let iq; |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
70 let changed_callback; |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
71 if (new_nickname) { |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
72 iq = $iq({type: 'set'}) |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
73 .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}) |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
74 .c('publish', {node: 'http://jabber.org/protocol/nick'}) |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
75 .c('item', {id: 'current'}) |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
76 .c('nick', {xmlns: 'http://jabber.org/protocol/nick'}) |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
77 .t(new_nickname); |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
78 changed_callback = onNicknameChanged.bind(null, 'changed'); |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
79 } else { |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
80 iq = $iq({type: 'set'}) |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
81 .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub#owner'}) |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
82 .c('delete', {node: 'http://jabber.org/protocol/nick'}); |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
83 changed_callback = onNicknameChanged.bind(null, 'deleted'); |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
84 } |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
85 connection.sendIQ(iq, changed_callback, onNicknameChangeError); |
15
3eed9fe0bd7c
End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
11
diff
changeset
|
86 displaySpinner(spinner_img); |
54
9ada1963558c
Disable the avatar access_model in more cases.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
52
diff
changeset
|
87 nick_access.disabled = true; |
0 | 88 }); |
89 | |
47
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
90 function onNicknameChanged(action, result_iq) |
0 | 91 { |
47
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
92 console.log('Nickname successfully ' + action + '.'); |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
93 nick_access.disabled = true; |
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
94 if (action === 'changed') |
52
2f45bee88b47
Add pubsub#access_model retrieval for the avatar node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
48
diff
changeset
|
95 getAccessModel(); |
15
3eed9fe0bd7c
End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
11
diff
changeset
|
96 spinnerOk(spinner_img); |
0 | 97 } |
98 | |
99 function onNicknameChangeError(iq) | |
100 { | |
47
b76146a09e07
Add pubsub#access_model retrieval for the nickname node.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
35
diff
changeset
|
101 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
|
102 spinnerError(spinner_img); |
54
9ada1963558c
Disable the avatar access_model in more cases.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
52
diff
changeset
|
103 nick_access.disabled = true; |
0 | 104 } |
35
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
105 |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
106 nick_access.addEventListener('change', function (evt) { |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
107 const iq = configurePEPField('http://jabber.org/protocol/nick', 'pubsub#access_model', evt.target.value); |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
108 connection.sendIQ(iq, onNicknameConfigured, onNicknameConfigureError); |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
109 displaySpinner(access_spinner_img); |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
110 }); |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
111 |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
112 function onNicknameConfigured(iq) |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
113 { |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
114 console.log('Successfully set nickname access model.') |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
115 spinnerOk(access_spinner_img); |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
116 } |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
117 |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
118 function onNicknameConfigureError(iq) |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
119 { |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
120 const string = 'Failed to configure nick node: ' + parseErrorIq(iq); |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
121 console.log(string); |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
122 spinnerError(access_spinner_img, string); |
084202870d76
Add nickname access model configuration.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
34
diff
changeset
|
123 } |
0 | 124 } |