Skip to content

Commit 1ae37de

Browse files
author
@soyjavi
committed
Refactor naming
1 parent 3d2c388 commit 1ae37de

6 files changed

+92
-85
lines changed

source/quo.gestures.basic.coffee

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
###
22
Quo Basic Gestures: tap, hold, singleTap, doubleTap
33
4+
@namespace Quo.Gestures
5+
@class Basic
6+
47
@author Ignacio Olalde Ramos <ina@tapquo.com> || @piniphone
58
###
69
"use strict"
710

811

912
Quo.Gestures.add
1013
name : "basic"
11-
events : "tap,hold,singleTap,doubleTap,touch".split(",")
12-
handler : do (gm = Quo.Gestures) ->
14+
events : ["tap", "hold", "singleTap", "doubleTap", "touch"]
1315

14-
ALLOWED_MOVE_PIXELS = 15
15-
TAP = 250
16-
DOUBLE_TAP = 400
17-
HOLD = 400
16+
handler : do (base = Quo.Gestures) ->
17+
GAP = 15
18+
DELAY =
19+
TAP : 250
20+
DOUBLE_TAP: 400
21+
HOLD : 400
1822

1923
_hold_timeout = null
2024
_simpletap_timeout = null
@@ -28,41 +32,40 @@ Quo.Gestures.add
2832
_start = time: new Date(), x: data[0].x, y: data[0].y
2933
_target = target
3034
_hold_timeout = setTimeout ->
31-
gm.trigger(target, "hold", data[0])
32-
, HOLD
35+
base.trigger(target, "hold", data[0])
36+
, DELAY.HOLD
3337
else do cancel
3438

3539
move = (target, data) ->
3640
if _start isnt null
3741
xDiff = data[0].x - _start.x
3842
yDiff = data[0].y - _start.y
39-
if xDiff > ALLOWED_MOVE_PIXELS or yDiff > ALLOWED_MOVE_PIXELS or data.length > 1
43+
if xDiff > GAP or yDiff > GAP or data.length > 1
4044
do cancel
4145

4246
end = (target, data) ->
43-
gm.trigger(target, "touch", data[0])
47+
base.trigger(target, "touch", data[0])
4448
return unless _start
4549
clearTimeout _hold_timeout
4650
now = new Date()
47-
if (now - _start.time) < TAP
48-
if (now - _last_tap) < DOUBLE_TAP
51+
if (now - _start.time) < DELAY.TAP
52+
if (now - _last_tap) < DELAY.DOUBLE_TAP
4953
clearTimeout _simpletap_timeout
50-
gm.trigger(target, "doubleTap", data[0])
54+
base.trigger(target, "doubleTap", data[0])
5155
_last_tap = null
5256
else
5357
_last_tap = now
54-
gm.trigger(target, "tap", data[0])
58+
base.trigger(target, "tap", data[0])
5559
_simpletap_timeout = setTimeout ->
56-
gm.trigger(target, "singleTap", data[0])
57-
, DOUBLE_TAP + 5
60+
base.trigger(target, "singleTap", data[0])
61+
, DELAY.DOUBLE_TAP + 5
5862

5963
cancel = () ->
6064
_start = null
6165
_valid = false
6266
clearTimeout _hold_timeout
6367
clearTimeout _simpletap_timeout
6468

65-
6669
start : start
6770
move : move
6871
end : end

source/quo.gestures.coffee

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,17 @@
22
Quo Gestures manager
33
44
@namespace Quo
5-
@class gesture
5+
@class Gestures
66
77
@author Ignacio Olalde Ramos <ina@tapquo.com> || @piniphone
88
@author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
99
###
1010
"use strict"
1111

12-
1312
Quo.Gestures = do ($$ = Quo) ->
1413

15-
STARTED = false
16-
HANDLERS = {}
17-
14+
_started = false
15+
_handlers = {}
1816
_data = null
1917
_originalEvent = null
2018

@@ -26,33 +24,33 @@ Quo.Gestures = do ($$ = Quo) ->
2624
environment.bind "touchcancel", _cancel
2725

2826
add = (gesture) ->
29-
HANDLERS[gesture.name] = gesture.handler
27+
_handlers[gesture.name] = gesture.handler
3028
_addDelegations gesture.events
3129

3230
trigger = (target, eventName, gestureData) ->
3331
$$(target).trigger(eventName, gestureData, _originalEvent)
3432

3533
# Private methods
3634
_start = (ev) ->
37-
STARTED = true
35+
_started = true
3836
_originalEvent = ev or event
3937
_data = _getFingersData(ev)
4038
_handle "start", ev.target, _data
4139

4240
_move = (ev) ->
43-
return unless STARTED
41+
return unless _started
4442
_originalEvent = ev or event
4543
_data = _getFingersData(ev)
4644
_handle "move", ev.target, _data
4745

