UNPKG

4.45 kBPlain TextView Raw
1/*
2* Copyright (C) 1998-2021 by Northwoods Software Corporation. All Rights Reserved.
3*/
4
5/*
6* This is an extension and not part of the main GoJS library.
7* Note that the API for this class may change with any version, even point releases.
8* If you intend to use an extension in production, you should copy the code to your own source directory.
9* Extensions can be found in the GoJS kit under the extensions or extensionsTS folders.
10* See the Extensions intro page (https://gojs.net/latest/intro/extensions.html) for more information.
11*/
12
13import * as go from '../release/go-module.js';
14
15/**
16 * The RealtimeDragSelectingTool class lets the user select and deselect Parts within the {@link DragSelectingTool#box}
17 * during a drag, not just at the end of the drag.
18 *
19 * If you want to experiment with this extension, try the <a href="../../extensionsJSM/RealtimeDragSelecting.html">Realtime Drag Selecting</a> sample.
20 * @category Tool Extension
21 */
22export class RealtimeDragSelectingTool extends go.DragSelectingTool {
23 private _originalSelection: go.Set<go.Part> = new go.Set<go.Part>();
24 private _temporarySelection: go.Set<go.Part> = new go.Set<go.Part>();
25
26 /**
27 * Remember the original collection of selected Parts.
28 */
29 public doActivate(): void {
30 super.doActivate();
31 // keep a copy of the original Set of selected Parts
32 this._originalSelection = this.diagram.selection.copy();
33 // these Part.isSelected may have been temporarily modified
34 this._temporarySelection.clear();
35 this.diagram.raiseDiagramEvent('ChangingSelection');
36 }
37
38 /**
39 * Release any references to selected Parts.
40 */
41 public doDeactivate(): void {
42 this.diagram.raiseDiagramEvent('ChangedSelection');
43 this._originalSelection.clear();
44 this._temporarySelection.clear();
45 super.doDeactivate();
46 }
47
48 /**
49 * Restore the selection which may have been modified during a drag.
50 */
51 public doCancel(): void {
52 const orig = this._originalSelection;
53 orig.each(function(p) { p.isSelected = true; });
54 this._temporarySelection.each(function(p) { if (!orig.contains(p)) p.isSelected = false; });
55 super.doCancel();
56 }
57
58 /**
59 * Select Parts within the bounds of the drag-select box.
60 */
61 public doMouseMove(): void {
62 if (this.isActive) {
63 super.doMouseMove();
64 this.selectInRect(this.computeBoxBounds());
65 }
66 }
67
68 /**
69 * Select Parts within the bounds of the drag-select box.
70 */
71 public doKeyDown(): void {
72 if (this.isActive) {
73 super.doKeyDown();
74 this.selectInRect(this.computeBoxBounds());
75 }
76 }
77
78 /**
79 * Select Parts within the bounds of the drag-select box.
80 */
81 public doKeyUp(): void {
82 if (this.isActive) {
83 super.doKeyUp();
84 this.selectInRect(this.computeBoxBounds());
85 }
86 }
87
88 /**
89 * For a given rectangle, select Parts that are within that rectangle.
90 * @param {Rect} r rectangular bounds in document coordinates.
91 */
92 public selectInRect(r: go.Rect): void {
93 const diagram = this.diagram;
94 const orig = this._originalSelection;
95 const temp = this._temporarySelection;
96 const e = diagram.lastInput;
97 const found = diagram.findPartsIn(r, this.isPartialInclusion);
98 if (e.control || e.meta) { // toggle or deselect
99 if (e.shift) { // deselect only
100 temp.each(function(p: go.Part) { if (!found.contains(p)) p.isSelected = orig.contains(p); });
101 found.each(function(p: go.Part) { p.isSelected = false; temp.add(p); });
102 } else { // toggle selectedness of parts based on _originalSelection
103 temp.each(function(p: go.Part) { if (!found.contains(p)) p.isSelected = orig.contains(p); });
104 found.each(function(p: go.Part) { p.isSelected = !orig.contains(p); temp.add(p); });
105 }
106 } else if (e.shift) { // extend selection only
107 temp.each(function(p: go.Part) { if (!found.contains(p)) p.isSelected = orig.contains(p); });
108 found.each(function(p: go.Part) { p.isSelected = true; temp.add(p); });
109 } else { // select found parts, and unselect all other previously selected parts
110 temp.each(function(p: go.Part) { if (!found.contains(p)) p.isSelected = false; });
111 orig.each(function(p: go.Part) { if (!found.contains(p)) p.isSelected = false; });
112 found.each(function(p: go.Part) { p.isSelected = true; temp.add(p); });
113 }
114 }
115}