UNPKG

4.71 kBJavaScriptView Raw
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*/
11import * as go from '../release/go-module.js';
12/**
13 * The RealtimeDragSelectingTool class lets the user select and deselect Parts within the {@link DragSelectingTool#box}
14 * during a drag, not just at the end of the drag.
15 *
16 * If you want to experiment with this extension, try the <a href="../../extensionsJSM/RealtimeDragSelecting.html">Realtime Drag Selecting</a> sample.
17 * @category Tool Extension
18 */
19export class RealtimeDragSelectingTool extends go.DragSelectingTool {
20 constructor() {
21 super(...arguments);
22 this._originalSelection = new go.Set();
23 this._temporarySelection = new go.Set();
24 }
25 /**
26 * Remember the original collection of selected Parts.
27 */
28 doActivate() {
29 super.doActivate();
30 // keep a copy of the original Set of selected Parts
31 this._originalSelection = this.diagram.selection.copy();
32 // these Part.isSelected may have been temporarily modified
33 this._temporarySelection.clear();
34 this.diagram.raiseDiagramEvent('ChangingSelection');
35 }
36 /**
37 * Release any references to selected Parts.
38 */
39 doDeactivate() {
40 this.diagram.raiseDiagramEvent('ChangedSelection');
41 this._originalSelection.clear();
42 this._temporarySelection.clear();
43 super.doDeactivate();
44 }
45 /**
46 * Restore the selection which may have been modified during a drag.
47 */
48 doCancel() {
49 const orig = this._originalSelection;
50 orig.each(function (p) { p.isSelected = true; });
51 this._temporarySelection.each(function (p) { if (!orig.contains(p))
52 p.isSelected = false; });
53 super.doCancel();
54 }
55 /**
56 * Select Parts within the bounds of the drag-select box.
57 */
58 doMouseMove() {
59 if (this.isActive) {
60 super.doMouseMove();
61 this.selectInRect(this.computeBoxBounds());
62 }
63 }
64 /**
65 * Select Parts within the bounds of the drag-select box.
66 */
67 doKeyDown() {
68 if (this.isActive) {
69 super.doKeyDown();
70 this.selectInRect(this.computeBoxBounds());
71 }
72 }
73 /**
74 * Select Parts within the bounds of the drag-select box.
75 */
76 doKeyUp() {
77 if (this.isActive) {
78 super.doKeyUp();
79 this.selectInRect(this.computeBoxBounds());
80 }
81 }
82 /**
83 * For a given rectangle, select Parts that are within that rectangle.
84 * @param {Rect} r rectangular bounds in document coordinates.
85 */
86 selectInRect(r) {
87 const diagram = this.diagram;
88 const orig = this._originalSelection;
89 const temp = this._temporarySelection;
90 const e = diagram.lastInput;
91 const found = diagram.findPartsIn(r, this.isPartialInclusion);
92 if (e.control || e.meta) { // toggle or deselect
93 if (e.shift) { // deselect only
94 temp.each(function (p) { if (!found.contains(p))
95 p.isSelected = orig.contains(p); });
96 found.each(function (p) { p.isSelected = false; temp.add(p); });
97 }
98 else { // toggle selectedness of parts based on _originalSelection
99 temp.each(function (p) { if (!found.contains(p))
100 p.isSelected = orig.contains(p); });
101 found.each(function (p) { p.isSelected = !orig.contains(p); temp.add(p); });
102 }
103 }
104 else if (e.shift) { // extend selection only
105 temp.each(function (p) { if (!found.contains(p))
106 p.isSelected = orig.contains(p); });
107 found.each(function (p) { p.isSelected = true; temp.add(p); });
108 }
109 else { // select found parts, and unselect all other previously selected parts
110 temp.each(function (p) { if (!found.contains(p))
111 p.isSelected = false; });
112 orig.each(function (p) { if (!found.contains(p))
113 p.isSelected = false; });
114 found.each(function (p) { p.isSelected = true; temp.add(p); });
115 }
116 }
117}