-
Notifications
You must be signed in to change notification settings - Fork 0
/
bdrawer.coffee
162 lines (133 loc) · 3.85 KB
/
bdrawer.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# BDrawer 0.1
# MIT Licensed
# By Anders Rex (andersrex.com)
class window.BDrawer
constructor: (options) ->
return null unless options.content and options.drawer
# Get options
@content = options.content
@drawer = options.drawer
@speed = options.speed or 200
@overlap = options.overlap or 60
@openable = options.openable or true
@prevented = options.prevented or {y1: false, y2: false}
@closed = options.closed or true
@options = options or {}
@width = @content.getBoundingClientRect().width
# Set up CSS for content element
style = @content.style
style.display = 'block'
style.position = 'fixed'
style.left = '0px'
style.top = '0px'
style.zIndex = 1000
style.width = @width + 'px'
# Set up CSS for drawer element
style = @drawer.style
style.display = 'block'
style.position = 'fixed'
style.left = '0px'
style.top = '0px'
style.zIndex = 900
style.width = @width + 'px'
# Set up mask element
@mask = document.createElement 'div'
style = @mask.style
style.position = 'absolute'
style.display = 'block'
style.width = '100%'
style.height = '100%'
style.left = 0
style.top = 0
style.zIndex = 1000
style.visibility = 'hidden'
style.opacity = 0
style.background = 'black'
@content.appendChild @mask
# Bind events for content element
@content.addEventListener 'touchstart', this, false
@content.addEventListener 'touchmove', this, false
@content.addEventListener 'touchend', this, false
@mask.addEventListener 'click', this, false
handleEvent: (e) ->
switch e.type
when "touchstart" then @_touchStart e
when "touchmove" then @_touchMove e
when "touchend" then @_touchEnd e
when "click" then @close()
open: () ->
@_move @width - @overlap, @speed
@_showMask()
@closed = false
close: () ->
@_move 0, @speed
@_hideMask()
@closed = true
toggle: () ->
if @closed
@open()
else
@close()
_showMask: ->
@mask.style.visibility = 'visible'
_hideMask: ->
@mask.style.visibility = 'hidden'
# Move the content element delta x (dx) with speed (speed)
_move: (dx, speed) ->
if @openable
style = @content.style
style.webkitTransitionDuration = speed + 'ms'
style.webkitTransform = 'translate3d(' + dx + 'px, 0, 0)'
_touchStart: (e) ->
@_x = e.touches[0].pageX
@_y = e.touches[0].pageY
@_t = Number( new Date() )
@_dx = 0
# For checking for Y axis scrolling
@_scrolling = null
_touchMove: (e) ->
# Check if touch is prevented
if @_touchPrevented() and @closed
return
# Check for other gestures than swipe
if e.touches.length > 1
return
# Delta X and delta Y
@_dx = e.touches[0].pageX - @_x
dy = e.touches[0].pageY - @_y
# If first delta y is twice bigger that delta x we are scrolling
if @_scrolling is null
@_scrolling = Math.abs(dy) > Math.abs(@_dx)
unless @_scrolling
if @closed and @openable
@_dx = 0 if @_dx < 0
@_move @_dx, 0
else
@_dx = 0 if @_dx > 0
@_dx = -@width+@overlap if @_dx+@width-@overlap < 0
@_move @_dx+@width-@overlap, 0
_touchEnd: (e) ->
if @_touchPrevented() or @_scrolling or @_dx is 0
return
dt = Number(new Date()) - @_t
# Check if swipe is enough to close or open drawer
isSwipe = (dt < 200 or
Math.abs(@_dx) > (@width-@overlap)/2) or
(Math.abs(@_dx) > 20 and dt < 150)
if isSwipe and not @_scrolling
if @_dx > 0
@open()
else
@close()
else
if @_dx > 0
@close()
else
@open()
_touchPrevented: ->
y1 = @prevented.y1
y2 = @prevented.y2
if y1 is false or @_y < y1 or y2 is false or @_y > y2
return false
else
return true