xref: /plugin/diagrams/script/mediafile-linkfix.js (revision 9630af60459ba461ada65795f3ea7ba94ebf4ceb)
1/**
2 * Open links in diagrams in the browser window instead of SVG frame
3 */
4window.addEventListener('load', function () {
5    /**
6     * Sets _top target for links within SVG
7     */
8    function manipulateLinkTarget() {
9        jQuery('object.diagrams-svg').each(function () {
10            jQuery(this.contentDocument).find('svg a').not('[target]').attr('target', '_top');
11        });
12    }
13
14    /* template agnostic selector */
15    const observable = document.querySelector('body');
16
17    const bodyObserver = new MutationObserver((mutationsList, observer) => {
18        for (const mutation of mutationsList) {
19            if (mutation.type !== 'childList') continue;
20
21            const nodes = mutation.addedNodes;
22
23            nodes.forEach(node => {
24                jQuery(node).find('object.diagrams-svg').each(function () {
25                    // SVG clicks do not bubble up, so we use an available event
26                    // FIXME this will not work on mobile devices
27                    jQuery(this).on('mouseover', manipulateLinkTarget);
28                });
29            });
30        }
31    });
32
33    /* rewrite link targets when document is initially loaded */
34    manipulateLinkTarget();
35
36    /**
37     * Observe DOM changes
38     *
39     * FIXME this should no longer be necessary after Jack
40     * @see https://github.com/dokuwiki/dokuwiki/pull/3957
41     */
42    bodyObserver.observe(observable, {
43        attributes: true,
44        childList: true,
45        subtree: true }
46    );
47});
48