Skip to content

Commit 5cc3442

Browse files
authored
fix(interaction): Prioritize mouse input (#280)
Determine by userAgent and prioritize mouse input type for non-mobile environment. Fix #92 Close #280
1 parent 8337555 commit 5cc3442

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

spec/interactions/interaction-spec.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ describe("INTERACTION", () => {
3030
};
3131
});
3232

33+
it("inputType should be 'mouse", () => {
34+
expect(chart.internal.inputType).to.be.equal("mouse");
35+
});
36+
3337
it("should have 4 event rects properly", () => {
3438
const lefts = [69, 130, 198, 403];
3539
const widths = [61, 68, 205, 197.5];
@@ -406,7 +410,7 @@ describe("INTERACTION", () => {
406410
});
407411
});
408412

409-
it("set inputType.mouse=false", () => {
413+
it("set option inputType.mouse=false", () => {
410414
args.interaction.inputType.mouse = false;
411415
});
412416

@@ -528,6 +532,10 @@ describe("INTERACTION", () => {
528532
};
529533
});
530534

535+
it("inputType should be 'touch", () => {
536+
expect(chart.internal.inputType).to.be.equal("touch");
537+
});
538+
531539
it("showed each data points tooltip?", done => {
532540
util.simulator(chart.internal.svg.node(), {
533541
pos: [250,150],

src/internals/ChartInternal.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,15 +1258,16 @@ export default class ChartInternal {
12581258
convertInputType() {
12591259
const $$ = this;
12601260
const config = $$.config;
1261-
const hasMouse = config.interaction_inputType_mouse ? ("onmouseover" in window) : false;
1261+
const isMobile = $$.isMobile();
1262+
const hasMouse = config.interaction_inputType_mouse && !isMobile ? ("onmouseover" in window) : false;
12621263
let hasTouch = false;
12631264

1264-
if (notEmpty(config.interaction_inputType_touch)) {
1265+
if (config.interaction_inputType_touch) {
12651266
// Ref: https://github.com/Modernizr/Modernizr/blob/master/feature-detects/touchevents.js
12661267
// On IE11 with IE9 emulation mode, ('ontouchstart' in window) is returning true
12671268
hasTouch = ("ontouchmove" in window) || (window.DocumentTouch && document instanceof window.DocumentTouch);
12681269
}
12691270

1270-
return (hasTouch && "touch") || (hasMouse && "mouse") || null;
1271+
return (hasMouse && "mouse") || (hasTouch && "touch") || null;
12711272
}
12721273
}

src/internals/ua.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@
55
import ChartInternal from "./ChartInternal";
66
import {extend} from "./util";
77

8+
const ua = window.navigator.userAgent;
9+
810
extend(ChartInternal.prototype, {
911
isSafari() {
10-
const ua = window.navigator.userAgent;
11-
12-
return ua.indexOf("Safari") >= 0 &&
13-
ua.indexOf("Chrome") < 0;
12+
return ua.indexOf("Safari") > -1 && !this.isChrome();
1413
},
1514

1615
isChrome() {
17-
return window.navigator.userAgent
18-
.indexOf("Chrome") >= 0;
16+
return ua.indexOf("Chrome") > -1;
17+
},
18+
19+
isMobile() {
20+
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Browser_detection_using_the_user_agent
21+
return ua.indexOf("Mobi") > -1;
1922
}
2023
});

0 commit comments

Comments
 (0)