jqtree 1.8.4 → 1.8.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,17 +1,17 @@
1
+ import { TriggerEvent } from "./jqtreeMethodTypes";
1
2
  import {
2
3
  getPositionInfoFromMouseEvent,
3
4
  getPositionInfoFromTouch,
4
5
  PositionInfo,
5
6
  } from "./mouseUtils";
6
7
  import { Node } from "./node";
7
- import { TriggerEvent } from "./jqtreeMethodTypes";
8
8
 
9
9
  interface ClickTarget {
10
10
  node: Node;
11
11
  type: "button" | "label";
12
12
  }
13
13
 
14
- type GetNode = (element: HTMLElement) => null | Node;
14
+ type GetNode = (element: HTMLElement) => Node | null;
15
15
 
16
16
  interface MouseHandlerParams {
17
17
  element: HTMLElement;
@@ -31,16 +31,157 @@ class MouseHandler {
31
31
  private element: HTMLElement;
32
32
  private getMouseDelay: () => number;
33
33
  private getNode: GetNode;
34
+
35
+ private handleClick = (e: MouseEvent): void => {
36
+ if (!e.target) {
37
+ return;
38
+ }
39
+
40
+ const clickTarget = this.getClickTarget(e.target as HTMLElement);
41
+
42
+ if (!clickTarget) {
43
+ return;
44
+ }
45
+
46
+ switch (clickTarget.type) {
47
+ case "button":
48
+ this.onClickButton(clickTarget.node);
49
+
50
+ e.preventDefault();
51
+ e.stopPropagation();
52
+ break;
53
+
54
+ case "label": {
55
+ const event = this.triggerEvent("tree.click", {
56
+ click_event: e,
57
+ node: clickTarget.node,
58
+ });
59
+
60
+ if (!event.isDefaultPrevented()) {
61
+ this.onClickTitle(clickTarget.node);
62
+ }
63
+ break;
64
+ }
65
+ }
66
+ };
67
+
68
+ private handleContextmenu = (e: MouseEvent) => {
69
+ if (!e.target) {
70
+ return;
71
+ }
72
+
73
+ const div = (e.target as HTMLElement).closest<HTMLElement>(
74
+ "ul.jqtree-tree .jqtree-element",
75
+ );
76
+
77
+ if (div) {
78
+ const node = this.getNode(div);
79
+ if (node) {
80
+ e.preventDefault();
81
+ e.stopPropagation();
82
+
83
+ this.triggerEvent("tree.contextmenu", {
84
+ click_event: e,
85
+ node,
86
+ });
87
+ return false;
88
+ }
89
+ }
90
+
91
+ return null;
92
+ };
93
+
94
+ private handleDblclick = (e: MouseEvent): void => {
95
+ if (!e.target) {
96
+ return;
97
+ }
98
+
99
+ const clickTarget = this.getClickTarget(e.target as HTMLElement);
100
+
101
+ if (clickTarget?.type === "label") {
102
+ this.triggerEvent("tree.dblclick", {
103
+ click_event: e,
104
+ node: clickTarget.node,
105
+ });
106
+ }
107
+ };
108
+
34
109
  private isMouseDelayMet: boolean;
35
110
  private isMouseStarted: boolean;
36
- private mouseDelayTimer: number | null;
37
- private mouseDownInfo: PositionInfo | null;
111
+ private mouseDelayTimer: null | number;
112
+
113
+ private mouseDown = (e: MouseEvent): void => {
114
+ // Left mouse button?
115
+ if (e.button !== 0) {
116
+ return;
117
+ }
118
+
119
+ const result = this.handleMouseDown(getPositionInfoFromMouseEvent(e));
120
+
121
+ if (result && e.cancelable) {
122
+ e.preventDefault();
123
+ }
124
+ };
125
+
126
+ private mouseDownInfo: null | PositionInfo;
127
+
128
+ private mouseMove = (e: MouseEvent): void => {
129
+ this.handleMouseMove(e, getPositionInfoFromMouseEvent(e));
130
+ };
131
+
132
+ private mouseUp = (e: MouseEvent): void => {
133
+ this.handleMouseUp(getPositionInfoFromMouseEvent(e));
134
+ };
135
+
38
136
  private onClickButton: (node: Node) => void;
39
137
  private onClickTitle: (node: Node) => void;
40
138
  private onMouseCapture: (positionInfo: PositionInfo) => boolean | null;
41
139
  private onMouseDrag: (positionInfo: PositionInfo) => void;
42
140
  private onMouseStart: (positionInfo: PositionInfo) => boolean;
43
141
  private onMouseStop: (positionInfo: PositionInfo) => void;
142
+
143
+ private touchEnd = (e: TouchEvent): void => {
144
+ if (e.touches.length > 1) {
145
+ return;
146
+ }
147
+
148
+ const touch = e.touches[0];
149
+
150
+ if (!touch) {
151
+ return;
152
+ }
153
+
154
+ this.handleMouseUp(getPositionInfoFromTouch(touch, e));
155
+ };
156
+
157
+ private touchMove = (e: TouchEvent): void => {
158
+ if (e.touches.length > 1) {
159
+ return;
160
+ }
161
+
162
+ const touch = e.touches[0];
163
+
164
+ if (!touch) {
165
+ return;
166
+ }
167
+
168
+ this.handleMouseMove(e, getPositionInfoFromTouch(touch, e));
169
+ };
170
+
171
+ private touchStart = (e: TouchEvent): void => {
172
+ if (e.touches.length > 1) {
173
+ return;
174
+ }
175
+
176
+ const touch = e.touches[0];
177
+
178
+ if (!touch) {
179
+ return;
180
+ }
181
+
182
+ this.handleMouseDown(getPositionInfoFromTouch(touch, e));
183
+ };
184
+
44
185
  private triggerEvent: TriggerEvent;
45
186
  private useContextMenu: boolean;
46
187
 
@@ -88,34 +229,35 @@ class MouseHandler {
88
229
  this.mouseDownInfo = null;
89
230
  }
90
231
 
91
- public deinit(): void {
92
- this.element.removeEventListener("click", this.handleClick);
93
- this.element.removeEventListener("dblclick", this.handleDblclick);
232
+ private getClickTarget(element: HTMLElement): ClickTarget | null {
233
+ const button = element.closest<HTMLElement>(".jqtree-toggler");
94
234
 
95
- if (this.useContextMenu) {
96
- this.element.removeEventListener(
97
- "contextmenu",
98
- this.handleContextmenu,
99
- );
100
- }
235
+ if (button) {
236
+ const node = this.getNode(button);
101
237
 
102
- this.element.removeEventListener("mousedown", this.mouseDown);
103
- this.element.removeEventListener("touchstart", this.touchStart);
104
- this.removeMouseMoveEventListeners();
105
- }
238
+ if (node) {
239
+ return {
240
+ node,
241
+ type: "button",
242
+ };
243
+ }
244
+ } else {
245
+ const jqTreeElement =
246
+ element.closest<HTMLElement>(".jqtree-element");
106
247
 
107
- private mouseDown = (e: MouseEvent): void => {
108
- // Left mouse button?
109
- if (e.button !== 0) {
110
- return;
248
+ if (jqTreeElement) {
249
+ const node = this.getNode(jqTreeElement);
250
+ if (node) {
251
+ return {
252
+ node,
253
+ type: "label",
254
+ };
255
+ }
256
+ }
111
257
  }
112
258
 
113
- const result = this.handleMouseDown(getPositionInfoFromMouseEvent(e));
114
-
115
- if (result && e.cancelable) {
116
- e.preventDefault();
117
- }
118
- };
259
+ return null;
260
+ }
119
261
 
120
262
  private handleMouseDown(positionInfo: PositionInfo): boolean {
121
263
  // We may have missed mouseup (out of window)
@@ -134,45 +276,6 @@ class MouseHandler {
134
276
  return true;
135
277
  }
136
278
 
137
- private handleStartMouse(): void {
138
- document.addEventListener("mousemove", this.mouseMove, {
139
- passive: false,
140
- });
141
- document.addEventListener("touchmove", this.touchMove, {
142
- passive: false,
143
- });
144
- document.addEventListener("mouseup", this.mouseUp, { passive: false });
145
- document.addEventListener("touchend", this.touchEnd, {
146
- passive: false,
147
- });
148
-
149
- const mouseDelay = this.getMouseDelay();
150
-
151
- if (mouseDelay) {
152
- this.startMouseDelayTimer(mouseDelay);
153
- } else {
154
- this.isMouseDelayMet = true;
155
- }
156
- }
157
-
158
- private startMouseDelayTimer(mouseDelay: number): void {
159
- if (this.mouseDelayTimer) {
160
- clearTimeout(this.mouseDelayTimer);
161
- }
162
-
163
- this.mouseDelayTimer = window.setTimeout(() => {
164
- if (this.mouseDownInfo) {
165
- this.isMouseDelayMet = true;
166
- }
167
- }, mouseDelay);
168
-
169
- this.isMouseDelayMet = false;
170
- }
171
-
172
- private mouseMove = (e: MouseEvent): void => {
173
- this.handleMouseMove(e, getPositionInfoFromMouseEvent(e));
174
- };
175
-
176
279
  private handleMouseMove(
177
280
  e: MouseEvent | TouchEvent,
178
281
  positionInfo: PositionInfo,
@@ -191,8 +294,7 @@ class MouseHandler {
191
294
  }
192
295
 
193
296
  if (this.mouseDownInfo) {
194
- this.isMouseStarted =
195
- this.onMouseStart(this.mouseDownInfo) !== false;
297
+ this.isMouseStarted = this.onMouseStart(this.mouseDownInfo);
196
298
  }
197
299
 
198
300
  if (this.isMouseStarted) {
@@ -206,10 +308,6 @@ class MouseHandler {
206
308
  }
207
309
  }
208
310
 
209
- private mouseUp = (e: MouseEvent): void => {
210
- this.handleMouseUp(getPositionInfoFromMouseEvent(e));
211
- };
212
-
213
311
  private handleMouseUp(positionInfo: PositionInfo): void {
214
312
  this.removeMouseMoveEventListeners();
215
313
  this.isMouseDelayMet = false;
@@ -221,6 +319,27 @@ class MouseHandler {
221
319
  }
222
320
  }
223
321
 
322
+ private handleStartMouse(): void {
323
+ document.addEventListener("mousemove", this.mouseMove, {
324
+ passive: false,
325
+ });
326
+ document.addEventListener("touchmove", this.touchMove, {
327
+ passive: false,
328
+ });
329
+ document.addEventListener("mouseup", this.mouseUp, { passive: false });
330
+ document.addEventListener("touchend", this.touchEnd, {
331
+ passive: false,
332
+ });
333
+
334
+ const mouseDelay = this.getMouseDelay();
335
+
336
+ if (mouseDelay) {
337
+ this.startMouseDelayTimer(mouseDelay);
338
+ } else {
339
+ this.isMouseDelayMet = true;
340
+ }
341
+ }
342
+
224
343
  private removeMouseMoveEventListeners() {
225
344
  document.removeEventListener("mousemove", this.mouseMove);
226
345
  document.removeEventListener("touchmove", this.touchMove);
@@ -228,157 +347,34 @@ class MouseHandler {
228
347
  document.removeEventListener("touchend", this.touchEnd);
229
348
  }
230
349
 
231
- private touchStart = (e: TouchEvent): void => {
232
- if (!e) {
233
- return;
234
- }
235
-
236
- if (e.touches.length > 1) {
237
- return;
238
- }
239
-
240
- const touch = e.touches[0];
241
-
242
- if (!touch) {
243
- return;
244
- }
245
-
246
- this.handleMouseDown(getPositionInfoFromTouch(touch, e));
247
- };
248
-
249
- private touchMove = (e: TouchEvent): void => {
250
- if (!e) {
251
- return;
252
- }
253
-
254
- if (e.touches.length > 1) {
255
- return;
256
- }
257
-
258
- const touch = e.touches[0];
259
-
260
- if (!touch) {
261
- return;
262
- }
263
-
264
- this.handleMouseMove(e, getPositionInfoFromTouch(touch, e));
265
- };
266
-
267
- private touchEnd = (e: TouchEvent): void => {
268
- if (!e) {
269
- return;
270
- }
271
-
272
- if (e.touches.length > 1) {
273
- return;
274
- }
275
-
276
- const touch = e.touches[0];
277
-
278
- if (!touch) {
279
- return;
280
- }
281
-
282
- this.handleMouseUp(getPositionInfoFromTouch(touch, e));
283
- };
284
-
285
- private handleClick = (e: MouseEvent): void => {
286
- if (!e.target) {
287
- return;
288
- }
289
-
290
- const clickTarget = this.getClickTarget(e.target as HTMLElement);
291
-
292
- if (!clickTarget) {
293
- return;
294
- }
295
-
296
- if (clickTarget.type === "button") {
297
- this.onClickButton(clickTarget.node);
298
-
299
- e.preventDefault();
300
- e.stopPropagation();
301
- } else if (clickTarget.type === "label") {
302
- const event = this.triggerEvent("tree.click", {
303
- node: clickTarget.node,
304
- click_event: e,
305
- });
306
-
307
- if (!event.isDefaultPrevented()) {
308
- this.onClickTitle(clickTarget.node);
309
- }
310
- }
311
- };
312
-
313
- private handleDblclick = (e: MouseEvent): void => {
314
- if (!e.target) {
315
- return;
316
- }
317
-
318
- const clickTarget = this.getClickTarget(e.target as HTMLElement);
319
-
320
- if (clickTarget?.type === "label") {
321
- this.triggerEvent("tree.dblclick", {
322
- node: clickTarget.node,
323
- click_event: e,
324
- });
325
- }
326
- };
327
-
328
- private handleContextmenu = (e: MouseEvent) => {
329
- if (!e.target) {
330
- return;
350
+ private startMouseDelayTimer(mouseDelay: number): void {
351
+ if (this.mouseDelayTimer) {
352
+ clearTimeout(this.mouseDelayTimer);
331
353
  }
332
354
 
333
- const div = (e.target as HTMLElement).closest<HTMLElement>(
334
- "ul.jqtree-tree .jqtree-element",
335
- );
336
-
337
- if (div) {
338
- const node = this.getNode(div);
339
- if (node) {
340
- e.preventDefault();
341
- e.stopPropagation();
342
-
343
- this.triggerEvent("tree.contextmenu", {
344
- node,
345
- click_event: e,
346
- });
347
- return false;
355
+ this.mouseDelayTimer = window.setTimeout(() => {
356
+ if (this.mouseDownInfo) {
357
+ this.isMouseDelayMet = true;
348
358
  }
349
- }
350
-
351
- return null;
352
- };
353
-
354
- private getClickTarget(element: HTMLElement): ClickTarget | null {
355
- const button = element.closest<HTMLElement>(".jqtree-toggler");
359
+ }, mouseDelay);
356
360
 
357
- if (button) {
358
- const node = this.getNode(button);
361
+ this.isMouseDelayMet = false;
362
+ }
359
363
 
360
- if (node) {
361
- return {
362
- type: "button",
363
- node,
364
- };
365
- }
366
- } else {
367
- const jqTreeElement =
368
- element.closest<HTMLElement>(".jqtree-element");
364
+ public deinit(): void {
365
+ this.element.removeEventListener("click", this.handleClick);
366
+ this.element.removeEventListener("dblclick", this.handleDblclick);
369
367
 
370
- if (jqTreeElement) {
371
- const node = this.getNode(jqTreeElement);
372
- if (node) {
373
- return {
374
- type: "label",
375
- node,
376
- };
377
- }
378
- }
368
+ if (this.useContextMenu) {
369
+ this.element.removeEventListener(
370
+ "contextmenu",
371
+ this.handleContextmenu,
372
+ );
379
373
  }
380
374
 
381
- return null;
375
+ this.element.removeEventListener("mousedown", this.mouseDown);
376
+ this.element.removeEventListener("touchstart", this.touchStart);
377
+ this.removeMouseMoveEventListeners();
382
378
  }
383
379
  }
384
380