Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
pleroma-fe
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Pleroma
pleroma-fe
Commits
c50e64f8
Commit
c50e64f8
authored
6 years ago
by
Shpuld Shpludson
Browse files
Options
Downloads
Patches
Plain Diff
Add tests for gesture service, fix bug with perpendicular directions
parent
e23c1946
No related branches found
Branches containing commit
No related tags found
Tags containing commit
2 merge requests
!1028
`master` refresh with `develop`
,
!714
Add a gesture service for easier touch gestures
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/services/gesture_service/gesture_service.js
+9
-6
9 additions, 6 deletions
src/services/gesture_service/gesture_service.js
test/unit/specs/services/gesture_service/gesture_service.spec.js
+120
-0
120 additions, 0 deletions
...it/specs/services/gesture_service/gesture_service.spec.js
with
129 additions
and
6 deletions
src/services/gesture_service/gesture_service.js
+
9
−
6
View file @
c50e64f8
...
...
@@ -10,11 +10,14 @@ const touchEventCoord = e => ([e.touches[0].screenX, e.touches[0].screenY])
const
vectorLength
=
v
=>
Math
.
sqrt
(
v
[
0
]
*
v
[
0
]
+
v
[
1
]
*
v
[
1
])
const
perpendicular
=
v
=>
[
v
[
1
],
v
[
0
]]
const
perpendicular
=
v
=>
[
v
[
1
],
-
v
[
0
]]
const
dotProduct
=
(
v1
,
v2
)
=>
v1
[
0
]
*
v2
[
0
]
+
v1
[
1
]
*
v2
[
1
]
const
vectorFlatten
=
(
v1
,
v2
)
=>
[
v1
[
0
]
*
v2
[
0
],
v1
[
1
]
*
v2
[
1
]]
const
project
=
(
v1
,
v2
)
=>
{
const
scalar
=
(
dotProduct
(
v1
,
v2
)
/
dotProduct
(
v2
,
v2
))
return
[
scalar
*
v2
[
0
],
scalar
*
v2
[
1
]]
}
// direction: either use the constants above or an arbitrary 2d vector.
// threshold: how many Px to move from touch origin before checking if the
...
...
@@ -46,12 +49,12 @@ const updateSwipe = (event, gesture) => {
// movement is opposite from direction
if
(
dotProduct
(
delta
,
gesture
.
direction
)
<
0
)
return
// movement perpendicular to direction is too much
const
towardsDir
=
vectorFlatten
(
gesture
.
direction
,
delta
)
const
towardsDir
=
project
(
delta
,
gesture
.
direction
)
const
perpendicularDir
=
perpendicular
(
gesture
.
direction
)
const
towardsPerpendicular
=
vectorFlatten
(
perpendicularDir
,
delta
)
const
towardsPerpendicular
=
project
(
delta
,
perpendicularDir
)
if
(
vectorLength
(
towardsDir
)
<
gesture
.
perpendicularTolerance
*
vectorLength
(
towardsPerpendicular
)
vectorLength
(
towardsDir
)
*
gesture
.
perpendicularTolerance
<
vectorLength
(
towardsPerpendicular
)
)
return
gesture
.
onSwipe
()
...
...
This diff is collapsed.
Click to expand it.
test/unit/specs/services/gesture_service/gesture_service.spec.js
0 → 100644
+
120
−
0
View file @
c50e64f8
import
GestureService
from
'
src/services/gesture_service/gesture_service.js
'
const
mockTouchEvent
=
(
x
,
y
)
=>
({
touches
:
[
{
screenX
:
x
,
screenY
:
y
}
]
})
describe
.
only
(
'
GestureService
'
,
()
=>
{
describe
(
'
swipeGesture
'
,
()
=>
{
it
(
'
calls the callback on a successful swipe
'
,
()
=>
{
let
swiped
=
false
const
callback
=
()
=>
{
swiped
=
true
}
const
gesture
=
GestureService
.
swipeGesture
(
GestureService
.
DIRECTION_RIGHT
,
callback
)
GestureService
.
beginSwipe
(
mockTouchEvent
(
100
,
100
),
gesture
)
GestureService
.
updateSwipe
(
mockTouchEvent
(
200
,
100
),
gesture
)
expect
(
swiped
).
to
.
eql
(
true
)
})
it
(
'
calls the callback only once per begin
'
,
()
=>
{
let
hits
=
0
const
callback
=
()
=>
{
hits
+=
1
}
const
gesture
=
GestureService
.
swipeGesture
(
GestureService
.
DIRECTION_RIGHT
,
callback
)
GestureService
.
beginSwipe
(
mockTouchEvent
(
100
,
100
),
gesture
)
GestureService
.
updateSwipe
(
mockTouchEvent
(
150
,
100
),
gesture
)
GestureService
.
updateSwipe
(
mockTouchEvent
(
200
,
100
),
gesture
)
expect
(
hits
).
to
.
eql
(
1
)
})
it
(
'
doesn
\'
t call the callback on an opposite swipe
'
,
()
=>
{
let
swiped
=
false
const
callback
=
()
=>
{
swiped
=
true
}
const
gesture
=
GestureService
.
swipeGesture
(
GestureService
.
DIRECTION_RIGHT
,
callback
)
GestureService
.
beginSwipe
(
mockTouchEvent
(
100
,
100
),
gesture
)
GestureService
.
updateSwipe
(
mockTouchEvent
(
0
,
100
),
gesture
)
expect
(
swiped
).
to
.
eql
(
false
)
})
it
(
'
doesn
\'
t call the callback on a swipe below threshold
'
,
()
=>
{
let
swiped
=
false
const
callback
=
()
=>
{
swiped
=
true
}
const
gesture
=
GestureService
.
swipeGesture
(
GestureService
.
DIRECTION_RIGHT
,
callback
,
100
)
GestureService
.
beginSwipe
(
mockTouchEvent
(
100
,
100
),
gesture
)
GestureService
.
updateSwipe
(
mockTouchEvent
(
150
,
100
),
gesture
)
expect
(
swiped
).
to
.
eql
(
false
)
})
it
(
'
doesn
\'
t call the callback on a perpendicular swipe
'
,
()
=>
{
let
swiped
=
false
const
callback
=
()
=>
{
swiped
=
true
}
const
gesture
=
GestureService
.
swipeGesture
(
GestureService
.
DIRECTION_RIGHT
,
callback
,
30
,
0.5
)
GestureService
.
beginSwipe
(
mockTouchEvent
(
100
,
100
),
gesture
)
GestureService
.
updateSwipe
(
mockTouchEvent
(
150
,
200
),
gesture
)
expect
(
swiped
).
to
.
eql
(
false
)
})
it
(
'
calls the callback on perpendicular swipe if within tolerance
'
,
()
=>
{
let
swiped
=
false
const
callback
=
()
=>
{
swiped
=
true
}
const
gesture
=
GestureService
.
swipeGesture
(
GestureService
.
DIRECTION_RIGHT
,
callback
,
30
,
2.0
)
GestureService
.
beginSwipe
(
mockTouchEvent
(
100
,
100
),
gesture
)
GestureService
.
updateSwipe
(
mockTouchEvent
(
150
,
150
),
gesture
)
expect
(
swiped
).
to
.
eql
(
true
)
})
it
(
'
works with any arbitrary 2d directions
'
,
()
=>
{
let
swiped
=
false
const
callback
=
()
=>
{
swiped
=
true
}
const
gesture
=
GestureService
.
swipeGesture
(
[
-
1
,
-
1
],
callback
,
30
,
0.1
)
GestureService
.
beginSwipe
(
mockTouchEvent
(
100
,
100
),
gesture
)
GestureService
.
updateSwipe
(
mockTouchEvent
(
60
,
60
),
gesture
)
expect
(
swiped
).
to
.
eql
(
true
)
})
})
})
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment