xref: /plugin/diagrams/script.js (revision bc39777fa364696c1e0053573aadb2d26ce57bd2)
1jQuery(function () {
2    /* DOKUWIKI:include script/helpers.js */
3    /* DOKUWIKI:include script/service.js */
4    /* DOKUWIKI:include script/elements.js */
5    /* DOKUWIKI:include script/mediamanager.js */
6
7    // add diagram edit button to diagram SVGs included in wiki pages
8    const $images = jQuery('object').filter('.diagrams-svg');
9
10    // collect image IDs with file extension
11    const imageIds = $images.map(function (key, image) {
12        return extractIdFromMediaUrl(image.data);
13    }).toArray();
14
15    let ajaxData = {};
16    ajaxData['call'] = 'plugin_diagrams_images';
17    ajaxData['images'] = imageIds;
18
19    // callback to attach buttons to editable diagrams
20    const attachButtons = function (result) {
21        const diagrams = JSON.parse(result);
22        $images.each(function () {
23            const id = extractIdFromMediaUrl(this.data);
24            const $current = jQuery(this);
25            if (diagrams.includes(id)) {
26                let $editButton = editDiagramButton(id);
27                if ($current.parent()[0].nodeName === 'A') {
28                    $current.parent().after("<br>", $editButton);
29                } else {
30                    $current.after("<br>", $editButton);
31                }
32            }
33        });
34    };
35
36    // query backend about permissions and SVG properties before attaching edit buttons
37    jQuery.get(
38        DOKU_BASE + 'lib/exe/ajax.php',
39        ajaxData,
40        attachButtons
41    );
42
43
44});
45
46// open links in diagrams in the browser window instead of SVG frame
47jQuery(window).on('load', function() {
48    jQuery('object.diagrams-svg').each( function() {
49        jQuery(this.contentDocument).find('svg').find('a').attr({'target': '_parent', 'style': 'pointer-events: all;'});
50    });
51});
52