Skip to content

Commit 2567bea

Browse files
authored
feat(ScrollEnablement): enhance implementation to work on desktop (#1374)
- improve implementation - add scroll enablement to tokenizer FIXES: #1368
1 parent 68cb73d commit 2567bea

File tree

4 files changed

+49
-15
lines changed

4 files changed

+49
-15
lines changed

packages/base/src/delegate/ScrollEnablement.js

+38-15
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
1+
import { isPhone } from "../Device.js";
12
import EventProvider from "../EventProvider.js";
23
import scroll from "../animations/scroll.js";
34

45
const scrollEventName = "scroll";
5-
const touchEndEventName = "touchend";
6+
const touchEndEventName = isPhone() ? "touchend" : "mouseup";
67

78
class ScrollEnablement extends EventProvider {
89
constructor(containerComponent) {
910
super();
10-
containerComponent.addEventListener("touchstart", this.ontouchstart.bind(this), { passive: true });
11-
containerComponent.addEventListener("touchmove", this.ontouchmove.bind(this), { passive: true });
12-
containerComponent.addEventListener("touchend", this.ontouchend.bind(this), { passive: true });
11+
this.containerComponent = containerComponent;
12+
this.mouseMove = this.ontouchmove.bind(this);
13+
this.mouseUp = this.ontouchend.bind(this);
14+
this.touchStart = this.ontouchstart.bind(this);
15+
16+
this.isPhone = isPhone();
17+
18+
if (this.isPhone) {
19+
containerComponent.addEventListener("touchstart", this.touchStart, { passive: true });
20+
containerComponent.addEventListener("touchmove", this.mouseMove, { passive: true });
21+
containerComponent.addEventListener("touchend", this.mouseUp, { passive: true });
22+
} else {
23+
containerComponent.addEventListener("mousedown", this.touchStart, { passive: true });
24+
}
1325
}
1426

1527
set scrollContainer(container) {
@@ -43,19 +55,25 @@ class ScrollEnablement extends EventProvider {
4355

4456
_isTouchInside(touch) {
4557
const rect = this._container.getBoundingClientRect();
46-
const x = touch.clientX;
47-
const y = touch.clientY;
58+
const x = this.isPhone ? touch.clientX : touch.x;
59+
const y = this.isPhone ? touch.clientY : touch.y;
4860

4961
return x >= rect.left && x <= rect.right
5062
&& y >= rect.top && y <= rect.bottom;
5163
}
5264

5365
ontouchstart(event) {
54-
const touch = event.touches[0];
55-
this._prevDragX = touch.pageX;
56-
this._prevDragY = touch.pageY;
66+
const touch = this.isPhone ? event.touches[0] : null;
67+
68+
if (!this.isPhone) {
69+
document.addEventListener("mouseup", this.mouseUp, { passive: true });
70+
document.addEventListener("mousemove", this.mouseMove, { passive: true });
71+
}
5772

58-
this._canScroll = this._isTouchInside(touch);
73+
this._prevDragX = this.isPhone ? touch.pageX : event.x;
74+
this._prevDragY = this.isPhone ? touch.pageY : event.y;
75+
76+
this._canScroll = this._isTouchInside(this.isPhone ? touch : event);
5977
}
6078

6179
ontouchmove(event) {
@@ -64,10 +82,10 @@ class ScrollEnablement extends EventProvider {
6482
}
6583

6684
const container = this._container;
67-
const touch = event.touches[0];
85+
const touch = this.isPhone ? event.touches[0] : null;
6886

69-
const dragX = touch.pageX;
70-
const dragY = touch.pageY;
87+
const dragX = this.isPhone ? touch.pageX : event.x;
88+
const dragY = this.isPhone ? touch.pageY : event.y;
7189

7290
container.scrollLeft += this._prevDragX - dragX;
7391
container.scrollTop += this._prevDragY - dragY;
@@ -87,8 +105,8 @@ class ScrollEnablement extends EventProvider {
87105
}
88106

89107
const container = this._container;
90-
const dragX = event.pageX;
91-
const dragY = event.pageY;
108+
const dragX = this.isPhone ? event.pageX : event.x;
109+
const dragY = this.isPhone ? event.pageY : event.y;
92110

93111
container.scrollLeft += this._prevDragX - dragX;
94112
container.scrollTop += this._prevDragY - dragY;
@@ -100,6 +118,11 @@ class ScrollEnablement extends EventProvider {
100118

101119
this._prevDragX = dragX;
102120
this._prevDragY = dragY;
121+
122+
if (!this.isPhone) {
123+
document.removeEventListener("mousemove", this.mouseMove, { passive: true });
124+
document.removeEventListener("mouseup", this.mouseUp);
125+
}
103126
}
104127
}
105128

packages/main/src/MultiComboBox.js

+2
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,8 @@ class MultiComboBox extends UI5Element {
425425

426426
tokenizer.tokens.forEach(token => { token.selected = false; });
427427

428+
this._tokenizer.contentDom.scrollLeft = 0;
429+
428430
if (tokensCount === 0 && this._deleting) {
429431
setTimeout(() => {
430432
this.shadowRoot.querySelector("input").focus();

packages/main/src/Tokenizer.js

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
22
import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
33
import ResizeHandler from "@ui5/webcomponents-base/dist/delegate/ResizeHandler.js";
44
import ItemNavigation from "@ui5/webcomponents-base/dist/delegate/ItemNavigation.js";
5+
import ScrollEnablement from "@ui5/webcomponents-base/dist/delegate/ScrollEnablement.js";
56
import { fetchI18nBundle, getI18nBundle } from "@ui5/webcomponents-base/dist/i18nBundle.js";
67
import TokenizerTemplate from "./generated/templates/TokenizerTemplate.lit.js";
78
import { MULTIINPUT_SHOW_MORE_TOKENS, TOKENIZER_ARIA_LABEL } from "./generated/i18n/i18n-defaults.js";
@@ -95,6 +96,7 @@ class Tokenizer extends UI5Element {
9596
this._tokensCount = 0;
9697
this._resizeHandler = this._handleResize.bind(this);
9798
this._itemNav = new ItemNavigation(this);
99+
this._scrollEnablement = new ScrollEnablement(this);
98100

99101
this._itemNav.getItemsCallback = () => {
100102
if (this.disabled) {
@@ -142,6 +144,8 @@ class Tokenizer extends UI5Element {
142144
this._invalidate();
143145
this._tokensCount = this.tokens.length;
144146
}
147+
148+
this._scrollEnablement.scrollContainer = this.expanded ? this.contentDom : this;
145149
}
146150

147151
_tokenDelete(event) {

packages/main/src/themes/Tokenizer.css

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
height: 100%;
3333
}
3434

35+
:host([expanded]) .ui5-tokenizer--content {
36+
display: inline-block;
37+
white-space: nowrap;
38+
}
39+
3540
.ui5-tokenizer--content {
3641
height: 100%;
3742
display: flex;

0 commit comments

Comments
 (0)