comparison pep.js @ 3:5aa1bf7154b0

Add a simple PEP node viewer and editor.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 22 Dec 2018 02:23:38 +0100
parents
children c2e2b107c7c1
comparison
equal deleted inserted replaced
2:db033e5eabcb 3:5aa1bf7154b0
1 'use strict';
2
3 function initPEP(connection) {
4 const pep_table = document.getElementById('pep-table');
5
6 const iq = $iq({type: 'get'})
7 .c('query', {xmlns: NS.disco_items});
8 connection.sendIQ(iq, onDiscoItems, onDiscoItemsError.bind(null, 'disco#items query failed.'));
9
10 function onDiscoItems(result_iq)
11 {
12 const items = parseXPath(result_iq, './disco_items:query/disco_items:item', XPathResult.ORDERED_NODE_ITERATOR_TYPE);
13 while (true) {
14 const item = items.iterateNext();
15 if (!item)
16 break;
17 const node = item.getAttributeNS(null, 'node');
18 const tr = document.createElementNS('http://www.w3.org/1999/xhtml', 'tr');
19 const td = document.createElementNS('http://www.w3.org/1999/xhtml', 'td');
20 td.textContent = node;
21 tr.appendChild(td);
22 pep_table.appendChild(tr);
23 const iq = $iq({type: 'get'})
24 .c('query', {xmlns: NS.disco_info, node: node});
25 connection.sendIQ(iq, onDiscoInfo.bind(tr), onDiscoInfoError.bind(tr, 'disco#items query failed.'));
26 }
27 }
28
29 function onDiscoItemsError(string)
30 {
31 console.log('Failed to retrieve the list of PEP nodes: ' + string);
32 }
33
34 function onDiscoInfo(result_iq)
35 {
36 const tr = this;
37 const node = parseXPath(result_iq, './disco_info:query').getAttributeNS(null, 'node');
38 const fields = parseXPath(result_iq, './disco_info:query/dataforms:x/dataforms:field', XPathResult.ORDERED_NODE_ITERATOR_TYPE);
39 const parsed_fields = {};
40 while (true) {
41 const field = fields.iterateNext();
42 if (!field)
43 break;
44 const type = field.getAttributeNS(null, 'type');
45 if (type == 'hidden')
46 continue;
47 const var_ = field.getAttributeNS(null, 'var');
48 const value = parseXPath(field, './dataforms:value');
49 parsed_fields[var_] = value ? value.textContent : '';
50 }
51 for (let i of ['pubsub#title', 'pubsub#description', 'pubsub#type']) {
52 const td = document.createElementNS('http://www.w3.org/1999/xhtml', 'td');
53 const input = document.createElementNS('http://www.w3.org/1999/xhtml', 'input');
54 input.value = parsed_fields[i];
55 input.onblur = function (evt) {
56 const iq = configurePEPField(node, i, evt.target.value);
57 connection.sendIQ(iq, onPEPConfigured, onPEPConfigureError.bind(null, 'PubSub configuration failed.'));
58 };
59 td.appendChild(input);
60 tr.appendChild(td);
61 }
62 const td = document.createElementNS('http://www.w3.org/1999/xhtml', 'td');
63 const button = document.createElementNS('http://www.w3.org/1999/xhtml', 'button');
64 button.textContent = 'Delete this node';
65 button.onclick = function (evt) {
66 const iq = $iq({type: 'set'})
67 .c('pubsub', {xmlns: NS.pubsub_owner})
68 .c('delete', {node: node});
69 connection.sendIQ(iq, onNodeDeleted.bind(node), onNodeDeletionError.bind(node, 'PEP node deletion failed.'));
70 };
71 td.appendChild(button);
72 tr.appendChild(td);
73 }
74
75 function onDiscoInfoError(string)
76 {
77 console.log('Failed to retrieve PEP node info: ' + string);
78 }
79
80 function onPEPConfigured(result_iq)
81 {
82 console.log('PEP node successfully configured.');
83 }
84
85 function onPEPConfigureError(string)
86 {
87 console.log('Configuration of PEP node failed: ' + string);
88 }
89
90 function onNodeDeleted(result_iq)
91 {
92 const node = this;
93 for (let child of pep_table.children) {
94 if (node != child.firstChild.textContent)
95 continue
96 child.remove();
97 break;
98 }
99 console.log('Node “' + node + '” successfully deleted.');
100 }
101
102 function onNodeDeletionError(string)
103 {
104 const node = this;
105 console.log('Deletion of PEP node “' + node + '” failed: ' + string);
106 }
107 }