Mercurial > xmpp-account-manager
annotate util.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 | d9da5c3e305d |
children | b15e1581c3d4 |
rev | line source |
---|---|
3
5aa1bf7154b0
Add a simple PEP node viewer and editor.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
1 const NS = { |
5aa1bf7154b0
Add a simple PEP node viewer and editor.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
2 xrd: 'http://docs.oasis-open.org/ns/xri/xrd-1.0', |
4
5e97e1808a35
Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3
diff
changeset
|
3 roster: 'jabber:iq:roster', |
3
5aa1bf7154b0
Add a simple PEP node viewer and editor.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
4 disco_items: 'http://jabber.org/protocol/disco#items', |
5aa1bf7154b0
Add a simple PEP node viewer and editor.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
5 disco_info: 'http://jabber.org/protocol/disco#info', |
5aa1bf7154b0
Add a simple PEP node viewer and editor.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
6 dataforms: 'jabber:x:data', |
5aa1bf7154b0
Add a simple PEP node viewer and editor.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
7 pubsub: 'http://jabber.org/protocol/pubsub', |
5aa1bf7154b0
Add a simple PEP node viewer and editor.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
8 pubsub_owner: 'http://jabber.org/protocol/pubsub#owner', |
5aa1bf7154b0
Add a simple PEP node viewer and editor.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
9 avatar_metadata: 'urn:xmpp:avatar:metadata', |
5aa1bf7154b0
Add a simple PEP node viewer and editor.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
10 avatar_data: 'urn:xmpp:avatar:data', |
5aa1bf7154b0
Add a simple PEP node viewer and editor.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
11 nickname: 'http://jabber.org/protocol/nick', |
25
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
24
diff
changeset
|
12 vcard4: 'urn:ietf:params:xml:ns:vcard-4.0', |
17
07543f7f5e89
Retrieve and change the default MAM prefs.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
15
diff
changeset
|
13 mam: 'urn:xmpp:mam:2', |
24
6c620e9f7d2c
Add support for retrieving all MAM messages, and for downloading it in a XEP-0227-like format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
19
diff
changeset
|
14 forward: 'urn:xmpp:forward:0', |
3
5aa1bf7154b0
Add a simple PEP node viewer and editor.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
15 }; |
5aa1bf7154b0
Add a simple PEP node viewer and editor.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
16 |
1
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
17 function nsResolver(prefix) { |
3
5aa1bf7154b0
Add a simple PEP node viewer and editor.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
18 return NS[prefix] || null; |
1
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
19 } |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
20 |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
21 function parseXPath(elem, xpath, result) |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
22 { |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
23 if (result === undefined) |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
24 result = XPathResult.FIRST_ORDERED_NODE_TYPE; |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
25 const value = elem.getRootNode().evaluate(xpath, elem, nsResolver, result, null); |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
26 if (result == XPathResult.FIRST_ORDERED_NODE_TYPE) |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
27 return value.singleNodeValue; |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
28 return value; |
d6df73b466f6
Implement XEP-0156 to discover the right BOSH endpoint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
29 } |
3
5aa1bf7154b0
Add a simple PEP node viewer and editor.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
30 |
25
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
24
diff
changeset
|
31 function parseXPathText(elem, xpath) |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
24
diff
changeset
|
32 { |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
24
diff
changeset
|
33 const value = parseXPath(elem, xpath); |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
24
diff
changeset
|
34 if (value === null) |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
24
diff
changeset
|
35 return null; |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
24
diff
changeset
|
36 return value.textContent; |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
24
diff
changeset
|
37 } |
d9da5c3e305d
Add support for setting some vCard4 fields.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
24
diff
changeset
|
38 |
3
5aa1bf7154b0
Add a simple PEP node viewer and editor.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
39 function configurePEPField(node, key, value, cb, err_cb) { |
5aa1bf7154b0
Add a simple PEP node viewer and editor.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
40 return $iq({type: 'set'}) |
5aa1bf7154b0
Add a simple PEP node viewer and editor.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
41 .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub#owner'}) |
5aa1bf7154b0
Add a simple PEP node viewer and editor.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
42 .c('configure', {node: node}) |
5aa1bf7154b0
Add a simple PEP node viewer and editor.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
43 .c('x', {xmlns: 'jabber:x:data', type: 'submit'}) |
5aa1bf7154b0
Add a simple PEP node viewer and editor.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
44 .c('field', {'var': 'FORM_TYPE', type: 'hidden'}) |
5aa1bf7154b0
Add a simple PEP node viewer and editor.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
45 .c('value') |
5aa1bf7154b0
Add a simple PEP node viewer and editor.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
46 .t('http://jabber.org/protocol/pubsub#node_config') |
5aa1bf7154b0
Add a simple PEP node viewer and editor.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
47 .up() |
5aa1bf7154b0
Add a simple PEP node viewer and editor.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
48 .up() |
5aa1bf7154b0
Add a simple PEP node viewer and editor.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
49 .c('field', {'var': key}) |
5aa1bf7154b0
Add a simple PEP node viewer and editor.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
50 .c('value') |
5aa1bf7154b0
Add a simple PEP node viewer and editor.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
51 .t(value) |
5aa1bf7154b0
Add a simple PEP node viewer and editor.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
52 .up() |
5aa1bf7154b0
Add a simple PEP node viewer and editor.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
53 .up(); |
5aa1bf7154b0
Add a simple PEP node viewer and editor.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
54 } |
15
3eed9fe0bd7c
End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
55 |
30
9ba4f8cc32f1
Display the returned error in the title of the spinner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
56 function parseErrorIq(iq) { |
9ba4f8cc32f1
Display the returned error in the title of the spinner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
57 // TODO: actually check that it is the first one. |
9ba4f8cc32f1
Display the returned error in the title of the spinner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
58 const error = iq.firstChild; |
9ba4f8cc32f1
Display the returned error in the title of the spinner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
59 if (error.namespaceURI !== 'jabber:client' || error.localName !== 'error') |
9ba4f8cc32f1
Display the returned error in the title of the spinner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
60 return null; |
9ba4f8cc32f1
Display the returned error in the title of the spinner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
61 const condition = error.firstChild; |
9ba4f8cc32f1
Display the returned error in the title of the spinner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
62 if (condition.namespaceURI !== 'urn:ietf:params:xml:ns:xmpp-stanzas') |
9ba4f8cc32f1
Display the returned error in the title of the spinner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
63 return null; |
9ba4f8cc32f1
Display the returned error in the title of the spinner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
64 const text = error.lastChild; |
9ba4f8cc32f1
Display the returned error in the title of the spinner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
65 if (text.namespaceURI !== 'urn:ietf:params:xml:ns:xmpp-stanzas' || text.localName !== 'text') |
9ba4f8cc32f1
Display the returned error in the title of the spinner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
66 return null; |
9ba4f8cc32f1
Display the returned error in the title of the spinner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
67 return [condition.localName, text.textContent]; |
9ba4f8cc32f1
Display the returned error in the title of the spinner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
68 } |
9ba4f8cc32f1
Display the returned error in the title of the spinner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
69 |
15
3eed9fe0bd7c
End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
70 function displaySpinner(spinner) { |
19
bde4f0bbfe69
Clear a spinner’s timeout on change if it had been set previously.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
17
diff
changeset
|
71 if ('timeoutid' in spinner.dataset) |
bde4f0bbfe69
Clear a spinner’s timeout on change if it had been set previously.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
17
diff
changeset
|
72 clearTimeout(spinner.dataset.timeoutid); |
15
3eed9fe0bd7c
End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
73 spinner.src = 'spinner.svg'; |
30
9ba4f8cc32f1
Display the returned error in the title of the spinner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
74 spinner.title = ''; |
15
3eed9fe0bd7c
End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
75 spinner.hidden = false; |
3eed9fe0bd7c
End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
76 } |
3eed9fe0bd7c
End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
77 |
3eed9fe0bd7c
End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
78 function spinnerOk(spinner) { |
19
bde4f0bbfe69
Clear a spinner’s timeout on change if it had been set previously.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
17
diff
changeset
|
79 if ('timeoutid' in spinner.dataset) |
bde4f0bbfe69
Clear a spinner’s timeout on change if it had been set previously.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
17
diff
changeset
|
80 clearTimeout(spinner.dataset.timeoutid); |
15
3eed9fe0bd7c
End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
81 spinner.src = 'ok.svg'; |
30
9ba4f8cc32f1
Display the returned error in the title of the spinner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
82 spinner.title = ''; |
15
3eed9fe0bd7c
End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
83 spinner.hidden = false; |
19
bde4f0bbfe69
Clear a spinner’s timeout on change if it had been set previously.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
17
diff
changeset
|
84 spinner.dataset.timeoutid = setTimeout(function () { |
15
3eed9fe0bd7c
End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
85 spinner.hidden = true; |
3eed9fe0bd7c
End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
86 }, 1000); |
3eed9fe0bd7c
End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
87 } |
3eed9fe0bd7c
End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
88 |
30
9ba4f8cc32f1
Display the returned error in the title of the spinner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
89 function spinnerError(spinner, title) { |
19
bde4f0bbfe69
Clear a spinner’s timeout on change if it had been set previously.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
17
diff
changeset
|
90 if ('timeoutid' in spinner.dataset) |
bde4f0bbfe69
Clear a spinner’s timeout on change if it had been set previously.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
17
diff
changeset
|
91 clearTimeout(spinner.dataset.timeoutid); |
15
3eed9fe0bd7c
End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
92 spinner.src = 'error.svg'; |
30
9ba4f8cc32f1
Display the returned error in the title of the spinner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
25
diff
changeset
|
93 spinner.title = title ? title : ''; |
15
3eed9fe0bd7c
End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
94 spinner.hidden = false; |
3eed9fe0bd7c
End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
95 } |
3eed9fe0bd7c
End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
96 |
3eed9fe0bd7c
End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
97 function hideSpinner(spinner) { |
19
bde4f0bbfe69
Clear a spinner’s timeout on change if it had been set previously.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
17
diff
changeset
|
98 if ('timeoutid' in spinner.dataset) |
bde4f0bbfe69
Clear a spinner’s timeout on change if it had been set previously.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
17
diff
changeset
|
99 clearTimeout(spinner.dataset.timeoutid); |
15
3eed9fe0bd7c
End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
100 spinner.hidden = true; |
3eed9fe0bd7c
End spinners with either a green ✔ or a red ✘.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
101 } |