Skip to content

Commit 99f8884

Browse files
Merge pull request #207 from SwiftFiddle/throttle
Throttle mouse event
2 parents 31a500c + a0b796b commit 99f8884

File tree

5 files changed

+87
-31
lines changed

5 files changed

+87
-31
lines changed

Public/js/lookup_view.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22

3-
import { Popover } from "./popover.js";
43
import "../css/lookup.css";
4+
import { Popover } from "./popover.js";
55

66
export class LoopupView {
77
set error(error) {
@@ -29,7 +29,6 @@ export class LoopupView {
2929
.find("span")
3030
.each(function () {
3131
$(this).mouseover(function (event) {
32-
event.preventDefault();
3332
event.stopPropagation();
3433

3534
const contents = [];
@@ -91,7 +90,6 @@ export class LoopupView {
9190
});
9291

9392
$(this).mouseout(function (event) {
94-
event.preventDefault();
9593
event.stopPropagation();
9694

9795
let element = event.target;

Public/js/popover.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22

33
import "../css/popover.css";
4+
import { throttle } from "./throttle.js";
45

56
export class Popover {
67
set maxWidth(value) {
@@ -28,16 +29,29 @@ export class Popover {
2829
this.popover.appendChild(this.popoverContent);
2930
document.body.appendChild(this.popover);
3031

31-
this.popover.addEventListener("mouseenter", (event) => {
32-
event.preventDefault();
33-
event.stopPropagation();
32+
const onmouseover = throttle((event) => {
3433
this.onmouseover(event);
3534
});
36-
this.popover.addEventListener("mouseleave", (event) => {
37-
event.preventDefault();
38-
event.stopPropagation();
35+
const onmouseout = throttle((event) => {
3936
this.onmouseout(event);
4037
});
38+
39+
this.popover.addEventListener(
40+
"mouseenter",
41+
(event) => {
42+
event.stopPropagation();
43+
onmouseover(event);
44+
},
45+
{ capture: false, once: false, passive: false }
46+
);
47+
this.popover.addEventListener(
48+
"mouseleave",
49+
(event) => {
50+
event.stopPropagation();
51+
onmouseout(event);
52+
},
53+
{ capture: false, once: false, passive: false }
54+
);
4155
}
4256

4357
show(target, options = {}) {

Public/js/statistics_view.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import "datatables.net-bs5/css/dataTables.bootstrap5.min.css";
55

66
import "../css/table.css";
77

8+
import { throttle } from "./throttle.js";
9+
810
export class StatisticsView {
911
set error(error) {
1012
this.container.innerHTML = `<div class="alert alert-danger m-3" role="alert">${error}</div>`;
@@ -36,18 +38,29 @@ export class StatisticsView {
3638
tr.innerHTML = `<td style="font-family: Menlo, Consolas, 'DejaVu Sans Mono', 'Ubuntu Mono', monospace;">${row.text}</td><td><div>${row.ranges.length}</div></td>`;
3739
body.appendChild(tr);
3840

39-
tr.addEventListener("mouseover", (event) => {
40-
event.preventDefault();
41-
event.stopPropagation();
42-
43-
this.onmouseover(event, tr, row.ranges);
41+
const onmouseover = throttle((event, element, data) => {
42+
this.onmouseover(event, element, data);
4443
});
45-
tr.addEventListener("mouseout", (event) => {
46-
event.preventDefault();
47-
event.stopPropagation();
48-
49-
this.onmouseout(event, tr);
44+
const onmouseout = throttle((event, element, data) => {
45+
this.onmouseout(event, element, data);
5046
});
47+
48+
tr.addEventListener(
49+
"mouseover",
50+
(event) => {
51+
event.stopPropagation();
52+
onmouseover(event, tr, row.ranges);
53+
},
54+
{ capture: false, once: false, passive: false }
55+
);
56+
tr.addEventListener(
57+
"mouseout",
58+
(event) => {
59+
event.stopPropagation();
60+
onmouseout(event, tr);
61+
},
62+
{ capture: false, once: false, passive: false }
63+
);
5164
}
5265

5366
if (this.dataTable) {

Public/js/throttle.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"use strict";
2+
3+
export function throttle(fn) {
4+
let raf;
5+
6+
return (...args) => {
7+
if (raf) {
8+
cancelAnimationFrame(raf);
9+
return;
10+
}
11+
12+
raf = requestAnimationFrame(() => {
13+
fn(...args);
14+
raf = undefined;
15+
});
16+
};
17+
}

Public/js/tree_view.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22

33
import "../css/tree_view.css";
4+
import { throttle } from "./throttle.js";
45

56
const downCaret = makeDownCaret();
67
const rightCaret = makeRightCaret();
@@ -82,21 +83,34 @@ export class TreeView {
8283
li.appendChild(element);
8384
}
8485

85-
li.addEventListener("mouseover", (event) => {
86-
event.preventDefault();
87-
event.stopPropagation();
88-
89-
li.classList.add("hover");
90-
this.onmouseover(event, element, node);
86+
const onmouseover = throttle((event, element, data) => {
87+
this.onmouseover(event, element, data);
9188
});
92-
li.addEventListener("mouseout", (event) => {
93-
event.preventDefault();
94-
event.stopPropagation();
95-
96-
li.classList.remove("hover");
97-
this.onmouseout(event, element, node);
89+
const onmouseout = throttle((event, element, data) => {
90+
this.onmouseout(event, element, data);
9891
});
9992

93+
li.addEventListener(
94+
"mouseover",
95+
(event) => {
96+
event.stopPropagation();
97+
98+
li.classList.add("hover");
99+
onmouseover(event, element, node);
100+
},
101+
{ capture: false, once: false, passive: false }
102+
);
103+
li.addEventListener(
104+
"mouseout",
105+
(event) => {
106+
event.stopPropagation();
107+
108+
li.classList.remove("hover");
109+
onmouseout(event, element, node);
110+
},
111+
{ capture: false, once: false, passive: false }
112+
);
113+
100114
ul.appendChild(li);
101115
return ul;
102116
}

0 commit comments

Comments
 (0)