view pep.js @ 60:6d861d881b96

Add license headers to all source files.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 24 May 2020 13:09:16 +0200
parents 427807059ca8
children
line wrap: on
line source

// SPDX-License-Identifier: AGPL-3.0-only
/*
 * Copyright © 2018-2020 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
 * Copyright © 2020 Mathieu Pasquet <mathieui@mathieui.net>
 *
 * Released under GNU AGPL v3 only, read the file 'LICENSE' for more information.
 */

'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.classList.add('btn');
        button.classList.add('btn-danger');
        button.textContent = '✕';
        button.onclick = function (evt) {
            const really = confirm(`{% trans 'Do you really want to remove the node ${node}?' %}`);
            if (!really)
                return;
            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);
    }
}