annotate roster.js @ 57:427807059ca8

Use `` in JS for templating, and add two forgotten strings.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 24 May 2020 12:51:07 +0200
parents 010b905a74d4
children 6d861d881b96
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 'use strict';
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 function initRoster(connection) {
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 const roster_table = document.getElementById('roster-table');
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 const iq = $iq({type: 'get'})
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 .c('query', {xmlns: NS.roster});
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 connection.sendIQ(iq, onRoster, onRosterError.bind(null, 'roster query failed.'));
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 function onRoster(result_iq)
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 {
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 const items = parseXPath(result_iq, './roster:query/roster:item', XPathResult.ORDERED_NODE_ITERATOR_TYPE);
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 while (true) {
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14 const item = items.iterateNext();
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15 if (!item)
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16 break;
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 const jid = item.getAttributeNS(null, 'jid');
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18 const subscription = item.getAttributeNS(null, 'subscription');
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 const name = item.getAttributeNS(null, 'name');
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20 const groups = item.children;
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21 const tr = document.createElementNS('http://www.w3.org/1999/xhtml', 'tr');
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22 let td = document.createElementNS('http://www.w3.org/1999/xhtml', 'td');
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 const a = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 a.setAttributeNS(null, 'href', 'xmpp:' + jid);
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 a.textContent = jid;
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 td.appendChild(a);
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 tr.appendChild(td);
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 td = document.createElementNS('http://www.w3.org/1999/xhtml', 'td');
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29 const input = document.createElementNS('http://www.w3.org/1999/xhtml', 'input');
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 input.value = name;
34
45bd945c5722 Change all blur events to change, prevents a stanza when no change occured.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
31 input.addEventListener('change', function (evt) {
4
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 const iq = $iq({type: 'set'})
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 .c('query', {xmlns: NS.roster})
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 .c('item', {jid: jid, name: evt.target.value});
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 for (let group of groups)
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 iq.c('group').t(group.textContent).up();
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 connection.sendIQ(iq, onRosterSet, onRosterSetError.bind(null, 'Roster set failed.'));
34
45bd945c5722 Change all blur events to change, prevents a stanza when no change occured.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 4
diff changeset
38 });
4
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 td.appendChild(input);
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 tr.appendChild(td);
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 td = document.createElementNS('http://www.w3.org/1999/xhtml', 'td');
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 td.textContent = subscription;
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 tr.appendChild(td);
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 td = document.createElementNS('http://www.w3.org/1999/xhtml', 'td');
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 for (let group of groups) {
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 const span = document.createElementNS('http://www.w3.org/1999/xhtml', 'span');
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 // TODO: use a tag system for the UI.
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 span.textContent = group.textContent;
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 td.appendChild(span);
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50 }
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51 tr.appendChild(td);
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 td = document.createElementNS('http://www.w3.org/1999/xhtml', 'td');
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 const button = document.createElementNS('http://www.w3.org/1999/xhtml', 'button');
38
c2e2b107c7c1 Commit changes from the July 2019 Lyon sprint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 34
diff changeset
54 button.classList.add('btn');
c2e2b107c7c1 Commit changes from the July 2019 Lyon sprint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 34
diff changeset
55 button.classList.add('btn-danger');
c2e2b107c7c1 Commit changes from the July 2019 Lyon sprint.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 34
diff changeset
56 button.textContent = '✕';
4
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57 button.onclick = function (evt) {
57
427807059ca8 Use `` in JS for templating, and add two forgotten strings.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 56
diff changeset
58 const really = confirm(`{% trans 'Do you really want to remove ${jid}?' %}`);
46
af2874ff7234 mathieui: Ask the user for confirmation before deleting PEP nodes or contacts.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 38
diff changeset
59 if (!really)
af2874ff7234 mathieui: Ask the user for confirmation before deleting PEP nodes or contacts.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 38
diff changeset
60 return;
4
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61 const iq = $iq({type: 'set'})
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 .c('query', {xmlns: NS.roster})
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63 .c('item', {jid: jid, subscription: 'unsubscribe'});
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64 connection.sendIQ(iq, onRosterSet.bind(node), onRosterSetError.bind(node, 'contact removal failed.'));
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 };
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66 td.appendChild(button);
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67 tr.appendChild(td);
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68 roster_table.appendChild(tr);
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
69 }
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
70 }
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72 function onRosterError(string)
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73 {
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
74 console.log('Failed to retrieve your contact list: ' + string);
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75 }
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77 function onRosterSet(result_iq)
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 {
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79 console.log(result_iq);
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80 }
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
81
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
82 function onRosterSetError(string)
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
83 {
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
84 console.log('Failed to retrieve your contact list: ' + string);
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
85 }
5e97e1808a35 Add support for the roster.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
86 }