xref: /plugin/diagrams/script.js (revision 222c56dd1eafb02ed58de158012730ec108aa198)
1jQuery( document ).ready( function() {
2    if( JSINFO['iseditor'] ) {
3        jQuery( 'img, object' ).filter( '.media, .medialeft, .mediacenter, .mediaright' ).add( 'iframe.svgpureinsert' ).each( function() {
4            var current = jQuery( this );
5            var src = this.nodeName == 'OBJECT' ? current.attr( 'data' ) : current.attr( 'src' );
6            var extension = src.split( '.' ).pop().toLowerCase();
7            if( extension == 'svg' ) {
8                var editlink = '<br><button class="drawio-btn btn btn-default btn-xs" style="clear:both" data-id="' + src.split('media=')[1].split('&')[0] + '">Editieren</button>';
9                if( current.parent()[0].nodeName == 'A' ) {
10                    current.parent().after( editlink );
11                } else {
12                    current.after( editlink );
13                }
14            }
15        } );
16    }
17
18    jQuery( 'button.drawio-btn' ).on( 'click', function() {
19        var drawio_url = 'https://www.draw.io/?embed=1&proto=json&spin=1';
20
21        if( !jQuery( '#drawio-frame' )[0] ) {
22            var fullId = jQuery( this ).data( 'id' );
23            var id = fullId;
24            var ns = '';
25            var ext = id.split( '.' ).pop().toLowerCase();
26            var idParts = id.split( ':' );
27            if( idParts.length > 1 ) {
28                ns = idParts[0];
29                id = idParts.slice( 1 ).join( ':' );
30            }
31            jQuery( 'body' ).append( '<iframe id="drawio-frame" style="border: 0;position: fixed; top: 0; left: 0; right:0; bottom: 0; width:100%; height:100%; z-index: 9999;"></iframe>' );
32            var onmessage = function( e ) {
33                var msg = JSON.parse( e.originalEvent.data );
34                var drawio = jQuery( '#drawio-frame' )[0].contentWindow;
35                if( msg.event == 'init' ) {
36                    jQuery.get( DOKU_BASE + 'lib/exe/fetch.php?media=' + fullId, function( data ) {
37                        drawio.postMessage( JSON.stringify( {action: 'load', xml: data} ), '*' );
38                    }, 'text' );
39                } else if( msg.event == 'save' ) {
40                    drawio.postMessage( JSON.stringify( {action: 'export', format: 'xmlsvg', spin: 'Speichern' } ), '*' );
41                } else if( msg.event == 'export' ) {
42                    if( msg.format != 'svg' ) {
43                        alert( 'Nicht unterstützt!' );
44                    } else {
45                        var datastr = '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n' +
46                            decodeURIComponent( atob( msg.data.split( ',' )[1] ).split( '' ).map( function( c ) {
47                                return '%' + ( '00' + c.charCodeAt( 0 ).toString( 16 ) ).slice( -2 );
48                            } ).join( '' ) );
49                        jQuery.post( DOKU_BASE + 'lib/exe/ajax.php?call=mediaupload&ow=true&ns=' + ns + '&qqfile=' + id + '&sectok=' + JSINFO['sectok'], datastr )
50                            .done( function() {
51                                jQuery( window ).off( 'message', onmessage );
52                                jQuery( '#drawio-frame' ).remove();
53                                setTimeout( function() {
54                                    location.reload();
55                                }, 200 );
56                            } ).fail( function() {
57                                alert( 'Fehler beim Speichern' );
58                            } );
59                    }
60                } else if( msg.event == 'exit' ) {
61                    jQuery( window ).off( 'message', onmessage );
62                    jQuery( '#drawio-frame' ).remove();
63                }
64            };
65            jQuery( window ).on( 'message', onmessage );
66            jQuery( '#drawio-frame' ).attr( 'src', drawio_url );
67        }
68    } );
69
70    jQuery( 'a#drawio-newfile-create' ).on( 'click', function( e ) {
71        e.preventDefault();
72        var ns = NS;
73        var id = prompt( 'Name des neuen Diagramms' );
74        if( !/^[\w][\w\.\-]*$/.test( id ) ) {
75            alert( 'Dateiname enthält ungültige Zeichen' );
76            return;
77        }
78        id += '.svg';
79        var datastr = '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' +
80            '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="1px" height="1px" version="1.1" content="&lt;mxfile userAgent=&quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36&quot; version=&quot;7.9.5&quot; editor=&quot;www.draw.io&quot;&gt;&lt;diagram id=&quot;8c846276-93cf-00fc-3101-d1fabb6ae99a&quot; name=&quot;Seite-1&quot;&gt;dZHBEoIgEIafhrtCNXY2q0snD51JEJjQdRBH6+nTwIyxuLB8/7+7sCCSVsPJ0EZegHGNcMQGRA4I43iDd+M2kYcjSbJ3QBjFvGkBuXpyDyNPO8V4GxgtgLaqCWEBdc0LGzBqDPShrQQddm2o4CuQF1Sv6VUxK/0rttHCz1wJOXeOI6/caHEXBrra90OYlO/l5IrOtby/lZRB/4VIhkhqAKyLqiHleprtPDaXd/yjfu5teG1/JIzBUns8BB9Ishc=&lt;/diagram&gt;&lt;/mxfile&gt;" style="background-color: rgb(255, 255, 255);"><defs/><g transform="translate(0.5,0.5)"/></svg>';
81        jQuery.post( DOKU_BASE + 'lib/exe/ajax.php?call=mediaupload&ns=' + ns + '&qqfile=' + id + '&sectok=' + JSINFO['sectok'], datastr )
82            .done( function( response ) {
83                if( response.error ) {
84                    alert( 'Fehler beim Speichern: ' + response.error );
85                } else {
86                    alert( 'Diagramm ' + response.id + ' angelegt' );
87                }
88            } ).fail( function() {
89                alert( 'Fehler beim Speichern' );
90            } );
91    } );
92} );
93