view 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
line wrap: on
line source

'use strict';

function initPEP(connection) {
    const pep_table = document.getElementById('pep-table');

    const iq = $iq({type: 'get'})
        .c('query', {xmlns: NS.disco_items});
    connection.sendIQ(iq, onDiscoItems, onDiscoItemsError.bind(null, 'disco#items query failed.'));

    function onDiscoItems(result_iq)
    {
        const items = parseXPath(result_iq, './disco_items:query/disco_items:item', XPathResult.ORDERED_NODE_ITERATOR_TYPE);
        while (true) {
            const item = items.iterateNext();
            if (!item)
                break;
            const node = item.getAttributeNS(null, 'node');
            const tr = document.createElementNS('http://www.w3.org/1999/xhtml', 'tr');
            const td = document.createElementNS('http://www.w3.org/1999/xhtml', 'td');
            td.textContent = node;
            tr.appendChild(td);
            pep_table.appendChild(tr);
            const iq = $iq({type: 'get'})
                .c('query', {xmlns: NS.disco_info, node: node});
            connection.sendIQ(iq, onDiscoInfo.bind(tr), onDiscoInfoError.bind(tr, 'disco#items query failed.'));
        }
    }

    function onDiscoItemsError(string)
    {
        console.log('Failed to retrieve the list of PEP nodes: ' + string);
    }

    function onDiscoInfo(result_iq)
    {
        const tr = this;
        const node = parseXPath(result_iq, './disco_info:query').getAttributeNS(null, 'node');
        const fields = parseXPath(result_iq, './disco_info:query/dataforms:x/dataforms:field', XPathResult.ORDERED_NODE_ITERATOR_TYPE);
        const parsed_fields = {};
        while (true) {
            const field = fields.iterateNext();
            if (!field)
                break;
            const type = field.getAttributeNS(null, 'type');
            if (type == 'hidden')
                continue;
            const var_ = field.getAttributeNS(null, 'var');
            const value = parseXPath(field, './dataforms:value');
            parsed_fields[var_] = value ? value.textContent : '';
        }
        for (let i of ['pubsub#title', 'pubsub#description', 'pubsub#type']) {
            const td = document.createElementNS('http://www.w3.org/1999/xhtml', 'td');
            const input = document.createElementNS('http://www.w3.org/1999/xhtml', 'input');
            input.value = parsed_fields[i];
            input.onblur = function (evt) {
                const iq = configurePEPField(node, i, evt.target.value);
                connection.sendIQ(iq, onPEPConfigured, onPEPConfigureError.bind(null, 'PubSub configuration failed.'));
            };
            td.appendChild(input);
            tr.appendChild(td);
        }
        const td = document.createElementNS('http://www.w3.org/1999/xhtml', 'td');
        const button = document.createElementNS('http://www.w3.org/1999/xhtml', 'button');
        button.textContent = 'Delete this node';
        button.onclick = function (evt) {
            const iq = $iq({type: 'set'})
                .c('pubsub', {xmlns: NS.pubsub_owner})
                    .c('delete', {node: node});
            connection.sendIQ(iq, onNodeDeleted.bind(node), onNodeDeletionError.bind(node, 'PEP node deletion failed.'));
        };
        td.appendChild(button);
        tr.appendChild(td);
    }

    function onDiscoInfoError(string)
    {
        console.log('Failed to retrieve PEP node info: ' + string);
    }

    function onPEPConfigured(result_iq)
    {
        console.log('PEP node successfully configured.');
    }

    function onPEPConfigureError(string)
    {
        console.log('Configuration of PEP node failed: ' + string);
    }

    function onNodeDeleted(result_iq)
    {
        const node = this;
        for (let child of pep_table.children) {
            if (node != child.firstChild.textContent)
                continue
            child.remove();
            break;
        }
        console.log('Node “' + node + '” successfully deleted.');
    }

    function onNodeDeletionError(string)
    {
        const node = this;
        console.log('Deletion of PEP node “' + node + '” failed: ' + string);
    }
}