1/**
2 * Attach editing button to editable diagrams
3 */
4document.addEventListener('DOMContentLoaded', () => {
5    // check if the current page is editable by the current user
6    if (!document.querySelector('head link[rel="edit"]')) return;
7
8    document.querySelectorAll('object.diagrams-svg[data-pos]').forEach(embed => {
9        const button = document.createElement('button');
10        button.className = 'diagrams-btn';
11        button.innerText = LANG.plugins.diagrams.editButtonShort;
12        button.title = LANG.plugins.diagrams.editButton;
13
14        const icon = ButtonFunctions.getButtonIcon('edit');
15        button.prepend(icon);
16
17        button.addEventListener('click', event => {
18            event.preventDefault();
19            const diagramsEditor = new DiagramsEditor(() => {
20                // replace instead of reload to avoid accidentally re-submitting forms
21                window.location.replace(window.location.href);
22            });
23            diagramsEditor.editEmbed(
24                JSINFO.id,
25                parseInt(embed.getAttribute('data-pos')),
26                parseInt(embed.getAttribute('data-len'))
27            );
28        });
29
30        embed.parentNode.querySelector('.diagrams-buttons').appendChild(button);
31    });
32});
33