xref: /plugin/diagrams/script/download.js (revision 0d9707b122de3cf6c290e57e492e9acf4cdcd62d)
1/**
2 * Attach download and open buttons to diagrams
3 */
4document.addEventListener('DOMContentLoaded', () => {
5
6    document.querySelectorAll('div.diagrams-buttons').forEach(diagramActions => {
7        $diagram = jQuery(diagramActions.parentNode.querySelector('object.diagrams-svg'));
8        const url = $diagram.attr('data');
9
10        let fileName = 'diagram';
11        if (typeof $diagram.data('id') !== "undefined") {
12            fileName = $diagram.data('id').split(':').pop();
13        }
14        // TODO num from section?
15
16        // download
17        const downloadLink = document.createElement('a');
18        downloadLink.href = url;
19        downloadLink.setAttribute('download', fileName);
20        const downloadButton = document.createElement('button');
21        downloadButton.className = 'diagrams-btn';
22        downloadButton.innerText = LANG.plugins.diagrams.downloadButtonShort;
23        downloadButton.title = LANG.plugins.diagrams.downloadButton;
24
25        const downloadIcon = DiagramsFunctions.getButtonIcon('download');
26        downloadButton.prepend(downloadIcon);
27        downloadLink.appendChild(downloadButton);
28        diagramActions.prepend(downloadLink);
29
30        // open
31        const openButton = document.createElement('button');
32        openButton.className = 'diagrams-btn';
33        openButton.innerText = LANG.plugins.diagrams.openButtonShort;
34        openButton.title = LANG.plugins.diagrams.openButton;
35
36        const openIcon = DiagramsFunctions.getButtonIcon('open');
37        openButton.prepend(openIcon);
38
39        openButton.addEventListener('click', event => {
40            event.preventDefault();
41            window.location = url;
42        });
43
44        diagramActions.prepend(openButton);
45    });
46});
47
48
49