forked from NorthwoodsSoftware/GoJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdragOutFields.html
218 lines (205 loc) · 9.42 KB
/
dragOutFields.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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Dragging a Field from a Record onto an HTML Element</title>
<meta name="description" content="Drag an item from a node out of the diagram and onto another HTML element." />
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Copyright 1998-2019 by Northwoods Software Corporation. -->
<script src="../release/go.js"></script>
<script src="../assets/js/goSamples.js"></script> <!-- this is only for the GoJS Samples framework -->
<script id="code">
// Custom DraggingTool for dragging fields instead of whole Parts.
// FieldDraggingTool.fieldTemplate needs to be set to a template of the field that you want shown while dragging.
function FieldDraggingTool() {
go.DraggingTool.call(this);
this.fieldTemplate = null; // THIS NEEDS TO BE SET before a drag starts
this.temporaryPart = null;
}
go.Diagram.inherit(FieldDraggingTool, go.DraggingTool);
// override this method
FieldDraggingTool.prototype.findDraggablePart = function() {
var diagram = this.diagram;
var obj = diagram.findObjectAt(diagram.lastInput.documentPoint);
while (obj !== null && obj.type !== go.Panel.TableRow) obj = obj.panel;
if (obj !== null && obj.type === go.Panel.TableRow &&
this.fieldTemplate !== null && this.temporaryPart === null) {
var tempPart =
go.GraphObject.make(go.Node, "Table",
{ layerName: "Tool", locationSpot: go.Spot.Center },
this.fieldTemplate.copy()); // copy the template!
this.temporaryPart = tempPart;
// assume OBJ is now a Panel representing a field, bound to field data
// update the temporary Part via data binding
tempPart.location = diagram.lastInput.documentPoint; // need to set location explicitly
diagram.add(tempPart); // add to Diagram before setting data
tempPart.data = obj.data; // bind to the same field data as being dragged
return tempPart;
}
return go.DraggingTool.prototype.findDraggablePart.call(this);
};
FieldDraggingTool.prototype.doActivate = function() {
if (this.temporaryPart === null) return go.DraggingTool.prototype.doActivate.call(this);
var diagram = this.diagram;
this.standardMouseSelect();
this.isActive = true;
// instead of the usual result of computeEffectiveCollection, just use the temporaryPart alone
var map = new go.Map(/*go.Part, go.DraggingInfo*/);
map.set(this.temporaryPart, new go.DraggingInfo(diagram.lastInput.documentPoint.copy()));
this.draggedParts = map;
this.startTransaction("Drag Field");
diagram.isMouseCaptured = true;
};
FieldDraggingTool.prototype.doDeactivate = function() {
if (this.temporaryPart === null) return go.DraggingTool.prototype.doDeactivate.call(this);
var diagram = this.diagram;
// make sure the temporary Part is no longer in the Diagram
diagram.remove(this.temporaryPart);
this.temporaryPart = null;
// now do all the standard deactivation cleanup,
// including setting isActive = false, clearing out draggedParts, calling stopTransaction(),
// and setting diagram.isMouseCaptured = false
go.DraggingTool.prototype.doDeactivate.call(this);
};
FieldDraggingTool.prototype.doMouseMove = function() {
if (!this.isActive) return;
if (this.temporaryPart === null) return go.DraggingTool.prototype.doMouseMove.call(this);
var diagram = this.diagram;
// just move the temporaryPart (in draggedParts), without regard to moving or copying permissions of the Node
var offset = diagram.lastInput.documentPoint.copy().subtract(diagram.firstInput.documentPoint);
this.moveParts(this.draggedParts, offset, false);
};
FieldDraggingTool.prototype.doMouseUp = function() {
if (!this.isActive) return;
if (this.temporaryPart === null) return go.DraggingTool.prototype.doMouseUp.call(this);
var diagram = this.diagram;
var data = this.temporaryPart.data;
var input = diagram.lastInput;
var id = input.event.target.id;
if (input.isTouchEvent) {
// Touch events always target the first object touched, we want the last.
// Determine if you are using Touch or Pointer:
var evt = input.event.changedTouches ? input.event.changedTouches[0] : input.event;
id = document.elementFromPoint(evt.clientX, evt.clientY).id;
}
if (input.event && id === "myDroppedFields") {
document.getElementById("myDroppedFields").textContent += data.name + " (" + data.info + ")\n";
}
this.transactionResult = "Dragged Field";
this.stopTool();
};
// end of FieldDraggingTool
function init() {
if (window.goSamples) goSamples(); // init for these samples -- you don't need to call this
var $ = go.GraphObject.make; // for conciseness in defining templates
myDiagram =
$(go.Diagram, "myDiagramDiv",
{
validCycle: go.Diagram.CycleNotDirected, // don't allow loops
draggingTool: $(FieldDraggingTool), // use custom DraggingTool
"undoManager.isEnabled": true
});
// This template is a Panel that is used to represent each item in a Panel.itemArray.
// The Panel is data bound to the item object.
var fieldTemplate =
$(go.Panel, "TableRow", // this Panel is a row in the containing Table
new go.Binding("portId", "name"), // this Panel is a "port"
{
background: "transparent", // so this port's background can be picked by the mouse
fromSpot: go.Spot.Right, // links only go from the right side to the left side
toSpot: go.Spot.Left
}, // allow drawing links from or to this port
$(go.Shape,
{ width: 12, height: 12, column: 0, strokeWidth: 2, margin: 4 },
new go.Binding("figure", "figure"),
new go.Binding("fill", "color")),
$(go.TextBlock,
{ margin: new go.Margin(0, 2), column: 1, font: "bold 13px sans-serif" },
new go.Binding("text", "name")),
$(go.TextBlock,
{ margin: new go.Margin(0, 2), column: 2, font: "13px sans-serif" },
new go.Binding("text", "info"))
);
// the FieldDraggingTool needs a template for what to show while dragging
myDiagram.toolManager.draggingTool.fieldTemplate = fieldTemplate;
// This template represents a whole "record".
myDiagram.nodeTemplate =
$(go.Node, "Auto",
{
movable: false,
copyable: false,
deletable: false
},
new go.Binding("location", "loc", go.Point.parse).makeTwoWay(go.Point.stringify),
// this rectangular shape surrounds the content of the node
$(go.Shape,
{ fill: "#EEEEEE" }),
// the content consists of a header and a list of items
$(go.Panel, "Vertical",
// this is the header for the whole node
$(go.Panel, "Auto",
{ stretch: go.GraphObject.Horizontal }, // as wide as the whole node
$(go.Shape,
{ fill: "#1570A6", stroke: null }),
$(go.TextBlock,
{
alignment: go.Spot.Center,
margin: 3,
stroke: "white",
textAlign: "center",
font: "bold 12pt sans-serif"
},
new go.Binding("text", "key"))),
// this Panel holds a Panel for each item object in the itemArray;
// each item Panel is defined by the itemTemplate to be a TableRow in this Table
$(go.Panel, "Table",
{
name: "TABLE",
padding: 2,
minSize: new go.Size(100, 10),
defaultStretch: go.GraphObject.Horizontal,
itemTemplate: fieldTemplate
},
new go.Binding("itemArray", "fields")
) // end Table Panel of items
) // end Vertical Panel
); // end Node
myDiagram.model =
$(go.GraphLinksModel,
{
linkFromPortIdProperty: "fromPort",
linkToPortIdProperty: "toPort",
copiesArrays: true,
copiesArrayObjects: true,
nodeDataArray: [
{
key: "Record1",
fields: [
{ name: "field1", info: "", color: "#F7B84B", figure: "Ellipse" },
{ name: "field2", info: "the second one", color: "#F25022", figure: "Ellipse" },
{ name: "fieldThree", info: "3rd", color: "#00BCF2" }
],
loc: "0 0"
},
{
key: "Record2",
fields: [
{ name: "fieldA", info: "", color: "#FFB900", figure: "Diamond" }
],
loc: "250 0"
}
]
});
}
</script>
</head>
<body onload="init()">
<div id="sample">
<div id="myDiagramDiv" style="border: solid 1px black; width:100%; height:300px"></div>
<p>Drag a field from one of the record nodes and drop onto the PRE element below.
The "record" Nodes are not movable or copyable or deletable.</p>
<p>Here you can drop a field from one of the records above:</p>
<pre id="myDroppedFields" style="width:200px;height:300px;border:dashed"></pre>
</div>
</body>
</html>