-
Notifications
You must be signed in to change notification settings - Fork 4
/
htmllabel.html
174 lines (145 loc) · 4.93 KB
/
htmllabel.html
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
<!--
Copyright (c) 2006-2013, JGraph Ltd
HTML label example for mxGraph. This example demonstrates using
HTML labels that are connected to the state of the user object.
-->
<html>
<head>
<title>HTML label example for mxGraph</title>
<!-- Sets the basepath for the library if not in same directory -->
<script type="text/javascript">
mxBasePath = '../src';
</script>
<!-- Loads and initializes the library -->
<script type="text/javascript" src="../src/js/mxClient.js"></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
{
// Disables the built-in context menu
mxEvent.disableContextMenu(container);
// Creates the graph inside the given container
var graph = new mxGraph(container);
// Enables HTML labels
graph.setHtmlLabels(true);
// Enables rubberband selection
new mxRubberband(graph);
// Creates a user object that stores the state
var doc = mxUtils.createXmlDocument();
var obj = doc.createElement('UserObject');
obj.setAttribute('label', 'Hello, World!');
obj.setAttribute('checked', 'false');
// Adds optional caching for the HTML label
var cached = true;
if (cached)
{
// Ignores cached label in codec
mxCodecRegistry.getCodec(mxCell).exclude.push('div');
// Invalidates cached labels
graph.model.setValue = function(cell, value)
{
cell.div = null;
mxGraphModel.prototype.setValue.apply(this, arguments);
};
}
// Overrides method to provide a cell label in the display
graph.convertValueToString = function(cell)
{
if (cached && cell.div != null)
{
// Uses cached label
return cell.div;
}
else if (mxUtils.isNode(cell.value) && cell.value.nodeName.toLowerCase() == 'userobject')
{
// Returns a DOM for the label
var div = document.createElement('div');
div.innerHTML = cell.getAttribute('label');
mxUtils.br(div);
var checkbox = document.createElement('input');
checkbox.setAttribute('type', 'checkbox');
if (cell.getAttribute('checked') == 'true')
{
checkbox.setAttribute('checked', 'checked');
checkbox.defaultChecked = true;
}
// Writes back to cell if checkbox is clicked
mxEvent.addListener(checkbox, (mxClient.IS_QUIRKS) ? 'click' : 'change', function(evt)
{
var elt = cell.value.cloneNode(true);
elt.setAttribute('checked', (checkbox.checked) ? 'true' : 'false');
graph.model.setValue(cell, elt);
});
div.appendChild(checkbox);
if (cached)
{
// Caches label
cell.div = div;
}
return div;
}
return '';
};
// Overrides method to store a cell label in the model
var cellLabelChanged = graph.cellLabelChanged;
graph.cellLabelChanged = function(cell, newValue, autoSize)
{
if (mxUtils.isNode(cell.value) && cell.value.nodeName.toLowerCase() == 'userobject')
{
// Clones the value for correct undo/redo
var elt = cell.value.cloneNode(true);
elt.setAttribute('label', newValue);
newValue = elt;
}
cellLabelChanged.apply(this, arguments);
};
// Overrides method to create the editing value
var getEditingValue = graph.getEditingValue;
graph.getEditingValue = function(cell)
{
if (mxUtils.isNode(cell.value) && cell.value.nodeName.toLowerCase() == 'userobject')
{
return cell.getAttribute('label');
}
};
var parent = graph.getDefaultParent();
graph.insertVertex(parent, null, obj, 20, 20, 80, 60);
// Undo/redo
var undoManager = new mxUndoManager();
var listener = function(sender, evt)
{
undoManager.undoableEditHappened(evt.getProperty('edit'));
};
graph.getModel().addListener(mxEvent.UNDO, listener);
graph.getView().addListener(mxEvent.UNDO, listener);
document.body.appendChild(mxUtils.button('Undo', function()
{
undoManager.undo();
}));
document.body.appendChild(mxUtils.button('Redo', function()
{
undoManager.redo();
}));
}
};
</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="position:relative;overflow:hidden;width:321px;height:241px;background:url('editors/images/grid.gif');cursor:default;">
</div>
</body>
</html>