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