4846
_end = (ev) ->
49-
return unless STARTED
47+
return unless _started
5048
_originalEvent = ev or event
5149
_handle "end", ev.target, _data
52-
STARTED = false
50+
_started = false
5351

5452
_cancel = (ev) ->
55-
STARTED = false
53+
_started = false
5654
_handle "cancel"
5755

5856
_addDelegations = (events) ->
@@ -62,14 +60,12 @@ Quo.Gestures = do ($$ = Quo) ->
6260
@
6361

6462
_handle = (eventName, target, data) ->
65-
for name, handler of HANDLERS when handler[eventName]
63+
for name, handler of _handlers when handler[eventName]
6664
handler[eventName].call(handler, target, data)
6765

6866
_getFingersData = (event) ->
6967
touches = if $$.isMobile() then event.touches else [event]
7068
return ({x: t.pageX, y: t.pageY} for t in touches)
7169

72-
7370
add : add
7471
trigger : trigger
75-

source/quo.gestures.drag.coffee

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
###
22
Quo Drag Gestures: drag, dragging
33
4+
@namespace Quo.Gestures
5+
@class Drag
6+
47
@author Ignacio Olalde Ramos <ina@tapquo.com> || @piniphone
58
###
69
"use strict"
710

811

912
Quo.Gestures.add
1013
name : "drag"
11-
events : "drag,dragging".split(",")
12-
handler : do (gm = Quo.Gestures) ->
13-
14-
MIN_PX = 20
14+
events : ["drag" ,"dragging"]
1515

16-
_target = null
17-
_num_fingers = null
18-
_start = null
19-
_last = null
16+
handler : do (base = Quo.Gestures) ->
17+
GAP = 20
18+
_target = null
19+
_num_fingers = null
20+
_start = null
21+
_last = null
2022

2123
start = (target, data) ->
2224
if data.length >= 2
@@ -49,11 +51,10 @@ Quo.Gestures.add
4951
y += parseInt(touch.y)
5052
return x: (x / touches.length), y: (y / touches.length)
5153

52-
_check = (is_moving) ->
53-
if is_moving then gm.trigger _target, "dragging", _last
54-
else if Math.abs(_last.delta.x) > MIN_PX or Math.abs(_last.delta.y) > MIN_PX
55-
gm.trigger _target, "drag", _last
56-
54+
_check = (moving) ->
55+
if moving then base.trigger _target, "dragging", _last
56+
else if Math.abs(_last.delta.x) > GAP or Math.abs(_last.delta.y) > GAP
57+
base.trigger _target, "drag", _last
5758

5859
start: start
5960
move: move

source/quo.gestures.pinch.coffee

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
###
22
Quo Pinch Gestures: pinch, pinching, pinchIn, pinchOut
33
4+
@namespace Quo.Gestures
5+
@class Pinch
6+
47
@author Ignacio Olalde Ramos <ina@tapquo.com> || @piniphone
58
###
69
"use strict"
710

811

912
Quo.Gestures.add
1013
name : "pinch"
11-
events : "pinch,pinching,pinchIn,pinchOut".split(",")
12-
handler : do (gm = Quo.Gestures) ->
13-
14-
TRIGGER_PIXELS = 20
14+
events : ["pinch", "pinching", "pinchIn", "pinchOut"]
1515

16+
handler : do (base = Quo.Gestures) ->
17+
GAP = 20
1618
_target = null
1719
_start = null
1820
_last = null
@@ -37,13 +39,12 @@ Quo.Gestures.add
3739
_distance = (A, B) ->
3840
Math.sqrt((B.x-A.x)*(B.x-A.x)+(B.y-A.y)*(B.y-A.y))
3941

40-
_check = (is_moving) ->
41-
if is_moving then gm.trigger(_target, "pinching", _last)
42-
else if Math.abs(_last.delta) > TRIGGER_PIXELS
43-
gm.trigger _target, "pinch", _last
42+
_check = (moving) ->
43+
if moving then base.trigger(_target, "pinching", _last)
44+
else if Math.abs(_last.delta) > GAP
45+
base.trigger _target, "pinch", _last
4446
ev = if _last.delta > 0 then "pinchOut" else "pinchIn"
45-
gm.trigger _target, ev, _last
46-
47+
base.trigger _target, ev, _last
4748

4849
start: start
4950
move: move

source/quo.gestures.rotation.coffee

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
###
22
Quo Rotation Gestures: rotate, rotating, rotateLeft, rotateRight
33
4+
@namespace Quo.Gestures
5+
@class Rotation
6+
47
@author Ignacio Olalde Ramos <ina@tapquo.com> || @piniphone
58
###
69
"use strict"
710

811

912
Quo.Gestures.add
1013
name : "rotation"
11-
events : "rotate,rotating,rotateLeft,rotateRight".split(",")
12-
handler : do (gm = Quo.Gestures) ->
14+
events : ["rotate", "rotating", "rotateLeft", "rotateRight"]
1315

