Skip to content

Commit 00a6755

Browse files
nomeaning777beeman
authored andcommitted
fix: fire dragStart event only when a cursor is moved (#283)
1 parent 6c990a5 commit 00a6755

File tree

2 files changed

+70
-30
lines changed

2 files changed

+70
-30
lines changed

cypress/integration/7.click.spec.js

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22

33
import { moveGutter, checkSplitDirAndSizes } from '../support/splitUtils'
44

5+
function checkEventCount({ dragStartCount, dragEndCount, gutterClickCount, gutterDblClickCount, transitionEndCount }) {
6+
if (dragStartCount !== undefined) {
7+
cy.get('.logs ul li').filter('.dragStart').should('have.length', dragStartCount)
8+
}
9+
if (dragEndCount !== undefined) {
10+
cy.get('.logs ul li').filter('.dragEnd').should('have.length', dragEndCount)
11+
}
12+
if (gutterClickCount !== undefined) {
13+
cy.get('.logs ul li').filter('.gutterClick').should('have.length', gutterClickCount)
14+
}
15+
if (gutterDblClickCount !== undefined) {
16+
cy.get('.logs ul li').filter('.gutterDblClick').should('have.length', gutterDblClickCount)
17+
}
18+
if (transitionEndCount !== undefined) {
19+
cy.get('.logs ul li').filter('.transitionEnd').should('have.length', transitionEndCount)
20+
}
21+
}
22+
523
context('Gutter click example page tests', () => {
624
const W = 1070
725
const H = 300
@@ -53,8 +71,8 @@ context('Gutter click example page tests', () => {
5371
it('Mix gutter click and dragging', () => {
5472
// Try move gutter event if disabled
5573
moveGutter('.as-split-gutter', 0, -100, 0)
74+
// gutterClick should be fired same as normal click event since dragging is disabled.
5675
checkSplitDirAndSizes('.split-example > as-split', 'horizontal', W, H, GUTTER, [262.5, 525, 262.5])
57-
cy.get('.logs ul li').should('have.length', 0)
5876

5977
// Enable gutters
6078
cy.get('.btns button').eq(1).click()
@@ -64,61 +82,65 @@ context('Gutter click example page tests', () => {
6482
checkSplitDirAndSizes('.split-example > as-split', 'horizontal', W, H, GUTTER, [162.5, 625, 262.5])
6583
cy.wait(10)
6684

67-
cy.get('.logs ul li').filter('.dragStart').should('have.length', 1)
68-
cy.get('.logs ul li').filter('.dragEnd').should('have.length', 1)
85+
checkEventCount({ dragStartCount: 1, dragEndCount: 1, gutterClickCount: 0, transitionEndCount: 0 })
6986

7087
// Click gutter1 to close area1
7188
cy.get('.as-split-gutter').eq(0).click()
7289
cy.wait(2000)
7390
checkSplitDirAndSizes('.split-example > as-split', 'horizontal', W, H, GUTTER, [0, 787.5, 262.5])
7491
cy.wait(10)
7592

76-
cy.get('.logs ul li').filter('.dragStart').should('have.length', 2)
77-
cy.get('.logs ul li').filter('.dragEnd').should('have.length', 1)
78-
cy.get('.logs ul li').filter('.gutterClick').should('have.length', 1)
79-
cy.get('.logs ul li').filter('.transitionEnd').should('have.length', 1)
93+
checkEventCount({ dragStartCount: 1, dragEndCount: 1, gutterClickCount: 1, transitionEndCount: 1 })
8094

8195
// Click gutter2 to close area3
8296
cy.get('.as-split-gutter').eq(1).click()
8397
cy.wait(2000)
8498
checkSplitDirAndSizes('.split-example > as-split', 'horizontal', W, H, GUTTER, [0, 1050, 0])
8599
cy.wait(10)
86100

87-
cy.get('.logs ul li').filter('.dragStart').should('have.length', 3)
88-
cy.get('.logs ul li').filter('.dragEnd').should('have.length', 1)
89-
cy.get('.logs ul li').filter('.gutterClick').should('have.length', 2)
90-
cy.get('.logs ul li').filter('.transitionEnd').should('have.length', 2)
101+
checkEventCount({ dragStartCount: 1, dragEndCount: 1, gutterClickCount: 2, transitionEndCount: 2 })
91102

92103
// Move gutter2 to enlarge area3
93104
moveGutter('.as-split-gutter', 1, -20, 0)
94105
checkSplitDirAndSizes('.split-example > as-split', 'horizontal', W, H, GUTTER, [0, 1030, 20])
95106

96-
cy.get('.logs ul li').filter('.dragStart').should('have.length', 4)
97-
cy.get('.logs ul li').filter('.dragEnd').should('have.length', 2)
98-
cy.get('.logs ul li').filter('.gutterClick').should('have.length', 2)
99-
cy.get('.logs ul li').filter('.transitionEnd').should('have.length', 2)
107+
checkEventCount({ dragStartCount: 2, dragEndCount: 2, gutterClickCount: 2, transitionEndCount: 2 })
100108

101109
// Click gutter2 to close area3
102110
cy.get('.as-split-gutter').eq(1).click()
103111
cy.wait(2000)
104112
checkSplitDirAndSizes('.split-example > as-split', 'horizontal', W, H, GUTTER, [0, 1050, 0])
105113
cy.wait(10)
106114

107-
cy.get('.logs ul li').filter('.dragStart').should('have.length', 5)
108-
cy.get('.logs ul li').filter('.dragEnd').should('have.length', 2)
109-
cy.get('.logs ul li').filter('.gutterClick').should('have.length', 3)
110-
cy.get('.logs ul li').filter('.transitionEnd').should('have.length', 3)
115+
checkEventCount({ dragStartCount: 2, dragEndCount: 2, gutterClickCount: 3, transitionEndCount: 3 })
111116

112117
// Click gutter1 to display area1
113118
cy.get('.as-split-gutter').eq(0).click()
114119
cy.wait(2000)
115120
checkSplitDirAndSizes('.split-example > as-split', 'horizontal', W, H, GUTTER, [262.5, 787.5, 0])
116121
cy.wait(10)
117122

118-
cy.get('.logs ul li').filter('.dragStart').should('have.length', 6)
119-
cy.get('.logs ul li').filter('.dragEnd').should('have.length', 2)
120-
cy.get('.logs ul li').filter('.gutterClick').should('have.length', 4)
121-
cy.get('.logs ul li').filter('.transitionEnd').should('have.length', 4)
123+
checkEventCount({ dragStartCount: 2, dragEndCount: 2, gutterClickCount: 4, transitionEndCount: 4 })
124+
125+
// It should fire Click Event on mouseup if the mouse cursor is not moved.
126+
cy.get('.as-split-gutter').eq(0).trigger('mousedown', { which: 1, clientX: 0, clientY: 0 })
127+
cy.get('.as-split-gutter').eq(0).trigger('mousemove', { which: 1, clientX: 0, clientY: 0 })
128+
cy.wait(10)
129+
checkEventCount({ dragStartCount: 2, dragEndCount: 2, gutterClickCount: 4, transitionEndCount: 4 })
130+
131+
cy.get('.as-split-gutter').eq(0).trigger('mouseup', { which: 1, clientX: 0, clientY: 0 })
132+
cy.wait(2000)
133+
checkEventCount({ dragStartCount: 2, dragEndCount: 2, gutterClickCount: 5, transitionEndCount: 5 })
134+
135+
// It should fire dragStart and should not fire Click Event on mousemove if the mouse cursor is moved.
136+
cy.get('.as-split-gutter').eq(0).trigger('mousedown', { which: 1, clientX: 0, clientY: 0 })
137+
cy.get('.as-split-gutter').eq(0).trigger('mousemove', { which: 1, clientX: 1, clientY: 0 })
138+
cy.wait(10)
139+
checkEventCount({ dragStartCount: 3, dragEndCount: 2, gutterClickCount: 5, transitionEndCount: 5 })
140+
141+
cy.get('.as-split-gutter').eq(0).trigger('mouseup', { which: 1, clientX: 0, clientY: 0 })
142+
cy.wait(20)
143+
checkEventCount({ dragStartCount: 3, dragEndCount: 3, gutterClickCount: 5, transitionEndCount: 5 })
122144
})
123145

124146
it('Test double click event', () => {

projects/angular-split/src/lib/component/split.component.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ export class SplitComponent implements AfterViewInit, OnDestroy {
230230

231231
private isDragging = false
232232
private isWaitingClear = false
233+
private isWaitingInitialMove = false
233234
private dragListeners: Array<Function> = []
234235
private snapshot: ISplitSnapshot | null = null
235236
private startPoint: IPoint | null = null
@@ -483,8 +484,13 @@ export class SplitComponent implements AfterViewInit, OnDestroy {
483484
public clickGutter(event: MouseEvent | TouchEvent, gutterNum: number): void {
484485
const tempPoint = getPointFromEvent(event)
485486

486-
// Be sure mouseup/touchend happened at same point as mousedown/touchstart to trigger click/dblclick
487-
if (this.startPoint && this.startPoint.x === tempPoint.x && this.startPoint.y === tempPoint.y) {
487+
// Be sure mouseup/touchend happened if touch/cursor is not moved.
488+
if (
489+
this.startPoint &&
490+
this.startPoint.x === tempPoint.x &&
491+
this.startPoint.y === tempPoint.y &&
492+
(!this.isDragging || this.isWaitingInitialMove)
493+
) {
488494
// If timeout in progress and new click > clearTimeout & dblClickEvent
489495
if (this._clickTimeout !== null) {
490496
window.clearTimeout(this._clickTimeout)
@@ -565,10 +571,7 @@ export class SplitComponent implements AfterViewInit, OnDestroy {
565571
this.displayedAreas.forEach((area) => area.component.lockEvents())
566572

567573
this.isDragging = true
568-
this.renderer.addClass(this.elRef.nativeElement, 'as-dragging')
569-
this.renderer.addClass(this.gutterEls.toArray()[this.snapshot.gutterNum - 1].nativeElement, 'as-dragged')
570-
571-
this.notify('start', this.snapshot.gutterNum)
574+
this.isWaitingInitialMove = true
572575
}
573576

574577
private dragEvent(event: MouseEvent | TouchEvent): void {
@@ -589,6 +592,21 @@ export class SplitComponent implements AfterViewInit, OnDestroy {
589592
return
590593
}
591594

595+
if (this.isWaitingInitialMove) {
596+
if (this.startPoint.x !== this.endPoint.x || this.startPoint.y !== this.endPoint.y) {
597+
this.ngZone.run(() => {
598+
this.isWaitingInitialMove = false
599+
600+
this.renderer.addClass(this.elRef.nativeElement, 'as-dragging')
601+
this.renderer.addClass(this.gutterEls.toArray()[this.snapshot.gutterNum - 1].nativeElement, 'as-dragged')
602+
603+
this.notify('start', this.snapshot.gutterNum)
604+
})
605+
} else {
606+
return
607+
}
608+
}
609+
592610
// Calculate steppedOffset
593611

594612
let offset =
@@ -706,7 +724,7 @@ export class SplitComponent implements AfterViewInit, OnDestroy {
706724
this.isDragging = false
707725

708726
// If moved from starting point, notify end
709-
if (this.endPoint && (this.startPoint.x !== this.endPoint.x || this.startPoint.y !== this.endPoint.y)) {
727+
if (this.isWaitingInitialMove === false) {
710728
this.notify('end', this.snapshot.gutterNum)
711729
}
712730

0 commit comments

Comments
 (0)