UNPKG

6.61 kBJavaScriptView Raw
1/*
2* Copyright (C) 1998-2021 by Northwoods Software Corporation. All Rights Reserved.
3*/
4/*
5* This is an extension and not part of the main GoJS library.
6* Note that the API for this class may change with any version, even point releases.
7* If you intend to use an extension in production, you should copy the code to your own source directory.
8* Extensions can be found in the GoJS kit under the extensions or extensionsTS folders.
9* See the Extensions intro page (https://gojs.net/latest/intro/extensions.html) for more information.
10*/
11import * as go from '../release/go-module.js';
12// HTML + JavaScript context menu, made with HTMLInfo
13// This file exposes one instance of HTMLInfo, window.myHTMLLightBox
14// see also LightBoxContextMenu.css and /samples/htmlLightBoxContextMenu.html
15(function (window) {
16 /* HTML for context menu:
17 <div id="contextMenuDIV">
18 <div id="cmLight"></div>
19 <div id="cmDark"></div>
20 </div>
21 */
22 const contextMenuDIV = document.createElement('div');
23 contextMenuDIV.id = 'contextMenuDIV';
24 // This is the actual HTML LightBox-style context menu, composed of buttons and a background:
25 const cmLight = document.createElement('div');
26 cmLight.id = 'cmLight';
27 cmLight.className = 'goCXforeground';
28 const cmDark = document.createElement('div');
29 cmDark.id = 'cmDark';
30 cmDark.className = 'goCXbackground';
31 contextMenuDIV.appendChild(cmLight);
32 contextMenuDIV.appendChild(cmDark);
33 const cxMenuButtons = [
34 {
35 text: 'Copy',
36 command: (diagram) => { diagram.commandHandler.copySelection(); },
37 isVisible: (diagram) => diagram.commandHandler.canCopySelection()
38 }, {
39 text: 'Cut',
40 command: (diagram) => { diagram.commandHandler.cutSelection(); },
41 isVisible: (diagram) => diagram.commandHandler.canCutSelection()
42 }, {
43 text: 'Delete',
44 command: (diagram) => { diagram.commandHandler.deleteSelection(); },
45 isVisible: (diagram) => diagram.commandHandler.canDeleteSelection()
46 }, {
47 text: 'Paste',
48 command: (diagram) => { diagram.commandHandler.pasteSelection(diagram.toolManager.contextMenuTool.mouseDownPoint); },
49 isVisible: (diagram) => diagram.commandHandler.canPasteSelection(diagram.toolManager.contextMenuTool.mouseDownPoint)
50 }, {
51 text: 'Select All',
52 command: (diagram) => { diagram.commandHandler.selectAll(); },
53 isVisible: (diagram) => diagram.commandHandler.canSelectAll()
54 }, {
55 text: 'Undo',
56 command: (diagram) => { diagram.commandHandler.undo(); },
57 isVisible: (diagram) => diagram.commandHandler.canUndo()
58 }, {
59 text: 'Redo',
60 command: (diagram) => { diagram.commandHandler.redo(); },
61 isVisible: (diagram) => diagram.commandHandler.canRedo()
62 }, {
63 text: 'Scroll To Part',
64 command: (diagram) => { diagram.commandHandler.scrollToPart(); },
65 isVisible: (diagram) => diagram.commandHandler.canScrollToPart()
66 }, {
67 text: 'Zoom To Fit',
68 command: (diagram) => { diagram.commandHandler.zoomToFit(); },
69 isVisible: (diagram) => diagram.commandHandler.canZoomToFit()
70 }, {
71 text: 'Reset Zoom',
72 command: (diagram) => { diagram.commandHandler.resetZoom(); },
73 isVisible: (diagram) => diagram.commandHandler.canResetZoom()
74 }, {
75 text: 'Group Selection',
76 command: (diagram) => { diagram.commandHandler.groupSelection(); },
77 isVisible: (diagram) => diagram.commandHandler.canGroupSelection()
78 }, {
79 text: 'Ungroup Selection',
80 command: (diagram) => { diagram.commandHandler.ungroupSelection(); },
81 isVisible: (diagram) => diagram.commandHandler.canUngroupSelection()
82 }, {
83 text: 'Edit Text',
84 command: (diagram) => { diagram.commandHandler.editTextBlock(); },
85 isVisible: (diagram) => diagram.commandHandler.canEditTextBlock()
86 }
87 ];
88 const $ = go.GraphObject.make;
89 const myContextMenu = $(go.HTMLInfo, {
90 show: showContextMenu,
91 hide: hideContextMenu
92 });
93 let firstTime = true;
94 function showContextMenu(obj, diagram, tool) {
95 if (firstTime) {
96 // We don't want the div acting as a context menu to have a (browser) context menu!
97 cmLight.addEventListener('contextmenu', (e) => { e.preventDefault(); return false; }, false);
98 cmLight.addEventListener('selectstart', (e) => { e.preventDefault(); return false; }, false);
99 contextMenuDIV.addEventListener('contextmenu', (e) => { e.preventDefault(); return false; }, false);
100 // Stop the context menu tool if you click on the dark part:
101 contextMenuDIV.addEventListener('click', (e) => { diagram.currentTool.stopTool(); return false; }, false);
102 firstTime = false;
103 }
104 // Empty the context menu and only show buttons that are relevant
105 cmLight.innerHTML = '';
106 const ul = document.createElement('ul');
107 cmLight.appendChild(ul);
108 for (let i = 0; i < cxMenuButtons.length; i++) {
109 const button = cxMenuButtons[i];
110 const command = button.command;
111 const isVisible = button.isVisible;
112 if (!(typeof command === 'function'))
113 continue;
114 // Only show buttons that have isVisible = true
115 if (typeof isVisible === 'function' && !isVisible(diagram))
116 continue;
117 const li = document.createElement('li');
118 const ahref = document.createElement('a');
119 ahref.href = '#';
120 ahref._command = button.command;
121 ahref.addEventListener('click', (e) => {
122 ahref._command(diagram);
123 tool.stopTool();
124 e.preventDefault();
125 return false;
126 }, false);
127 ahref.textContent = button.text;
128 li.appendChild(ahref);
129 ul.appendChild(li);
130 }
131 // show the whole LightBox context menu
132 document.body.appendChild(contextMenuDIV);
133 }
134 function hideContextMenu(diagram, tool) {
135 document.body.removeChild(contextMenuDIV);
136 }
137 window.myHTMLLightBox = myContextMenu;
138})(window);