14-
TRIGGER_ANGLE = 5
15-
IMPOSIBLE_ROTATION_FACTOR = 20
16+
handler : do (base = Quo.Gestures) ->
17+
GAP = 5
18+
ROTATION_LIMIT = 20
1619

17-
_target = null
18-
_num_rotations = 0
19-
_start = null
20-
_last = null
20+
_target = null
21+
_num_rotations = 0
22+
_start = null
23+
_last = null
2124

2225
start = (target, data) ->
2326
if data.length is 2
@@ -28,7 +31,7 @@ Quo.Gestures.add
2831
move = (target, data) ->
2932
if _start and data.length is 2
3033
delta = _rotation(data[0], data[1]) - _start
31-
if _last and Math.abs(_last.delta - delta) > IMPOSIBLE_ROTATION_FACTOR
34+
if _last and Math.abs(_last.delta - delta) > ROTATION_LIMIT
3235
delta += (360 * _sign(_last.delta))
3336
if Math.abs(delta) > 360
3437
_num_rotations++
@@ -51,12 +54,12 @@ Quo.Gestures.add
5154
theta = Math.atan2(A.y-B.y, A.x-B.x)
5255
(if theta < 0 then theta + 2 * Math.PI else theta) * 180 / Math.PI
5356

54-
_check = (is_moving) ->
55-
if is_moving then gm.trigger _target, "rotating", _last
56-
else if Math.abs(_last.delta) > TRIGGER_ANGLE
57-
gm.trigger _target, "rotate", _last
57+
_check = (moving) ->
58+
if moving then base.trigger _target, "rotating", _last
59+
else if Math.abs(_last.delta) > GAP
60+
base.trigger _target, "rotate", _last
5861
ev = if _last.delta > 0 then "rotateRight" else "rotateLeft"
59-
gm.trigger _target, ev, _last
62+
base.trigger _target, ev, _last
6063

6164
start: start
6265
move: move

source/quo.gestures.swipe.coffee

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,26 @@
22
Quo Swipe Gestures: swipe, swiping, swipeLeft, swipeRight, swipeUp, swipeDown
33
New gestures added: swipingHorizontal, swipingVertical
44
5+
@namespace Quo.Gestures
6+
@class Swipe
7+
58
@author Ignacio Olalde Ramos <ina@tapquo.com> || @piniphone
69
###
710
"use strict"
811

912

1013
Quo.Gestures.add
1114
name : "swipe"
12-
events : "swipe,swiping,swipeLeft,swipeRight,swipeUp,swipeDown,swipingHorizontal,swipingVertical".split(",")
13-
handler : do (gm = Quo.Gestures) ->
14-
15-
TRIGGER_PIXELS = 20
15+
events : ["swipe",
16+
"swipeLeft", "swipeRight", "swipeUp", "swipeDown",
17+
"swiping", "swipingHorizontal", "swipingVertical"]
1618

17-
_target = null
18-
_start = null
19-
_start_axis = null
20-
_last = null
19+
handler : do (base = Quo.Gestures) ->
20+
GAP = 20
21+
_target = null
22+
_start = null
23+
_start_axis = null
24+
_last = null
2125

2226
start = (target, data) ->
2327
if data.length is 1
@@ -38,21 +42,21 @@ Quo.Gestures.add
3842
_check(false)
3943
_last = null
4044

41-
_check = (is_moving, first_move = false) ->
42-
if is_moving
45+
_check = (moving, first_move = false) ->
46+
if moving
4347
if first_move then _start_axis = _getInitialAxis(_last.delta.x, _last.delta.y)
4448
if _start_axis isnt null
45-
gm.trigger(_target, "swiping#{_start_axis}", _last)
46-
gm.trigger(_target, "swiping", _last)
49+
base.trigger(_target, "swiping#{_start_axis}", _last)
50+
base.trigger(_target, "swiping", _last)
4751
else
4852
directions = []
49-
if Math.abs(_last.delta.y) > TRIGGER_PIXELS
53+
if Math.abs(_last.delta.y) > GAP
5054
directions.push(if _last.delta.y < 0 then "Up" else "Down")
51-
if Math.abs(_last.delta.x) > TRIGGER_PIXELS
55+
if Math.abs(_last.delta.x) > GAP
5256
directions.push(if _last.delta.x < 0 then "Left" else "Right")
5357
if directions.length
54-
gm.trigger(_target, "swipe", _last)
55-
gm.trigger(_target, "swipe#{direction}", _last) for direction in directions
58+
base.trigger(_target, "swipe", _last)
59+
base.trigger(_target, "swipe#{direction}", _last) for direction in directions
5660

5761
_getInitialAxis = (x, y) ->
5862
axis = null
@@ -63,4 +67,3 @@ Quo.Gestures.add
6367
start: start
6468
move: move
6569
end: end
66-

0 commit comments

Comments
 (0)