This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CustomScrollView: own scrollbars logic taken to its own class
- Loading branch information
Showing
5 changed files
with
174 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
require('jquery-mousewheel') $ | ||
|
||
JView = require './../../core/jview' | ||
KDCustomHTMLView = require './../../core/customhtmlview' | ||
KDScrollTrack = require './scrolltrack' | ||
KDCustomScrollViewWrapper = require './customscrollviewinner' | ||
|
||
module.exports = class KDCustomScrollView extends KDCustomHTMLView | ||
|
||
constructor:(options = {}, data)-> | ||
|
||
options.naturalScroll ?= yes | ||
options.cssClass = KD.utils.curry 'kdcustomscrollview', options.cssClass | ||
options.mouseWheelSpeed ?= 3 | ||
|
||
super options, data | ||
|
||
{mouseWheelSpeed} = @getOptions() | ||
|
||
@wrapper = new KDCustomScrollViewWrapper { | ||
tagName : 'main' | ||
mouseWheelSpeed | ||
} | ||
|
||
@verticalTrack = new KDScrollTrack delegate : @wrapper | ||
@horizontalTrack = new KDScrollTrack delegate : @wrapper, type : 'horizontal' | ||
@wrapper.verticalThumb = @verticalTrack.thumb | ||
@wrapper.horizontalThumb = @horizontalTrack.thumb | ||
|
||
@wrapper.on 'ScrollTrackShown', (type)=> @setClass "has-#{type}" | ||
@wrapper.on 'ScrollTrackHidden', (type)=> @unsetClass "has-#{type}" | ||
|
||
|
||
pistachio:-> | ||
|
||
""" | ||
{{> @wrapper}} | ||
{{> @verticalTrack}} | ||
{{> @horizontalTrack}} | ||
""" | ||
|
||
|
||
viewAppended:-> | ||
|
||
JView::viewAppended.call this | ||
|
||
@wrapper.observeMutations() | ||
|
||
@wrapper.on 'MutationHappened', @verticalTrack.thumb.bound 'handleMutation' | ||
@wrapper.on 'MutationHappened', @horizontalTrack.thumb.bound 'handleMutation' | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
require('jquery-mousewheel') $ | ||
KDCustomHTMLView = require './../../core/customhtmlview' | ||
KDScrollView = require './scrollview' | ||
KDScrollThumb = require './scrollthumb' | ||
KDScrollTrack = require './scrolltrack' | ||
|
||
module.exports = class KDCustomScrollViewWrapper extends KDScrollView | ||
|
||
scroll:(event)-> | ||
|
||
if @verticalThumb.beingDragged or @horizontalThumb.beingDragged | ||
return KD.utils.stopDOMEvent event | ||
|
||
|
||
mouseWheel:(event)-> | ||
|
||
super | ||
|
||
{_delta, deltaFactor} = event | ||
|
||
return unless _delta | ||
|
||
speed = deltaFactor or @getOptions().mouseWheelSpeed | ||
x = _delta.deltaX | ||
y = _delta.deltaY | ||
|
||
resX = if x isnt 0 and @getScrollWidth() > @getWidth() | ||
then @_scrollHorizontally {speed, velocity : x} | ||
else no | ||
resY = if y isnt 0 and @getScrollHeight() > @getHeight() | ||
then @_scrollVertically {speed, velocity : y} | ||
else no | ||
|
||
stop = if Math.abs(x) > Math.abs(y) then resX else resY | ||
|
||
return !stop | ||
|
||
|
||
_scrollVertically: do -> | ||
|
||
lastPosition = 0 | ||
|
||
({speed, velocity})-> | ||
|
||
stepInPixels = velocity * speed | ||
actPosition = @getScrollTop() | ||
newPosition = actPosition - stepInPixels | ||
shouldStop = if velocity > 0 | ||
then lastPosition > newPosition | ||
else lastPosition < newPosition | ||
|
||
@setScrollTop lastPosition = newPosition | ||
|
||
return shouldStop | ||
|
||
_scrollHorizontally: do -> | ||
|
||
lastPosition = 0 | ||
|
||
({speed, velocity})-> | ||
|
||
stepInPixels = velocity * speed | ||
actPosition = @getScrollLeft() | ||
newPosition = actPosition + stepInPixels | ||
shouldStop = if velocity < 0 | ||
then lastPosition >= newPosition | ||
else lastPosition <= newPosition | ||
|
||
@setScrollLeft lastPosition = newPosition | ||
|
||
return shouldStop | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,33 @@ | ||
KDView = require './../../core/view.coffee' | ||
JView = require './../../core/jview' | ||
|
||
module.exports = class KDScrollTrack extends KDView | ||
module.exports = class KDScrollTrack extends JView | ||
|
||
show:-> @unsetClass 'invisible' | ||
constructor:(options = {}, data)-> | ||
|
||
hide:-> @setClass 'invisible' | ||
options.type or= 'vertical' | ||
options.cssClass = KD.utils.curry "kdscrolltrack #{options.type}", options.cssClass | ||
|
||
super options, data | ||
|
||
{@type} = @getOptions() | ||
|
||
@thumb = new KDScrollThumb | ||
cssClass : 'kdscrollthumb' | ||
type : @type | ||
track : this | ||
|
||
|
||
pistachio: -> "{{> @thumb}}" | ||
|
||
|
||
show:-> | ||
|
||
@getDelegate().emit "ScrollTrackShown", @type | ||
@unsetClass 'invisible' | ||
|
||
|
||
hide:-> | ||
|
||
@getDelegate().emit "ScrollTrackHidden", @type | ||
@setClass 'invisible' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters