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 | */
|
11 | import * as go from '../release/go-module.js';
|
12 | /**
|
13 | * The NonRealtimeDraggingTool class lets the user drag an image instead of actually moving any selected nodes,
|
14 | * until the mouse-up event.
|
15 | *
|
16 | * If you want to experiment with this extension, try the <a href="../../extensionsJSM/NonRealtimeDragging.html">Non Realtime Dragging</a> sample.
|
17 | * @category Tool Extension
|
18 | */
|
19 | export class NonRealtimeDraggingTool extends go.DraggingTool {
|
20 | constructor() {
|
21 | super(...arguments);
|
22 | this._duration = 0; // duration of movement animation; <= 0 to disable
|
23 | this._imagePart = null; // a Part holding a translucent image of what would be dragged
|
24 | this._ghostDraggedParts = null; // a Map of the _imagePart and its dragging information
|
25 | this._originalDraggedParts = null; // the saved normal value of DraggingTool.draggedParts
|
26 | }
|
27 | /**
|
28 | * Gets or sets how long the movement animation should be to move the actual parts upon a mouse-up.
|
29 | * The default value is zero -- there is no animation of the movement.
|
30 | */
|
31 | get duration() { return this._duration; }
|
32 | set duration(val) { this._duration = val; }
|
33 | /**
|
34 | * Call the base method, and then make an image of the returned collection,
|
35 | * show it using a Picture, and hold the Picture in a temporary Part, as _imagePart.
|
36 | * @param {Iterable.<Part>} parts A {@link Set} or {@link List} of {@link Part}s.
|
37 | * @return {Map.<Part,DraggingInfo>}
|
38 | */
|
39 | computeEffectiveCollection(coll) {
|
40 | const map = super.computeEffectiveCollection(coll, this.dragOptions);
|
41 | if (this.isActive && this._imagePart === null) {
|
42 | const bounds = this.diagram.computePartsBounds(map.toKeySet());
|
43 | const offset = this.diagram.lastInput.documentPoint.copy().subtract(bounds.position);
|
44 | const $ = go.GraphObject.make;
|
45 | this._imagePart =
|
46 | $(go.Part, { layerName: 'Tool', opacity: 0.5, locationSpot: new go.Spot(0, 0, offset.x, offset.y) }, $(go.Picture, { element: this.diagram.makeImage({ parts: map.toKeySet() }) }));
|
47 | }
|
48 | return map;
|
49 | }
|
50 | /**
|
51 | * When activated, replace the {@link #draggedParts} with the ghost dragged parts, which
|
52 | * consists of just one Part, the image, added to the Diagram at the current mouse point.
|
53 | */
|
54 | doActivate() {
|
55 | super.doActivate();
|
56 | if (this._imagePart !== null) {
|
57 | this._imagePart.location = this.diagram.lastInput.documentPoint;
|
58 | this.diagram.add(this._imagePart);
|
59 | this._originalDraggedParts = this.draggedParts;
|
60 | this._ghostDraggedParts = super.computeEffectiveCollection(new go.List().add(this._imagePart), this.dragOptions);
|
61 | this.draggedParts = this._ghostDraggedParts;
|
62 | }
|
63 | }
|
64 | /**
|
65 | * When deactivated, make sure any image is removed from the Diagram and all references are cleared out.
|
66 | */
|
67 | doDeactivate() {
|
68 | if (this._imagePart !== null) {
|
69 | this.diagram.remove(this._imagePart);
|
70 | }
|
71 | this._imagePart = null;
|
72 | this._ghostDraggedParts = null;
|
73 | this._originalDraggedParts = null;
|
74 | super.doDeactivate();
|
75 | }
|
76 | /**
|
77 | * Do the normal mouse-up behavior, but only after restoring {@link #draggedParts}.
|
78 | */
|
79 | doMouseUp() {
|
80 | const partsmap = this._originalDraggedParts;
|
81 | if (partsmap !== null) {
|
82 | this.draggedParts = partsmap;
|
83 | }
|
84 | super.doMouseUp();
|
85 | if (partsmap !== null && this.duration > 0) {
|
86 | var anim = new go.Animation();
|
87 | anim.duration = this.duration;
|
88 | partsmap.each(function (kvp) {
|
89 | var part = kvp.key;
|
90 | anim.add(part, "location", kvp.value.point, part.location);
|
91 | });
|
92 | anim.start();
|
93 | }
|
94 | }
|
95 | /**
|
96 | * If the user changes to "copying" mode by holding down the Control key,
|
97 | * return to the regular behavior and remove the image.
|
98 | */
|
99 | doKeyDown() {
|
100 | if (this._imagePart !== null && this._originalDraggedParts !== null &&
|
101 | (this.diagram.lastInput.control || this.diagram.lastInput.meta) && this.mayCopy()) {
|
102 | this.draggedParts = this._originalDraggedParts;
|
103 | this.diagram.remove(this._imagePart);
|
104 | }
|
105 | super.doKeyDown();
|
106 | }
|
107 | /**
|
108 | * If the user changes back to "moving" mode,
|
109 | * show the image again and go back to dragging the ghost dragged parts.
|
110 | */
|
111 | doKeyUp() {
|
112 | if (this._imagePart !== null && this._ghostDraggedParts !== null && this.mayMove()) {
|
113 | this._imagePart.location = this.diagram.lastInput.documentPoint;
|
114 | this.diagram.add(this._imagePart);
|
115 | this.draggedParts = this._ghostDraggedParts;
|
116 | }
|
117 | super.doKeyUp();
|
118 | }
|
119 | }
|