forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverview.html
More file actions
330 lines (293 loc) · 10.4 KB
/
serverview.html
File metadata and controls
330 lines (293 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<!--
Copyright (c) 2006-2013, JGraph Ltd
Server-View example for mxGraph. This example demonstrates using
a server-side image of the graph as the diagram in the client. This
may be used to improve drawing-speed in older browser and on devices
with slower processors.
-->
<html>
<head>
<title>mxGraph using Server-side Image</title>
<!-- Sets the basepath for the library if not in same directory -->
<script type="text/javascript">
mxBasePath = '/mxgraph/javascript/src';
</script>
<!-- Loads and initializes the library -->
<script type="text/javascript" src="/mxgraph/javascript/src/js/mxClient.js"></script>
<!-- Overrides certain methods required for the server-side-image to be used. -->
<script type="text/javascript">
// Makes the background of the in-place editor non-transparent
var previousStartEditing = mxCellEditor.prototype.startEditing;
mxCellEditor.prototype.startEditing = function (cell, trigger)
{
previousStartEditing.apply(this, arguments);
var state = this.graph.getView().getState(cell);
if (state != null)
{
var color = mxUtils.getValue(state.style, mxConstants.STYLE_FILLCOLOR, 'white');
this.textarea.style.background = color;
}
};
// Replaces the event firing mechanism in the graph view since there are
// no longer any DOM elements that fire events for the actual states we
// have to find the state under the mouse using graph.getCellAt and then
// fire the event for the state from here instead.
// FIXME: Since we do not render the label we don't have the label bounds
// here which means hit detection will only work for the vertex bounds,
// the edge but not for overlapping labels or most part of the edge labels.
mxGraphView.prototype.installListeners = function()
{
var graph = this.graph;
var container = graph.container;
if (container != null)
{
mxEvent.addGestureListeners(container,
mxUtils.bind(this, function(evt)
{
var pt = mxUtils.convertPoint(graph.container,
mxEvent.getClientX(evt), mxEvent.getClientY(evt));
var cell = graph.getCellAt(pt.x, pt.y);
var state = this.getState(cell);
if (state != null)
{
graph.fireMouseEvent(mxEvent.MOUSE_DOWN,
new mxMouseEvent(evt, state));
}
// Condition to avoid scrollbar events starting a rubberband
// selection
else if (this.isContainerEvent(evt) &&
((!mxClient.IS_IE &&
!mxClient.IS_GC && !mxClient.IS_OP && !mxClient.IS_SF) ||
!this.isScrollEvent(evt)))
{
graph.fireMouseEvent(mxEvent.MOUSE_DOWN,
new mxMouseEvent(evt));
}
}),
mxUtils.bind(this, function(evt)
{
var pt = mxUtils.convertPoint(graph.container,
mxEvent.getClientX(evt), mxEvent.getClientY(evt));
var cell = graph.getCellAt(pt.x, pt.y);
var state = this.getState(cell);
if (state != null)
{
graph.fireMouseEvent(mxEvent.MOUSE_MOVE,
new mxMouseEvent(evt, state));
}
else if (this.isContainerEvent(evt))
{
graph.fireMouseEvent(mxEvent.MOUSE_MOVE,
new mxMouseEvent(evt));
}
}),
mxUtils.bind(this, function(evt)
{
var pt = mxUtils.convertPoint(graph.container,
mxEvent.getClientX(evt), mxEvent.getClientY(evt));
var cell = graph.getCellAt(pt.x, pt.y);
var state = this.getState(cell);
if (state != null)
{
graph.fireMouseEvent(mxEvent.MOUSE_UP,
new mxMouseEvent(evt, state));
}
else if (this.isContainerEvent(evt))
{
graph.fireMouseEvent(mxEvent.MOUSE_UP,
new mxMouseEvent(evt));
}
}));
// Adds listener for double click handling on background
mxEvent.addListener(container, 'dblclick',
mxUtils.bind(this, function(evt)
{
var pt = mxUtils.convertPoint(graph.container,
mxEvent.getClientX(evt), mxEvent.getClientY(evt));
var cell = graph.getCellAt(pt.x, pt.y);
graph.dblClick(evt, cell);
})
);
// Adds basic listeners for graph event dispatching outside of the
// container and finishing the handling of a single gesture
mxEvent.addGestureListeners(document,
mxUtils.bind(this, function(evt)
{
if (this.isContainerEvent(evt))
{
graph.popupMenuHandler.hideMenu();
}
}),
mxUtils.bind(this, function(evt)
{
// Hides the tooltip if mouse is outside container
if (graph.tooltipHandler != null &&
graph.tooltipHandler.isHideOnHover())
{
graph.tooltipHandler.hide();
}
if (this.captureDocumentGesture &&
graph.isMouseDown &&
!mxEvent.isConsumed(evt))
{
graph.fireMouseEvent(mxEvent.MOUSE_MOVE,
new mxMouseEvent(evt));
}
}),
mxUtils.bind(this, function(evt)
{
if (this.captureDocumentGesture)
{
graph.fireMouseEvent(mxEvent.MOUSE_UP,
new mxMouseEvent(evt));
}
})
);
}
};
</script>
<!-- Example code -->
<script type="text/javascript">
// Program starts here. Creates a sample graph in the
// DOM node with the specified ID. This function is invoked
// from the onLoad event handler of the document (see below).
function main(container)
{
// Checks if the browser is supported
if (!mxClient.isBrowserSupported())
{
// Displays an error message if the browser is not supported.
mxUtils.error('Browser is not supported!', 200, false);
}
else
{
// Creates the graph inside the given container
var graph = new mxGraph(container);
// Holds the current image
var img = null;
var loader = new Image();
loader.src = 'images/loading.gif';
loader.style.zIndex = 1;
loader.style.visibility = 'hidden';
loader.style.position = 'absolute';
graph.container.insertBefore(loader, graph.container.firstChild);
// Disables the DOM-based rendering in the graph and updates the
// display image on each validation step using the Export servlet
// on the graph
graph.view.rendering = false;
// Installs a post-validation repaint of the complete graph
graph.view.validate = function()
{
loader.style.left = graph.container.scrollLeft + graph.container.clientWidth / 2 - 32;
loader.style.top = graph.container.scrollTop + graph.container.clientHeight / 2 - 32;
loader.style.visibility = 'visible';
mxGraphView.prototype.validate.apply(this, arguments);
var t0 = new Date().getTime();
var bounds = graph.getGraphBounds();
// Note that we send out an XML version of the view which
// allows us to keep most customizations on the client-side.
// No deltas are used here, the complete view is sent to the
// server on each update.
var node = mxUtils.getViewXml(graph, 1);
var xml = encodeURIComponent(mxUtils.getXml(node));
var onload = function(req)
{
var dt = (new Date().getTime() - t0) / 1000;
//mxLog.debug('post returned after '+dt+' secs');
if (req.getStatus() == 200)
{
var image =new Image();
// Disable DnD events on images in IE. FIXME: Rubberband
// events are ignored in IE when starting on the image.
if (mxClient.IS_IE)
{
graph.view.isContainerEvent = function(evt)
{
var source = mxEvent.getSource(evt);
return (source == image ||
source == this.graph.container ||
source.parentNode == this.backgroundPane ||
(source.parentNode != null &&
source.parentNode.parentNode == this.backgroundPane) ||
source == this.canvas.parentNode ||
source == this.canvas ||
source == this.backgroundPane ||
source == this.drawPane ||
source == this.overlayPane);
};
}
// Less flickering if the old image is removed after
// the new image was received from the server
image.onload = function()
{
if (img != null)
{
img.parentNode.removeChild(img);
}
img = image;
graph.container.insertBefore(img, graph.container.firstChild);
loader.style.visibility = 'hidden';
var dt = (new Date().getTime() - t0) / 1000;
//mxLog.debug('received '+img.clientWidth+'x'+img.clientHeight+' pixels in '+dt+' secs');
graph.setEnabled(true);
};
// URL contains timestamp to avoid caching in the browser
image.src = '/ServerView?'+new Date().getTime();
image.style.position = 'absolute';
image.style.left = bounds.x - 4;
image.style.top = bounds.y - 4;
image.style.zIndex = -1;
}
};
var onerror = function(req)
{
//mxLog.debug('error: '+req.getStatus());
}
//mxLog.debug('sent '+(xml.length/1024)+' KB');
graph.setEnabled(false);
new mxXmlRequest('/ServerView', 'xml='+xml).send(onload, onerror);
};
// Uncomment the following if you want the container
// to fit the size of the graph
//graph.setResizeContainer(true);
// Enables rubberband selection
new mxRubberband(graph);
graph.setConnectable(true);
graph.setPanning(true);
graph.setTooltips(true);
// Gets the default parent for inserting new cells. This
// is normally the first child of the root (ie. layer 0).
var parent = graph.getDefaultParent();
//mxLog.show();
// 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();
}
}
};
</script>
</head>
<!-- Page passes the container for the graph to the program -->
<body onload="main(document.getElementById('graphContainer'))">
<!-- Creates a container for the graph with a grid wallpaper -->
<div id="graphContainer"
style="overflow:auto;width:500px;height:500px;border: black solid 1px;cursor:default;">
</div>
<br>
<strong>Important:</strong>
<p>
To use this example, start com.mxgraph.examples.web.Main in Java
and point your<br>browser to:
<a href="http://localhost:8080/mxgraph/javascript/examples/serverview.html">http://localhost:8080/mxgraph/javascript/examples/serverview.html</a>
</p>
</body>
</html>