美文网首页mxGraph
mxgraph示例解析(-) menustyle右键菜单

mxgraph示例解析(-) menustyle右键菜单

作者: JuvoS | 来源:发表于2019-07-23 13:45 被阅读0次
    menustyle右键菜单效果图

    官方示例

    <style type="text/css">
    body div.mxPopupMenu {
        -webkit-box-shadow: 3px 3px 6px #C0C0C0;
        -moz-box-shadow: 3px 3px 6px #C0C0C0;
        box-shadow: 3px 3px 6px #C0C0C0;
        background: white;
        position: absolute;
        border: 3px solid #e7e7e7;
        padding: 3px;
    }
    body table.mxPopupMenu {
        border-collapse: collapse;
        margin: 0px;
    }
    body tr.mxPopupMenuItem {
        color: black;
        cursor: default;
    }
    body td.mxPopupMenuItem {
        padding: 6px 60px 6px 30px;
        font-family: Arial;
        font-size: 10pt;
    }
    body td.mxPopupMenuIcon {
        background-color: white;
        padding: 0px;
    }
    body tr.mxPopupMenuItemHover {
        background-color: #eeeeee;
        color: black;
    }
    table.mxPopupMenu hr {
        border-top: solid 1px #cccccc;
    }
    table.mxPopupMenu tr {
        font-size: 4pt;
    }
    </style>
    
    <script type="text/javascript">
    // Disables built-in context menu
    mxEvent.disableContextMenu(document.body);
    
    // Changes some default colors
    mxConstants.HANDLE_FILLCOLOR = '#99ccff';
    mxConstants.HANDLE_STROKECOLOR = '#0088cf';
    mxConstants.VERTEX_SELECTION_COLOR = '#00a8ff';
    
    // Creates the graph inside the given container
    var graph = new mxGraph(container);
    graph.setTooltips(true);
    
    // Enables rubberband selection
    new mxRubberband(graph);
    
    // Gets the default parent for inserting new cells. This
    // is normally the first child of the root (ie. layer 0).
    var parent = graph.getDefaultParent();
                    
    // Adds cells to the model in a single step
    graph.getModel().beginUpdate();
    try
    {
        var v1 = graph.insertVertex(parent, null, 'Hello,', 20, 20, 80, 30);
        var v2 = graph.insertVertex(parent, null, 'World!', 200, 150, 80, 30);
        var e1 = graph.insertEdge(parent, null, '', v1, v2);
    }
    finally
    {
        // Updates the display
        graph.getModel().endUpdate();
    }
    
    // Creates a new overlay with an image and a tooltip and makes it "transparent" to events
    var overlay = new mxCellOverlay(new mxImage('editors/images/overlays/check.png', 16, 16), 'Overlay tooltip');
    
    var mxCellRendererInstallCellOverlayListeners = mxCellRenderer.prototype.installCellOverlayListeners;
    mxCellRenderer.prototype.installCellOverlayListeners = function(state, overlay, shape)
    {
        mxCellRendererInstallCellOverlayListeners.apply(this, arguments);
        var graph  = state.view.graph;
    
        mxEvent.addGestureListeners(shape.node,
            function (evt)
            {
                graph.fireMouseEvent(mxEvent.MOUSE_DOWN, new mxMouseEvent(evt, state));
            },
            function (evt)
            {
                graph.fireMouseEvent(mxEvent.MOUSE_MOVE, new mxMouseEvent(evt, state));
            },
            function (evt)
            {
                if (mxClient.IS_QUIRKS)
                {
                    graph.fireMouseEvent(mxEvent.MOUSE_UP, new mxMouseEvent(evt, state));
                }
            });
        
        if (!mxClient.IS_TOUCH)
        {
            mxEvent.addListener(shape.node, 'mouseup', function (evt)
            {
                overlay.fireEvent(new mxEventObject(mxEvent.CLICK,
                        'event', evt, 'cell', state.cell));
            });
        }
    };
    
    // Sets the overlay for the cell in the graph
    graph.addCellOverlay(v1, overlay);
    
    // Configures automatic expand on mouseover
    graph.popupMenuHandler.autoExpand = true;
    
    // Installs context menu
    graph.popupMenuHandler.factoryMethod = function(menu, cell, evt)
    {
        menu.addItem('Item 1', null, function()
        {
            alert('Item 1');
        });
        
        menu.addItem('Item 2', null, function()
        {
            alert('Item 2');
        });
    
        menu.addSeparator();
        
        var submenu1 = menu.addItem('Submenu 1', null, null);
        
        menu.addItem('Subitem 1', null, function()
        {
            alert('Subitem 1');
        }, submenu1);
        menu.addItem('Subitem 1', null, function()
        {
            alert('Subitem 2');
        }, submenu1);
    };
    </script>
    

    实现原理

    • 禁用右键菜单
    mxEvent.disableContextMenu(document.body);
    
    • 创建右键菜单
    // Configures automatic expand on mouseover
    graph.popupMenuHandler.autoExpand = true;
    
    // Installs context menu
    graph.popupMenuHandler.factoryMethod = function(menu, cell, evt)
    {
        menu.addItem('Item 1', null, function()
        {
            alert('Item 1');
        });
        
        menu.addItem('Item 2', null, function()
        {
            alert('Item 2');
        });
    
        menu.addSeparator();
        
        var submenu1 = menu.addItem('Submenu 1', null, null);
        
        menu.addItem('Subitem 1', null, function()
        {
            alert('Subitem 1');
        }, submenu1);
        menu.addItem('Subitem 1', null, function()
        {
            alert('Subitem 2');
        }, submenu1);
    };
    

    简化示例

    // Configures automatic expand on mouseover
    graph.popupMenuHandler.autoExpand = true;
    
    // Installs context menu
    graph.popupMenuHandler.factoryMethod = function(menu, cell, evt)
    {
        menu.addItem('Item 1', null, function()
        {
            alert('Item 1');
        });
        
        menu.addItem('Item 2', null, function()
        {
            alert('Item 2');
        });
    
        menu.addSeparator();
        
        var submenu1 = menu.addItem('Submenu 1', null, null);
        
        menu.addItem('Subitem 1', null, function()
        {
            alert('Subitem 1');
        }, submenu1);
        menu.addItem('Subitem 1', null, function()
        {
            alert('Subitem 2');
        }, submenu1);
    };
    

    相关文章

      网友评论

        本文标题:mxgraph示例解析(-) menustyle右键菜单

        本文链接:https://www.haomeiwen.com/subject/rrrolctx.html