-
Notifications
You must be signed in to change notification settings - Fork 4
/
infinite-slider.coffee
449 lines (364 loc) · 15.4 KB
/
infinite-slider.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
##
## <infinite-slider> attributes
##
## acceleration || 1.15 # "acceleration" > 1
## friction || 0.95 # "friction" < 1
## spring-back || 0.1 # spring-back 0..1 1=fastest
## click-fudge || 2 # pixels of movement that still allow click
## max-velocity || 70 # maximum scrollwheel velocity
## snap || false # per-item snapping **ONLY WORKS IF ALL ITEMS ARE THE SAME WIDTH
##
angular.module('gilbox.infiniteSlider.helpers', []).factory 'browserHelper', ['$window', ($window) ->
_has3d = undefined
has3d: -> # perform check the first time the function is invoked
return _has3d if _has3d isnt `undefined`
return _has3d = (-> # http://stackoverflow.com/questions/5661671/detecting-transform-translate3d-support
el = document.createElement("p")
has3d = undefined
transforms =
webkitTransform: "-webkit-transform"
OTransform: "-o-transform"
msTransform: "-ms-transform"
MozTransform: "-moz-transform"
transform: "transform"
document.body.insertBefore el, null
for t of transforms
if el.style[t] isnt `undefined`
el.style[t] = "translate3d(1px,1px,1px)"
has3d = $window.getComputedStyle(el).getPropertyValue(transforms[t])
document.body.removeChild el
has3d isnt `undefined` and has3d.length > 0 and has3d isnt "none")()
getTouchPoint: (event) ->
e = switch
when event.touches?
event.touches[0]
when event.originalEvent? && event.originalEvent.touches? && event.originalEvent.touches.length
event.originalEvent.touches[0]
else event
# TODO: Make this more efficient. We are extending the new object here because mobile safari re-uses touch event objects.
angular.extend {x: e.pageX, y: e.pageY}, e
] # /browserHelper
# Optional mousewheel support
deps = ['gilbox.infiniteSlider.helpers']
try
angular.module 'monospaced.mousewheel'
deps.unshift 'monospaced.mousewheel'
catch e
angular.module('gilbox.infiniteSlider', deps)
# define a touch-region using infinite-slider-boundary
# may be a parent, or child, of the infinite-slider element.
.directive 'infiniteSliderBoundary', ->
restrict: 'AE'
scope: {} # necessary to isolate msd-wheel scope with multiple sliders
transclude: true
replace: true
template: '<div ng-transclude msd-wheel="wheel($event, $delta, $deltaX, $deltaY)"></div>'
require: '?^infiniteSlider'
controller: ['$scope', '$element', ($scope, $element) ->
@elm = $element
@setWheelFn = (fn) -> $scope.wheel = fn
@ # why is this necessary?
]
link: (scope, element, attrs, ctrl) ->
if ctrl
ctrl.setBoundaryElm(element)
scope.wheel = ctrl.wheelFn
.directive 'infiniteSliderContent', ->
restrict: 'AE'
require: '^infiniteSlider'
link: (scope, element, attrs, ctrl) -> ctrl.setContElm element
.directive 'infiniteSlider', ['$window', '$document', '$timeout', 'browserHelper', ($window, $document, $timeout, browserHelper) ->
restrict: 'AE'
scope: {
snappedItemId: '=?',
closestItemId: '=?',
isJumping: '=?'
}
require: '?^infiniteSliderBoundary'
controller: [ '$scope', ($scope) ->
@setBoundaryElm = (elm) -> $scope.boundaryElm = elm
@setContElm = (elm) -> $scope.contElm = elm
@wheelFn = (event, delta, deltaX, deltaY) -> $scope.wheelFn(event, delta, deltaX, deltaY)
@
]
link: (scope, element, attrs, boundaryCtrl) ->
animationFrame = new AnimationFrame()
a = attrs.acceleration || 1.05 # "acceleration" > 1
f = attrs.friction || 0.95 # 0 < "friction" < 1
spring = attrs.springBack || 0.3 # spring-back 0..1 1=fastest
clickFudge = attrs.clickFudge || 2 # pixels of movement that still allow click
snap = attrs.hasOwnProperty('snap') && attrs.snap != 'false'
snapVelocityTrigger = attrs.snapVelocityTrigger || 5
classifyClosest = attrs.hasOwnProperty('classifyClosest') && attrs.classifyClosest != 'false'
classifySnapped = attrs.hasOwnProperty('classifySnapped') && attrs.classifySnapped != 'false'
elmScope = element.scope()
v = 0 # "velocity"
xCont = 0
winElm = angular.element($window)
boundaryElm = scope.boundaryElm || (boundaryCtrl && boundaryCtrl.elm) || element;
contElm = scope.contElm || element.children().eq(0)
items = null
endTypes = 'touchend touchcancel mouseup mouseleave'
moveTypes = 'touchmove mousemove'
startTypes = 'touchstart mousedown'
allowClick = true
elementStartX = 0
interactionStart = null
interactionCurrent = null
prevInteraction = null
xMin = 0
xMax = 0
firstItem = null
lastItem = null
itemWidth = 0
jumping = false
snappedItemId = scope.snappedItemId # we'll keep track of snapped item id even if an attribute isn't present
snappedItemId_isBound = scope.hasOwnProperty('snappedItemId')
closestItemId_isBound = scope.hasOwnProperty('closestItemId')
isJumping_isBound = scope.hasOwnProperty('isJumping')
scope.isJumping = false if isJumping_isBound
lastWheelTime = new Date()
running = false
has3d = browserHelper.has3d()
# declare this variable here which is calculated in run() so it may be utilized by the snappedItemId $watcher
snapTargetX = 0
toIds = {}
setTimeoutWithId = (fn, ms, id) ->
clearTimeout toIds[id]
toIds[id] = setTimeout fn, ms
setAllowClick = (v) ->
allowClick = v
element.toggleClass 'allow-click', !v
setAllowClick true
endHandler = (event) -> # drag end
unless (allowClick)
event.preventDefault()
if (interactionStart == null || (Math.abs(interactionCurrent.x - interactionStart.x) < clickFudge && Math.abs(interactionCurrent.y - interactionStart.y) < clickFudge))
setAllowClick true # click now
el = document.elementFromPoint(interactionCurrent.x, interactionCurrent.y);
if el? and !interactionCurrent.button
document.elementFromPoint(interactionCurrent.clientX, interactionCurrent.clientY).click();
else
v = prevInteraction.x - interactionCurrent.x # momentum-generated velocity
setTimeout (-> setAllowClick true), 100 # don't allow click todo: seems hacky, a better way to do this?
interactionStart = null
$document.off moveTypes, moveHandler
moveHandler = (event) -> # drag move
event.preventDefault()
prevInteraction = interactionCurrent if interactionCurrent
interactionCurrent = browserHelper.getTouchPoint event
if prevInteraction # viewport scrolling (up/down)
dy = prevInteraction.y - interactionCurrent.y
dx = prevInteraction.x - interactionCurrent.x
if (Math.abs(dy) > Math.abs(dx))
$window.scrollBy(0, dy)
prevInteraction.y += dy
interactionCurrent.y += dy
xCont = elementStartX + (interactionCurrent.x - interactionStart.x)
doTransform()
startHandler = (event) -> # drag start
event.preventDefault()
setAllowClick false
v = 0
elementStartX = xCont
interactionStart = interactionCurrent = browserHelper.getTouchPoint event
$document.on moveTypes, moveHandler
clickHandler = (event) ->
event.preventDefault() if (!allowClick)
allowClick
attrs.$observe 'disableDrag',
(next) ->
if next != 'true'
$document.on endTypes, endHandler
boundaryElm.on startTypes, startHandler
boundaryElm.on 'click', clickHandler
else
$document.off endTypes, endHandler
boundaryElm.off startTypes, startHandler
boundaryElm.off 'click', clickHandler
setSnappedItem = (newSnappedItem) ->
scope.snappedItemElm.removeClass 'snapped' if scope.snappedItemElm and classifySnapped
newSnappedItem.addClass 'snapped' if classifySnapped
scope.snappedItemElm = newSnappedItem
snappedItemId = newSnappedItem.idx
scope.snappedItemId = snappedItemId if snappedItemId_isBound
setClosestItem = (newClosestItem) ->
scope.closestItem.removeClass 'closest' if scope.closestItem
newClosestItem.addClass 'closest'
if closestItemId_isBound && scope.closestItem # skip first call by checking if cloestItem is undefined
scope.closestItemId = newClosestItem.idx
scope.$apply()
scope.closestItem = newClosestItem
onFrame = ->
if !jumping && items && itemWidth
xchanged = false
if classifyClosest || snap
snapTargetX = itemWidth * Math.round(xCont / itemWidth)
newSnappedItemId = (firstItem.idx + Math.abs(firstItem.x + snapTargetX)/itemWidth) %% items.length
newSnappedItem = items[newSnappedItemId].elm
if classifyClosest && scope.closestItem != newSnappedItem
setClosestItem newSnappedItem
if allowClick && Math.abs(v) < snapVelocityTrigger
if xCont != snapTargetX
xCont += (snapTargetX-xCont)*spring
xCont = snapTargetX if Math.abs(snapTargetX-xCont)<1
xchanged = true
v = 0
else
if newSnappedItemId != snappedItemId
setSnappedItem newSnappedItem
scope.$apply() if snappedItemId_isBound
if v
v *= f
xCont -= v
v = 0 if Math.abs(v) < 0.001
xchanged = true
if xchanged
doTransform()
rearrange()
animationFrame.request(onFrame) if running
run = ->
running = true
animationFrame.request(onFrame)
# endless loop rearrange
rearrange = ->
if items
rearr = (prevItem)->
if firstItem.x + xCont < xMin - firstItem.clientWidth * 0.51
prev = firstItem
firstItem.x = lastItem.x + firstItem.clientWidth
positionItem firstItem
[firstItem, lastItem] = [firstItem.nextItem, firstItem]
rearrange(prev) if prevItem != firstItem
else if lastItem.x + xCont > xMax #+ lastItem.clientWidth * 0.51
prev = lastItem
lastItem.x = firstItem.x - lastItem.clientWidth
positionItem lastItem
[firstItem, lastItem] = [lastItem, lastItem.prevItem]
rearrange(prev)
rearr()
positionItem = (item) ->
item.style.left = item.x + 'px'
# use css transition (transition==true) for a smooth animation
# when jumping to a new snapped-item-id
doTransform = (transition)->
if has3d
if transition
contElm.css
'-webkit-transition': '-webkit-transform .5s'
'transition': 'transform .5s'
jumping = true
scope.isJumping = true if isJumping_isBound
setTimeoutWithId ->
contElm.css
'-webkit-transition': 'none'
'transition': 'none'
jumping = false
scope.isJumping = false if isJumping_isBound
, 500, 1
contElm.css
'-webkit-transform': 'translate3d(' + xCont + 'px, 0px, 0px)'
'-moz-transform': 'translate3d(' + xCont + 'px, 0px, 0px)'
'-o-transform': 'translate3d(' + xCont + 'px, 0px, 0px)'
'-ms-transform': 'translate3d(' + xCont + 'px, 0px, 0px)'
'transform': 'translate3d(' + xCont + 'px, 0px,0px)'
else
contElm.css('left', xCont + 'px');
# returns false if unable to calculate a content width,
# otherwise assigns the value to contentWidth
calcContentWidth = ->
return if !items
contentWidth = 0
lastidx = items.length-1
firstItem = items[0]
lastItem = items[lastidx]
itemWidth = firstItem.clientWidth
unless itemWidth
setTimeoutWithId calcContentWidth, 200, 2 # todo: a better way? seems hacky
return false
for item,i in items
if item is lastItem then item.nextItem = firstItem else item.nextItem = items[i+1]
if item is firstItem then item.prevItem = lastItem else item.prevItem = items[i-1]
item.x = contentWidth
item.elm = items.eq(i)
item.idx = item.elm.idx = i
positionItem(item)
contentWidth += item.clientWidth
boundsOffsetX = element[0].clientWidth/2 - itemWidth/2
if attrs.infiniteSliderAlign == 'left'
xMax = contentWidth - boundsOffsetX
xMin = boundsOffsetX
else
xMax = contentWidth/2 + boundsOffsetX
xMin = boundsOffsetX - contentWidth/2
true
onWinResize = ->
if calcContentWidth()
if items
if snap && ! scope.snappedItemElm then setSnappedItem items[snappedItemId || 0].elm
if classifyClosest && ! scope.closestItem then setClosestItem items[snappedItemId || 0].elm
if snappedItemId?
# to prevent snapped item from changing on window resize, we recalculate
# xCont accordingly, but will responsive layout still pose a problem ??
xCont = -itemWidth * snappedItemId
rearrange()
doTransform()
else
rearrange()
scope.wheelFn = (event, delta, deltaX, deltaY) ->
# note: because of the refactor described below, when snapping behavior is turned off the wheel doesn't work
#
# - Wheeling modifies the snappedItemId instead of effecting velocity
# - To overcome the erratic behavior of inertial scrolling on OS X we ignore wheel
# events when:
# the previous deltaX was greater than the current delta X
# when the last wheel event was also less than 100ms ago
newTime = (new Date()).getTime()
if deltaX and !jumping and Math.abs(deltaX) > Math.abs(deltaY) and (newTime - lastWheelTime > 50)
event.preventDefault()
if deltaX > 0
scope.closestItemId = scope.snappedItemId = (scope.snappedItemId+1) %% items.length
else
scope.closestItemId = scope.snappedItemId = (scope.snappedItemId-1) %% items.length
lastWheelTime = newTime
return true
if boundaryCtrl and snap then boundaryCtrl.setWheelFn(scope.wheelFn)
readItems = ->
items = contElm.children()
items = null if !items? || !items.length
if attrs.slides
elmScope.$watch attrs.slides, ->
readItems()
onWinResize()
, true
else
$timeout ->
readItems()
onWinResize()
onSnappedItemIdChange = (newId) ->
# the third condition determines if the id was changed internally,
# because if it was then we don't need to do this stuff
if items and 0 <= newId < items.length and scope.snappedItemId != scope.snappedItemElm.idx
# calculate the shortest distance to the newId item because otherwise it breaks the endless effect
# @todo: there might be a simpler way to do this
vId = if newId<snappedItemId then items.length+newId else newId-items.length
targetId = if Math.abs(vId-snappedItemId) < Math.abs(newId-snappedItemId) then vId else newId
deltaId = targetId - snappedItemId
setSnappedItem items[newId].elm
xCont = snapTargetX - itemWidth * deltaId
calcContentWidth()
rearrange()
doTransform(true)
scope.$watch 'snappedItemId', (newId) ->
onWinResize()
onSnappedItemIdChange(parseInt(newId))
run()
winElm.on 'resize', onWinResize
scope.$on '$destroy', ->
winElm.off 'resize', onWinResize
$document.off endTypes, endHandler
boundaryElm.off startTypes, startHandler
$document.off moveTypes, moveHandler
boundaryElm.off 'click', clickHandler
running = false
] # /infiniteSlider